OrbManagement.cxx
上传用户:xfwatch
上传日期:2020-12-14
资源大小:872k
文件大小:8k
源码类别:

中间件编程

开发平台:

Java

  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2008, Red Hat, Inc., and others contributors as indicated
  4.  * by the @authors tag. All rights reserved.
  5.  * See the copyright.txt in the distribution for a
  6.  * full listing of individual contributors.
  7.  * This copyrighted material is made available to anyone wishing to use,
  8.  * modify, copy, or redistribute it subject to the terms and conditions
  9.  * of the GNU Lesser General Public License, v. 2.1.
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT A
  11.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  13.  * You should have received a copy of the GNU Lesser General Public License,
  14.  * v.2.1 along with this distribution; if not, write to the Free Software
  15.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16.  * MA  02110-1301, USA.
  17.  */
  18. #include <string.h>
  19. #include <tao/ORB.h>
  20. #include <tao/Object.h>
  21. //includes for looking up orbs
  22. #include "tao/ORB_Core.h"
  23. #include "tao/ORB_Table.h"
  24. #include "tao/ORB_Core_Auto_Ptr.h"
  25. #include <orbsvcs/CosNamingS.h>
  26. #include "OrbManagement.h"
  27. #include "AtmiBrokerEnv.h"
  28. #include "AtmiBrokerEnvXml.h"
  29. #include "log4cxx/logger.h"
  30. #include "Worker.h"
  31. #include "AtmiBrokerPoaFac.h"
  32. log4cxx::LoggerPtr loggerOrbManagement(log4cxx::Logger::getLogger(
  33. "OrbManagment"));
  34. int references = 0;
  35. CORBA_CONNECTION* connection;
  36. CORBA_CONNECTION* initOrb(char* connectionName) {
  37. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "initOrb" << connectionName);
  38. if (references == 0) {
  39. AtmiBrokerEnv::get_instance();
  40. connection = new CORBA_CONNECTION;
  41. connection->connectionName = connectionName;
  42. connection->orbRef = NULL;
  43. connection->root_poa = NULL;
  44. connection->root_poa_manager = NULL;
  45. connection->default_ctx = NULL;
  46. connection->callback_poa = NULL;
  47. connection->worker = NULL;
  48. connection->poaFactory = NULL;
  49. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "initOrb initing ORB");
  50. std::string values = orbConfig.opt;
  51. LOG4CXX_TRACE(loggerOrbManagement, (char*) "initOrb OPT: " << values);
  52. char * cstr, *p;
  53. cstr = new char[values.size() + 1];
  54. strcpy(cstr, values.c_str());
  55. char** vals = (char**) malloc(sizeof(char) * values.size() * 2);
  56. p = strtok(cstr, " ");
  57. int i = 0;
  58. while (p != NULL) {
  59. vals[i] = strdup(p);
  60. p = strtok(NULL, " ");
  61. i++;
  62. }
  63. int n = i;
  64. //when ORB_init return, i is 0
  65. connection->orbRef = CORBA::ORB_init(i, vals, connectionName);
  66. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "Created orb: "
  67. << connection->orbRef);
  68. int j;
  69. for (j = 0; j < n; j++) {
  70. free(vals[j]);
  71. }
  72. free(vals);
  73. delete[] cstr;
  74. LOG4CXX_DEBUG(loggerOrbManagement,
  75. (char*) "getNamingServiceAndContext getting Naming Service Ext");
  76. {
  77. CORBA::Object_var tmp_ref =
  78. connection->orbRef->resolve_initial_references(
  79. "NameService");
  80. LOG4CXX_DEBUG(
  81. loggerOrbManagement,
  82. (char*) "getNamingServiceAndContext got orbRef->resolve_initial_references, tmp_ref = %p"
  83. << (void*) tmp_ref);
  84. connection->default_ctx = CosNaming::NamingContextExt::_narrow(
  85. tmp_ref);
  86. }
  87. LOG4CXX_DEBUG(
  88. loggerOrbManagement,
  89. (char*) "getNamingServiceAndContext narrowed tmp_ref, default_context = "
  90. << connection->default_ctx);
  91. LOG4CXX_DEBUG(
  92. loggerOrbManagement,
  93. (char*) "getNamingServiceAndContext got Naming Service Instance ");
  94. connection->worker = new Worker(connection->orbRef, connectionName);
  95. if (((Worker*) connection->worker)->activate(
  96. THR_NEW_LWP | THR_JOINABLE, 1, 0, ACE_DEFAULT_THREAD_PRIORITY,
  97. -1, 0, 0, 0, 0, 0, 0) != 0) {
  98. delete ((Worker*) connection->worker);
  99. connection->worker = NULL;
  100. LOG4CXX_ERROR(loggerOrbManagement,
  101. (char*) "Could not start thread pool");
  102. }
  103. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "resolving the root POA");
  104. CORBA::Object_var tmp_ref =
  105. connection->orbRef->resolve_initial_references("RootPOA");
  106. connection->root_poa = PortableServer::POA::_narrow(tmp_ref);
  107. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "resolved the root POA: "
  108. << connection->root_poa);
  109. LOG4CXX_DEBUG(loggerOrbManagement,
  110. (char*) "getting the root POA manager");
  111. connection->root_poa_manager = connection->root_poa->the_POAManager();
  112. LOG4CXX_DEBUG(loggerOrbManagement,
  113. (char*) "getRootPOAAndManager got the root POA manager: "
  114. << connection->root_poa_manager);
  115. LOG4CXX_DEBUG(loggerOrbManagement,
  116. (char*) "creating POA with name client");
  117. AtmiBrokerPoaFac* poaFac = new AtmiBrokerPoaFac();
  118. connection->callback_poa = poaFac->createCallbackPoa(
  119. connection->orbRef, connectionName, connection->root_poa,
  120. connection->root_poa_manager);
  121. connection->poaFactory = poaFac;
  122. LOG4CXX_DEBUG(loggerOrbManagement,
  123. (char*) "createClientCallbackPOA created POA: "
  124. << connection->callback_poa);
  125. connection->root_poa_manager->activate();
  126. LOG4CXX_DEBUG(loggerOrbManagement,
  127. (char*) "activated poa - started processing requests");
  128. AtmiBrokerEnv::discard_instance();
  129. } else {
  130. LOG4CXX_DEBUG(loggerOrbManagement,
  131. (char*) "Returning corbaConnection singleton");
  132. }
  133. references++;
  134. return connection;
  135. }
  136. void shutdownBindings(CORBA_CONNECTION* connection) {
  137. LOG4CXX_DEBUG(loggerOrbManagement, "shutdownBindings");
  138. references--;
  139. if (references == 0) {
  140. LOG4CXX_DEBUG(loggerOrbManagement, "Closing Bindings");
  141. if (connection->orbRef) {
  142. if (!CORBA::is_nil(connection->orbRef)) {
  143. LOG4CXX_DEBUG(loggerOrbManagement,
  144. "shutdownBindings shutting down ORB: "
  145. << connection->orbRef);
  146. try {
  147. connection->orbRef->shutdown(1);
  148. LOG4CXX_DEBUG(loggerOrbManagement,
  149. "shutdownBindings shut down ORB");
  150. } catch (CORBA::Exception &ex) {
  151. LOG4CXX_ERROR(
  152. loggerOrbManagement,
  153. (char*) "shutdownBindings Unexpected CORBA exception shutting down orb: "
  154. << ex._name());
  155. } catch (...) {
  156. LOG4CXX_FATAL(
  157. loggerOrbManagement,
  158. (char*) "shutdownBindings Unexpected fatal exception");
  159. }
  160. if (connection->worker != NULL) {
  161. ((Worker*) connection->worker)->wait();
  162. delete ((Worker*) connection->worker);
  163. connection->worker = NULL;
  164. }
  165. if (connection->poaFactory != NULL) {
  166. delete connection->poaFactory;
  167. connection->poaFactory = NULL;
  168. }
  169. try {
  170. LOG4CXX_DEBUG(loggerOrbManagement,
  171. (char*) "shutdownBindings destroying ORB");
  172. connection->orbRef->destroy();
  173. LOG4CXX_DEBUG(loggerOrbManagement,
  174. (char*) "shutdownBindings destroyed ORB");
  175. } catch (CORBA::Exception &ex) {
  176. LOG4CXX_ERROR(
  177. loggerOrbManagement,
  178. (char*) "shutdownBindings Unexpected CORBA exception destroying orb: "
  179. << ex._name());
  180. }
  181. if (connection->callback_poa) {
  182. LOG4CXX_DEBUG(loggerOrbManagement,
  183. (char*) "shutdownBindings destroying callback_poa");
  184. // delete innerPoa;
  185. connection->callback_poa = NULL;
  186. LOG4CXX_DEBUG(loggerOrbManagement,
  187. (char*) "shutdownBindings destroyed callback_poa");
  188. }
  189. if (connection->default_ctx) {
  190. LOG4CXX_DEBUG(loggerOrbManagement,
  191. (char*) "shutdownBindings destroying default_ctx");
  192. // delete ctx;
  193. CORBA::release(connection->default_ctx);
  194. connection->default_ctx = NULL;
  195. LOG4CXX_DEBUG(loggerOrbManagement,
  196. (char*) "shutdownBindings destroyed default_ctx");
  197. }
  198. if (connection->root_poa_manager) {
  199. LOG4CXX_DEBUG(
  200. loggerOrbManagement,
  201. (char*) "shutdownBindings destroying root_poa_manager");
  202. // delete poa_manager;
  203. connection->root_poa_manager = NULL;
  204. LOG4CXX_DEBUG(
  205. loggerOrbManagement,
  206. (char*) "shutdownBindings destroyed root_poa_manager");
  207. }
  208. if (connection->root_poa) {
  209. LOG4CXX_DEBUG(loggerOrbManagement,
  210. (char*) "shutdownBindings destroying root_poa");
  211. // delete poa;
  212. //connection->root_poa->destroy(false, false); // hmm it doesn't like this
  213. //destroy(in boolean etherealize_objects,  in boolean wait_for_completion)
  214. connection->root_poa = NULL;
  215. LOG4CXX_DEBUG(loggerOrbManagement,
  216. (char*) "shutdownBindings destroyed root_poa");
  217. } //*/
  218. connection->orbRef = NULL;
  219. delete connection;
  220. LOG4CXX_DEBUG(loggerOrbManagement, (char*) "Closed Bindings");
  221. }
  222. }
  223. } else {
  224. LOG4CXX_DEBUG(loggerOrbManagement, "Not Closing Bindings");
  225. }
  226. }