Salome HOME
Merge from V5_1_main 14/05/2010
[modules/kernel.git] / src / Launcher / SALOME_LauncherServer.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOME_Launcher.hxx"
24 #include "utilities.h"
25 #include <sstream>
26 #include <iostream>
27 #include <stdexcept>
28 #include <libxml/parser.h>
29
30 void AttachDebugger()
31 {
32 #ifndef WIN32
33   if(getenv ("DEBUGGER"))
34     {
35       std::stringstream exec;
36       exec << "$DEBUGGER SALOME_LauncherServer " << getpid() << "&";
37       MESSAGE ( exec.str() );
38       system(exec.str().c_str());
39       while(1);
40     }
41 #endif
42 }
43
44 void terminateHandler(void)
45 {
46   MESSAGE ( "Terminate: not managed exception !"  );
47   AttachDebugger();
48 }
49
50 void unexpectedHandler(void)
51 {
52   MESSAGE ( "Unexpected: unexpected exception !"  );
53   AttachDebugger();
54 }
55
56
57 int main(int argc, char* argv[])
58 {
59   if(getenv ("DEBUGGER"))
60     {
61 //       setsig(SIGSEGV,&Handler);
62       std::set_terminate(&terminateHandler);
63       std::set_unexpected(&unexpectedHandler);
64     }
65   /* Init libxml 
66    * To avoid memory leak, need to call xmlInitParser in the main thread
67    * and not call xmlCleanupParser later (cause implicit reinitialization in thread)
68    */
69   xmlInitParser();
70
71   PortableServer::POA_var root_poa;
72   PortableServer::POAManager_var pman;
73   CORBA::Object_var obj;
74
75   CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
76   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
77   INFOS_COMPILATION;
78   BEGIN_OF(argv[0]);
79   try{ 
80     obj = orb->resolve_initial_references("RootPOA");
81     if(!CORBA::is_nil(obj))
82       root_poa = PortableServer::POA::_narrow(obj);
83     if(!CORBA::is_nil(root_poa))
84       pman = root_poa->the_POAManager();
85   }
86   catch(CORBA::COMM_FAILURE&){
87     MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
88   }
89   try{
90     SALOME_Launcher *lServ=new SALOME_Launcher(orb,root_poa);
91     pman->activate();
92     lServ->_remove_ref();
93     orb->run();
94     orb->destroy();
95   }catch(CORBA::SystemException&){
96     MESSAGE("Caught CORBA::SystemException.");
97   }catch(PortableServer::POA::WrongPolicy&){
98     MESSAGE("Caught CORBA::WrongPolicyException.");
99   }catch(PortableServer::POA::ServantAlreadyActive&){
100     MESSAGE("Caught CORBA::ServantAlreadyActiveException");
101   }catch(CORBA::Exception&){
102     MESSAGE("Caught CORBA::Exception.");
103   }catch(std::exception& exc){
104     MESSAGE("Caught std::exception - "<<exc.what()); 
105   }catch(...){
106     MESSAGE("Caught unknown exception.");
107   }
108   END_OF(argv[0]);
109   //  delete myThreadTrace;
110 }
111