1 23 24 package com.sun.ejb; 25 26 import java.io.*; 27 import java.security.*; 28 import java.rmi.*; 29 import javax.rmi.PortableRemoteObject ; 30 import javax.ejb.*; 31 import javax.naming.*; 32 import java.util.SortedMap ; 33 import java.util.Properties ; 34 import java.util.TreeMap ; 35 import java.util.Map ; 36 import java.util.Collection ; 37 38 import java.lang.reflect.Method ; 39 import java.lang.reflect.Constructor ; 40 import java.lang.reflect.Modifier ; 41 import java.lang.reflect.Field ; 42 import java.lang.reflect.InvocationTargetException ; 43 44 import com.sun.enterprise.*; 45 import com.sun.enterprise.log.Log; 46 import com.sun.enterprise.util.ObjectInputStreamWithLoader; 47 import com.sun.ejb.containers.EJBLocalHomeImpl; 48 import com.sun.ejb.containers.EJBLocalObjectImpl; 49 import com.sun.ejb.containers.GenericEJBLocalHome; 50 import com.sun.ejb.containers.RemoteBusinessWrapperBase; 51 import com.sun.ejb.containers.BaseContainer; 52 import com.sun.ejb.containers.RemoteBusinessIntfInvocationHandler; 53 import com.sun.ejb.portable.*; 54 import com.sun.enterprise.deployment.EjbReferenceDescriptor; 55 import com.sun.enterprise.deployment.EjbDescriptor; 56 import com.sun.ejb.codegen.RemoteGenerator; 57 import com.sun.ejb.codegen.Remote30WrapperGenerator; 58 import com.sun.ejb.codegen.SerializableBeanGenerator; 59 import com.sun.ejb.codegen.GenericHomeGenerator; 60 import com.sun.ejb.codegen.ClassGeneratorFactory; 61 import static com.sun.corba.ee.spi.codegen.Wrapper.*; 62 63 import java.util.logging.*; 64 import com.sun.logging.*; 65 66 import com.sun.ejb.base.io.IOUtils; 67 68 72 public class EJBUtils { 73 private static Logger _logger=null; 74 static { 75 _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER); 76 } 77 78 private static final String EJB_USE_STATIC_CODEGEN_PROP = 83 "com.sun.ejb.UseStaticCodegen"; 84 85 private static final String REMOTE30_HOME_JNDI_SUFFIX = 86 "__3_x_Internal_RemoteBusinessHome__"; 87 88 private static Boolean ejbUseStaticCodegen_ = null; 89 90 private static final String CORBA_INS_PREFIX = "corbaname:"; 92 93 102 public static final byte[] serializeObject(Object obj, 103 boolean replaceObject) 104 throws IOException 105 { 106 return IOUtils.serializeObject(obj, replaceObject); 107 } 108 109 public static final byte[] serializeObject(Object obj) 110 throws IOException 111 { 112 return IOUtils.serializeObject(obj, true); 113 } 114 115 121 public static final Object deserializeObject(byte[] data, 122 ClassLoader loader, boolean resolveObject) 123 throws Exception 124 { 125 return IOUtils.deserializeObject(data, resolveObject, loader); 126 } 127 128 public static final Object deserializeObject(byte[] data, 129 ClassLoader loader) 130 throws Exception 131 { 132 return IOUtils.deserializeObject(data, true, loader); 133 } 134 135 public static boolean useStaticCodegen() { 136 synchronized (EJBUtils.class) { 137 if( ejbUseStaticCodegen_ == null ) { 138 String ejbStaticCodegenProp = (String ) 139 java.security.AccessController.doPrivileged 140 (new java.security.PrivilegedAction () { 141 public java.lang.Object run() { 142 return 143 System.getProperty(EJB_USE_STATIC_CODEGEN_PROP); 144 }}); 145 146 boolean useStaticCodegen = 147 ( (ejbStaticCodegenProp != null) && 148 ejbStaticCodegenProp.equalsIgnoreCase("true")); 149 150 ejbUseStaticCodegen_ = new Boolean (useStaticCodegen); 151 152 _logger.log(Level.FINE, "EJB Static codegen is " + 153 (useStaticCodegen ? "ENABLED" : "DISABLED") + 154 " ejbUseStaticCodegenProp = " + 155 ejbStaticCodegenProp); 156 } 157 } 158 159 return ejbUseStaticCodegen_.booleanValue(); 160 161 } 162 163 public static boolean isEjbRefCacheable(EjbReferenceDescriptor refDesc) { 164 165 return ( (!isEJB30Ref(refDesc)) && 168 (refDesc.getEjbDescriptor() != null) ); 169 } 170 171 private static String getClassPackageName(String intf) { 172 int dot = intf.lastIndexOf('.'); 173 return (dot == -1) ? null : intf.substring(0, dot); 174 } 175 176 private static String getClassSimpleName(String intf) { 177 int dot = intf.lastIndexOf('.'); 178 return (dot == -1) ? intf : intf.substring(dot+1); 179 } 180 181 public static String getGeneratedSerializableClassName(String beanClass) { 182 String packageName = getClassPackageName(beanClass); 183 String simpleName = getClassSimpleName(beanClass); 184 String generatedSimpleName = "_" + simpleName + "_Serializable"; 185 return (packageName != null) ? 186 packageName + "." + generatedSimpleName : generatedSimpleName; 187 } 188 189 public static String getGeneratedRemoteIntfName(String businessIntf) { 190 String packageName = getClassPackageName(businessIntf); 191 String simpleName = getClassSimpleName(businessIntf); 192 String generatedSimpleName = "_" + simpleName + "_Remote"; 193 return (packageName != null) ? 194 packageName + "." + generatedSimpleName : generatedSimpleName; 195 } 196 197 public static String getGeneratedRemoteWrapperName(String businessIntf) { 198 String packageName = getClassPackageName(businessIntf); 199 String simpleName = getClassSimpleName(businessIntf); 200 String generatedSimpleName = "_" + simpleName + "_Wrapper"; 201 return (packageName != null) ? 202 packageName + "." + generatedSimpleName : generatedSimpleName; 203 } 204 205 public static String getGenericEJBHomeClassName() { 206 return "com.sun.ejb.codegen.GenericEJBHome_Generated"; 207 } 208 209 231 public static String getRemoteEjbJndiName(EjbReferenceDescriptor refDesc) { 232 233 return getRemoteEjbJndiName(refDesc.isEJB30ClientView(), 234 refDesc.getEjbInterface(), 235 refDesc.getJndiName()); 236 } 237 238 public static String getRemote30HomeJndiName(String jndiName) { 239 return jndiName + REMOTE30_HOME_JNDI_SUFFIX; 240 } 241 242 public static String getRemoteEjbJndiName(boolean businessView, 243 String interfaceName, 244 String jndiName) { 245 246 String returnValue = jndiName; 247 248 if( businessView ) { 249 if( jndiName.startsWith(CORBA_INS_PREFIX) ) { 250 251 returnValue = getRemote30HomeJndiName(jndiName); 258 259 } else { 260 if( !jndiName.endsWith("#" + interfaceName) ) { 261 returnValue = jndiName + "#" + interfaceName; 262 } 263 } 264 } 265 266 return returnValue; 267 } 268 269 public static Object resolveEjbRefObject(EjbReferenceDescriptor refDesc, 270 Object jndiObj) 271 throws NamingException { 272 273 Object returnObject = jndiObj; 274 275 if( refDesc.isLocal() ) { 276 277 EjbDescriptor target = refDesc.getEjbDescriptor(); 278 ContainerFactory cf = Switch.getSwitch().getContainerFactory(); 279 BaseContainer container = (BaseContainer) 280 cf.getContainer(target.getUniqueId()); 281 282 if( refDesc.isEJB30ClientView() ) { 283 GenericEJBLocalHome genericLocalHome = 284 container.getEJBLocalBusinessHome(); 285 returnObject = genericLocalHome.create(refDesc.getEjbInterface()); 286 } else { 287 returnObject = container.getEJBLocalHome(); 288 } 289 290 } else { 291 292 299 if ( refDesc.isEJB30ClientView() && 300 !(jndiObj instanceof RemoteBusinessWrapperBase) ) { 301 returnObject = EJBUtils.lookupRemote30BusinessObject 302 (jndiObj, refDesc.getEjbInterface()); 303 } 304 305 } 306 307 return returnObject; 308 309 } 310 311 public static Object lookupRemote30BusinessObject(Object jndiObj, 312 String businessInterface) 313 throws NamingException 314 315 { 316 Object returnObject = null; 317 318 try { 319 320 ClassLoader loader = 321 getBusinessIntfClassLoader(businessInterface); 322 323 Class genericEJBHome = loadGeneratedGenericEJBHomeClass 324 (loader); 325 326 final Object genericHomeObj = 327 PortableRemoteObject.narrow(jndiObj, genericEJBHome); 328 329 loadGeneratedRemoteBusinessClasses(businessInterface); 335 336 String generatedRemoteIntfName = EJBUtils. 337 getGeneratedRemoteIntfName(businessInterface); 338 339 Method createMethod = genericEJBHome.getMethod 340 ("create", String .class); 341 342 final java.rmi.Remote delegate = (java.rmi.Remote ) 343 createMethod.invoke(genericHomeObj, 344 generatedRemoteIntfName); 345 346 returnObject = createRemoteBusinessObject 347 (loader, businessInterface, delegate); 348 349 } catch(Exception e) { 350 NamingException ne = new NamingException 351 ("ejb ref resolution error for remote business interface" 352 + businessInterface); 353 354 ne.initCause(e instanceof InvocationTargetException ? 355 e.getCause() : e); 356 throw ne; 357 } 358 359 return returnObject; 360 361 } 362 363 public static void loadGeneratedRemoteBusinessClasses 364 (String businessInterfaceName) throws Exception { 365 366 ClassLoader appClassLoader = 367 getBusinessIntfClassLoader(businessInterfaceName); 368 369 loadGeneratedRemoteBusinessClasses(appClassLoader, 370 businessInterfaceName); 371 } 372 373 public static void loadGeneratedRemoteBusinessClasses 374 (ClassLoader appClassLoader, String businessInterfaceName) 375 throws Exception { 376 377 String generatedRemoteIntfName = EJBUtils. 378 getGeneratedRemoteIntfName(businessInterfaceName); 379 380 String wrapperClassName = EJBUtils. 381 getGeneratedRemoteWrapperName(businessInterfaceName); 382 383 Class generatedRemoteIntf = null; 384 try { 385 generatedRemoteIntf = 386 appClassLoader.loadClass(generatedRemoteIntfName); 387 } catch(Exception e) { 388 } 389 390 Class generatedRemoteWrapper = null; 391 try { 392 generatedRemoteWrapper = 393 appClassLoader.loadClass(wrapperClassName); 394 } catch(Exception e) { 395 } 396 397 if( (generatedRemoteIntf != null) && 398 (generatedRemoteWrapper != null) ) { 399 return; 400 } 401 402 if( generatedRemoteIntf == null ) { 403 404 RemoteGenerator gen = new RemoteGenerator(appClassLoader, 405 businessInterfaceName); 406 407 Class developerClass = appClassLoader.loadClass(businessInterfaceName); 408 generatedRemoteIntf = generateAndLoad(gen, appClassLoader, developerClass); 409 410 } 411 412 if( generatedRemoteWrapper == null ) { 413 414 Remote30WrapperGenerator gen = new Remote30WrapperGenerator 415 (appClassLoader, businessInterfaceName, 416 generatedRemoteIntfName); 417 418 Class developerClass = appClassLoader.loadClass(businessInterfaceName); 419 generatedRemoteWrapper = generateAndLoad(gen, appClassLoader, developerClass); 420 } 421 422 } 423 424 public static Class loadGeneratedGenericEJBHomeClass 425 (ClassLoader appClassLoader) throws Exception { 426 427 String className = getGenericEJBHomeClassName(); 428 429 Class generatedGenericEJBHomeClass = null; 430 431 try { 432 generatedGenericEJBHomeClass = appClassLoader.loadClass(className); 433 } catch(Exception e) { 434 } 435 436 if( generatedGenericEJBHomeClass == null ) { 437 438 GenericHomeGenerator gen =new GenericHomeGenerator(appClassLoader); 439 440 441 generatedGenericEJBHomeClass =generateAndLoad(gen, appClassLoader, EJBUtils.class); 442 } 443 444 return generatedGenericEJBHomeClass; 445 } 446 447 public static Class loadGeneratedSerializableClass 448 (ClassLoader appClassLoader, 449 String developerClassName) throws Exception { 450 451 String generatedSerializableClassName = 452 getGeneratedSerializableClassName(developerClassName); 453 454 455 Class generatedSerializableClass = null; 456 try { 457 generatedSerializableClass = 458 appClassLoader.loadClass(generatedSerializableClassName); 459 460 } catch(Exception e) { 461 } 462 463 if( generatedSerializableClass == null ) { 464 465 SerializableBeanGenerator gen = 466 new SerializableBeanGenerator(appClassLoader, 467 developerClassName); 468 469 Class developerClass = appClassLoader.loadClass(developerClassName); 470 generatedSerializableClass = generateAndLoad(gen, appClassLoader, developerClass); 471 472 } 473 474 return generatedSerializableClass; 475 } 476 477 private static Class generateAndLoad(ClassGeneratorFactory cgf, 478 final ClassLoader loader, 479 final Class protectionDomainBase) { 480 481 cgf.evaluate(); 482 483 final Properties props = new Properties (); 484 if( _logger.isLoggable(Level.FINE) ) { 485 486 props.put(DUMP_AFTER_SETUP_VISITOR, "true"); 487 props.put(TRACE_BYTE_CODE_GENERATION, "true"); 488 props.put(USE_ASM_VERIFIER, "true"); 489 490 try { 491 492 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 493 PrintStream ps = new PrintStream(baos); 494 495 _sourceCode(ps, props); 496 _logger.fine(baos.toString()); 497 498 } catch(Exception e) { 499 _logger.log(Level.FINE, "exception generating src", e); 500 } 501 502 } 503 504 return (Class ) java.security.AccessController.doPrivileged 505 (new java.security.PrivilegedAction () { 506 public java.lang.Object run() { 507 return _generate(loader, protectionDomainBase.getProtectionDomain(), props); 508 }}); 509 510 } 511 512 public static RemoteBusinessWrapperBase createRemoteBusinessObject 513 (String businessInterface, java.rmi.Remote delegate) 514 throws Exception { 515 516 ClassLoader appClassLoader = 517 getBusinessIntfClassLoader(businessInterface); 518 519 return createRemoteBusinessObject(appClassLoader, 520 businessInterface, delegate); 521 } 522 523 524 public static RemoteBusinessWrapperBase createRemoteBusinessObject 525 (ClassLoader loader, String businessInterface, 526 java.rmi.Remote delegate) 527 528 throws Exception { 529 530 String wrapperClassName = EJBUtils.getGeneratedRemoteWrapperName 531 (businessInterface); 532 533 Class clientWrapperClass = loader.loadClass(wrapperClassName); 534 535 Constructor ctors[] = clientWrapperClass.getConstructors(); 536 537 Constructor ctor = null; 538 for(Constructor next : ctors) { 539 if (next.getParameterTypes().length > 0 ) { 540 ctor = next; 541 break; 542 } 543 } 544 545 Object obj = ctor.newInstance(new Object [] 546 { delegate, businessInterface } ); 547 548 return (RemoteBusinessWrapperBase) obj; 549 } 550 551 private static ClassLoader getBusinessIntfClassLoader 552 (String businessInterface) throws Exception { 553 554 ClassLoader contextLoader = (ClassLoader ) 555 java.security.AccessController.doPrivileged 556 (new java.security.PrivilegedAction () { 557 public java.lang.Object run() { 558 ClassLoader cl = 562 Thread.currentThread().getContextClassLoader(); 563 return (cl != null) ? cl : 564 ClassLoader.getSystemClassLoader(); 565 566 }}); 567 568 final Class businessInterfaceClass = 569 contextLoader.loadClass(businessInterface); 570 571 ClassLoader appClassLoader = (ClassLoader ) 572 java.security.AccessController.doPrivileged 573 (new java.security.PrivilegedAction () { 574 public java.lang.Object run() { 575 return businessInterfaceClass.getClassLoader(); 576 577 }}); 578 579 return appClassLoader; 580 } 581 582 583 private static boolean isEJB30Ref(EjbReferenceDescriptor refDesc) { 584 return refDesc.isEJB30ClientView(); 585 } 586 587 588 public static void serializeObjectFields(Class clazz, 589 Object instance, 590 ObjectOutputStream oos) 591 throws IOException { 592 593 final ObjectOutputStream objOutStream = oos; 594 595 for(Field next : getSerializationFields(clazz)) { 597 598 final Field nextField = next; 599 final Object theInstance = instance; 600 try { 601 Object value = 602 java.security.AccessController.doPrivileged( 603 new java.security.PrivilegedExceptionAction () { 604 public java.lang.Object run() throws Exception { 605 if( !nextField.isAccessible() ) { 606 nextField.setAccessible(true); 607 } 608 609 return nextField.get(theInstance); 610 } 611 }); 612 objOutStream.writeObject(value); 613 } catch(Throwable t) { 614 IOException ioe = new IOException(); 615 Throwable cause = (t instanceof InvocationTargetException ) ? 616 ((InvocationTargetException )t).getCause() : t; 617 ioe.initCause( cause ); 618 throw ioe; 619 } 620 } 621 } 622 623 public static void deserializeObjectFields(Class clazz, 624 Object instance, 625 ObjectInputStream ois) 626 throws IOException { 627 628 final ObjectInputStream objInputStream = ois; 629 630 for(Field next : getSerializationFields(clazz)) { 634 635 try { 636 637 final Field nextField = next; 638 final Object value = ois.readObject(); 639 final Object theInstance = instance; 640 641 java.security.AccessController.doPrivileged( 642 new java.security.PrivilegedExceptionAction () { 643 public java.lang.Object run() throws Exception { 644 if( !nextField.isAccessible() ) { 645 nextField.setAccessible(true); 646 } 647 648 nextField.set(theInstance, value); 649 return null; 650 } 651 }); 652 } catch(Throwable t) { 653 IOException ioe = new IOException(); 654 Throwable cause = (t instanceof InvocationTargetException ) ? 655 ((InvocationTargetException )t).getCause() : t; 656 ioe.initCause( cause ); 657 throw ioe; 658 } 659 } 660 } 661 662 private static Collection <Field > getSerializationFields(Class clazz) { 663 664 Field [] fields = clazz.getDeclaredFields(); 665 666 SortedMap <String , Field > sortedMap = new TreeMap <String , Field >(); 667 668 for(Field next : fields) { 669 670 int modifiers = next.getModifiers(); 671 if( Modifier.isStatic(modifiers) || 672 Modifier.isTransient(modifiers) ) { 673 continue; 674 } 675 676 sortedMap.put(next.getName(), next); 680 681 } 682 683 return (Collection <Field >) sortedMap.values(); 684 } 685 686 } 687 | Popular Tags |