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
45 from launchConfigureParser import verbose
47 #=============================================================================
49 #define an implementation of the container interface
51 class SALOME_ContainerPy_i (Engines__POA.Container):
55 _listInstances_map = {}
57 #-------------------------------------------------------------------------
59 def __init__(self, orb, poa, containerName):
60 MESSAGE( "SALOME_ContainerPy_i::__init__" )
63 myMachine=getShortHostName()
64 Container_path = "/Containers/" + myMachine + "/" + containerName
65 #self._containerName = containerName
66 self._containerName = Container_path
67 if verbose(): print "container name ",self._containerName
69 naming_service = SALOME_NamingServicePy_i(self._orb)
70 self._naming_service = naming_service
71 MESSAGE( str(Container_path) )
72 naming_service.Register(self._this(), Container_path)
74 #-------------------------------------------------------------------------
76 def start_impl(self, ContainerName):
77 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
78 myMachine=getShortHostName()
79 theContainer = "/Containers/" + myMachine + "/" + ContainerName
81 obj = self._naming_service.Resolve(theContainer)
84 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
88 container = obj._narrow(Engines.Container)
90 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
92 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
94 #shstr = os.getenv( "PWD" ) + "/"
95 #shstr += "runSession ./SALOME_ContainerPy.py "
96 shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
97 #shstr = "runSession SALOME_ContainerPy.py "
98 shstr += ContainerName
100 # mpv: fix for SAL4731 - allways create new file to write log of server
104 fileName = "/tmp/"+ContainerName+"_%i.log"%num
105 if not os.path.exists(fileName):
115 #shstr += ContainerName
116 #shstr += ".log 2>&1 &"
118 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
121 while container is None :
124 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
126 obj = self._naming_service.Resolve(theContainer)
132 container = obj._narrow(Engines.Container)
133 if container is None:
134 MESSAGE( str(containerName) + ".object exists but is not a Container" )
139 #-------------------------------------------------------------------------
141 def instance(self, nameToRegister, componentName):
142 MESSAGE( "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
143 self._numInstance = self._numInstance +1
144 instanceName = nameToRegister + "_inst_" + `self._numInstance`
146 component=__import__(componentName)
147 factory=getattr(component,componentName)
148 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
149 instanceName, nameToRegister)
151 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
152 comp_o = comp_i._this()
155 #-------------------------------------------------------------------------
157 def load_impl(self, nameToRegister, componentName):
158 MESSAGE( "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
159 self._numInstance = self._numInstance +1
160 instanceName = nameToRegister + "_inst_" + `self._numInstance`
161 interfaceName = nameToRegister
162 the_command = "import " + nameToRegister + "\n"
163 the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
164 the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
165 MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
167 comp_o = comp_i._this()
170 #-------------------------------------------------------------------------
172 def import_component(self, componentName):
173 MESSAGE( "SALOME_Container_i::import_component" )
176 if verbose(): print "try import ",componentName
177 __import__(componentName)
178 if verbose(): print "import ",componentName," successful"
182 traceback.print_exc()
183 print "import ",componentName," not possible"
186 #-------------------------------------------------------------------------
188 def load_component_Library(self, componentName):
189 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
191 instanceName = componentName + "_inst_" + `self._numInstance`
192 interfaceName = componentName
193 #the_command = "import " + componentName + "\n"
194 #the_command = the_command + "comp_i = " + componentName + "." + componentName
195 #the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
196 #MESSAGE( "SALOME_ContainerPy_i::load_component_Library :" + str (the_command) )
198 #comp_o = comp_i._this()
199 #if comp_o is not None:
202 # --- try to import Python component
203 # retImpl = self.import_component(componentName)
208 #import isn't possible
211 return self.import_component(componentName)
213 #-------------------------------------------------------------------------
215 def create_component_instance(self, componentName, studyId):
216 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
218 MESSAGE( "Study ID is lower than 0!" )
221 self._numInstance = self._numInstance +1
222 instanceName = componentName + "_inst_" + `self._numInstance`
225 component=__import__(componentName)
226 factory=getattr(component,componentName)
227 comp_i=factory(self._orb,
234 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
235 comp_o = comp_i._this()
236 self._listInstances_map[instanceName] = comp_i
239 traceback.print_exc()
240 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
243 #-------------------------------------------------------------------------
245 def find_component_instance(self, registeredName, studyId):
247 keysList = self._listInstances_map.keys()
249 while i < len(keysList):
250 instance = keysList[i]
251 if find(instance,registeredName) == 0:
252 anEngine = self._listInstances_map[instance]
253 if studyId == anEngine.getStudyId():
254 return anEngine._this()
256 return anEngine._this()
259 #-------------------------------------------------------------------------
261 def remove_impl(self, component):
262 MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
263 instanceName = component._get_instanceName()
264 MESSAGE( "unload component " + str(instanceName) )
265 self._listInstances_map.remove(instanceName)
267 self._naming_service.Destroy_Name(str(instanceName))
269 #-------------------------------------------------------------------------
271 def finalize_removal(self):
272 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
275 #-------------------------------------------------------------------------
278 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
281 #-------------------------------------------------------------------------
286 #-------------------------------------------------------------------------
289 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
290 return self._containerName
292 #-------------------------------------------------------------------------
294 def getHostName(self):
295 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
296 self._machineName = "localhost"
297 return self._machineName
299 #-------------------------------------------------------------------------
301 def _get_machineName(self):
302 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
303 self._machineName = "localhost"
304 return self._machineName
306 #-------------------------------------------------------------------------
309 self._orb.shutdown(0)
312 #=============================================================================
314 #initialise the ORB and find the root POA
315 print "Starting ",sys.argv[1]
316 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
317 poa = orb.resolve_initial_references("RootPOA")
318 print "ORB and POA initialized"
320 #create an instance of SALOME_ContainerPy_i and a Container reference
321 #containerName = "FactoryServerPy"
322 MESSAGE( str(sys.argv) )
323 containerName = sys.argv[1]
324 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
325 print "SALOME_ContainerPy_i instance created ",cpy_i
326 cpy_o = cpy_i._this()
327 print "SALOME_ContainerPy_i instance activated ",cpy_o
330 poaManager = poa._get_the_POAManager()
331 poaManager.activate()