Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / ORB_Local_Config / Bug_2612 / DllOrb.cpp
blob7b5b18d717e59db72aba48e993a4375b42d16401
1 /*
2 * @author Iliyan jeliazkov <iliyan@ociweb.com>
3 * @author Lothar Werzinger <lothar@tradescape.biz>
4 */
6 #include "ace/Arg_Shifter.h"
7 #include "ace/SString.h"
8 #include "ace/OS_NS_unistd.h"
10 #include "tao/corba.h"
11 #include "tao/TAO_Singleton_Manager.h"
13 #include "DllOrb.h"
15 DllOrb::DllOrb (int nthreads)
17 m_nthreads_ (nthreads),
18 m_failPrePostInit(0),
19 #if defined (ACE_HAS_THREADS)
20 mp_barrier(0),
21 #endif
22 mv_orb(),
23 mv_rootPOA()
27 DllOrb::~DllOrb ( )
28 throw ()
30 #if defined (ACE_HAS_THREADS)
31 delete mp_barrier;
32 #endif
35 int DllOrb::init (int argc, ACE_TCHAR *argv[])
37 int result = 0;
38 int threadCnt = this->m_nthreads_;
40 try
42 ACE_Arg_Shifter as (argc, argv);
43 const ACE_TCHAR *currentArg = 0;
44 while (as.is_anything_left ())
46 if (0 != (currentArg = as.get_the_parameter (ACE_TEXT ("-t"))))
48 int num = ACE_OS::atoi (currentArg);
49 if (num >= 1)
50 threadCnt = num;
51 as.consume_arg ();
53 else
54 as.ignore_arg ();
57 if (m_failPrePostInit < 3)
59 ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Pre-ORB initialization ...\n"));
61 // -----------------------------------------------------------------
62 // Pre-ORB initialization steps necessary for proper DLL ORB
63 // support.
64 // -----------------------------------------------------------------
65 // Make sure TAO's singleton manager is initialized, and set to not
66 // register itself with the ACE_Object_Manager since it is under the
67 // control of the Service Configurator. If we register with the
68 // ACE_Object_Manager, then the ACE_Object_Manager will still hold
69 // (dangling) references to instances of objects created by this
70 // module and destroyed by this object when it is dynamically
71 // unloaded.
72 int register_with_object_manager = 0;
73 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance ();
74 result = p_tsm->init (register_with_object_manager);
76 if (result == -1 && m_failPrePostInit == 0)
77 return -1;
80 // Initialize the ORB
81 mv_orb = CORBA::ORB_init (argc, argv);
82 if (CORBA::is_nil (mv_orb.in ()))
83 return -1;
85 CORBA::Object_var v_poa = mv_orb->resolve_initial_references ("RootPOA");
87 mv_rootPOA = PortableServer::POA::_narrow (v_poa.in ());
88 if (CORBA::is_nil (mv_rootPOA.in ()))
89 return -1;
91 mv_poaManager = mv_rootPOA->the_POAManager ();
92 if (CORBA::is_nil (mv_poaManager.in ()))
93 return -1;
95 mv_poaManager->activate ();
97 catch(CORBA::Exception& ex)
99 ex._tao_print_exception (ACE_TEXT ("(%P|%t) init failed:"));
100 return -1;
102 catch (...)
104 ACE_ERROR_RETURN ((LM_ERROR,
105 ACE_TEXT ("(%P|%t) init failed\n")),
106 -1);
109 #if defined (ACE_HAS_THREADS)
110 mp_barrier = new ACE_Thread_Barrier (threadCnt + 1);
112 this->activate(
113 THR_NEW_LWP|THR_JOINABLE|THR_INHERIT_SCHED,
114 threadCnt
116 mp_barrier->wait ();
117 #endif
119 return 0;
122 int DllOrb::fini (void)
124 int result;
128 #if !defined (CORBA_E_COMPACT) && !defined (CORBA_E_MICRO) && !defined (TAO_HAS_MINIMUM_POA)
129 mv_poaManager->deactivate(1, 1);
130 #endif
131 mv_poaManager = PortableServer::POAManager::_nil();
133 // attempt to protect against sporadic BAD_INV_ORDER exceptions
134 ACE_OS::sleep(ACE_Time_Value(0, 500));
136 mv_orb->shutdown(1);
138 catch(CORBA::Exception& ex)
140 ex._tao_print_exception (ACE_TEXT ("(%P|%t) fini failed:"));
141 return -1;
143 catch(...)
145 ACE_ERROR_RETURN ((LM_ERROR,
146 ACE_TEXT ("(%P|%t) fini failed to deactivate/shutdown\n")),
147 -1);
151 // wait for our threads to finish
152 this->wait();
154 #if defined (ACE_HAS_THREADS)
155 delete mp_barrier;
156 mp_barrier = 0;
157 #endif
161 mv_orb->destroy();
162 mv_orb = CORBA::ORB::_nil();
164 catch(CORBA::Exception& ex)
166 ex._tao_print_exception (
167 ACE_TEXT (
168 "(%P|%t) init failed to destroy the orb:"));
169 return -1;
172 if (m_failPrePostInit < 3)
174 ACE_DEBUG((LM_DEBUG, "TEST (%P|%t) Post-ORB finalization ...\n"));
176 // -----------------------------------------------------------------
177 // Post-ORB finalization steps necessary for proper DLL ORB
178 // support.
179 // -----------------------------------------------------------------
180 // Explicitly clean up singletons created by TAO before
181 // unloading this module.
182 TAO_Singleton_Manager * p_tsm = TAO_Singleton_Manager::instance();
183 result = p_tsm->fini();
184 if (result == -1 && m_failPrePostInit == 0)
185 ACE_ERROR_RETURN ((LM_ERROR,
186 ACE_TEXT ("(%P|%t) fini failed to destroy TAO_Singleton_Manager\n")),
187 -1);
188 } /* end of if */
190 return 0;
193 int DllOrb::svc (void)
195 #if defined (ACE_HAS_THREADS)
196 mp_barrier->wait();
197 #endif
201 mv_orb->run();
203 catch(CORBA::BAD_INV_ORDER const & rc_ex)
205 const CORBA::ULong VMCID = rc_ex.minor() & 0xFFFFF000U;
206 const CORBA::ULong minorCode = rc_ex.minor() & 0xFFFU;
207 if (VMCID != CORBA::OMGVMCID || minorCode != 4)
208 ACE_ERROR_RETURN ((LM_ERROR,
209 ACE_TEXT ("(%P|%t) svc exits (-1)\n")),
210 -1);
212 catch(CORBA::Exception& ex)
214 ex._tao_print_exception (ACE_TEXT ("(%P|%t) svc - orb->run() failed:"));
215 return -1;
217 catch(...)
219 ACE_ERROR_RETURN ((LM_ERROR,
220 ACE_TEXT ("(%P|%t) svc got some exception\n")),
221 -1);
223 return 0;
226 ACE_FACTORY_DEFINE (DllOrb, DllOrb)