]> SALOME platform Git repositories - modules/kernel.git/blob - src/NamingService/SALOME_NamingServicePy.py
Salome HOME
merge from branch BR_V5_DEV
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python
2 #  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 #  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 #
7 #  This library is free software; you can redistribute it and/or
8 #  modify it under the terms of the GNU Lesser General Public
9 #  License as published by the Free Software Foundation; either
10 #  version 2.1 of the License.
11 #
12 #  This library is distributed in the hope that it will be useful,
13 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public
18 #  License along with this library; if not, write to the Free Software
19 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 #
21 #  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #
23 #  SALOME NamingService : wrapping NamingService services
24 #  File   : SALOME_NamingServicePy.py
25 #  Author : Estelle Deville, CEA
26 #  Module : SALOME
27 #  $Header$
28 #
29 import sys
30 import time
31 from omniORB import CORBA
32 import CosNaming
33 import string
34 from string import *
35
36 from SALOME_utilities import *
37 #=============================================================================
38
39 class SALOME_NamingServicePy_i(object):
40     _orb = None
41     _root_context=None
42     _current_context=None
43     _obj=None
44     
45     #-------------------------------------------------------------------------
46
47     def __init__(self, orb):
48         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
49         self._orb = orb
50         # initialize root context and current context
51         ok = 0
52         steps = 240
53         while steps > 0 and ok == 0:
54           try:
55             obj =self._orb.resolve_initial_references("NameService")
56             self._root_context =obj._narrow(CosNaming.NamingContext)
57             self._current_context = self._root_context
58
59         
60             if self._root_context is None :
61               #MESSAGE ( "Name Service Reference is invalid" )
62               #sys.exit(1)
63               MESSAGE(" Name service not found")
64             else:
65               ok = 1
66           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
67             MESSAGE(" Name service not found")
68           time.sleep(0.25)
69           steps = steps - 1
70         if steps == 0 and self._root_context is None: 
71           MESSAGE ( "Name Service Reference is invalid" )
72           sys.exit(1)
73
74     #-------------------------------------------------------------------------
75
76     def Register(self,ObjRef, Path):
77         """ ns.Register(object,pathname )  
78         
79         register a CORBA object under a pathname
80         """
81
82         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
83         _not_exist = 0
84         path_list = list(Path)
85         if path_list[0]=='/':
86             self._current_context = self._root_context
87             #delete first '/' before split
88             Path=Path[1:]
89
90         result_resolve_path = string.split(Path,'/')
91         if len(result_resolve_path)>1:
92             # A directory is treated (not only an object name)
93             # We had to test if the directory where ObjRef should be recorded 
94             # is already done
95             # If not, the new context has to be created
96             _context_name = []
97             for i in range(len(result_resolve_path)-1):
98                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
99             
100             try:
101                 obj = self._current_context.resolve(_context_name)
102                 self._current_context = obj._narrow(CosNaming.NamingContext)
103             except CosNaming.NamingContext.NotFound, ex:
104                 _not_exist = 1
105             except CosNaming.NamingContext.InvalidName, ex:
106                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
107             except CosNaming.NamingContext.CannotProceed, ex:
108                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
109             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
110                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
111
112             if _not_exist:
113                 # at least one context of the complete path is not created, we had
114                 # to create it or them
115                 _context_name = []
116                 for i in range(len(result_resolve_path)-1):
117                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
118
119                     try:
120                         obj = self._current_context.resolve(_context_name)
121                         self._current_context = obj._narrow(CosNaming.NamingContext)
122                     except CosNaming.NamingContext.NotFound, ex:
123                         #This context is not created. It will be done
124                         self._current_context = self._current_context.bind_new_context(_context_name)
125
126         #The current directory is now the directory where the object should 
127         #be recorded
128          
129         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
130         try:
131             self._current_context.bind(_context_name,ObjRef)
132         except CosNaming.NamingContext.NotFound, ex:
133             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
134         except CosNaming.NamingContext.InvalidName, ex:
135             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
136         except CosNaming.NamingContext.CannotProceed, ex:
137             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
138         except CosNaming.NamingContext.AlreadyBound, ex:
139             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
140             self._current_context.rebind(_context_name,ObjRef)
141         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
142             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
143
144     #-------------------------------------------------------------------------
145
146     def Resolve(self, Path):
147         """ ns.Resolve(pathname) -> object
148
149         find a CORBA object (ior) by its pathname
150         """
151         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
152         path_list = list(Path)
153         if path_list[0]=='/':
154             self._current_context = self._root_context
155             #delete first '/' before split
156             Path=Path[1:]
157
158         result_resolve_path = string.split(Path,'/')
159         _context_name=[]
160         for i in range(len(result_resolve_path)-1):
161             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
162         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
163         try:
164             self._obj = self._current_context.resolve(_context_name)
165         except CosNaming.NamingContext.NotFound, ex:
166             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
167             self._obj = None
168         except CosNaming.NamingContext.InvalidName, ex:
169             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
170             self._obj = None
171         except CosNaming.NamingContext.CannotProceed, ex:
172             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
173             self._obj = None
174         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
175             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
176             self._obj = None
177         return self._obj
178
179
180     #-------------------------------------------------------------------------
181
182     def Create_Directory(self,ObjRef, Path):
183         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
184         _not_exist = 0
185         path_list = list(Path)
186         if path_list[0]=='/':
187             self._current_context = self._root_context
188             #delete first '/' before split
189             Path=Path[1:]
190
191         result_resolve_path = string.split(Path,'/')
192         _context_name = []
193         for i in range(len(result_resolve_path)):
194             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
195             try:
196                 obj = self._current_context.resolve(_context_name)
197                 self._current_context = obj._narrow(CosNaming.NamingContext)
198             except CosNaming.NamingContext.NotFound, ex:
199                 self._current_context = self._current_context.bind_new_context(_context_name)
200             except CosNaming.NamingContext.InvalidName, ex:
201                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
202             except CosNaming.NamingContext.CannotProceed, ex:
203                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
204             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
205                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
206  
207     def Destroy_Name(self,Path):
208       resolve_path=string.split(Path,'/')
209       if resolve_path[0] == '': del resolve_path[0]
210       dir_path=resolve_path[:-1]
211       context_name=[]
212       for e in dir_path:
213          context_name.append(CosNaming.NameComponent(e,"dir"))
214       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
215       
216       try:
217         self._root_context.unbind(context_name)
218       except CosNaming.NamingContext.NotFound, ex:
219         return
220       except CORBA.Exception,ex:
221         return
222
223     def Destroy_FullDirectory(self,Path):
224       context_name=[]
225       for e in string.split(Path,'/'):
226         if e == '':continue
227         context_name.append(CosNaming.NameComponent(e,"dir"))
228
229       try:
230         context=self._root_context.resolve(context_name)
231       except CosNaming.NamingContext.NotFound, ex:
232         return
233       except CORBA.Exception,ex:
234         return
235
236       bl,bi=context.list(0)
237       if bi is not None:
238          ok,b=bi.next_one()
239          while(ok):
240             for s in b.binding_name :
241                if s.kind == "object":
242                   context.unbind([s])
243                elif s.kind == "dir":
244                   context.unbind([s])
245             ok,b=bi.next_one()
246
247       context.destroy()
248       self._root_context.unbind(context_name)