1 22 package org.jboss.invocation.iiop; 23 24 import java.net.InetAddress ; 25 import java.util.Collections ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Hashtable ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.naming.Name ; 32 import javax.naming.NamingException ; 33 import javax.naming.Reference ; 34 import javax.naming.spi.ObjectFactory ; 35 36 import org.omg.CORBA.LocalObject ; 37 import org.omg.CORBA.Policy ; 38 import org.omg.CORBA.SetOverrideType ; 39 import org.omg.CORBA.UNKNOWN ; 40 import org.omg.PortableServer.IdAssignmentPolicyValue ; 41 import org.omg.PortableServer.IdUniquenessPolicyValue ; 42 import org.omg.PortableServer.LifespanPolicyValue ; 43 import org.omg.PortableServer.POA ; 44 import org.omg.PortableServer.POAManagerPackage.AdapterInactive ; 45 import org.omg.PortableServer.RequestProcessingPolicyValue ; 46 import org.omg.PortableServer.Servant ; 47 import org.omg.PortableServer.ServantLocator ; 48 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder ; 49 import org.omg.PortableServer.ServantRetentionPolicyValue ; 50 51 import org.jboss.iiop.CorbaORBService; 52 import org.jboss.naming.Util; 53 import org.jboss.system.ServiceMBeanSupport; 54 import org.jboss.system.Registry; 55 56 77 public class IIOPInvoker 78 extends ServiceMBeanSupport 79 implements IIOPInvokerMBean, ServantRegistries, ObjectFactory 80 { 81 82 84 85 private static IIOPInvoker theIIOPInvoker; 86 87 88 private POA rootPOA; 89 90 91 private ServantRegistry registryWithSharedTransientPOA; 92 93 94 private POA transientPOA; 95 96 97 private Map transientServantMap; 98 99 100 private ServantRegistry registryWithSharedPersistentPOA; 101 102 103 private POA persistentPOA; 104 105 106 private Map persistentServantMap; 107 108 109 private ServantRegistry registryWithTransientPOAPerServant; 110 111 112 private Map transientPoaMap; 113 114 115 private Policy [] transientPoaPolicies; 116 117 118 private ServantRegistry registryWithPersistentPOAPerServant; 119 120 121 private Map persistentPoaMap; 122 123 124 private Policy [] persistentPoaPolicies; 125 126 127 129 public void createService() 130 throws Exception 131 { 132 theIIOPInvoker = this; 133 transientServantMap = Collections.synchronizedMap(new HashMap ()); 134 persistentServantMap = Collections.synchronizedMap(new HashMap ()); 135 transientPoaMap = Collections.synchronizedMap(new HashMap ()); 136 persistentPoaMap = Collections.synchronizedMap(new HashMap ()); 137 } 138 139 public void startService() 140 throws Exception 141 { 142 try { 144 rootPOA = (POA )new InitialContext ().lookup("java:/" 145 + CorbaORBService.POA_NAME); 146 } 147 catch (NamingException e) { 148 throw new RuntimeException ("Cannot lookup java:/" 149 + CorbaORBService.POA_NAME + ": " + e); 150 } 151 152 transientPoaPolicies = new Policy [] { 154 rootPOA.create_lifespan_policy( 155 LifespanPolicyValue.TRANSIENT), 156 rootPOA.create_id_assignment_policy( 157 IdAssignmentPolicyValue.USER_ID), 158 rootPOA.create_servant_retention_policy( 159 ServantRetentionPolicyValue.NON_RETAIN), 160 rootPOA.create_request_processing_policy( 161 RequestProcessingPolicyValue.USE_DEFAULT_SERVANT), 162 rootPOA.create_id_uniqueness_policy( 163 IdUniquenessPolicyValue.MULTIPLE_ID), 164 }; 165 166 persistentPoaPolicies = new Policy [] { 168 rootPOA.create_lifespan_policy( 169 LifespanPolicyValue.PERSISTENT), 170 rootPOA.create_id_assignment_policy( 171 IdAssignmentPolicyValue.USER_ID), 172 rootPOA.create_servant_retention_policy( 173 ServantRetentionPolicyValue.NON_RETAIN), 174 rootPOA.create_request_processing_policy( 175 RequestProcessingPolicyValue.USE_DEFAULT_SERVANT), 176 rootPOA.create_id_uniqueness_policy( 177 IdUniquenessPolicyValue.MULTIPLE_ID), 178 }; 179 180 Policy [] policies = new Policy [] { 182 rootPOA.create_lifespan_policy( 183 LifespanPolicyValue.TRANSIENT), 184 rootPOA.create_id_assignment_policy( 185 IdAssignmentPolicyValue.USER_ID), 186 rootPOA.create_servant_retention_policy( 187 ServantRetentionPolicyValue.NON_RETAIN), 188 rootPOA.create_request_processing_policy( 189 RequestProcessingPolicyValue.USE_SERVANT_MANAGER), 190 rootPOA.create_id_uniqueness_policy( 191 IdUniquenessPolicyValue.MULTIPLE_ID) 192 }; 193 194 transientPOA = rootPOA.create_POA("TPOA", null, policies); 197 transientPOA.set_servant_manager(new TransientServantLocator()); 198 199 policies[0] = rootPOA.create_lifespan_policy( 201 LifespanPolicyValue.PERSISTENT); 202 203 persistentPOA = rootPOA.create_POA("PPOA", null, policies); 206 persistentPOA.set_servant_manager(new PersistentServantLocator()); 207 208 registryWithSharedTransientPOA = 210 new ServantRegistryWithSharedTransientPOA(); 211 registryWithSharedPersistentPOA = 212 new ServantRegistryWithSharedPersistentPOA(); 213 registryWithTransientPOAPerServant = 214 new ServantRegistryWithTransientPOAPerServant(); 215 registryWithPersistentPOAPerServant = 216 new ServantRegistryWithPersistentPOAPerServant(); 217 218 Registry.bind(getServiceName(), this); 220 221 transientPOA.the_POAManager().activate(); 223 persistentPOA.the_POAManager().activate(); 224 225 Context context = new InitialContext (); 226 227 Util.rebind( 229 context, 231 "invokers/" + InetAddress.getLocalHost().getHostName() + "/iiop", 233 new Reference (getClass().getName(), 235 getClass().getName(), 236 null)); 237 238 getLog().debug("Bound IIOP invoker for JMX node"); 239 } 240 241 public void stopService() 242 throws Exception 243 { 244 try { 246 transientPOA.the_POAManager().deactivate( 247 false, 248 true ); 249 persistentPOA.the_POAManager().deactivate( 250 false, 251 true ); 252 transientPOA.destroy(false, 253 false ); 254 persistentPOA.destroy(false, 255 false ); 256 } 257 catch (AdapterInactive adapterInactive) { 258 getLog().error("Cannot deactivate home POA", adapterInactive); 259 } 260 } 261 262 264 private static Policy [] concatPolicies(Policy [] policies1, 265 Policy [] policies2) 266 { 267 Policy [] policies = new Policy [policies1.length + policies2.length]; 268 int j = 0; 269 for (int i = 0; i < policies1.length; i++, j++) { 270 policies[j] = policies1[i]; 271 } 272 for (int i = 0; i < policies2.length; i++, j++) { 273 policies[j] = policies2[i]; 274 } 275 return policies; 276 } 277 278 279 281 public ServantRegistry getServantRegistry(ServantRegistryKind kind) 282 { 283 if (kind == ServantRegistryKind.SHARED_TRANSIENT_POA) { 284 return registryWithSharedTransientPOA; 285 } 286 else if (kind == ServantRegistryKind.SHARED_PERSISTENT_POA) { 287 return registryWithSharedPersistentPOA; 288 } 289 else if (kind == ServantRegistryKind.TRANSIENT_POA_PER_SERVANT) { 290 return registryWithTransientPOAPerServant; 291 } 292 else if (kind == ServantRegistryKind.PERSISTENT_POA_PER_SERVANT) { 293 return registryWithPersistentPOAPerServant; 294 } 295 else { 296 return null; 297 } 298 } 299 300 302 public Object getObjectInstance(Object obj, Name name, 303 Context nameCtx, Hashtable environment) 304 throws Exception 305 { 306 String s = name.toString(); 307 if (getLog().isTraceEnabled()) 308 getLog().trace("getObjectInstance: obj.getClass().getName=\"" + 309 obj.getClass().getName() + 310 "\n name=" + s); 311 if (s.equals("iiop")) 312 return theIIOPInvoker; 313 else 314 return null; 315 } 316 317 319 static class PoaAndPoliciesReferenceFactory 320 implements ReferenceFactory 321 { 322 private POA poa; 323 private String servantName; 324 private Policy [] policies; 325 private byte[] servantId; 326 327 PoaAndPoliciesReferenceFactory(POA poa, 328 String servantName, Policy [] policies) 329 { 330 this.poa = poa; 331 this.servantName = servantName; 332 this.policies = policies; 333 servantId = ReferenceData.create(servantName); 334 } 335 336 PoaAndPoliciesReferenceFactory(POA poa, Policy [] policies) 337 { 338 this(poa, null, policies); 339 } 340 341 public org.omg.CORBA.Object createReference(String interfId) 342 throws Exception 343 { 344 org.omg.CORBA.Object corbaRef = 345 poa.create_reference_with_id(servantId, interfId); 346 return corbaRef._set_policy_override(policies, 347 SetOverrideType.ADD_OVERRIDE); 348 } 349 350 public org.omg.CORBA.Object createReferenceWithId(Object id, 351 String interfId) 352 throws Exception 353 { 354 byte[] referenceData = 355 (servantName == null) ? ReferenceData.create(id) 356 : ReferenceData.create(servantName, id); 357 org.omg.CORBA.Object corbaRef = 358 poa.create_reference_with_id(referenceData, interfId); 359 return corbaRef._set_policy_override(policies, 360 SetOverrideType.ADD_OVERRIDE); 361 } 362 363 public POA getPOA() 364 { 365 return poa; 366 } 367 368 } 369 370 static class PoaReferenceFactory 371 implements ReferenceFactory 372 { 373 private POA poa; 374 private String servantName; 375 private byte[] servantId; 376 377 378 PoaReferenceFactory(POA poa, String servantName) 379 { 380 this.poa = poa; 381 this.servantName = servantName; 382 servantId = ReferenceData.create(servantName); 383 } 384 385 PoaReferenceFactory(POA poa) 386 { 387 this(poa, null); 388 } 389 390 public org.omg.CORBA.Object createReference(String interfId) 391 throws Exception 392 { 393 return poa.create_reference_with_id(servantId, interfId); 394 } 395 396 public org.omg.CORBA.Object createReferenceWithId(Object id, 397 String interfId) 398 throws Exception 399 { 400 byte[] referenceData = 401 (servantName == null) ? ReferenceData.create(id) 402 : ReferenceData.create(servantName, id); 403 return poa.create_reference_with_id(referenceData, interfId); 404 } 405 406 public POA getPOA() 407 { 408 return poa; 409 } 410 411 } 412 413 415 416 class ServantRegistryWithSharedTransientPOA 417 implements ServantRegistry 418 { 419 public ReferenceFactory bind(String name, 420 Servant servant, 421 Policy [] policies) 422 { 423 if (servant instanceof ServantWithMBeanServer) { 424 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 425 } 426 transientServantMap.put(name, servant); 427 return new PoaAndPoliciesReferenceFactory(transientPOA, 428 name, policies); 429 } 430 431 public ReferenceFactory bind(String name, Servant servant) 432 { 433 if (servant instanceof ServantWithMBeanServer) { 434 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 435 } 436 transientServantMap.put(name, servant); 437 return new PoaReferenceFactory(transientPOA, name); 438 } 439 440 public void unbind(String name) 441 { 442 transientServantMap.remove(name); 443 } 444 445 } 446 447 448 class ServantRegistryWithSharedPersistentPOA 449 implements ServantRegistry 450 { 451 public ReferenceFactory bind(String name, 452 Servant servant, 453 Policy [] policies) 454 { 455 if (servant instanceof ServantWithMBeanServer) { 456 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 457 } 458 persistentServantMap.put(name, servant); 459 return new PoaAndPoliciesReferenceFactory(persistentPOA, 460 name, policies); 461 } 462 463 public ReferenceFactory bind(String name, Servant servant) 464 { 465 if (servant instanceof ServantWithMBeanServer) { 466 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 467 } 468 persistentServantMap.put(name, servant); 469 return new PoaReferenceFactory(persistentPOA, name); 470 } 471 472 public void unbind(String name) 473 { 474 persistentServantMap.remove(name); 475 } 476 477 } 478 479 480 class ServantRegistryWithTransientPOAPerServant 481 implements ServantRegistry 482 { 483 484 public ReferenceFactory bind(String name, 485 Servant servant, 486 Policy [] policies) 487 throws Exception 488 { 489 if (servant instanceof ServantWithMBeanServer) { 490 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 491 } 492 Policy [] poaPolicies = concatPolicies(transientPoaPolicies, policies); 493 POA poa = rootPOA.create_POA(name, null, poaPolicies); 494 transientPoaMap.put(name, poa); 495 poa.set_servant(servant); 496 poa.the_POAManager().activate(); 497 return new PoaReferenceFactory(poa); } 500 501 public ReferenceFactory bind(String name, Servant servant) 502 throws Exception 503 { 504 if (servant instanceof ServantWithMBeanServer) { 505 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 506 } 507 POA poa = rootPOA.create_POA(name, null, transientPoaPolicies); 508 transientPoaMap.put(name, poa); 509 poa.set_servant(servant); 510 poa.the_POAManager().activate(); 511 return new PoaReferenceFactory(poa); } 514 515 public void unbind(String name) 516 throws Exception 517 { 518 POA poa = (POA ) transientPoaMap.remove(name); 519 if (poa != null) { 520 poa.the_POAManager().deactivate(false, 521 true ); 522 poa.destroy(false, 523 false ); 524 } 525 } 526 527 } 528 529 530 class ServantRegistryWithPersistentPOAPerServant 531 implements ServantRegistry 532 { 533 534 public ReferenceFactory bind(String name, 535 Servant servant, 536 Policy [] policies) 537 throws Exception 538 { 539 if (servant instanceof ServantWithMBeanServer) { 540 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 541 } 542 Policy [] poaPolicies = 543 concatPolicies(persistentPoaPolicies, policies); 544 POA poa = rootPOA.create_POA(name, null, poaPolicies); 545 persistentPoaMap.put(name, poa); 546 poa.set_servant(servant); 547 poa.the_POAManager().activate(); 548 return new PoaReferenceFactory(poa); } 551 552 public ReferenceFactory bind(String name, Servant servant) 553 throws Exception 554 { 555 if (servant instanceof ServantWithMBeanServer) { 556 ((ServantWithMBeanServer)servant).setMBeanServer(getServer()); 557 } 558 POA poa = rootPOA.create_POA(name, null, persistentPoaPolicies); 559 persistentPoaMap.put(name, poa); 560 poa.set_servant(servant); 561 poa.the_POAManager().activate(); 562 return new PoaReferenceFactory(poa); } 565 566 public void unbind(String name) 567 throws Exception 568 { 569 POA poa = (POA ) persistentPoaMap.remove(name); 570 if (poa != null) { 571 poa.the_POAManager().deactivate(false, 572 true ); 573 poa.destroy(false, 574 false ); 575 } 576 } 577 578 } 579 580 582 583 class TransientServantLocator 584 extends LocalObject 585 implements ServantLocator 586 { 587 588 public Servant preinvoke(byte[] oid, 589 POA adapter, 590 String operation, 591 CookieHolder the_cookie) 592 { 593 try { 594 the_cookie.value = null; 595 Object id = ReferenceData.extractServantId(oid); 596 return (Servant )transientServantMap.get(id); 597 } 598 catch (Exception e) { 599 getLog().trace("Unexpected exception in preinvoke:", e); 600 throw new UNKNOWN (e.toString()); 601 } 602 } 603 604 public void postinvoke(byte[] oid, 605 POA adapter, 606 String operation, 607 Object the_cookie, 608 Servant the_servant) 609 { 610 } 611 612 } 613 614 615 class PersistentServantLocator 616 extends LocalObject 617 implements ServantLocator 618 { 619 620 public Servant preinvoke(byte[] oid, 621 POA adapter, 622 String operation, 623 CookieHolder the_cookie) 624 { 625 try { 626 the_cookie.value = null; 627 Object id = ReferenceData.extractServantId(oid); 628 return (Servant )persistentServantMap.get(id); 629 } 630 catch (Exception e) { 631 getLog().trace("Unexpected exception in preinvoke:", e); 632 throw new UNKNOWN (e.toString()); 633 } 634 } 635 636 public void postinvoke(byte[] oid, 637 POA adapter, 638 String operation, 639 Object the_cookie, 640 Servant the_servant) 641 { 642 } 643 644 } 645 646 } 647 | Popular Tags |