3 # SALOME Container : implementation of container and engine for Kernel
5 # Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
26 # File : SALOME_ContainerPy.py
27 # Author : Paul RASCLE, EDF
35 import omnipatch # PAL10310
37 from omniORB import CORBA, PortableServer
39 import Engines, Engines__POA
40 from SALOME_NamingServicePy import *
41 from SALOME_ComponentPy import *
43 from SALOME_utilities import *
44 from Utils_Identity import getShortHostName
46 #=============================================================================
48 #define an implementation of the container interface
50 class SALOME_ContainerPy_i (Engines__POA.Container):
54 _listInstances_map = {}
56 #-------------------------------------------------------------------------
58 def __init__(self, orb, poa, containerName):
59 MESSAGE( "SALOME_ContainerPy_i::__init__" )
62 myMachine=getShortHostName()
63 Container_path = "/Containers/" + myMachine + "/" + containerName
64 #self._containerName = containerName
65 self._containerName = Container_path
66 print "container name ",self._containerName
68 naming_service = SALOME_NamingServicePy_i(self._orb)
69 self._naming_service = naming_service
70 MESSAGE( str(Container_path) )
71 naming_service.Register(self._this(), Container_path)
73 #-------------------------------------------------------------------------
75 def start_impl(self, ContainerName):
76 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
77 myMachine=getShortHostName()
78 theContainer = "/Containers/" + myMachine + "/" + ContainerName
80 obj = self._naming_service.Resolve(theContainer)
83 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
87 container = obj._narrow(Engines.Container)
89 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
91 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
93 #shstr = os.getenv( "PWD" ) + "/"
94 #shstr += "runSession ./SALOME_ContainerPy.py "
95 shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
96 #shstr = "runSession SALOME_ContainerPy.py "
97 shstr += ContainerName
99 # mpv: fix for SAL4731 - allways create new file to write log of server
103 fileName = "/tmp/"+ContainerName+"_%i.log"%num
104 if not os.path.exists(fileName):
114 #shstr += ContainerName
115 #shstr += ".log 2>&1 &"
117 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
120 while container is None :
123 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
125 obj = self._naming_service.Resolve(theContainer)
131 container = obj._narrow(Engines.Container)
132 if container is None:
133 MESSAGE( str(containerName) + ".object exists but is not a Container" )
138 #-------------------------------------------------------------------------
140 def instance(self, nameToRegister, componentName):
141 MESSAGE( "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
142 self._numInstance = self._numInstance +1
143 instanceName = nameToRegister + "_inst_" + `self._numInstance`
145 component=__import__(componentName)
146 factory=getattr(component,componentName)
147 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
148 instanceName, nameToRegister)
150 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
151 comp_o = comp_i._this()
154 #-------------------------------------------------------------------------
156 def load_impl(self, nameToRegister, componentName):
157 MESSAGE( "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
158 self._numInstance = self._numInstance +1
159 instanceName = nameToRegister + "_inst_" + `self._numInstance`
160 interfaceName = nameToRegister
161 the_command = "import " + nameToRegister + "\n"
162 the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
163 the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
164 MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
166 comp_o = comp_i._this()
169 #-------------------------------------------------------------------------
171 def import_component(self, componentName):
172 MESSAGE( "SALOME_Container_i::import_component" )
175 print "try import ",componentName
176 __import__(componentName)
177 print "import ",componentName," successful"
181 traceback.print_exc()
182 print "import ",componentName," not possible"
185 #-------------------------------------------------------------------------
187 def load_component_Library(self, componentName):
188 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
190 instanceName = componentName + "_inst_" + `self._numInstance`
191 interfaceName = componentName
192 #the_command = "import " + componentName + "\n"
193 #the_command = the_command + "comp_i = " + componentName + "." + componentName
194 #the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
195 #MESSAGE( "SALOME_ContainerPy_i::load_component_Library :" + str (the_command) )
197 #comp_o = comp_i._this()
198 #if comp_o is not None:
201 # --- try to import Python component
202 # retImpl = self.import_component(componentName)
207 #import isn't possible
210 return self.import_component(componentName)
212 #-------------------------------------------------------------------------
214 def create_component_instance(self, componentName, studyId):
215 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
217 MESSAGE( "Study ID is lower than 0!" )
220 self._numInstance = self._numInstance +1
221 instanceName = componentName + "_inst_" + `self._numInstance`
224 component=__import__(componentName)
225 factory=getattr(component,componentName)
226 comp_i=factory(self._orb,
233 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
234 comp_o = comp_i._this()
235 self._listInstances_map[instanceName] = comp_i
238 traceback.print_exc()
239 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
242 #-------------------------------------------------------------------------
244 def find_component_instance(self, registeredName, studyId):
246 keysList = self._listInstances_map.keys()
248 while i < len(keysList):
249 instance = keysList[i]
250 if find(instance,registeredName) == 0:
251 anEngine = self._listInstances_map[instance]
252 if studyId == anEngine.getStudyId():
253 return anEngine._this()
255 return anEngine._this()
258 #-------------------------------------------------------------------------
260 def remove_impl(self, component):
261 MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
262 instanceName = component._get_instanceName()
263 MESSAGE( "unload component " + str(instanceName) )
264 self._listInstances_map.remove(instanceName)
266 self._naming_service.Destroy_Name(str(instanceName))
268 #-------------------------------------------------------------------------
270 def finalize_removal(self):
271 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
274 #-------------------------------------------------------------------------
277 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
280 #-------------------------------------------------------------------------
285 #-------------------------------------------------------------------------
288 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
289 return self._containerName
291 #-------------------------------------------------------------------------
293 def getHostName(self):
294 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
295 self._machineName = "localhost"
296 return self._machineName
298 #-------------------------------------------------------------------------
300 def _get_machineName(self):
301 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
302 self._machineName = "localhost"
303 return self._machineName
305 #-------------------------------------------------------------------------
308 self._orb.shutdown(0)
311 #=============================================================================
313 #initialise the ORB and find the root POA
314 print "Starting ",sys.argv[1]
315 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
316 poa = orb.resolve_initial_references("RootPOA")
317 print "ORB and POA initialized"
319 #create an instance of SALOME_ContainerPy_i and a Container reference
320 #containerName = "FactoryServerPy"
321 MESSAGE( str(sys.argv) )
322 containerName = sys.argv[1]
323 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
324 print "SALOME_ContainerPy_i instance created ",cpy_i
325 cpy_o = cpy_i._this()
326 print "SALOME_ContainerPy_i instance activated ",cpy_o
329 poaManager = poa._get_the_POAManager()
330 poaManager.activate()