Salome HOME
NRI : Temporary modification for reading catalog of modules.
[modules/kernel.git] / src / NamingService / SALOME_NamingServicePy.py
1 #! /usr/bin/env python
2
3 #=============================================================================
4 # File      : SALOME_NamingServicePy.py
5 # Created   : lun oct  15 14:55 CEST 2001
6 # Author    : Estelle Deville, CEA
7 # Project   : SALOME
8 # Copyright : CEA 2001
9 # $Header$
10 #=============================================================================
11
12 import sys
13 from omniORB import CORBA
14 import CosNaming
15 from string import *
16
17 from SALOME_utilities import *
18 #=============================================================================
19
20 class SALOME_NamingServicePy_i:
21     _orb = None
22     _root_context=None
23     _current_context=None
24     _obj=None
25     
26     #-------------------------------------------------------------------------
27
28     def __init__(self, orb):
29         MESSAGE ( "SALOME_NamingServicePy_i::__init__" )
30         self._orb = orb
31         # initialize root context and current context
32         obj =self._orb.resolve_initial_references("NameService")
33         self._root_context =obj._narrow(CosNaming.NamingContext)
34         self._current_context = self._root_context
35
36         
37         if self._root_context is None :
38             MESSAGE ( "Name Service Reference is invalid" )
39             sys.exit(1)
40  
41     #-------------------------------------------------------------------------
42     def Register(self,ObjRef, Path):
43         MESSAGE ( "SALOME_NamingServicePy_i::Register" )
44         _not_exist = 0
45         path_list = list(Path)
46         if path_list[0]=='/':
47             self._current_context = self._root_context
48             #delete first '/' before split
49             Path=Path[1:]
50
51         result_resolve_path = split(Path,'/')
52         if len(result_resolve_path)>1:
53             # A directory is treated (not only an object name)
54             # We had to test if the directory where ObjRef should be recorded 
55             # is already done
56             # If not, the new context has to be created
57             _context_name = []
58             for i in range(len(result_resolve_path)-1):
59                 _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
60             
61             try:
62                 obj = self._current_context.resolve(_context_name)
63                 self._current_context = obj._narrow(CosNaming.NamingContext)
64             except CosNaming.NamingContext.NotFound, ex:
65                 _not_exist = 1
66             except CosNaming.NamingContext.InvalidName, ex:
67                 MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
68             except CosNaming.NamingContext.CannotProceed, ex:
69                 MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
70             except CORBA.COMM_FAILURE, ex:
71                 MESSAGE ( "Register : CORBA.COMM_FAILURE" )
72
73             if _not_exist:
74                 # at least one context of the complete path is not created, we had
75                 # to create it or them
76                 _context_name = []
77                 for i in range(len(result_resolve_path)-1):
78                     _context_name = [CosNaming.NameComponent(result_resolve_path[i],"dir")]
79
80                     try:
81                         obj = self._current_context.resolve(_context_name)
82                         self._current_context = obj._narrow(CosNaming.NamingContext)
83                     except CosNaming.NamingContext.NotFound, ex:
84                         #This context is not created. It will be done
85                         self._current_context = self._current_context.bind_new_context(_context_name)
86
87         #The current directory is now the directory where the object should 
88         #be recorded
89          
90         _context_name = [CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object")]
91         try:
92             self._current_context.bind(_context_name,ObjRef)
93         except CosNaming.NamingContext.NotFound, ex:
94             MESSAGE ( "Register : CosNaming.NamingContext.NotFound" )
95         except CosNaming.NamingContext.InvalidName, ex:
96             MESSAGE ( "Register : CosNaming.NamingContext.InvalidName" )
97         except CosNaming.NamingContext.CannotProceed, ex:
98             MESSAGE ( "Register : CosNaming.NamingContext.CannotProceed" )
99         except CosNaming.NamingContext.AlreadyBound, ex:
100             MESSAGE ( "Register : CosNaming.NamingContext.AlreadyBound, object will be rebind" )
101             self._current_context.rebind(_context_name,ObjRef)
102         except CORBA.COMM_FAILURE, ex:
103             MESSAGE ( "Register : CORBA.COMM_FAILURE" )
104
105             
106     #-------------------------------------------------------------------------
107     def Resolve(self, Path):
108         MESSAGE ( "SALOME_NamingServicePy_i::Resolve" )
109         path_list = list(Path)
110         if path_list[0]=='/':
111             self._current_context = self._root_context
112             #delete first '/' before split
113             Path=Path[1:]
114
115         result_resolve_path = split(Path,'/')
116         _context_name=[]
117         for i in range(len(result_resolve_path)-1):
118             _context_name.append(CosNaming.NameComponent(result_resolve_path[i],"dir"))
119         _context_name.append(CosNaming.NameComponent(result_resolve_path[len(result_resolve_path)-1],"object"))
120         try:
121             self._obj = self._current_context.resolve(_context_name)
122         except CosNaming.NamingContext.NotFound, ex:
123             MESSAGE ( "Resolve : CosNaming.NamingContext.NotFound" )
124             self._obj = None
125         except CosNaming.NamingContext.InvalidName, ex:
126             MESSAGE ( "Resolve : CosNaming.NamingContext.InvalidName" )
127             self._obj = None
128         except CosNaming.NamingContext.CannotProceed, ex:
129             MESSAGE ( "Resolve : CosNaming.NamingContext.CannotProceed" )
130             self._obj = None
131         except CORBA.COMM_FAILURE, ex:
132             MESSAGE ( "Resolve : CORBA.COMM_FAILURE" )
133             self._obj = None
134         return self._obj
135
136
137
138     #-------------------------------------------------------------------------
139     def Create_Directory(self,ObjRef, Path):
140         MESSAGE ( "SALOME_NamingServicePy_i::Create_Directory" )
141         _not_exist = 0
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)):
151             _context_name[CosNaming.NameComponent(result_resolve_path[i],"dir")]            
152             try:
153                 obj = self._current_context.resolve(_context_name)
154                 self._current_context = obj._narrow(CosNaming.NamingContext)
155             except CosNaming.NamingContext.NotFound, ex:
156                 self._current_context = self._current_context.bind_new_context(_context_name)
157             except CosNaming.NamingContext.InvalidName, ex:
158                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.InvalidName" )
159             except CosNaming.NamingContext.CannotProceed, ex:
160                 MESSAGE ( "Create_Directory : CosNaming.NamingContext.CannotProceed" )
161             except CORBA.COMM_FAILURE, ex:
162                 MESSAGE ( "Create_Directory : CORBA.COMM_FAILURE" )
163
164  
165
166