Salome HOME
CCAR: some corrections in :
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python
2 #
3 #  SALOME NamingService : wrapping NamingService services
4 #
5 #  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 #  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
7
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. 
12
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. 
17
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 
21
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24 #
25 #
26 #  File   : SALOME_NamingServicePy.py
27 #  Author : Estelle Deville, CEA
28 #  Module : SALOME
29 #  $Header$
30
31 import sys
32 import time
33 from omniORB import CORBA
34 import CosNaming
35 import string
36 from string import *
37
38 from SALOME_utilities import *
39 #=============================================================================
40
41 class SALOME_NamingServicePy_i(object):
42     _orb = None
43     _root_context=None
44     _current_context=None
45     _obj=None
46     
47     #-------------------------------------------------------------------------
48
49     def __init__(self, orb):
50         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
51         self._orb = orb
52         # initialize root context and current context
53         ok = 0
54         steps = 240
55         while steps > 0 and ok == 0:
56           try:
57             obj =self._orb.resolve_initial_references("NameService")
58             self._root_context =obj._narrow(CosNaming.NamingContext)
59             self._current_context = self._root_context
60
61         
62             if self._root_context is None :
63               #MESSAGE ( "Name Service Reference is invalid" )
64               #sys.exit(1)
65               MESSAGE(" Name service not found")
66             else:
67               ok = 1
68           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
69             MESSAGE(" Name service not found")
70           time.sleep(0.25)
71           steps = steps - 1
72         if steps == 0 and self._root_context is None: 
73           MESSAGE ( "Name Service Reference is invalid" )
74           sys.exit(1)
75
76     #-------------------------------------------------------------------------
77
78     def Register(self,ObjRef, Path):
79         """ ns.Register(object,pathname )  
80         
81         register a CORBA object under a pathname
82         """
83
84         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
85         _not_exist = 0
86         path_list = list(Path)
87         if path_list[0]=='/':
88             self._current_context = self._root_context
89             #delete first '/' before split
90             Path=Path[1:]
91
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 
96             # is already done
97             # If not, the new context has to be created
98             _context_name = []
99             for i in range(len(result_resolve_path)-1):
100                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
101             
102             try:
103                 obj = self._current_context.resolve(_context_name)
104                 self._current_context = obj._narrow(CosNaming.NamingContext)
105             except CosNaming.NamingContext.NotFound, ex:
106                 _not_exist = 1
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" )
113
114             if _not_exist:
115                 # at least one context of the complete path is not created, we had
116                 # to create it or them
117                 _context_name = []
118                 for i in range(len(result_resolve_path)-1):
119                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
120
121                     try:
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)
127
128         #The current directory is now the directory where the object should 
129         #be recorded
130          
131         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
132         try:
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" )
145
146     #-------------------------------------------------------------------------
147
148     def Resolve(self, Path):
149         """ ns.Resolve(pathname) -> object
150
151         find a CORBA object (ior) by its pathname
152         """
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
158             Path=Path[1:]
159
160         result_resolve_path = string.split(Path,'/')
161         _context_name=[]
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"))
165         try:
166             self._obj = self._current_context.resolve(_context_name)
167         except CosNaming.NamingContext.NotFound, ex:
168             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
169             self._obj = None
170         except CosNaming.NamingContext.InvalidName, ex:
171             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
172             self._obj = None
173         except CosNaming.NamingContext.CannotProceed, ex:
174             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
175             self._obj = None
176         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
177             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
178             self._obj = None
179         return self._obj
180
181
182     #-------------------------------------------------------------------------
183
184     def Create_Directory(self,ObjRef, Path):
185         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
186         _not_exist = 0
187         path_list = list(Path)
188         if path_list[0]=='/':
189             self._current_context = self._root_context
190             #delete first '/' before split
191             Path=Path[1:]
192
193         result_resolve_path = string.split(Path,'/')
194         _context_name = []
195         for i in range(len(result_resolve_path)):
196             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
197             try:
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" )
208  
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]
213       context_name=[]
214       for e in dir_path:
215          context_name.append(CosNaming.NameComponent(e,"dir"))
216       context_name.append(CosNaming.NameComponent(resolve_path[-1],"object"))
217       
218       try:
219         self._root_context.unbind(context_name)
220       except CosNaming.NamingContext.NotFound, ex:
221         return
222       except CORBA.Exception,ex:
223         return
224
225     def Destroy_FullDirectory(self,Path):
226       context_name=[]
227       for e in string.split(Path,'/'):
228         if e == '':continue
229         context_name.append(CosNaming.NameComponent(e,"dir"))
230
231       try:
232         context=self._root_context.resolve(context_name)
233       except CosNaming.NamingContext.NotFound, ex:
234         return
235       except CORBA.Exception,ex:
236         return
237
238       bl,bi=context.list(0)
239       if bi is not None:
240          ok,b=bi.next_one()
241          while(ok):
242             for s in b.binding_name :
243                if s.kind == "object":
244                   context.unbind([s])
245                elif s.kind == "dir":
246                   context.unbind([s])
247             ok,b=bi.next_one()
248
249       context.destroy()
250       self._root_context.unbind(context_name)