Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / Launcher / SALOME_LauncherServer.cxx
1 // Copyright (C) 2007-2012  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 void terminateHandler(void);
32 void unexpectedHandler(void);
33
34 void AttachDebugger()
35 {
36 #ifndef WIN32
37   if(getenv ("DEBUGGER"))
38     {
39       std::stringstream exec;
40       exec << "$DEBUGGER SALOME_LauncherServer " << getpid() << "&";
41       MESSAGE ( exec.str() );
42       system(exec.str().c_str());
43       while(1);
44     }
45 #endif
46 }
47
48 void terminateHandler(void)
49 {
50   MESSAGE ( "Terminate: not managed exception !"  );
51   AttachDebugger();
52 }
53
54 void unexpectedHandler(void)
55 {
56   MESSAGE ( "Unexpected: unexpected exception !"  );
57   AttachDebugger();
58 }
59
60
61 int main(int argc, char* argv[])
62 {
63   if(getenv ("DEBUGGER"))
64     {
65 //       setsig(SIGSEGV,&Handler);
66       std::set_terminate(&terminateHandler);
67       std::set_unexpected(&unexpectedHandler);
68     }
69   /* Init libxml 
70    * To avoid memory leak, need to call xmlInitParser in the main thread
71    * and not call xmlCleanupParser later (cause implicit reinitialization in thread)
72    */
73   xmlInitParser();
74
75   PortableServer::POA_var root_poa;
76   PortableServer::POAManager_var pman;
77   CORBA::Object_var obj;
78
79   CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ;
80   //  LocalTraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb);
81   INFOS_COMPILATION;
82   BEGIN_OF(argv[0]);
83   try{ 
84     obj = orb->resolve_initial_references("RootPOA");
85     if(!CORBA::is_nil(obj))
86       root_poa = PortableServer::POA::_narrow(obj);
87     if(!CORBA::is_nil(root_poa))
88       pman = root_poa->the_POAManager();
89   }
90   catch(CORBA::COMM_FAILURE&){
91     MESSAGE( "Container: CORBA::COMM_FAILURE: Unable to contact the Naming Service" );
92   }
93   try{
94     SALOME_Launcher *lServ=new SALOME_Launcher(orb,root_poa);
95     pman->activate();
96     lServ->_remove_ref();
97     orb->run();
98     orb->destroy();
99   }catch(CORBA::SystemException&){
100     MESSAGE("Caught CORBA::SystemException.");
101   }catch(PortableServer::POA::WrongPolicy&){
102     MESSAGE("Caught CORBA::WrongPolicyException.");
103   }catch(PortableServer::POA::ServantAlreadyActive&){
104     MESSAGE("Caught CORBA::ServantAlreadyActiveException");
105   }catch(CORBA::Exception&){
106     MESSAGE("Caught CORBA::Exception.");
107   }catch(std::exception& exc){
108     MESSAGE("Caught std::exception - "<<exc.what()); 
109   }catch(...){
110     MESSAGE("Caught unknown exception.");
111   }
112   END_OF(argv[0]);
113   //  delete myThreadTrace;
114 }
115