FileDocCategorySizeDatePackage
JavaSECMPInitializer.javaAPI DocGlassfish v2 API17958Wed Aug 01 11:35:16 BST 2007oracle.toplink.essentials.internal.ejb.cmp3

JavaSECMPInitializer

public class JavaSECMPInitializer extends Object implements PersistenceInitializationActivator
INTERNAL: JavaSECMPInitializer is used to bootstrap the deployment of EntityBeans in EJB 3.0 when deployed in a non-managed setting It is called internally by our Provider
see
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider

Fields Summary
public static Instrumentation
globalInstrumentation
protected boolean
shouldCreateInternalLoader
protected static JavaSECMPInitializer
javaSECMPInitializer
protected ClassLoader
sessionClassLoader
Constructors Summary
protected JavaSECMPInitializer()
INTERNAL: User should not instantiate JavaSECMPInitializer.

        super();
    
Methods Summary
protected booleancallPredeploy(oracle.toplink.essentials.ejb.cmp3.persistence.SEPersistenceUnitInfo persistenceUnitInfo, java.util.Map m, PersistenceInitializationActivator persistenceActivator)
INTERNAL predeploy (with deploy) is one of the two steps required in deployment of entities This method will prepare to call predeploy, call it and finally register the transformer returned to be used for weaving.

        ClassLoader tempLoader = null;
        // we will only attempt to deploy when TopLink is specified as the provider or the provider is unspecified
        String providerClassName = persistenceUnitInfo.getPersistenceProviderClassName();
        if (persistenceActivator.isPersistenceProviderSupported(providerClassName)){
            EntityManagerSetupImpl emSetupImpl = EntityManagerFactoryProvider.getEntityManagerSetupImpl(persistenceUnitInfo.getPersistenceUnitRootUrl()+persistenceUnitInfo.getPersistenceUnitName());
            
            // if we already have an EntityManagerSetupImpl this PU has already been processed.  Use the existing one
            if (emSetupImpl != null && !emSetupImpl.isUndeployed()){
                return false;
            }
            Set tempLoaderSet = PersistenceUnitProcessor.buildClassSet(persistenceUnitInfo, Thread.currentThread().getContextClassLoader());

            Map mergedProperties = EntityManagerFactoryProvider.mergeMaps(m, persistenceUnitInfo.getProperties());

            String weaving = EntityManagerFactoryProvider.getConfigPropertyAsString(TopLinkProperties.WEAVING, mergedProperties, null);
            // Bug#4452468  When globalInstrumentation is null, there is no weaving
            if (globalInstrumentation == null) {
                if (weaving == null) {
                   mergedProperties.put(TopLinkProperties.WEAVING, "false");
                   weaving = "false";
                }else if (weaving.equalsIgnoreCase("true")){
                    throw new PersistenceException(EntityManagerSetupException.wrongWeavingPropertyValue());
                }
            }
            
            // Bug#2741: If weaving disabled then use regular loader, not a temp one
            if (weaving != null && (weaving.equalsIgnoreCase("false") || weaving.equalsIgnoreCase("static"))) {
                shouldCreateInternalLoader=false;
            }
            // Create the temp loader that will not cache classes for entities in our persistence unit
            tempLoader = createTempLoader(tempLoaderSet);
            persistenceUnitInfo.setNewTempClassLoader(tempLoader);
            persistenceUnitInfo.setClassLoader(getMainLoader());
            if (emSetupImpl == null){
                emSetupImpl = new EntityManagerSetupImpl();
                EntityManagerFactoryProvider.addEntityManagerSetupImpl(persistenceUnitInfo.getPersistenceUnitRootUrl()+persistenceUnitInfo.getPersistenceUnitName(), emSetupImpl);
            }
            // Make the callback
            AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_invoke_predeploy", persistenceUnitInfo.getPersistenceUnitName());

            // A call to predeploy will partially build the session we will use
            final ClassTransformer transformer = emSetupImpl.predeploy(persistenceUnitInfo, mergedProperties);
    
            // If we got a transformer then register it 
            if ((transformer != null) && (globalInstrumentation != null)) {
                AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_register_transformer", persistenceUnitInfo.getPersistenceUnitName());
                globalInstrumentation.addTransformer(new ClassFileTransformer(){
                    // adapt ClassTransformer to ClassFileTransformer interface
                    public byte[] transform(
                            ClassLoader loader, String className,
                            Class<?> classBeingRedefined,
                            ProtectionDomain protectionDomain,
                            byte[] classfileBuffer) throws IllegalClassFormatException {
                        return transformer.transform(loader, className, classBeingRedefined, protectionDomain, classfileBuffer);
                    }
                });
            } else if (transformer == null) {
                AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_transformer_is_null");
            } else if (globalInstrumentation == null) {
                AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_globalInstrumentation_is_null");
            }
            return true;
        }
        return false;
    
protected java.lang.ClassLoadercreateTempLoader(java.util.Collection col)
Create a temporary class loader that can be used to inspect classes and then thrown away. This allows classes to be introspected prior to loading them with application's main class loader enabling weaving.

        return createTempLoader(col, true);
    
