1# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 2# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 3# 4# This file is part of logilab-common. 5# 6# logilab-common is free software: you can redistribute it and/or modify it under 7# the terms of the GNU Lesser General Public License as published by the Free 8# Software Foundation, either version 2.1 of the License, or (at your option) any 9# later version. 10# 11# logilab-common is distributed in the hope that it will be useful, but WITHOUT 12# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 13# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 14# details. 15# 16# You should have received a copy of the GNU Lesser General Public License along 17# with logilab-common. If not, see <http://www.gnu.org/licenses/>. 18"""A set of utility function to ease the use of OmniORBpy. 19 20 21 22 23""" 24__docformat__ = "restructuredtext en" 25 26from omniORB import CORBA, PortableServer 27import CosNaming 28 29orb = None 30 31def get_orb(): 32 """ 33 returns a reference to the ORB. 34 The first call to the method initialized the ORB 35 This method is mainly used internally in the module. 36 """ 37 38 global orb 39 if orb is None: 40 import sys 41 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID) 42 return orb 43 44def get_root_context(): 45 """ 46 returns a reference to the NameService object. 47 This method is mainly used internally in the module. 48 """ 49 50 orb = get_orb() 51 nss = orb.resolve_initial_references("NameService") 52 rootContext = nss._narrow(CosNaming.NamingContext) 53 assert rootContext is not None, "Failed to narrow root naming context" 54 return rootContext 55 56def register_object_name(object, namepath): 57 """ 58 Registers a object in the NamingService. 59 The name path is a list of 2-uples (id,kind) giving the path. 60 61 For instance if the path of an object is [('foo',''),('bar','')], 62 it is possible to get a reference to the object using the URL 63 'corbaname::hostname#foo/bar'. 64 [('logilab','rootmodule'),('chatbot','application'),('chatter','server')] 65 is mapped to 66 'corbaname::hostname#logilab.rootmodule/chatbot.application/chatter.server' 67 68 The get_object_reference() function can be used to resolve such a URL. 69 """ 70 context = get_root_context() 71 for id, kind in namepath[:-1]: 72 name = [CosNaming.NameComponent(id, kind)] 73 try: 74 context = context.bind_new_context(name) 75 except CosNaming.NamingContext.AlreadyBound as ex: 76 context = context.resolve(name)._narrow(CosNaming.NamingContext) 77 assert context is not None, \ 78 'test context exists but is not a NamingContext' 79 80 id, kind = namepath[-1] 81 name = [CosNaming.NameComponent(id, kind)] 82 try: 83 context.bind(name, object._this()) 84 except CosNaming.NamingContext.AlreadyBound as ex: 85 context.rebind(name, object._this()) 86 87def activate_POA(): 88 """ 89 This methods activates the Portable Object Adapter. 90 You need to call it to enable the reception of messages in your code, 91 on both the client and the server. 92 """ 93 orb = get_orb() 94 poa = orb.resolve_initial_references('RootPOA') 95 poaManager = poa._get_the_POAManager() 96 poaManager.activate() 97 98def run_orb(): 99 """ 100 Enters the ORB mainloop on the server. 101 You should not call this method on the client. 102 """ 103 get_orb().run() 104 105def get_object_reference(url): 106 """ 107 Resolves a corbaname URL to an object proxy. 108 See register_object_name() for examples URLs 109 """ 110 return get_orb().string_to_object(url) 111 112def get_object_string(host, namepath): 113 """given an host name and a name path as described in register_object_name, 114 return a corba string identifier 115 """ 116 strname = '/'.join(['.'.join(path_elt) for path_elt in namepath]) 117 return 'corbaname::%s#%s' % (host, strname) 118