1 23 package com.sun.enterprise.iiop; 24 25 import java.rmi.Remote ; 26 import java.rmi.RemoteException ; 27 import java.net.URL ; 28 import java.io.File ; 29 import java.util.*; 30 import java.util.logging.*; 31 import java.lang.reflect.Method ; 32 33 import javax.rmi.PortableRemoteObject ; 34 import javax.rmi.CORBA.*; 35 import javax.transaction.*; 36 import javax.ejb.EJBHome ; 37 import javax.ejb.EJBObject ; 38 import javax.ejb.NoSuchObjectLocalException ; 39 import javax.naming.*; 40 41 import org.omg.CORBA.*; 42 import org.omg.CORBA.portable.Delegate ; 43 import org.omg.CosNaming.NamingContext ; 44 45 import org.omg.PortableServer.POA ; 46 import org.omg.PortableServer.Servant ; 47 import org.omg.PortableServer.ServantLocator ; 48 import org.omg.PortableServer.ServantLocatorPackage.CookieHolder ; 49 50 import com.sun.logging.LogDomains; 51 import com.sun.enterprise.*; 52 import com.sun.enterprise.deployment.*; 53 import com.sun.enterprise.util.*; 54 import com.sun.enterprise.server.*; 55 import com.sun.enterprise.deployment.runtime.IASEjbExtraDescriptors; 56 import com.sun.ejb.*; 57 58 import com.sun.corba.ee.spi.ior.*; 59 import com.sun.corba.ee.spi.extension.ServantCachingPolicy; 60 import com.sun.corba.ee.spi.extension.ZeroPortPolicy; 61 import com.sun.corba.ee.spi.extension.CopyObjectPolicy; 62 import com.sun.corba.ee.spi.extension.RequestPartitioningPolicy; 63 import com.sun.corba.ee.spi.orbutil.threadpool.ThreadPoolManager; 64 import com.sun.corba.ee.spi.orbutil.threadpool.NoSuchThreadPoolException; 65 import com.sun.corba.ee.spi.presentation.rmi.PresentationManager ; 66 import com.sun.corba.ee.spi.presentation.rmi.DynamicStub; 67 import com.sun.corba.ee.spi.presentation.rmi.StubAdapter; 68 import com.sun.corba.ee.spi.oa.rfm.ReferenceFactory ; 69 import com.sun.corba.ee.spi.oa.rfm.ReferenceFactoryManager ; 70 71 import com.sun.corba.ee.impl.orbutil.ORBConstants; 72 import com.sun.corba.ee.impl.naming.cosnaming.TransientNameService; 73 import com.sun.corba.ee.impl.util.SUNVMCID; 74 75 import com.sun.enterprise.util.Utility; 76 90 91 public final class POARemoteReferenceFactory extends org.omg.CORBA.LocalObject 92 implements RemoteReferenceFactory, ServantLocator 93 { 94 static final int PASS_BY_VALUE_ID = 0; 95 static final int PASS_BY_REFERENCE_ID = 1; 96 97 static final int OTS_POLICY_TYPE = SUNVMCID.value + 123; 98 static final int CSIv2_POLICY_TYPE = SUNVMCID.value + 124; 99 static final int REQUEST_DISPATCH_POLICY_TYPE = SUNVMCID.value + 125; 100 101 private static final java.util.logging.Logger logger = 102 java.util.logging.Logger.getLogger(LogDomains.CORBA_LOGGER); 103 private static final int GET_TIE_EXCEPTION_CODE = 9999; 104 105 private Container container; 106 private EjbDescriptor ejbDescriptor; 107 108 private ORB orb; 109 private POAProtocolMgr protocolMgr; 110 private PresentationManager presentationMgr; 111 112 private ReferenceFactory ejbHomeReferenceFactory ; 113 private PresentationManager.StubFactory ejbHomeStubFactory; 114 private String ejbHomeRepositoryId; 115 116 private ReferenceFactory ejbObjectReferenceFactory ; 117 private PresentationManager.StubFactory ejbObjectStubFactory; 118 private String ejbObjectRepositoryId; 119 120 private String remoteBusinessIntf; 121 122 private boolean isRemoteHomeView; 125 126 private String poaId_EJBHome; 127 private String poaId_EJBObject; 128 129 static final int EJBID_OFFSET = 0; 136 private static final int INSTANCEKEYLEN_OFFSET = 8; 137 private static final int INSTANCEKEY_OFFSET = 12; 138 139 140 141 POARemoteReferenceFactory(Container container, POAProtocolMgr protocolMgr, 142 ORB orb, boolean remoteHomeView, String id) { 143 144 this.protocolMgr = protocolMgr; 145 this.orb = orb; 146 this.poaId_EJBHome = id + "-EJBHome"; 147 this.poaId_EJBObject = id + "-EJBObject"; 148 this.presentationMgr = 149 ((com.sun.corba.ee.spi.orb.ORB) orb).getPresentationManager(); 150 this.container = container; 151 this.ejbDescriptor = container.getEjbDescriptor(); 152 this.isRemoteHomeView = remoteHomeView; 153 154 ClassLoader loader = container.getClassLoader(); 155 156 158 if (logger.isLoggable(Level.FINE)) { 159 logger.log(Level.FINE, 160 "POARemoteReferenceFactory:" 161 + " " + poaId_EJBHome 162 + " " + poaId_EJBObject 163 + " " + ejbDescriptor); 164 } 165 } 166 167 private String getRepositoryId(Class c) throws Exception { 168 169 172 PresentationManager.ClassData cData = presentationMgr.getClassData(c); 173 String [] typeIds = cData.getTypeIds(); 174 175 if (logger.isLoggable(Level.FINE)) { 176 logger.log(Level.FINE, ".getRepositoryId: " + typeIds[0]); 177 } 178 179 return typeIds[0]; 181 } 182 183 public void setRepositoryIds(Class homeIntf, Class remoteIntf) 184 { 185 186 ClassLoader appClassLoader = container.getClassLoader(); 187 188 PresentationManager.StubFactoryFactory sff = 189 ((com.sun.corba.ee.spi.orb.ORB) orb).getStubFactoryFactory(); 190 191 ejbHomeStubFactory = 193 sff.createStubFactory( homeIntf.getName(), false, 194 "", null, appClassLoader); 195 String [] ejbHomeTypeIds = ejbHomeStubFactory.getTypeIds(); 196 ejbHomeRepositoryId = ejbHomeTypeIds[0]; 197 198 ejbObjectStubFactory = 199 sff.createStubFactory( remoteIntf.getName(), false, 200 "", null, appClassLoader); 201 String [] ejbObjectTypeIds = ejbObjectStubFactory.getTypeIds(); 202 ejbObjectRepositoryId = ejbObjectTypeIds[0]; 203 204 if (logger.isLoggable(Level.FINE)) { 205 logger.log(Level.FINE, 206 ".setRepositoryIds:" 207 + " " + ejbHomeRepositoryId 208 + " " + ejbObjectRepositoryId); 209 } 210 211 try { 212 213 ejbHomeReferenceFactory 214 = createReferenceFactory(poaId_EJBHome, ejbHomeRepositoryId); 215 ejbObjectReferenceFactory 216 = createReferenceFactory(poaId_EJBObject, ejbObjectRepositoryId); 217 } catch (Exception e) { 218 throw new RuntimeException (e); 219 } 220 221 if( !isRemoteHomeView ) { 222 remoteBusinessIntf = remoteIntf.getName(); 223 } 224 225 } 226 227 public void cleanupClass(Class clazz) { 228 229 try { 230 presentationMgr.flushClass(clazz); 231 } catch(Exception e) { 232 logger.log(Level.FINE, "cleanupClass error", e); 233 } 234 } 235 236 private ReferenceFactory createReferenceFactory(String poaId, String repoid ) throws Exception { 237 try { 238 if (logger.isLoggable(Level.FINE)) { 239 logger.log(Level.WARNING, 240 ".createReferenceFactory->: " + poaId + " " + repoid); 241 } 242 243 ReferenceFactoryManager rfm = 244 (ReferenceFactoryManager)orb.resolve_initial_references( 245 ORBConstants.REFERENCE_FACTORY_MANAGER ) ; 246 247 List<Policy> policies = new ArrayList<Policy>(); 248 249 policies.add(ServantCachingPolicy.getPolicy()); 251 252 policies.add(new OTSPolicy()); 254 255 if (logger.isLoggable(Level.FINE)) { 256 logger.log(Level.WARNING, 257 ".createReferenceFactory: " + poaId + " " + repoid 258 + ": " + ejbDescriptor); 259 } 260 261 policies.add(new CSIv2Policy(ejbDescriptor)); 263 264 IASEjbExtraDescriptors extraDesc 265 = ejbDescriptor.getIASEjbExtraDescriptors(); 266 String threadPoolName = extraDesc.getUseThreadPoolId(); 267 int threadPoolNumericID = 0; 268 boolean usePassByReference = extraDesc.getPassByReference(); 269 270 if (usePassByReference) { 271 policies.add(new CopyObjectPolicy(PASS_BY_REFERENCE_ID)); 272 } 273 274 if (threadPoolName != null) { 275 ThreadPoolManager threadPoolManager 276 = S1ASThreadPoolManager.getThreadPoolManager(); 277 try { 278 threadPoolNumericID = threadPoolManager.getThreadPoolNumericId( 279 threadPoolName); 280 policies.add(new RequestPartitioningPolicy(threadPoolNumericID)); 281 } catch (Exception ex) { 282 logger.log(Level.WARNING, "Not using threadpool-request-partitioning...", ex); 283 } 284 } 285 286 if (logger.isLoggable(Level.FINE)) { 287 String jndiName = ejbDescriptor.getJndiName(); 288 logger.log(Level.FINE, "Using Thread-Pool: [" 289 + threadPoolName + " ==> " + threadPoolNumericID 290 + "] for jndi name: " + jndiName); 291 logger.log(Level.FINE, "Pass by reference: [" 292 + usePassByReference 293 + "] for jndi name: " + usePassByReference); 294 } 295 296 CSIV2TaggedComponentInfo ctc = new CSIV2TaggedComponentInfo(orb); 299 Set iorDescSet = ejbDescriptor.getIORConfigurationDescriptors(); 300 if ( ctc.allMechanismsRequireSSL(iorDescSet) ) { 301 if (logger.isLoggable(Level.FINE)) { 302 logger.log(Level.WARNING, 303 ".createReferenceFactory: " + poaId + " " + repoid 304 + ": adding ZeroPortPolicy"); 305 } 306 policies.add(ZeroPortPolicy.getPolicy()); 307 } 308 309 if (logger.isLoggable(Level.FINE)) { 310 logger.log(Level.WARNING, 311 ".createReferenceFactory: " + poaId + " " + repoid 312 + ": policies: " + policies); 313 } 314 ReferenceFactory rf = rfm.create( poaId, repoid, policies, this ) ; 315 return rf ; 316 317 } finally { 318 if (logger.isLoggable(Level.FINE)) { 319 logger.log(Level.WARNING, 320 ".createReferenceFactory<-: " + poaId + " " + repoid); 321 } 322 } 323 } 324 325 326 public java.rmi.Remote createRemoteReference(byte[] instanceKey) 327 { 328 return createRef(instanceKey, ejbObjectReferenceFactory, 329 ejbObjectStubFactory, ejbObjectRepositoryId ); 330 } 331 332 333 public EJBHome createHomeReference(byte[] homeKey) 334 { 335 return (EJBHome )createRef(homeKey, ejbHomeReferenceFactory, 336 ejbHomeStubFactory, ejbHomeRepositoryId ) ; 337 } 338 339 private Remote createRef(byte[] instanceKey, ReferenceFactory rf, 340 PresentationManager.StubFactory stubFactory, String repoid ) 341 { 342 try { 343 PresentationManager.StubFactory stubFact = stubFactory; 344 org.omg.CORBA.Object ref = _createRef(rf, instanceKey,repoid); 345 346 org.omg.CORBA.Object stub = stubFact.makeStub(); 347 348 Delegate delegate = StubAdapter.getDelegate(ref); 349 StubAdapter.setDelegate(stub, delegate); 350 351 return (Remote ) stub; 352 353 } catch(Exception e) { 354 logger.log(Level.SEVERE, "iiop.createreference_exception", 355 e.toString()); 356 357 throw new RuntimeException ("Unable to create reference ",e); 358 } 359 } 360 361 private org.omg.CORBA.Object _createRef( ReferenceFactory rf, 363 byte[] instanceKey, String repoid ) throws Exception { 364 365 if ( logger.isLoggable(Level.FINE) ) { 366 logger.info("\t\tIn POARemoteReferenceFactory._createRef, " + 367 "repositoryId = " + repoid); 368 } 369 370 byte[] ejbKey = createEJBKey(ejbDescriptor.getUniqueId(), 372 instanceKey); 373 374 org.omg.CORBA.Object obj = rf.createReference( ejbKey ) ; 375 376 return obj; 377 } 378 379 private byte[] createEJBKey(long ejbId, byte[] instanceKey) 380 { 381 byte[] ejbkey = new byte[INSTANCEKEY_OFFSET+instanceKey.length]; 382 383 Utility.longToBytes(ejbId, ejbkey, EJBID_OFFSET); 384 Utility.intToBytes(instanceKey.length, ejbkey, INSTANCEKEYLEN_OFFSET); 385 System.arraycopy(instanceKey, 0, ejbkey, INSTANCEKEY_OFFSET, 386 instanceKey.length); 387 return ejbkey; 388 } 389 390 391 392 395 public void destroyReference(Remote remoteRef, Remote remoteObj) 396 { 397 try { 402 Util.unexportObject(remoteObj); 403 } catch ( RuntimeException ex ) { 404 } catch ( java.lang.Exception nsoe ){ 407 } 409 } 410 411 412 417 public Servant preinvoke(byte[] ejbKey, POA adapter, String operation, 418 CookieHolder cookieHolder) 419 throws org.omg.PortableServer.ForwardRequest 420 { 421 if (logger.isLoggable(Level.FINE)) { 422 logger.log(Level.FINE,"In preinvoke for operation:" + operation); 423 } 424 425 int keyLen = Utility.bytesToInt(ejbKey, INSTANCEKEYLEN_OFFSET); 427 byte[] instanceKey = new byte[keyLen]; 428 System.arraycopy(ejbKey, INSTANCEKEY_OFFSET, instanceKey, 0, keyLen); 429 430 Servant servant = null; 431 try { 432 while ( servant == null ) { 433 Remote targetObj = 435 container.getTargetObject(instanceKey, 436 (isRemoteHomeView ? null : 437 remoteBusinessIntf)); 438 439 if( targetObj != null ) { 445 Tie tie = presentationMgr.getTie(); 447 tie.setTarget(targetObj); 448 servant = (Servant ) tie; 449 } 450 } 451 } catch (NoSuchObjectLocalException e) { 452 logger.log(Level.SEVERE,"iiop.gettie_exception", e); 453 throw new OBJECT_NOT_EXIST( GET_TIE_EXCEPTION_CODE, 454 CompletionStatus.COMPLETED_NO); 455 } catch (RuntimeException e) { 456 logger.log(Level.SEVERE,"iiop.runtime_exception", e); 457 throw e; 458 } 459 return servant; 460 } 461 462 public void postinvoke(byte[] ejbKey, POA adapter, String operation, 463 java.lang.Object cookie, Servant servant) 464 { 465 Remote target = null; 466 if ( servant != null ) { 467 target = ((Tie)servant).getTarget(); 468 } 469 container.releaseTargetObject(target); 472 } 473 474 475 public void destroy() { 476 try { 477 478 ejbHomeReferenceFactory.destroy() ; 479 ejbObjectReferenceFactory.destroy() ; 480 ejbHomeReferenceFactory = null ; 481 ejbObjectReferenceFactory = null ; 482 483 container = null; 484 ejbDescriptor = null; 485 486 orb = null; 487 protocolMgr = null; 488 489 } catch (Throwable th) { 490 logger.log(Level.SEVERE, "Exception during " 491 + "POARemoteRefFactory::destroy()", th); 492 } 493 } 494 495 public boolean hasSameContainerID(org.omg.CORBA.Object obj) 496 throws Exception 497 { 498 boolean result = false; 499 try { 500 IOR ior = ((com.sun.corba.ee.spi.orb.ORB)orb).getIOR(obj, false); 501 java.util.Iterator iter = ior.iterator(); 502 503 byte[] oid = null; 504 if (iter.hasNext()) { 505 TaggedProfile profile = (TaggedProfile) iter.next(); 506 ObjectKey objKey = profile.getObjectKey(); 507 oid = objKey.getId().getId(); 508 } 509 510 if ((oid != null) && (oid.length > INSTANCEKEY_OFFSET)) { 511 long cid = Utility.bytesToLong(oid, EJBID_OFFSET); 512 int keyLen = Utility.bytesToInt(oid, INSTANCEKEYLEN_OFFSET); 515 if (oid.length == keyLen + INSTANCEKEY_OFFSET) { 516 result = (cid == ejbDescriptor.getUniqueId()); 517 } 518 if (logger.isLoggable(Level.FINE)) { 519 StringBuffer sbuf = new StringBuffer (); 520 sbuf.append("hasSameContainerID() result: ").append(result) 521 .append("; because ==> oid.length: ").append(oid.length) 522 .append("; instance-key-length: ").append(keyLen) 523 .append("; expected oid.length: ") 524 .append(keyLen).append("+").append(INSTANCEKEY_OFFSET) 525 .append("; myContainrID: ") 526 .append(ejbDescriptor.getUniqueId()) 527 .append("; obj.containerID: ") 528 .append(cid); 529 logger.log(Level.FINE, sbuf.toString()); 530 } 531 } else { 532 if (logger.isLoggable(Level.FINE)) { 533 if (oid == null) { 534 logger.log(Level.FINE, "hasSameContainerID() failed because oid=null"); 535 } else { 536 logger.log(Level.FINE, "hasSameContainerID() failed because " 537 + "oid.length= " + oid.length 538 + "; but INSTANCE_KEY_OFFSET= " + INSTANCEKEY_OFFSET); 539 } 540 } 541 } 542 } catch (Exception ex) { 543 logger.log(Level.FINE, "Exception while checking for same containerID", ex); 544 throw ex; 545 } 546 return result; 547 } 548 549 } 550 | Popular Tags |