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 from omniORB import CORBA, PortableServer
37 import Engines, Engines__POA
38 from SALOME_NamingServicePy import *
39 from SALOME_ComponentPy import *
41 from SALOME_utilities import *
42 from Utils_Identity import getShortHostName
43 from launchConfigureParser import verbose
45 #=============================================================================
47 #define an implementation of the container interface
49 class SALOME_ContainerPy_i (Engines__POA.Container):
53 _listInstances_map = {}
55 #-------------------------------------------------------------------------
57 def __init__(self, orb, poa, containerName):
58 MESSAGE( "SALOME_ContainerPy_i::__init__" )
61 myMachine=getShortHostName()
62 Container_path = "/Containers/" + myMachine + "/" + containerName
63 #self._containerName = containerName
64 self._containerName = Container_path
65 if verbose(): print "container name ",self._containerName
67 naming_service = SALOME_NamingServicePy_i(self._orb)
68 self._naming_service = naming_service
69 MESSAGE( str(Container_path) )
70 naming_service.Register(self._this(), Container_path)
72 #-------------------------------------------------------------------------
74 def start_impl(self, ContainerName):
75 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) )
76 myMachine=getShortHostName()
77 theContainer = "/Containers/" + myMachine + "/" + ContainerName
79 obj = self._naming_service.Resolve(theContainer)
82 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object not found in Naming Service" )
86 container = obj._narrow(Engines.Container)
88 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(containerName) + ".object exists but is not a Container" )
90 MESSAGE( "SALOME_ContainerPy_i::start_impl " + str(ContainerName) + ".object found without new launch" )
92 #shstr = os.getenv( "PWD" ) + "/"
93 #shstr += "runSession ./SALOME_ContainerPy.py "
94 shstr = os.getenv("KERNEL_ROOT_DIR") + "/bin/salome/SALOME_ContainerPy.py ";
95 #shstr = "runSession SALOME_ContainerPy.py "
96 shstr += ContainerName
98 # mpv: fix for SAL4731 - allways create new file to write log of server
102 fileName = "/tmp/"+ContainerName+"_%i.log"%num
103 if not os.path.exists(fileName):
113 #shstr += ContainerName
114 #shstr += ".log 2>&1 &"
116 MESSAGE( "SALOME_ContainerPy_i::start_impl " + "os.system(" + str(shstr) + ")" )
119 while container is None :
122 MESSAGE( str(count) + ". Waiting for " + str(theContainer) )
124 obj = self._naming_service.Resolve(theContainer)
130 container = obj._narrow(Engines.Container)
131 if container is None:
132 MESSAGE( str(containerName) + ".object exists but is not a Container" )
137 #-------------------------------------------------------------------------
139 def instance(self, nameToRegister, componentName):
140 MESSAGE( "SALOME_ContainerPy_i::instance " + str(nameToRegister) + ' ' + str(componentName) )
141 self._numInstance = self._numInstance +1
142 instanceName = nameToRegister + "_inst_" + `self._numInstance`
144 component=__import__(componentName)
145 factory=getattr(component,componentName)
146 comp_i=factory(self._orb, self._poa, self._this(), self._containerName,
147 instanceName, nameToRegister)
149 MESSAGE( "SALOME_ContainerPy_i::instance : component created")
150 comp_o = comp_i._this()
153 #-------------------------------------------------------------------------
155 def load_impl(self, nameToRegister, componentName):
156 MESSAGE( "SALOME_ContainerPy_i::load_impl " + str(nameToRegister) + ' ' + str(componentName) )
157 self._numInstance = self._numInstance +1
158 instanceName = nameToRegister + "_inst_" + `self._numInstance`
159 interfaceName = nameToRegister
160 the_command = "import " + nameToRegister + "\n"
161 the_command = the_command + "comp_i = " + nameToRegister + "." + nameToRegister
162 the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
163 MESSAGE( "SALOME_ContainerPy_i::load_impl :" + str (the_command) )
165 comp_o = comp_i._this()
168 #-------------------------------------------------------------------------
170 def import_component(self, componentName):
171 MESSAGE( "SALOME_Container_i::import_component" )
174 if verbose(): print "try import ",componentName
175 module=__import__(componentName)
176 if verbose(): print "import ",componentName," successful"
180 traceback.print_exc()
181 print "import ",componentName," not possible"
184 #-------------------------------------------------------------------------
186 def load_component_Library(self, componentName):
187 MESSAGE( "SALOME_ContainerPy_i::load_component_Library " + str(componentName) )
189 instanceName = componentName + "_inst_" + `self._numInstance`
190 interfaceName = componentName
191 #the_command = "import " + componentName + "\n"
192 #the_command = the_command + "comp_i = " + componentName + "." + componentName
193 #the_command = the_command + "(self._orb, self._poa, self._this(), self._containerName, instanceName, interfaceName)\n"
194 #MESSAGE( "SALOME_ContainerPy_i::load_component_Library :" + str (the_command) )
196 #comp_o = comp_i._this()
197 #if comp_o is not None:
200 # --- try to import Python component
201 # retImpl = self.import_component(componentName)
206 #import isn't possible
209 return self.import_component(componentName)
211 #-------------------------------------------------------------------------
213 def create_component_instance(self, componentName, studyId):
214 MESSAGE( "SALOME_ContainerPy_i::create_component_instance ==> " + str(componentName) + ' ' + str(studyId) )
216 MESSAGE( "Study ID is lower than 0!" )
219 self._numInstance = self._numInstance +1
220 instanceName = componentName + "_inst_" + `self._numInstance`
223 component=__import__(componentName)
224 factory=getattr(component,componentName)
225 comp_i=factory(self._orb,
232 MESSAGE( "SALOME_Container_i::create_component_instance : OK")
233 comp_o = comp_i._this()
234 self._listInstances_map[instanceName] = comp_i
237 traceback.print_exc()
238 MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
241 #-------------------------------------------------------------------------
243 def find_component_instance(self, registeredName, studyId):
245 keysList = self._listInstances_map.keys()
247 while i < len(keysList):
248 instance = keysList[i]
249 if find(instance,registeredName) == 0:
250 anEngine = self._listInstances_map[instance]
251 if studyId == anEngine.getStudyId():
252 return anEngine._this()
254 return anEngine._this()
257 #-------------------------------------------------------------------------
259 def remove_impl(self, component):
260 MESSAGE( "SALOME_ContainerPy_i::remove_impl" )
261 instanceName = component._get_instanceName()
262 MESSAGE( "unload component " + str(instanceName) )
263 self._listInstances_map.remove(instanceName)
265 self._naming_service.Destroy_Name(str(instanceName))
267 #-------------------------------------------------------------------------
269 def finalize_removal(self):
270 MESSAGE( "SALOME_ContainerPy_i::finalize_removal" )
273 #-------------------------------------------------------------------------
276 MESSAGE( "SALOME_ContainerPy_i::ping() pid " + str(os.getpid()) )
279 #-------------------------------------------------------------------------
284 #-------------------------------------------------------------------------
287 MESSAGE( "SALOME_ContainerPy_i::_get_name" )
288 return self._containerName
290 #-------------------------------------------------------------------------
292 def getHostName(self):
293 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
294 self._machineName = "localhost"
295 return self._machineName
297 #-------------------------------------------------------------------------
299 def _get_machineName(self):
300 MESSAGE( "SALOME_ContainerPy_i::_get_MachineName" )
301 self._machineName = "localhost"
302 return self._machineName
304 #-------------------------------------------------------------------------
307 self._orb.shutdown(0)
310 def _get_logfilename(self):
311 return self._logfilename
312 def _set_logfilename(self,logfilename):
313 self._logfilename=logfilename
314 def _get_workingdir(self):
317 #=============================================================================
319 #initialise the ORB and find the root POA
320 print "Starting ",sys.argv[1]
321 orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
322 poa = orb.resolve_initial_references("RootPOA")
323 if verbose(): print "ORB and POA initialized"
325 #create an instance of SALOME_ContainerPy_i and a Container reference
326 #containerName = "FactoryServerPy"
327 MESSAGE( str(sys.argv) )
328 containerName = sys.argv[1]
329 cpy_i = SALOME_ContainerPy_i(orb, poa, containerName)
330 if verbose(): print "SALOME_ContainerPy_i instance created ",cpy_i
331 cpy_o = cpy_i._this()
332 if verbose(): print "SALOME_ContainerPy_i instance activated ",cpy_o
337 poaManager = poa._get_the_POAManager()
338 poaManager.activate()