PageRenderTime 3859ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jboss-5.1.0/iiop/src/main/org/jboss/invocation/iiop/IIOPInvoker.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 646 lines | 467 code | 88 blank | 91 comment | 30 complexity | 71cdbfab68cf9292665c8d0efb71a6ae MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.invocation.iiop;
  23. import java.net.InetAddress;
  24. import java.util.Collections;
  25. import java.util.Map;
  26. import java.util.HashMap;
  27. import java.util.Hashtable;
  28. import javax.naming.Context;
  29. import javax.naming.InitialContext;
  30. import javax.naming.Name;
  31. import javax.naming.NamingException;
  32. import javax.naming.Reference;
  33. import javax.naming.spi.ObjectFactory;
  34. import org.omg.CORBA.LocalObject;
  35. import org.omg.CORBA.Policy;
  36. import org.omg.CORBA.SetOverrideType;
  37. import org.omg.CORBA.UNKNOWN;
  38. import org.omg.PortableServer.IdAssignmentPolicyValue;
  39. import org.omg.PortableServer.IdUniquenessPolicyValue;
  40. import org.omg.PortableServer.LifespanPolicyValue;
  41. import org.omg.PortableServer.POA;
  42. import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
  43. import org.omg.PortableServer.RequestProcessingPolicyValue;
  44. import org.omg.PortableServer.Servant;
  45. import org.omg.PortableServer.ServantLocator;
  46. import org.omg.PortableServer.ServantLocatorPackage.CookieHolder;
  47. import org.omg.PortableServer.ServantRetentionPolicyValue;
  48. import org.jboss.iiop.CorbaORBService;
  49. import org.jboss.naming.Util;
  50. import org.jboss.system.ServiceMBeanSupport;
  51. import org.jboss.system.Registry;
  52. /**
  53. * IIOP invoker that routs IIOP requests to CORBA servants.
  54. * It implements the interface <code>ServantRegistries</code>, which
  55. * gives access to four <code>ServantRegistry</code> instances:
  56. * <ul>
  57. * <li>a <code>ServantRegistry</code> with a single transient POA
  58. * shared among all its servants;</li>
  59. * <li>a <code>ServantRegistry</code> with a single persistent POA
  60. * shared among all its servants;</li>
  61. * <li>a <code>ServantRegistry</code> with a transient POA per servant;</li>
  62. * <li>a <code>ServantRegistry</code> with persistent POA per servant.</li>
  63. * </ul>
  64. *
  65. * CORBA servants registered with any of these
  66. * <code>ServantRegistry</code> instances will receive IIOP invocations.
  67. * These CORBA servants will typically be thin wrappers that merely forward
  68. * to the JBoss MBean server any invocations they receive.
  69. *
  70. * @author <a href="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
  71. * @version $Revision: 81018 $
  72. */
  73. public class IIOPInvoker
  74. extends ServiceMBeanSupport
  75. implements IIOPInvokerMBean, ServantRegistries, ObjectFactory
  76. {
  77. // Attributes -------------------------------------------------------------
  78. /** A reference to the singleton IIOPInvoker. */
  79. private static IIOPInvoker theIIOPInvoker;
  80. /** The root POA. **/
  81. private POA rootPOA;
  82. /** A ServantRegistry with a transient POA shared by all servants. */
  83. private ServantRegistry registryWithSharedTransientPOA;
  84. /** The transient POA used by the ServantRegistry above. */
  85. private POA transientPOA;
  86. /** The transient servant map used by the ServantRegistry above. */
  87. private Map transientServantMap;
  88. /** A ServantRegistry with a persistent POA shared by all servants. */
  89. private ServantRegistry registryWithSharedPersistentPOA;
  90. /** The persistent POA used by the ServantRegistry above. */
  91. private POA persistentPOA;
  92. /** The persistent servant map used by the ServantRegistry above. */
  93. private Map persistentServantMap;
  94. /** A ServantRegistry with a transient POA per servant. */
  95. private ServantRegistry registryWithTransientPOAPerServant;
  96. /** The transient POA map used by the ServantRegistry above. */
  97. private Map transientPoaMap;
  98. /** POA policies used by the ServantRegistry above. */
  99. private Policy[] transientPoaPolicies;
  100. /** A ServantRegistry with a persistent POA per servant. */
  101. private ServantRegistry registryWithPersistentPOAPerServant;
  102. /** The persistent POA map used by the ServantRegistry above. */
  103. private Map persistentPoaMap;
  104. /** POA policies used by the ServantRegistry above. */
  105. private Policy[] persistentPoaPolicies;
  106. // ServiceMBeanSupport overrides ---------------------------------
  107. public void createService()
  108. throws Exception
  109. {
  110. theIIOPInvoker = this;
  111. transientServantMap = Collections.synchronizedMap(new HashMap());
  112. persistentServantMap = Collections.synchronizedMap(new HashMap());
  113. transientPoaMap = Collections.synchronizedMap(new HashMap());
  114. persistentPoaMap = Collections.synchronizedMap(new HashMap());
  115. }
  116. public void startService()
  117. throws Exception
  118. {
  119. // Get a reference for the root POA
  120. try {
  121. rootPOA = (POA)new InitialContext().lookup("java:/"
  122. + CorbaORBService.POA_NAME);
  123. }
  124. catch (NamingException e) {
  125. throw new RuntimeException("Cannot lookup java:/"
  126. + CorbaORBService.POA_NAME + ": " + e);
  127. }
  128. // Policies for per-servant transient POAs
  129. transientPoaPolicies = new Policy[] {
  130. rootPOA.create_lifespan_policy(
  131. LifespanPolicyValue.TRANSIENT),
  132. rootPOA.create_id_assignment_policy(
  133. IdAssignmentPolicyValue.USER_ID),
  134. rootPOA.create_servant_retention_policy(
  135. ServantRetentionPolicyValue.NON_RETAIN),
  136. rootPOA.create_request_processing_policy(
  137. RequestProcessingPolicyValue.USE_DEFAULT_SERVANT),
  138. rootPOA.create_id_uniqueness_policy(
  139. IdUniquenessPolicyValue.MULTIPLE_ID),
  140. };
  141. // Policies for per-servant persistent POAs
  142. persistentPoaPolicies = new Policy[] {
  143. rootPOA.create_lifespan_policy(
  144. LifespanPolicyValue.PERSISTENT),
  145. rootPOA.create_id_assignment_policy(
  146. IdAssignmentPolicyValue.USER_ID),
  147. rootPOA.create_servant_retention_policy(
  148. ServantRetentionPolicyValue.NON_RETAIN),
  149. rootPOA.create_request_processing_policy(
  150. RequestProcessingPolicyValue.USE_DEFAULT_SERVANT),
  151. rootPOA.create_id_uniqueness_policy(
  152. IdUniquenessPolicyValue.MULTIPLE_ID),
  153. };
  154. // Policies for this IIOPInvoker's shared transient POA
  155. Policy[] policies = new Policy[] {
  156. rootPOA.create_lifespan_policy(
  157. LifespanPolicyValue.TRANSIENT),
  158. rootPOA.create_id_assignment_policy(
  159. IdAssignmentPolicyValue.USER_ID),
  160. rootPOA.create_servant_retention_policy(
  161. ServantRetentionPolicyValue.NON_RETAIN),
  162. rootPOA.create_request_processing_policy(
  163. RequestProcessingPolicyValue.USE_SERVANT_MANAGER),
  164. rootPOA.create_id_uniqueness_policy(
  165. IdUniquenessPolicyValue.MULTIPLE_ID)
  166. };
  167. // Create this IIOPInvoker's shared transient POA
  168. // and set its servant locator
  169. transientPOA = rootPOA.create_POA("TPOA", null, policies);
  170. transientPOA.set_servant_manager(new TransientServantLocator());
  171. // Change just one policy for this IIOPInvoker's shared persistent POA
  172. policies[0] = rootPOA.create_lifespan_policy(
  173. LifespanPolicyValue.PERSISTENT);
  174. // Create this IIOPInvoker's shared persisten POA
  175. // and set its servant locator
  176. persistentPOA = rootPOA.create_POA("PPOA", null, policies);
  177. persistentPOA.set_servant_manager(new PersistentServantLocator());
  178. // Create this IIOPInvoker's ServantRegistry implementations
  179. registryWithSharedTransientPOA =
  180. new ServantRegistryWithSharedTransientPOA();
  181. registryWithSharedPersistentPOA =
  182. new ServantRegistryWithSharedPersistentPOA();
  183. registryWithTransientPOAPerServant =
  184. new ServantRegistryWithTransientPOAPerServant();
  185. registryWithPersistentPOAPerServant =
  186. new ServantRegistryWithPersistentPOAPerServant();
  187. // Export this invoker
  188. Registry.bind(getServiceName(), this);
  189. // Activate my shared POAs
  190. transientPOA.the_POAManager().activate();
  191. persistentPOA.the_POAManager().activate();
  192. Context context = new InitialContext();
  193. // Bind the invoker in the JNDI invoker naming space
  194. Util.rebind(
  195. // The context
  196. context,
  197. // It should look like so "invokers/<name>/iiop"
  198. "invokers/" + InetAddress.getLocalHost().getHostName() + "/iiop",
  199. // A reference to this invoker
  200. new Reference(getClass().getName(),
  201. getClass().getName(),
  202. null));
  203. getLog().debug("Bound IIOP invoker for JMX node");
  204. }
  205. public void stopService()
  206. throws Exception
  207. {
  208. // Destroy my shared POAs
  209. try {
  210. transientPOA.the_POAManager().deactivate(
  211. false, /* etherealize_objects */
  212. true /* wait_for_completion */ );
  213. persistentPOA.the_POAManager().deactivate(
  214. false, /* etherealize_objects */
  215. true /* wait_for_completion */ );
  216. transientPOA.destroy(false, /* etherealize_objects */
  217. false /* wait_for_completion */ );
  218. persistentPOA.destroy(false, /* etherealize_objects */
  219. false /* wait_for_completion */ );
  220. }
  221. catch (AdapterInactive adapterInactive) {
  222. getLog().error("Cannot deactivate home POA", adapterInactive);
  223. }
  224. }
  225. // Auxiliary static methods -----------------------------------------------
  226. private static Policy[] concatPolicies(Policy[] policies1,
  227. Policy[] policies2)
  228. {
  229. Policy[] policies = new Policy[policies1.length + policies2.length];
  230. int j = 0;
  231. for (int i = 0; i < policies1.length; i++, j++) {
  232. policies[j] = policies1[i];
  233. }
  234. for (int i = 0; i < policies2.length; i++, j++) {
  235. policies[j] = policies2[i];
  236. }
  237. return policies;
  238. }
  239. // Implementation of the interface ServantRegistries -----------------------
  240. public ServantRegistry getServantRegistry(ServantRegistryKind kind)
  241. {
  242. if (kind == ServantRegistryKind.SHARED_TRANSIENT_POA) {
  243. return registryWithSharedTransientPOA;
  244. }
  245. else if (kind == ServantRegistryKind.SHARED_PERSISTENT_POA) {
  246. return registryWithSharedPersistentPOA;
  247. }
  248. else if (kind == ServantRegistryKind.TRANSIENT_POA_PER_SERVANT) {
  249. return registryWithTransientPOAPerServant;
  250. }
  251. else if (kind == ServantRegistryKind.PERSISTENT_POA_PER_SERVANT) {
  252. return registryWithPersistentPOAPerServant;
  253. }
  254. else {
  255. return null;
  256. }
  257. }
  258. // Implementation of the interface ObjectFactory ---------------------------
  259. public Object getObjectInstance(Object obj, Name name,
  260. Context nameCtx, Hashtable environment)
  261. throws Exception
  262. {
  263. String s = name.toString();
  264. if (getLog().isTraceEnabled())
  265. getLog().trace("getObjectInstance: obj.getClass().getName=\"" +
  266. obj.getClass().getName() +
  267. "\n name=" + s);
  268. if (s.equals("iiop"))
  269. return theIIOPInvoker;
  270. else
  271. return null;
  272. }
  273. // Static nested classes that implement the interface ReferenceFactory -----
  274. static class PoaAndPoliciesReferenceFactory
  275. implements ReferenceFactory
  276. {
  277. private POA poa;
  278. private String servantName;
  279. private Policy[] policies;
  280. private byte[] servantId;
  281. PoaAndPoliciesReferenceFactory(POA poa,
  282. String servantName, Policy[] policies)
  283. {
  284. this.poa = poa;
  285. this.servantName = servantName;
  286. this.policies = policies;
  287. servantId = ReferenceData.create(servantName);
  288. }
  289. PoaAndPoliciesReferenceFactory(POA poa, Policy[] policies)
  290. {
  291. this(poa, null, policies);
  292. }
  293. public org.omg.CORBA.Object createReference(String interfId)
  294. throws Exception
  295. {
  296. org.omg.CORBA.Object corbaRef =
  297. poa.create_reference_with_id(servantId, interfId);
  298. return corbaRef._set_policy_override(policies,
  299. SetOverrideType.ADD_OVERRIDE);
  300. }
  301. public org.omg.CORBA.Object createReferenceWithId(Object id,
  302. String interfId)
  303. throws Exception
  304. {
  305. byte[] referenceData =
  306. (servantName == null) ? ReferenceData.create(id)
  307. : ReferenceData.create(servantName, id);
  308. org.omg.CORBA.Object corbaRef =
  309. poa.create_reference_with_id(referenceData, interfId);
  310. return corbaRef._set_policy_override(policies,
  311. SetOverrideType.ADD_OVERRIDE);
  312. }
  313. public POA getPOA()
  314. {
  315. return poa;
  316. }
  317. }
  318. static class PoaReferenceFactory
  319. implements ReferenceFactory
  320. {
  321. private POA poa;
  322. private String servantName;
  323. private byte[] servantId;
  324. PoaReferenceFactory(POA poa, String servantName)
  325. {
  326. this.poa = poa;
  327. this.servantName = servantName;
  328. servantId = ReferenceData.create(servantName);
  329. }
  330. PoaReferenceFactory(POA poa)
  331. {
  332. this(poa, null);
  333. }
  334. public org.omg.CORBA.Object createReference(String interfId)
  335. throws Exception
  336. {
  337. return poa.create_reference_with_id(servantId, interfId);
  338. }
  339. public org.omg.CORBA.Object createReferenceWithId(Object id,
  340. String interfId)
  341. throws Exception
  342. {
  343. byte[] referenceData =
  344. (servantName == null) ? ReferenceData.create(id)
  345. : ReferenceData.create(servantName, id);
  346. return poa.create_reference_with_id(referenceData, interfId);
  347. }
  348. public POA getPOA()
  349. {
  350. return poa;
  351. }
  352. }
  353. // Inner classes that implement the interface ServantRegistry --------------
  354. /** ServantRegistry with a shared transient POA */
  355. class ServantRegistryWithSharedTransientPOA
  356. implements ServantRegistry
  357. {
  358. public ReferenceFactory bind(String name,
  359. Servant servant,
  360. Policy[] policies)
  361. {
  362. if (servant instanceof ServantWithMBeanServer) {
  363. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  364. }
  365. transientServantMap.put(name, servant);
  366. return new PoaAndPoliciesReferenceFactory(transientPOA,
  367. name, policies);
  368. }
  369. public ReferenceFactory bind(String name, Servant servant)
  370. {
  371. if (servant instanceof ServantWithMBeanServer) {
  372. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  373. }
  374. transientServantMap.put(name, servant);
  375. return new PoaReferenceFactory(transientPOA, name);
  376. }
  377. public void unbind(String name)
  378. {
  379. transientServantMap.remove(name);
  380. }
  381. }
  382. /** ServantRegistry with a shared persistent POA */
  383. class ServantRegistryWithSharedPersistentPOA
  384. implements ServantRegistry
  385. {
  386. public ReferenceFactory bind(String name,
  387. Servant servant,
  388. Policy[] policies)
  389. {
  390. if (servant instanceof ServantWithMBeanServer) {
  391. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  392. }
  393. persistentServantMap.put(name, servant);
  394. return new PoaAndPoliciesReferenceFactory(persistentPOA,
  395. name, policies);
  396. }
  397. public ReferenceFactory bind(String name, Servant servant)
  398. {
  399. if (servant instanceof ServantWithMBeanServer) {
  400. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  401. }
  402. persistentServantMap.put(name, servant);
  403. return new PoaReferenceFactory(persistentPOA, name);
  404. }
  405. public void unbind(String name)
  406. {
  407. persistentServantMap.remove(name);
  408. }
  409. }
  410. /** ServantRegistry with a transient POA per servant */
  411. class ServantRegistryWithTransientPOAPerServant
  412. implements ServantRegistry
  413. {
  414. public ReferenceFactory bind(String name,
  415. Servant servant,
  416. Policy[] policies)
  417. throws Exception
  418. {
  419. if (servant instanceof ServantWithMBeanServer) {
  420. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  421. }
  422. Policy[] poaPolicies = concatPolicies(transientPoaPolicies, policies);
  423. POA poa = rootPOA.create_POA(name, null, poaPolicies);
  424. transientPoaMap.put(name, poa);
  425. poa.set_servant(servant);
  426. poa.the_POAManager().activate();
  427. return new PoaReferenceFactory(poa); // no servantName: in this case
  428. // name is the POA name
  429. }
  430. public ReferenceFactory bind(String name, Servant servant)
  431. throws Exception
  432. {
  433. if (servant instanceof ServantWithMBeanServer) {
  434. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  435. }
  436. POA poa = rootPOA.create_POA(name, null, transientPoaPolicies);
  437. transientPoaMap.put(name, poa);
  438. poa.set_servant(servant);
  439. poa.the_POAManager().activate();
  440. return new PoaReferenceFactory(poa); // no servantName: in this case
  441. // name is the POA name
  442. }
  443. public void unbind(String name)
  444. throws Exception
  445. {
  446. POA poa = (POA) transientPoaMap.remove(name);
  447. if (poa != null) {
  448. poa.the_POAManager().deactivate(false, /* etherealize_objects */
  449. true /* wait_for_completion */ );
  450. poa.destroy(false, /* etherealize_objects */
  451. false /* wait_for_completion */ );
  452. }
  453. }
  454. }
  455. /** ServantRegistry with a persistent POA per servant */
  456. class ServantRegistryWithPersistentPOAPerServant
  457. implements ServantRegistry
  458. {
  459. public ReferenceFactory bind(String name,
  460. Servant servant,
  461. Policy[] policies)
  462. throws Exception
  463. {
  464. if (servant instanceof ServantWithMBeanServer) {
  465. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  466. }
  467. Policy[] poaPolicies =
  468. concatPolicies(persistentPoaPolicies, policies);
  469. POA poa = rootPOA.create_POA(name, null, poaPolicies);
  470. persistentPoaMap.put(name, poa);
  471. poa.set_servant(servant);
  472. poa.the_POAManager().activate();
  473. return new PoaReferenceFactory(poa); // no servantName: in this case
  474. // name is the POA name
  475. }
  476. public ReferenceFactory bind(String name, Servant servant)
  477. throws Exception
  478. {
  479. if (servant instanceof ServantWithMBeanServer) {
  480. ((ServantWithMBeanServer)servant).setMBeanServer(getServer());
  481. }
  482. POA poa = rootPOA.create_POA(name, null, persistentPoaPolicies);
  483. persistentPoaMap.put(name, poa);
  484. poa.set_servant(servant);
  485. poa.the_POAManager().activate();
  486. return new PoaReferenceFactory(poa); // no servantName: in this case
  487. // name is the POA name
  488. }
  489. public void unbind(String name)
  490. throws Exception
  491. {
  492. POA poa = (POA) persistentPoaMap.remove(name);
  493. if (poa != null) {
  494. poa.the_POAManager().deactivate(false, /* etherealize_objects */
  495. true /* wait_for_completion */ );
  496. poa.destroy(false, /* etherealize_objects */
  497. false /* wait_for_completion */ );
  498. }
  499. }
  500. }
  501. // Inner classes that implement the interface ServantLocator ---------------
  502. /** ServantLocator for the shared transient POA */
  503. class TransientServantLocator
  504. extends LocalObject
  505. implements ServantLocator
  506. {
  507. public Servant preinvoke(byte[] oid,
  508. POA adapter,
  509. String operation,
  510. CookieHolder the_cookie)
  511. {
  512. try {
  513. the_cookie.value = null;
  514. Object id = ReferenceData.extractServantId(oid);
  515. return (Servant)transientServantMap.get(id);
  516. }
  517. catch (Exception e) {
  518. getLog().trace("Unexpected exception in preinvoke:", e);
  519. throw new UNKNOWN(e.toString());
  520. }
  521. }
  522. public void postinvoke(byte[] oid,
  523. POA adapter,
  524. String operation,
  525. Object the_cookie,
  526. Servant the_servant)
  527. {
  528. }
  529. }
  530. /** ServantLocator for the shared persistent POA */
  531. class PersistentServantLocator
  532. extends LocalObject
  533. implements ServantLocator
  534. {
  535. public Servant preinvoke(byte[] oid,
  536. POA adapter,
  537. String operation,
  538. CookieHolder the_cookie)
  539. {
  540. try {
  541. the_cookie.value = null;
  542. Object id = ReferenceData.extractServantId(oid);
  543. return (Servant)persistentServantMap.get(id);
  544. }
  545. catch (Exception e) {
  546. getLog().trace("Unexpected exception in preinvoke:", e);
  547. throw new UNKNOWN(e.toString());
  548. }
  549. }
  550. public void postinvoke(byte[] oid,
  551. POA adapter,
  552. String operation,
  553. Object the_cookie,
  554. Servant the_servant)
  555. {
  556. }
  557. }
  558. }