corbautils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. __docformat__ = "restructuredtext en"
  21. from omniORB import CORBA, PortableServer
  22. import CosNaming
  23. orb = None
  24. def get_orb():
  25. """
  26. returns a reference to the ORB.
  27. The first call to the method initialized the ORB
  28. This method is mainly used internally in the module.
  29. """
  30. global orb
  31. if orb is None:
  32. import sys
  33. orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
  34. return orb
  35. def get_root_context():
  36. """
  37. returns a reference to the NameService object.
  38. This method is mainly used internally in the module.
  39. """
  40. orb = get_orb()
  41. nss = orb.resolve_initial_references("NameService")
  42. rootContext = nss._narrow(CosNaming.NamingContext)
  43. assert rootContext is not None, "Failed to narrow root naming context"
  44. return rootContext
  45. def register_object_name(object, namepath):
  46. """
  47. Registers a object in the NamingService.
  48. The name path is a list of 2-uples (id,kind) giving the path.
  49. For instance if the path of an object is [('foo',''),('bar','')],
  50. it is possible to get a reference to the object using the URL
  51. 'corbaname::hostname#foo/bar'.
  52. [('logilab','rootmodule'),('chatbot','application'),('chatter','server')]
  53. is mapped to
  54. 'corbaname::hostname#logilab.rootmodule/chatbot.application/chatter.server'
  55. The get_object_reference() function can be used to resolve such a URL.
  56. """
  57. context = get_root_context()
  58. for id, kind in namepath[:-1]:
  59. name = [CosNaming.NameComponent(id, kind)]
  60. try:
  61. context = context.bind_new_context(name)
  62. except CosNaming.NamingContext.AlreadyBound, ex:
  63. context = context.resolve(name)._narrow(CosNaming.NamingContext)
  64. assert context is not None, \
  65. 'test context exists but is not a NamingContext'
  66. id, kind = namepath[-1]
  67. name = [CosNaming.NameComponent(id, kind)]
  68. try:
  69. context.bind(name, object._this())
  70. except CosNaming.NamingContext.AlreadyBound, ex:
  71. context.rebind(name, object._this())
  72. def activate_POA():
  73. """
  74. This methods activates the Portable Object Adapter.
  75. You need to call it to enable the reception of messages in your code,
  76. on both the client and the server.
  77. """
  78. orb = get_orb()
  79. poa = orb.resolve_initial_references('RootPOA')
  80. poaManager = poa._get_the_POAManager()
  81. poaManager.activate()
  82. def run_orb():
  83. """
  84. Enters the ORB mainloop on the server.
  85. You should not call this method on the client.
  86. """
  87. get_orb().run()
  88. def get_object_reference(url):
  89. """
  90. Resolves a corbaname URL to an object proxy.
  91. See register_object_name() for examples URLs
  92. """
  93. return get_orb().string_to_object(url)
  94. def get_object_string(host, namepath):
  95. """given an host name and a name path as described in register_object_name,
  96. return a corba string identifier
  97. """
  98. strname = '/'.join(['.'.join(path_elt) for path_elt in namepath])
  99. return 'corbaname::%s#%s' % (host, strname)