Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
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 from string import *
36
37 from SALOME_utilities import *
38 #=============================================================================
39
40 class SALOME_NamingServicePy_i:
41     _orb = None
42     _root_context=None
43     _current_context=None
44     _obj=None
45     
46     #-------------------------------------------------------------------------
47
48     def __init__(self, orb):
49         #MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
50         self._orb = orb
51         # initialize root context and current context
52         ok = 0
53         steps = 40
54         while steps > 0 and ok == 0:
55           try:
56             obj =self._orb.resolve_initial_references("NameService")
57             self._root_context =obj._narrow(CosNaming.NamingContext)
58             self._current_context = self._root_context
59
60         
61             if self._root_context is None :
62               #MESSAGE ( "Name Service Reference is invalid" )
63               #sys.exit(1)
64               MESSAGE(" Name service not found")
65             else:
66               ok = 1
67           except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
68             MESSAGE(" Name service not found")
69           time.sleep(0.25)
70           steps = steps - 1
71         if steps == 0: 
72           MESSAGE ( "Name Service Reference is invalid" )
73           sys.exit(1)
74     #-------------------------------------------------------------------------
75     def Register(self,ObjRef, Path):
76         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
77         _not_exist = 0
78         path_list = list(Path)
79         if path_list[0]=='/':
80             self._current_context = self._root_context
81             #delete first '/' before split
82             Path=Path[1:]
83
84         result_resolve_path = split(Path,'/')
85         if len(result_resolve_path)>1:
86             # A directory is treated (not only an object name)
87             # We had to test if the directory where ObjRef should be recorded 
88             # is already done
89             # If not, the new context has to be created
90             _context_name = []
91             for i in range(len(result_resolve_path)-1):
92                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
93             
94             try:
95                 obj = self._current_context.resolve(_context_name)
96                 self._current_context = obj._narrow(CosNaming.NamingContext)
97             except CosNaming.NamingContext.NotFound, ex:
98                 _not_exist = 1
99             except CosNaming.NamingContext.InvalidName, ex:
100                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
101             except CosNaming.NamingContext.CannotProceed, ex:
102                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
103             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
104                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
105
106             if _not_exist:
107                 # at least one context of the complete path is not created, we had
108                 # to create it or them
109                 _context_name = []
110                 for i in range(len(result_resolve_path)-1):
111                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
112
113                     try:
114                         obj = self._current_context.resolve(_context_name)
115                         self._current_context = obj._narrow(CosNaming.NamingContext)
116                     except CosNaming.NamingContext.NotFound, ex:
117                         #This context is not created. It will be done
118                         self._current_context = self._current_context.bind_new_context(_context_name)
119
120         #The current directory is now the directory where the object should 
121         #be recorded
122          
123         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
124         try:
125             self._current_context.bind(_context_name,ObjRef)
126         except CosNaming.NamingContext.NotFound, ex:
127             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
128         except CosNaming.NamingContext.InvalidName, ex:
129             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
130         except CosNaming.NamingContext.CannotProceed, ex:
131             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
132         except CosNaming.NamingContext.AlreadyBound, ex:
133             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
134             self._current_context.rebind(_context_name,ObjRef)
135         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
136             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
137
138             
139     #-------------------------------------------------------------------------
140     def Resolve(self, Path):
141         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
142         path_list = list(Path)
143         if path_list[0]=='/':
144             self._current_context = self._root_context
145             #delete first '/' before split
146             Path=Path[1:]
147
148         result_resolve_path = split(Path,'/')
149         _context_name=[]
150         for i in range(len(result_resolve_path)-1):
151             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
152         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
153         try:
154             self._obj = self._current_context.resolve(_context_name)
155         except CosNaming.NamingContext.NotFound, ex:
156             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
157             self._obj = None
158         except CosNaming.NamingContext.InvalidName, ex:
159             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
160             self._obj = None
161         except CosNaming.NamingContext.CannotProceed, ex:
162             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
163             self._obj = None
164         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
165             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
166             self._obj = None
167         return self._obj
168
169
170
171     #-------------------------------------------------------------------------
172     def Create_Directory(self,ObjRef, Path):
173         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
174         _not_exist = 0
175         path_list = list(Path)
176         if path_list[0]=='/':
177             self._current_context = self._root_context
178             #delete first '/' before split
179             Path=Path[1:]
180
181         result_resolve_path = split(Path,'/')
182         _context_name = []
183         for i in range(len(result_resolve_path)):
184             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
185             try:
186                 obj = self._current_context.resolve(_context_name)
187                 self._current_context = obj._narrow(CosNaming.NamingContext)
188             except CosNaming.NamingContext.NotFound, ex:
189                 self._current_context = self._current_context.bind_new_context(_context_name)
190             except CosNaming.NamingContext.InvalidName, ex:
191                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
192             except CosNaming.NamingContext.CannotProceed, ex:
193                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
194             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
195                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
196
197  
198
199