3 # SALOME NamingService : wrapping NamingService services
5 # Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 # File : SALOME_NamingServicePy.py
27 # Author : Estelle Deville, CEA
33 from omniORB import CORBA
38 from SALOME_utilities import *
39 #=============================================================================
41 class SALOME_NamingServicePy_i(object):
47 #-------------------------------------------------------------------------
49 def __init__(self, orb):
50 #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
52 # initialize root context and current context
55 while steps > 0 and ok == 0:
57 obj =self._orb.resolve_initial_references("NameService")
58 self._root_context =obj._narrow(CosNaming.NamingContext)
59 self._current_context = self._root_context
62 if self._root_context is None :
63 #MESSAGE ( "Name Service Reference is invalid" )
65 MESSAGE(" Name service not found")
68 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
69 MESSAGE(" Name service not found")
72 if steps == 0 and self._root_context is None:
73 MESSAGE ( "Name Service Reference is invalid" )
76 #-------------------------------------------------------------------------
78 def Register(self,ObjRef, Path):
79 """ ns.Register(object,pathname )
81 register a CORBA object under a pathname
84 MESSAGE ( "SALOME_NamingServicePy_i::Register" )
86 path_list = list(Path)
88 self._current_context = self._root_context
89 #delete first '/' before split
92 result_resolve_path = string.split(Path,'/')
93 if len(result_resolve_path)>1:
94 # A directory is treated (not only an object name)
95 # We had to test if the directory where ObjRef should be recorded
97 # If not, the new context has to be created
99 for i in range(len(result_resolve_path)-1):
100 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
103 obj = self._current_context.resolve(_context_name)
104 self._current_context = obj._narrow(CosNaming.NamingContext)
105 except CosNaming.NamingContext.NotFound, ex:
107 except CosNaming.NamingContext.InvalidName, ex:
108 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
109 except CosNaming.NamingContext.CannotProceed, ex:
110 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
111 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
112 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
115 # at least one context of the complete path is not created, we had
116 # to create it or them
118 for i in range(len(result_resolve_path)-1):
119 _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
122 obj = self._current_context.resolve(_context_name)
123 self._current_context = obj._narrow(CosNaming.NamingContext)
124 except CosNaming.NamingContext.NotFound, ex:
125 #This context is not created. It will be done
126 self._current_context = self._current_context.bind_new_context(_context_name)
128 #The current directory is now the directory where the object should
131 _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
133 self._current_context.bind(_context_name,ObjRef)
134 except CosNaming.NamingContext.NotFound, ex:
135 MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
136 except CosNaming.NamingContext.InvalidName, ex:
137 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
138 except CosNaming.NamingContext.CannotProceed, ex:
139 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
140 except CosNaming.NamingContext.AlreadyBound, ex:
141 MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
142 self._current_context.rebind(_context_name,ObjRef)
143 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
144 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
146 #-------------------------------------------------------------------------
148 def Resolve(self, Path):
149 """ ns.Resolve(pathname) -> object
151 find a CORBA object (ior) by its pathname
153 #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
154 path_list = list(Path)
155 if path_list[0]=='/':
156 self._current_context = self._root_context
157 #delete first '/' before split
160 result_resolve_path = string.split(Path,'/')
162 for i in range(len(result_resolve_path)-1):
163 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
164 _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
166 self._obj = self._current_context.resolve(_context_name)
167 except CosNaming.NamingContext.NotFound, ex:
168 MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
170 except CosNaming.NamingContext.InvalidName, ex:
171 MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
173 except CosNaming.NamingContext.CannotProceed, ex:
174 MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
176 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
177 MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
182 #-------------------------------------------------------------------------
184 def Create_Directory(self,ObjRef, Path):
185 MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
187 path_list = list(Path)
188 if path_list[0]=='/':
189 self._current_context = self._root_context
190 #delete first '/' before split
193 result_resolve_path = string.split(Path,'/')
195 for i in range(len(result_resolve_path)):
196 _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]
198 obj = self._current_context.resolve(_context_name)
199 self._current_context = obj._narrow(CosNaming.NamingContext)
200 except CosNaming.NamingContext.NotFound, ex:
201 self._current_context = self._current_context.bind_new_context(_context_name)
202 except CosNaming.NamingContext.InvalidName, ex:
203 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
204 except CosNaming.NamingContext.CannotProceed, ex:
205 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
206 except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
207 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
209 def Destroy_Name(self,Path):
210 resolve_path=string.split(Path,'/')
211 if resolve_path[0] == '': del resolve_path[0]
212 dir_path=resolve_path[:-1]
215 context_name.append(CosNaming.NameComponent(e,"dir"))
216 context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
219 self._root_context.unbind(context_name)
220 except CosNaming.NamingContext.NotFound, ex:
222 except CORBA.Exception,ex:
225 def Destroy_FullDirectory(self,Path):
227 for e in string.split(Path,'/'):
229 context_name.append(CosNaming.NameComponent(e,"dir"))
232 context=self._root_context.resolve(context_name)
233 except CosNaming.NamingContext.NotFound, ex:
235 except CORBA.Exception,ex:
238 bl,bi=context.list(0)
242 for s in b.binding_name :
243 if s.kind == "object":
245 elif s.kind == "dir":
250 self._root_context.unbind(context_name)