protected java.lang.ClassLoadercreateTempLoader(java.util.Collection col, boolean shouldOverrideLoadClassForCollectionMembers)

        if (!shouldCreateInternalLoader) {
            return Thread.currentThread().getContextClassLoader();
        }

        ClassLoader currentLoader = Thread.currentThread().getContextClassLoader();
        if (!(currentLoader instanceof URLClassLoader)) {
            //we can't create a TempEntityLoader so just use the current one
            //shouldn't be a problem (and should only occur) in JavaSE
            return currentLoader;
        }
        URL[] urlPath = ((URLClassLoader)currentLoader).getURLs();
        ClassLoader tempLoader = new TempEntityLoader(urlPath, currentLoader, col, shouldOverrideLoadClassForCollectionMembers);

        AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_tempLoader_created", tempLoader);
        AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_shouldOverrideLoadClassForCollectionMembers", new Boolean(shouldOverrideLoadClassForCollectionMembers));

        return tempLoader;
    
public static synchronized oracle.toplink.essentials.internal.ejb.cmp3.JavaSECMPInitializergetJavaSECMPInitializer()
INTERNAL: Get the singleton entityContainer.

return
EnityContainer


                
         
        if (javaSECMPInitializer == null) {
           javaSECMPInitializer = new JavaSECMPInitializer();
        }
        AbstractSessionLog.getLog().setLevel(JavaSECMPInitializer.getTopLinkLoggingLevel());
        return javaSECMPInitializer;
    
public static java.lang.ClassLoadergetMainLoader()

        return Thread.currentThread().getContextClassLoader();
    
public static intgetTopLinkLoggingLevel()
Look in the System properties for a logging level property and return a integer that can be used to set the logging level in TopLink

return

        String logLevel = System.getProperty(TopLinkProperties.LOGGING_LEVEL);
        return AbstractSessionLog.translateStringToLoggingLevel(logLevel);  
    
protected voidinitPersistenceUnits(oracle.toplink.essentials.ejb.cmp3.persistence.Archive archive, java.util.Map m, PersistenceInitializationActivator persistenceActivator)
Initialize one persistence unit. Initialization is a two phase process. First the predeploy process builds the metadata and creates any required transformers. Second the deploy process creates a TopLink session based on that metadata.

        Iterator<SEPersistenceUnitInfo> persistenceUnits = PersistenceUnitProcessor.getPersistenceUnits(archive, sessionClassLoader).iterator();
        while (persistenceUnits.hasNext()){
            SEPersistenceUnitInfo persistenceUnitInfo = persistenceUnits.next();
            callPredeploy(persistenceUnitInfo, m, persistenceActivator);
        }
    
public voidinitialize(java.util.Map m, PersistenceInitializationActivator persistenceActivator)
INTERNAL This method initializes the container. Essentially, it will try to load the class that contains the list of entities and reflectively call the method that contains that list. It will then initialize the container with that list. If succeeded return true, false otherwise.

        sessionClassLoader = getMainLoader();
        final Set<Archive> pars = PersistenceUnitProcessor.findPersistenceArchives();
        for (Archive archive: pars){
            AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_init_initialize", archive);
            initPersistenceUnits(archive, m, persistenceActivator);
        }
    
protected static voidinitializeFromAgent(java.lang.instrument.Instrumentation instrumentation)
INTERNAL: Should be called only by the agent. (when weaving classes) If succeeded return true, false otherwise.

        AbstractSessionLog.getLog().setLevel(JavaSECMPInitializer.getTopLinkLoggingLevel());

        // Squirrel away the instrumentation for later
        globalInstrumentation = instrumentation;
        // Create JavaSECMPInitializer singleton
        javaSECMPInitializer = new JavaSECMPInitializer();
        // Initialize it
        javaSECMPInitializer.initialize(new HashMap(), javaSECMPInitializer);
    
public booleanisPersistenceProviderSupported(java.lang.String providerClassName)
Returns whether the given persistence provider class is supported by this implementation

param
providerClassName
return

        return (providerClassName == null) || providerClassName.equals("") || providerClassName.equals(EntityManagerFactoryProvider.class.getName()) || providerClassName.equals(PersistenceProvider.class.getName());
    
public static booleanisSingletonInitialized()
Return whether initialization has occured without actually triggering initialization

        return javaSECMPInitializer != null;
    
protected java.util.SetloadEntityClasses(java.util.Collection entityNames, java.lang.ClassLoader classLoader)
INTERNAL: Create a list of java.lang.Class that contains the classes of all the entities that we will be deploying

        Set entityClasses = new HashSet();

        // Load the classes using the loader passed in
        AbstractSessionLog.getLog().log(SessionLog.FINER, "cmp_loading_entities_using_loader", classLoader);
        for (Iterator iter = entityNames.iterator(); iter.hasNext();) {
            String entityClassName = (String)iter.next();
            try {
                entityClasses.add(classLoader.loadClass(entityClassName));
            } catch (ClassNotFoundException cnfEx) {
                throw ValidationException.entityClassNotFound(entityClassName, classLoader, cnfEx);
            }
        }
        return entityClasses;