Salome HOME
Merge branch 'master' into abn/paravis_rearch
[modules/gui.git] / src / Session / SALOME_Session_Server.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 //  SALOME Session : implementation of Session.idl
24 //  File : SALOME_Session_Server.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27
28 #include <SALOME_NamingService.hxx>
29 #include <SALOME_ModuleCatalog_impl.hxx>
30 #include <SALOME_LifeCycleCORBA.hxx>
31 #include <SALOME_Event.h>
32
33 #include <Basics_OCCTVersion.hxx>
34
35 #include <Container_init_python.hxx>
36 #include <ConnectionManager_i.hxx>
37 #include <RegistryService.hxx>
38
39 #ifdef ENABLE_TESTRECORDER
40   #include <TestApplication.h>
41 #endif
42
43 #include <OpUtil.hxx>
44 #include <Utils_ORB_INIT.hxx>
45 #include <Utils_SINGLETON.hxx>
46 #include <Utils_SALOME_Exception.hxx>
47 #include <Utils_CorbaException.hxx>
48
49 #include <utilities.h>
50 #include "Session_ServerLauncher.hxx"
51 #include "Session_ServerCheck.hxx"
52 #include "Session_Session_i.hxx"
53
54 #include <Qtx.h>
55 #include <QtxSplash.h>
56
57 #include <Style_Salome.h>
58
59 #include "GUI_version.h"
60 #include <SUIT_Tools.h>
61 #include <SUIT_Session.h>
62 #include <SUIT_Application.h>
63 #include <SUIT_Desktop.h>
64 #include <SUIT_ResourceMgr.h>
65 #include <SUIT_ExceptionHandler.h>
66
67 #include <SALOMEconfig.h>
68 #include CORBA_SERVER_HEADER(SALOME_Session)
69 #include CORBA_SERVER_HEADER(SALOMEDS)
70
71 #include <QDir>
72 #include <QFile>
73 #include <QApplication>
74 #include <QMutex>
75 #include <QWaitCondition>
76 #include <QRegExp>
77 #include <QTextStream>
78
79 /*! - read arguments, define list of server to launch with their arguments.
80  * - wait for naming service
81  * - create and run a thread for launch of all servers
82  *
83 */
84
85 //! CORBA server for SALOME Session
86 /*!
87  * SALOME_Session Server launches a SALOME session servant.
88  * The servant registers to the Naming Service.
89  * See SALOME_Session.idl for interface specification.
90  *
91  * Main services offered by the servant are:
92  * - launch GUI
93  * - stop Session ( must be idle )
94  * - get session state
95  */
96
97 void MessageOutput( QtMsgType type, const char* msg )
98 {
99   switch ( type )
100   {
101   case QtDebugMsg:
102     //MESSAGE( "Debug: " << msg );
103     break;
104   case QtWarningMsg:
105     MESSAGE( "Warning: " << msg );
106     break;
107   case QtFatalMsg:
108     MESSAGE( "Fatal: " << msg );
109     break;
110   }
111 }
112
113 QString salomeVersion()
114 {
115   return GUI_VERSION_STR;
116 }
117
118 class SALOME_ResourceMgr : public SUIT_ResourceMgr
119 {
120 public:
121   SALOME_ResourceMgr( const QString& app, const QString& resVarTemplate ) : SUIT_ResourceMgr( app, resVarTemplate )
122   {
123     setCurrentFormat( "xml" );
124     setOption( "translators", QString( "%P_msg_%L.qm|%P_icons.qm|%P_images.qm" ) );
125   }
126   static void initResourceMgr()
127   {
128     if ( myExtAppName.isNull() || myExtAppVersion.isNull() ) {
129       SALOME_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
130       resMgr.loadLanguage( "LightApp",  "en" );
131       resMgr.loadLanguage( "SalomeApp", "en" );
132
133       myExtAppName = QObject::tr( "APP_NAME" ).trimmed();
134       if ( myExtAppName == "APP_NAME" || myExtAppName.toLower() == "salome" ) 
135         myExtAppName = "SalomeApp";
136       myExtAppVersion = QObject::tr( "APP_VERSION" );
137       if ( myExtAppVersion == "APP_VERSION" ) {
138         if ( myExtAppName != "SalomeApp" )
139           myExtAppVersion = "";
140         else myExtAppVersion = salomeVersion();
141       }
142     }
143   }
144   QString version() const { return myExtAppVersion; }
145
146 protected:
147   QString userFileName( const QString& appName, const bool for_load ) const
148   { 
149     if ( version().isEmpty()  ) return ""; 
150     return SUIT_ResourceMgr::userFileName( myExtAppName, for_load );
151   }
152
153   virtual long userFileId( const QString& _fname ) const
154   {
155     //////////////////////////////////////////////////////////////////////////////////////////////
156     // In SALOME and SALOME-based applications the user preferences file is named as
157     // - <AppName>.xml.<AppVersion> on Windows
158     // - <AppName>rc.<AppVersion> on Linux
159     // where
160     //   * AppName is application name, default SalomeApp (can be customized in SALOME-based
161     //     applications
162     //   * AppVersion is application version
163     //
164     // Since version 6.5.0 of SALOME, user file is situated in the ~/.config/salome
165     // directory. For backward compatibility, when user preferences from nearest
166     // version of application is searched, user home directory is also looked through,
167     // with lower priority.
168     // 
169     // Since version 6.6.0 of SALOME, user file name on Linux is no more prefixed by dot
170     // symbol since it is situated in hidden ~/.config/salome directory. Files with dot
171     // prefix also though taken into account (with lower priority) for backward compatibility.
172     //
173     // Notes:
174     // - Currently the following format of version number is supported:
175     //   <major>[.<minor>[.<release>[<type><dev>]]]
176     //   Parts in square brackets are considered optional. Here:
177     //   * major   - major version id
178     //   * minor   - minor version id
179     //   * release - maintenance version id
180     //   * type    - dev or patch marker; it can be either one alphabetical symbol (from 'a' to 'z')
181     //               or 'rc' to point release candidate (case-insensitive)
182     //   * dev     - dev version or patch number
183     //   All numerical values must be of range [1-99].
184     //   Examples: 1.0, 6.5.0, 1.2.0a1, 3.3.3rc3 (release candidate 3), 11.0.0p1 (patch 1)
185     //
186     // - Versioning approach can be customized by implementing and using own resource manager class,
187     //   see QtxResurceMgr, SUIT_ResourceMgr classes.
188     //////////////////////////////////////////////////////////////////////////////////////////////
189     long id = -1;
190     if ( !myExtAppName.isEmpty() ) {
191 #ifdef WIN32
192       // On Windows, user file name is something like SalomeApp.xml.6.5.0 where
193       // - SalomeApp is an application name (can be customized)
194       // - xml is a file format (xml or ini)
195       // - 6.5.0 is an application version, can include alfa/beta/rc marks, e.g. 6.5.0a3, 6.5.0rc1
196       QRegExp exp( QString( "%1\\.%2\\.([a-zA-Z0-9.]+)" ).arg( myExtAppName ).arg( currentFormat() ) );
197 #else
198       // On Linux, user file name is something like SalomeApprc.6.5.0 where
199       // - SalomeApp is an application name (can be customized)
200       // - 6.5.0 is an application version, can include alfa/beta/rc marks, e.g. 6.5.0a3, 6.5.0rc1
201
202       // VSR 24/09/2012: issue 0021781: since version 6.6.0 user filename is not prepended with "."
203       // when it is stored in the ~/.config/<appname> directory;
204       // for backward compatibility we also check files prepended with "." with lower priority
205       QRegExp exp( QString( "\\.?%1rc\\.([a-zA-Z0-9.]+)" ).arg( myExtAppName ) );
206 #endif
207       QString fname = QFileInfo( _fname ).fileName();
208       if ( exp.exactMatch( fname ) ) {
209         long fid = Qtx::versionToId( exp.cap( 1 ) );
210         if ( fid > 0 ) id = fid;
211       }
212     }
213     return id;
214   }
215
216 public:
217   static QString myExtAppName;
218   static QString myExtAppVersion;
219 };
220
221 QString SALOME_ResourceMgr::myExtAppName    = QString();
222 QString SALOME_ResourceMgr::myExtAppVersion = QString();
223
224 class SALOME_Session : public SUIT_Session
225 {
226 public:
227   SALOME_Session() : SUIT_Session() {}
228   virtual ~SALOME_Session() {}
229
230 public:
231   virtual SUIT_ResourceMgr* createResourceMgr( const QString& appName ) const
232   {
233     SALOME_ResourceMgr::initResourceMgr();
234     SALOME_ResourceMgr* resMgr = new SALOME_ResourceMgr( appName, QString( "%1Config" ) );
235     return resMgr;
236   }
237 };
238
239 #ifdef ENABLE_TESTRECORDER
240   class SALOME_QApplication : public TestApplication
241 #else
242   class SALOME_QApplication : public QApplication
243 #endif
244 {
245 public:
246 #ifdef ENABLE_TESTRECORDER
247   SALOME_QApplication( int& argc, char** argv ) : TestApplication( argc, argv ), myHandler ( 0 ) {}
248 #else
249   SALOME_QApplication( int& argc, char** argv )
250 #ifndef WIN32
251   // san: Opening an X display and choosing a visual most suitable for 3D visualization
252   // in order to make SALOME viewers work with non-native X servers
253   : QApplication( (Display*)Qtx::getDisplay(), argc, argv, Qtx::getVisual() ),
254 #else
255   : QApplication( argc, argv ), 
256 #endif
257     myHandler ( 0 ) {}
258 #endif
259
260   virtual bool notify( QObject* receiver, QEvent* e )
261   {
262
263 #ifdef ENABLE_TESTRECORDER
264     return myHandler ? myHandler->handle( receiver, e ) :
265       TestApplication::notify( receiver, e );
266 #else
267     try {
268       return myHandler ? myHandler->handle( receiver, e ) : QApplication::notify( receiver, e );
269     }
270     catch (std::exception& e) {
271       std::cerr << e.what()  << std::endl;
272     }
273     catch (CORBA::Exception& e) {
274       std::cerr << "Caught CORBA::Exception"  << std::endl;
275       CORBA::Any tmp;
276       tmp<<= e;
277       CORBA::TypeCode_var tc = tmp.type();
278       const char *p = tc->name();
279       std::cerr << "notify(): CORBA exception of the kind : " << p << " is caught" << std::endl;
280     }
281     catch (...) {
282       std::cerr << "Unknown exception caught in Qt handler: it's probably a bug in SALOME platform" << std::endl;
283     }
284     return false;  // return false when exception is caught
285 #endif
286   }
287   SUIT_ExceptionHandler* handler() const { return myHandler; }
288   void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
289
290 private:
291   SUIT_ExceptionHandler* myHandler;
292 };
293
294 // class which calls SALOME::Session::GetInterface() from another thread
295 // to avoid mutual lock ( if called from the same thread as main()
296 class GetInterfaceThread : public QThread
297 {
298 public:
299   GetInterfaceThread( SALOME::Session_var s ) : session ( s )
300   {
301     start();
302   }
303 protected:
304   virtual void run()
305   {
306     if ( !CORBA::is_nil( session ) )
307       session->GetInterface();
308     else
309       printf( "\nFATAL ERROR: SALOME::Session object is nil! Can not display GUI\n\n" );
310   }
311 private:
312   SALOME::Session_var session;
313 };
314
315 // returns true if 'str' is found in argv
316 bool isFound( const char* str, int argc, char** argv )
317 {
318   for ( int i = 1; i <= ( argc-1 ); i++ )
319     if ( !strcmp( argv[i], str ) )
320       return true;
321   return false;
322 }
323
324 void killOmniNames()
325 {
326   SALOME_LifeCycleCORBA::killOmniNames();
327 }
328
329 // shutdown standalone servers
330 void shutdownServers( SALOME_NamingService* theNS )
331 {
332   SALOME_LifeCycleCORBA lcc(theNS);
333   lcc.shutdownServers();
334 }
335
336 // ---------------------------- MAIN -----------------------
337 int main( int argc, char **argv )
338 {
339   // Install Qt debug messages handler
340   qInstallMsgHandler( MessageOutput );
341
342   //Set a "native" graphic system in case if application runs on the remote host
343   QString remote(getenv("REMOTEHOST"));
344   QString client(getenv("SSH_CLIENT"));
345   if(remote.length() > 0 || client.length() > 0 ) {
346     QApplication::setGraphicsSystem(QLatin1String("native"));
347   }
348   
349   // add $QTDIR/plugins to the pluins search path for image plugins
350   QString qtdir( ::getenv( "QTDIR" ) );
351   if ( !qtdir.isEmpty() )
352     QApplication::addLibraryPath( QDir( qtdir ).absoluteFilePath( "plugins" ) );
353
354   {
355     SALOME_Session s;
356     QApplication::setApplicationName( "salome" );
357     SUIT_ResourceMgr* resMgr = s.createResourceMgr( "SalomeApp" );
358     bool isCloc = resMgr->booleanValue( "language", "locale", true );
359     if ( isCloc ) { 
360       QLocale::setDefault( QLocale::c() );
361     }
362     else {
363       QLocale::setDefault( QLocale::system() );
364     }
365   }
366
367   // Create Qt application instance;
368   // this should be done the very first!
369   SALOME_QApplication _qappl( argc, argv );
370   _qappl.setOrganizationName( "salome" );
371   _qappl.setApplicationName( "salome" );
372   _qappl.setApplicationVersion( salomeVersion() );
373
374   // Add application library path (to search style plugin etc...)
375   QString path = QDir::convertSeparators( SUIT_Tools::addSlash( QString( ::getenv( "GUI_ROOT_DIR" ) ) ) + QString( "bin/salome" ) );
376   _qappl.addLibraryPath( path );
377
378   bool isGUI    = isFound( "GUI",    argc, argv );
379   bool isSplash = isFound( "SPLASH", argc, argv );
380   // Show splash screen (only if both the "GUI" and "SPLASH" parameters are set)
381   // Note, that user preferences are not taken into account for splash settings -
382   // it is a property of the application!
383   QtxSplash* splash = 0;
384   if ( isGUI && isSplash ) {
385     // ...create resource manager
386     SUIT_ResourceMgr resMgr( "SalomeApp", QString( "%1Config" ) );
387     resMgr.setCurrentFormat( "xml" );
388     resMgr.setWorkingMode( QtxResourceMgr::IgnoreUserValues );
389     resMgr.loadLanguage( "LightApp" );
390     //
391     splash = QtxSplash::splash( QPixmap() );
392     splash->readSettings( &resMgr );
393     if ( splash->pixmap().isNull() )
394       splash->setPixmap( resMgr.loadPixmap( "LightApp", QObject::tr( "ABOUT_SPLASH" ) ) );
395     if ( splash->pixmap().isNull() ) {
396       delete splash;
397       splash = 0;
398     }
399     else {
400       splash->setOption( "%A", QObject::tr( "APP_NAME" ) );
401       splash->setOption( "%V", QObject::tr( "ABOUT_VERSION" ).arg( salomeVersion() ) );
402       splash->setOption( "%L", QObject::tr( "ABOUT_LICENSE" ) );
403       splash->setOption( "%C", QObject::tr( "ABOUT_COPYRIGHT" ) );
404       splash->show();
405       QApplication::instance()->processEvents();
406     }
407   }
408
409   
410   // Initialization
411   int result = -1;
412
413   CORBA::ORB_var orb;
414   PortableServer::POA_var poa;
415
416   SUIT_Session* aGUISession = 0;
417   SALOME_NamingService* _NS = 0;
418   GetInterfaceThread* guiThread = 0;
419   Session_ServerLauncher* myServerLauncher = 0;
420
421   try {
422     // ...initialize Python (only once)
423     int   _argc   = 1;
424     char* _argv[] = {(char*)""};
425     KERNEL_PYTHON::init_python( _argc,_argv );
426
427     // ...create ORB, get RootPOA object, NamingService, etc.
428     ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
429     ASSERT( SINGLETON_<ORB_INIT>::IsAlreadyExisting() );
430     int orbArgc = 1;
431     orb = init( orbArgc, argv );
432
433     CORBA::Object_var obj = orb->resolve_initial_references( "RootPOA" );
434     poa = PortableServer::POA::_narrow( obj );
435
436     PortableServer::POAManager_var pman = poa->the_POAManager();
437     pman->activate() ;
438     MESSAGE( "pman->activate()" );
439
440     _NS = new SALOME_NamingService( orb );
441
442     result = 0;
443   }
444   catch ( SALOME_Exception& e ) {
445     INFOS( "run(): SALOME::SALOME_Exception is caught: "<<e.what() );
446   }
447   catch ( CORBA::SystemException& e ) {
448     INFOS( "Caught CORBA::SystemException." );
449   }
450   catch ( CORBA::Exception& e ) {
451     INFOS( "Caught CORBA::Exception." );
452     CORBA::Any tmp;
453     tmp<<= e;
454     CORBA::TypeCode_var tc = tmp.type();
455     const char *p = tc->name();
456     INFOS ( "run(): CORBA exception of the kind : "<<p<< " is caught" );
457   }
458   catch ( std::exception& e ) {
459     INFOS( "run(): An exception has been caught: " <<e.what() );
460   }
461   catch (...) {
462     INFOS( "Caught unknown exception." );
463   }
464
465   QMutex _GUIMutex, _SessionMutex, _SplashMutex;
466   QWaitCondition _ServerLaunch, _SessionStarted, _SplashStarted;
467
468   // lock session mutex to ensure that GetInterface is not called
469   // until all initialization is done
470   _SessionMutex.lock();
471
472   if ( !result ) {
473     // Start embedded servers launcher (Registry, SALOMEDS, etc.)
474     // ...lock mutex to block embedded servers launching thread until wait( mutex )
475     _GUIMutex.lock();  
476     // ...create launcher
477     myServerLauncher = new Session_ServerLauncher( argc, argv, orb, poa, &_GUIMutex, &_ServerLaunch, &_SessionMutex, &_SessionStarted );
478     // ...block this thread until launcher is ready
479     _ServerLaunch.wait( &_GUIMutex );
480     
481     // Start servers check thread (splash)
482     if ( splash ) {
483       // ...lock mutex to block splash thread until wait( mutex )
484       _SplashMutex.lock();
485       // ...create servers checking thread
486       Session_ServerCheck sc( &_SplashMutex, &_SplashStarted );
487       // ... set initial progress
488       splash->setProgress( 0, sc.totalSteps() );
489       // start check loop 
490       while ( true ) {
491         int step    = sc.currentStep();
492         int total   = sc.totalSteps();
493         QString msg = sc.currentMessage();
494         QString err = sc.error();
495         if ( !err.isEmpty() ) {
496           QtxSplash::setError( err );
497           QApplication::instance()->processEvents();
498           result = -1;
499           break;
500         }
501         QtxSplash::setStatus( msg, step );
502         QApplication::instance()->processEvents();
503         if ( step >= total )
504           break;
505         // ...block this thread until servers checking is finished
506         _SplashStarted.wait( &_SplashMutex );
507       }
508       // ...unlock mutex 'cause it is no more needed
509       _SplashMutex.unlock();
510     }
511
512     // Finalize embedded servers launcher 
513     // ...block this thread until launcher is finished
514     _ServerLaunch.wait( &_GUIMutex );
515     // ...unlock mutex 'cause it is no more needed
516     _GUIMutex.unlock();
517   }
518
519   // Obtain Session interface reference
520   CORBA::Object_var obj = _NS->Resolve( "/Kernel/Session" );
521   SALOME::Session_var session = SALOME::Session::_narrow( obj ) ;
522
523   bool shutdownAll = false;
524   bool shutdownSession = false;
525   if ( !result ) {
526     // Launch GUI activator
527     if ( isGUI ) {
528       if ( splash )
529         splash->setStatus( QApplication::translate( "", "Activating desktop..." ) );
530       // ...create GUI launcher
531       MESSAGE( "Session activated, Launch IAPP..." );
532       guiThread = new GetInterfaceThread( session );
533     }
534
535     // GUI activation
536     // Allow multiple activation/deactivation of GUI
537     while ( true ) {
538       MESSAGE( "waiting wakeAll()" );
539       _SessionStarted.wait( &_SessionMutex ); // to be reseased by Launch server thread when ready:
540       // atomic operation lock - unlock on mutex
541       // unlock mutex: serverThread runs, calls _ServerLaunch->wakeAll()
542       // this thread wakes up, and lock mutex
543
544       _SessionMutex.unlock();
545
546       // Session might be shutdowning here, check status
547       SALOME::StatSession stat = session->GetStatSession();
548       shutdownSession = stat.state == SALOME::shutdown;
549       if ( shutdownSession ) {
550         _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
551         break;
552       }
553
554       // SUIT_Session creation
555       aGUISession = new SALOME_Session();
556
557       // Load SalomeApp dynamic library
558       MESSAGE( "creation SUIT_Application" );
559       SUIT_Application* aGUIApp = aGUISession->startApplication( "SalomeApp", 0, 0 );
560       if ( aGUIApp )
561       {
562         Style_Salome::initialize( aGUIApp->resourceMgr() );
563         if ( aGUIApp->resourceMgr()->booleanValue( "Style", "use_salome_style", true ) )
564           Style_Salome::apply();
565
566         if ( !isFound( "noexcepthandler", argc, argv ) )
567           _qappl.setHandler( aGUISession->handler() ); // after loading SalomeApp application
568                                                        // aGUISession contains SalomeApp_ExceptionHandler
569         // Run GUI loop
570         MESSAGE( "run(): starting the main event loop" );
571
572         if ( splash )
573           splash->finish( aGUIApp->desktop() );
574
575         result = _qappl.exec();
576         
577         splash = 0;
578
579         if ( result == SUIT_Session::NORMAL ) {
580         // desktop is explicitly closed by user from GUI
581         // exit flags says if it's necessary to shutdown all servers
582         // all session server only
583           shutdownAll = aGUISession->exitFlags();
584         }
585         else {
586           // desktop might be closed from:
587           // - StopSesion() /temporarily/ or
588           // - Shutdown() /permanently/
589           stat = session->GetStatSession();
590           shutdownSession = stat.state == SALOME::shutdown;
591         }
592         if ( shutdownAll || shutdownSession ) {
593           _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
594           break;
595         }
596       }
597
598       delete aGUISession;
599       aGUISession = 0;
600       
601       // Prepare _GUIMutex for a new GUI activation
602       _SessionMutex.lock();
603     }
604   }
605
606   // unlock Session mutex
607   _SessionMutex.unlock();
608   
609   if ( myServerLauncher )
610     myServerLauncher->ShutdownAll(); // shutdown embedded servers
611
612   if ( shutdownAll )                 // shutdown standalone servers
613     shutdownServers( _NS );
614
615   if ( myServerLauncher )
616     myServerLauncher->KillAll();     // kill embedded servers
617
618   // Unregister session server
619   SALOME_Session_i* sessionServant = dynamic_cast<SALOME_Session_i*>( poa->reference_to_servant( session.in() ) );
620   if ( sessionServant )
621     sessionServant->NSunregister();
622
623   delete aGUISession;
624   delete guiThread;
625   delete myServerLauncher;
626   delete _NS;
627
628   try  {
629     orb->shutdown(0);
630   }
631   catch (...) {
632     //////////////////////////////////////////////////////////////
633     // VSR: silently skip exception:
634     // CORBA.BAD_INV_ORDER.BAD_INV_ORDER_ORBHasShutdown 
635     // exception is raised when orb->destroy() is called and
636     // cpp continer is launched in the embedded mode
637     //////////////////////////////////////////////////////////////
638     // std::cerr << "Caught unexpected exception on shutdown : ignored !!" << std::endl;
639     if ( shutdownAll )
640       killOmniNames();
641     abort(); //abort program to avoid deadlock in destructors or atexit when shutdown has been interrupted
642   }
643
644     //Destroy orb from python (for chasing memory leaks)
645   //PyRun_SimpleString("from omniORB import CORBA");
646   //PyRun_SimpleString("orb=CORBA.ORB_init([''], CORBA.ORB_ID)");
647   //PyRun_SimpleString("orb.destroy()");
648
649   // Destroy the ORB:
650   MESSAGE("Explicitely destroying the ORB (hoping to kill omniORB threads ...)");
651   ORB_INIT * init = SINGLETON_<ORB_INIT>::Instance();
652   if (init)
653     init->explicit_destroy();
654
655   // After ORB destruction
656   PyGILState_Ensure();
657   if(Py_IsInitialized())
658     Py_Finalize();
659
660   if ( shutdownAll )
661     killOmniNames();
662
663   MESSAGE( "Salome_Session_Server:endofserver" );
664   return result;
665 }