Salome HOME
Merge branch 'V7_7_BR' into hydro/imps_2015
[modules/gui.git] / src / PVServerService / ServiceLoader / PVServer_ServiceLoader.cxx
1 // Copyright (C) 2010-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author: Adrien Bruneton (CEA)
20
21 #include "PVServer_ServiceLoader.h"
22 #include "PVServer_ServiceWrapper.h"
23
24 #include <SALOME_LifeCycleCORBA.hxx>
25 #include <SALOME_NamingService.hxx>
26 #include <utilities.h> // MESSAGE() macro
27
28 PVServer_ServiceLoader::PVServer_ServiceLoader():
29      _lcc(0)
30 {
31   _lcc = new SALOME_LifeCycleCORBA();
32   _ns = _lcc->namingService();
33   _cm = _lcc->getContainerManager();
34   _orb = _lcc->orb();
35
36 }
37
38 PVServer_ServiceLoader::~PVServer_ServiceLoader()
39 {
40   if (_lcc)
41     delete _lcc;
42   _lcc = 0;
43 }
44
45 std::string PVServer_ServiceLoader::findOrLoadService(const char * containerName)
46 {
47   std::string ior = findService(containerName);
48   if (ior == "")
49     ior = loadService(containerName);
50   return ior;
51 }
52
53 std::string PVServer_ServiceLoader::findService(const char * containerName)
54 {
55   std::string path = std::string(containerName) + "/PVSERVER";
56   CORBA::Object_ptr obj = _ns->Resolve(path.c_str());
57   if(!CORBA::is_nil(obj))
58     {
59       MESSAGE("PVServer_ServiceLoader::findService(): Service found! ");
60       char * ior = _orb->object_to_string(obj);
61       return std::string(ior);
62     }
63   else
64     {
65       MESSAGE("PVServer_ServiceLoader::findService(): Service NOT found! ");
66       return std::string("");
67     }
68
69 }
70
71 std::string PVServer_ServiceLoader::loadService(const char * containerName)
72 {
73   // Get the requested container
74   Engines::ContainerParameters params;
75   params.container_name = CORBA::string_dup(containerName);
76   params.isMPI = false;
77   params.mode = "getorstart";
78   Engines::Container_ptr contain = _cm->GiveContainer(params);
79
80   if (!CORBA::is_nil(contain))
81     {
82       char * reason;
83       char * ior = contain->create_python_service_instance("PVSERVER", reason);
84       std::string reas(reason);
85       CORBA::string_free(reason);
86       if (reas != "")
87         {
88           MESSAGE("PVServer_ServiceLoader::loadService(): Python service instance could not be created! ");
89           MESSAGE("PVServer_ServiceLoader::loadService(): " << reas);
90           return std::string("");
91         }
92       else
93         {
94           MESSAGE("PVServer_ServiceLoader::loadService(): Container and service loaded! ");
95           return std::string(ior);
96         }
97     }
98   else
99     {
100       MESSAGE("PVServer_ServiceLoader::loadService(): Container could not be retrieved! ");
101       return std::string("");
102     }
103 }
104