Salome HOME
e4a44d309227ab9827f859aef0b8523b486193d2
[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 from string import *
36
37 from SALOME_utilities import *
38 #=============================================================================
39
40 class SALOME_NamingServicePy_i(object):
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 = 240
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 and self._root_context is None: 
72           MESSAGE ( "Name Service Reference is invalid" )
73           sys.exit(1)
74
75     #-------------------------------------------------------------------------
76
77     def Register(self,ObjRef, Path):
78         """ ns.Register(object,pathname )  
79         
80         register a CORBA object under a pathname
81         """
82
83         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
84         _not_exist = 0
85         path_list = list(Path)
86         if path_list[0]=='/':
87             self._current_context = self._root_context
88             #delete first '/' before split
89             Path=Path[1:]
90
91         result_resolve_path = split(Path,'/')
92         if len(result_resolve_path)>1:
93             # A directory is treated (not only an object name)
94             # We had to test if the directory where ObjRef should be recorded 
95             # is already done
96             # If not, the new context has to be created
97             _context_name = []
98             for i in range(len(result_resolve_path)-1):
99                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
100             
101             try:
102                 obj = self._current_context.resolve(_context_name)
103                 self._current_context = obj._narrow(CosNaming.NamingContext)
104             except CosNaming.NamingContext.NotFound, ex:
105                 _not_exist = 1
106             except CosNaming.NamingContext.InvalidName, ex:
107                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
108             except CosNaming.NamingContext.CannotProceed, ex:
109                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
110             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
111                 MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
112
113             if _not_exist:
114                 # at least one context of the complete path is not created, we had
115                 # to create it or them
116                 _context_name = []
117                 for i in range(len(result_resolve_path)-1):
118                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
119
120                     try:
121                         obj = self._current_context.resolve(_context_name)
122                         self._current_context = obj._narrow(CosNaming.NamingContext)
123                     except CosNaming.NamingContext.NotFound, ex:
124                         #This context is not created. It will be done
125                         self._current_context = self._current_context.bind_new_context(_context_name)
126
127         #The current directory is now the directory where the object should 
128         #be recorded
129          
130         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
131         try:
132             self._current_context.bind(_context_name,ObjRef)
133         except CosNaming.NamingContext.NotFound, ex:
134             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
135         except CosNaming.NamingContext.InvalidName, ex:
136             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
137         except CosNaming.NamingContext.CannotProceed, ex:
138             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
139         except CosNaming.NamingContext.AlreadyBound, ex:
140             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
141             self._current_context.rebind(_context_name,ObjRef)
142         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
143             MESSAGE ( "Register : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
144
145     #-------------------------------------------------------------------------
146
147     def Resolve(self, Path):
148         """ ns.Resolve(pathname) -> object
149
150         find a CORBA object (ior) by its pathname
151         """
152         #MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
153         path_list = list(Path)
154         if path_list[0]=='/':
155             self._current_context = self._root_context
156             #delete first '/' before split
157             Path=Path[1:]
158
159         result_resolve_path = split(Path,'/')
160         _context_name=[]
161         for i in range(len(result_resolve_path)-1):
162             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
163         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
164         try:
165             self._obj = self._current_context.resolve(_context_name)
166         except CosNaming.NamingContext.NotFound, ex:
167             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
168             self._obj = None
169         except CosNaming.NamingContext.InvalidName, ex:
170             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
171             self._obj = None
172         except CosNaming.NamingContext.CannotProceed, ex:
173             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
174             self._obj = None
175         except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
176             MESSAGE ( "Resolve : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
177             self._obj = None
178         return self._obj
179
180
181     #-------------------------------------------------------------------------
182
183     def Create_Directory(self,ObjRef, Path):
184         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
185         _not_exist = 0
186         path_list = list(Path)
187         if path_list[0]=='/':
188             self._current_context = self._root_context
189             #delete first '/' before split
190             Path=Path[1:]
191
192         result_resolve_path = split(Path,'/')
193         _context_name = []
194         for i in range(len(result_resolve_path)):
195             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
196             try:
197                 obj = self._current_context.resolve(_context_name)
198                 self._current_context = obj._narrow(CosNaming.NamingContext)
199             except CosNaming.NamingContext.NotFound, ex:
200                 self._current_context = self._current_context.bind_new_context(_context_name)
201             except CosNaming.NamingContext.InvalidName, ex:
202                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
203             except CosNaming.NamingContext.CannotProceed, ex:
204                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
205             except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
206                 MESSAGE ( "Create_Directory : CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE" )
207
208  
209
210