1 21 package oracle.toplink.essentials.internal.ejb.cmp3; 23 24 import java.util.*; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.lang.instrument.*; 28 import java.security.ProtectionDomain ; 29 30 import oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo; 31 32 import oracle.toplink.essentials.logging.AbstractSessionLog; 33 import oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl; 34 import oracle.toplink.essentials.ejb.cmp3.persistence.PersistenceUnitProcessor; 35 import oracle.toplink.essentials.exceptions.*; 36 import oracle.toplink.essentials.logging.SessionLog; 37 import oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider; 38 import oracle.toplink.essentials.PersistenceProvider; 39 40 import javax.persistence.spi.ClassTransformer; 41 import oracle.toplink.essentials.config.TopLinkProperties; 42 43 53 public class JavaSECMPInitializer { 54 55 public static Instrumentation globalInstrumentation; 57 58 protected boolean shouldCreateInternalLoader = true; 61 62 protected static JavaSECMPInitializer javaSECMPInitializer; 64 65 protected HashMap<String , EntityManagerSetupImpl> emSetupImpls = null; 69 protected HashMap<String , SEPersistenceUnitInfo> emSetupPersistenceUnitInfos = null; 70 71 protected ClassLoader sessionClassLoader = null; 72 73 78 public static JavaSECMPInitializer getJavaSECMPInitializer(Map properties) { 79 if (javaSECMPInitializer == null) { 80 initializeFromMain(properties); 81 } 82 return javaSECMPInitializer; 83 } 84 85 89 public static boolean isSingletonInitialized(){ 90 return javaSECMPInitializer != null; 91 } 92 93 98 public static int getTopLinkLoggingLevel(){ 99 String logLevel = System.getProperty(TopLinkProperties.LOGGING_LEVEL); 100 return AbstractSessionLog.translateStringToLoggingLevel(logLevel); 101 } 102 103 107 protected JavaSECMPInitializer() { 108 super(); 109 emSetupImpls = new HashMap<String , EntityManagerSetupImpl>(); 110 emSetupPersistenceUnitInfos = new HashMap<String , SEPersistenceUnitInfo>(); 111 } 112 113 119 protected boolean callPredeploy(SEPersistenceUnitInfo persistenceUnitInfo, Map m) { 120 String providerClassName = persistenceUnitInfo.getPersistenceProviderClassName(); 122 if (providerClassName == null || providerClassName.equals("") || providerClassName.equals(EntityManagerFactoryProvider.class.getName()) || providerClassName.equals(PersistenceProvider.class.getName())){ 123 Set tempLoaderSet = PersistenceUnitProcessor.buildClassSet(persistenceUnitInfo); 124 ClassLoader tempLoader = createTempLoader(tempLoaderSet); 126 persistenceUnitInfo.setNewTempClassLoader(tempLoader); 127 128 EntityManagerSetupImpl emSetupImpl = new EntityManagerSetupImpl(); 129 emSetupImpls.put(persistenceUnitInfo.getPersistenceUnitName(), emSetupImpl); 130 emSetupPersistenceUnitInfos.put(persistenceUnitInfo.getPersistenceUnitName(), persistenceUnitInfo); 131 132 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_invoke_predeploy", persistenceUnitInfo.getPersistenceUnitName()); 134 135 Map mergedProperties = EntityManagerFactoryProvider.mergeMaps(m, persistenceUnitInfo.getProperties()); 136 if (globalInstrumentation == null && EntityManagerFactoryProvider.getConfigPropertyAsString(TopLinkProperties.WEAVING, mergedProperties, null) == null) { 138 if (m == null) { 139 m = new HashMap(); 140 } 141 m.put(TopLinkProperties.WEAVING, "false"); 142 } 143 144 final ClassTransformer transformer = emSetupImpl.predeploy(persistenceUnitInfo, m); 146 147 if ((transformer != null) && (globalInstrumentation != null)) { 149 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_register_transformer", persistenceUnitInfo.getPersistenceUnitName()); 150 globalInstrumentation.addTransformer(new ClassFileTransformer(){ 151 public byte[] transform( 153 ClassLoader loader, String className, 154 Class <?> classBeingRedefined, 155 ProtectionDomain protectionDomain, 156 byte[] classfileBuffer) throws IllegalClassFormatException { 157 return transformer.transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer); 158 } 159 }); 160 } else if (transformer == null) { 161 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_transformer_is_null"); 162 } else if (globalInstrumentation == null) { 163 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_globalInstrumentation_is_null"); 164 } 165 persistenceUnitInfo.setClassLoader(getMainLoader()); 166 return true; 167 } 168 return false; 169 } 170 171 176 protected ClassLoader createTempLoader(Collection col) { 177 return createTempLoader(col, true); 178 } 179 180 protected ClassLoader createTempLoader(Collection col, boolean shouldOverrideLoadClassForCollectionMembers) { 181 if (!shouldCreateInternalLoader) { 182 return Thread.currentThread().getContextClassLoader(); 183 } 184 185 ClassLoader currentLoader = Thread.currentThread().getContextClassLoader(); 186 if (!(currentLoader instanceof URLClassLoader )) { 187 return currentLoader; 190 } 191 URL [] urlPath = ((URLClassLoader )currentLoader).getURLs(); 192 ClassLoader tempLoader = new TempEntityLoader(urlPath, currentLoader, col, shouldOverrideLoadClassForCollectionMembers); 193 194 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_tempLoader_created", tempLoader); 195 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_shouldOverrideLoadClassForCollectionMembers", new Boolean (shouldOverrideLoadClassForCollectionMembers)); 196 197 return tempLoader; 198 } 199 200 204 public EntityManagerSetupImpl getEntityManagerSetupImpl(String emName){ 205 if (emName == null){ 206 return (EntityManagerSetupImpl)emSetupImpls.get(""); 207 } 208 return (EntityManagerSetupImpl)emSetupImpls.get(emName); 209 } 210 211 public static ClassLoader getMainLoader() { 212 return Thread.currentThread().getContextClassLoader(); 213 } 214 215 221 protected void initPersistenceUnits(URL url, Map m) { 222 Iterator<SEPersistenceUnitInfo> persistenceUnits = PersistenceUnitProcessor.getPersistenceUnits(url, sessionClassLoader).iterator(); 223 while (persistenceUnits.hasNext()){ 224 SEPersistenceUnitInfo persistenceUnitInfo = persistenceUnits.next(); 225 callPredeploy(persistenceUnitInfo, m); 226 } 227 } 228 229 236 public void initialize(Map m) { 237 sessionClassLoader = getMainLoader(); 238 for (URL url: PersistenceUnitProcessor.findPersistenceArchives()){ 239 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", url); 240 initPersistenceUnits(url, m); 241 } 242 } 243 244 249 protected static void initializeFromAgent(Instrumentation instrumentation) throws Exception { 250 AbstractSessionLog.getLog().setLevel(JavaSECMPInitializer.getTopLinkLoggingLevel()); 251 252 globalInstrumentation = instrumentation; 254 javaSECMPInitializer = new JavaSECMPInitializer(); 256 javaSECMPInitializer.initialize(new HashMap()); 258 } 259 260 266 public static void initializeFromMain(Map m) { 267 if (javaSECMPInitializer != null) { 268 return; 269 } 270 271 javaSECMPInitializer = new JavaSECMPInitializer(); 272 AbstractSessionLog.getLog().setLevel(JavaSECMPInitializer.getTopLinkLoggingLevel()); 273 274 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize_from_main"); 275 276 javaSECMPInitializer.initialize(m); 278 } 279 280 285 protected Set loadEntityClasses(Collection entityNames, ClassLoader classLoader) { 286 Set entityClasses = new HashSet(); 287 288 AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_loading_entities_using_loader", classLoader); 290 for (Iterator iter = entityNames.iterator(); iter.hasNext();) { 291 String entityClassName = (String )iter.next(); 292 try { 293 entityClasses.add(classLoader.loadClass(entityClassName)); 294 } catch (ClassNotFoundException cnfEx) { 295 throw ValidationException.entityClassNotFound(entityClassName, classLoader, cnfEx); 296 } 297 } 298 return entityClasses; 299 } 300 301 302 303 304 312 public class TempEntityLoader extends URLClassLoader { 313 Collection classNames; 314 boolean shouldOverrideLoadClassForCollectionMembers; 315 316 public Enumeration<URL > getResources(String name) throws java.io.IOException { 318 return this.getParent().getResources(name); 319 } 320 321 public TempEntityLoader(URL [] urls, ClassLoader parent, Collection classNames, boolean shouldOverrideLoadClassForCollectionMembers) { 322 super(urls, parent); 323 this.classNames = classNames; 324 this.shouldOverrideLoadClassForCollectionMembers = shouldOverrideLoadClassForCollectionMembers; 325 } 326 327 public TempEntityLoader(URL [] urls, ClassLoader parent, Collection classNames) { 328 this(urls, parent, classNames, true); 329 } 330 331 protected boolean shouldOverrideLoadClass(String name) { 334 if (shouldOverrideLoadClassForCollectionMembers) { 335 return (classNames != null) && classNames.contains(name); 337 } else { 338 return !name.startsWith("java.") && !name.startsWith("javax.") && ((classNames == null) || !classNames.contains(name)); 343 } 344 } 345 346 protected synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException { 347 if (shouldOverrideLoadClass(name)) { 348 Class c = findLoadedClass(name); 354 if (c == null) { 355 c = findClass(name); 356 } 357 if (resolve) { 358 resolveClass(c); 359 } 360 return c; 361 } else { 362 return super.loadClass(name, resolve); 363 } 364 } 365 } 366 367 } 368 | Popular Tags |