FileDocCategorySizeDatePackage
StaticWeaveClassTransformer.javaAPI DocGlassfish v2 API9045Tue May 22 16:54:44 BST 2007oracle.toplink.essentials.internal.weaving

StaticWeaveClassTransformer

public class StaticWeaveClassTransformer extends Object

Description: This class provides the implementation of class transformer by leveraging on the following existing APIs,

  • PersistenceUnitProcessor.processORMetadata() - get class descriptor.
  • PersistenceUnitProcessor.buildEntityList() - get entity classes lsit.
  • TransformerFactory.createTransformerAndModifyProject - get class transformer.

Responsibilities:

  • Create the classtransformer for each persistence unit individually and store them into the list.
  • Provide class transfom method to perform weaving function.

Fields Summary
private ArrayList
classTransformers
private Writer
logWriter
private int
logLevel
private ClassLoader
aClassLoader
Constructors Summary
public StaticWeaveClassTransformer(URL inputArchiveURL, ClassLoader aclassloader)
Constructs an instance of StaticWeaveClassTransformer

param
inputArchiveURL
param
aclassloader
throws
Exception

    
                    
          
        this(inputArchiveURL,aclassloader,null,SessionLog.OFF);
    
public StaticWeaveClassTransformer(URL inputArchiveURL, ClassLoader aclassloader, Writer logWriter, int loglevel)
Constructs an instance of StaticWeaveClassTransformer

param
inputArchiveURL
param
aclassloader
param
log
param
loglevel
throws
Exception

        this.aClassLoader = aclassloader;
        this.logWriter=logWriter;
        this.logLevel=loglevel;
        buildClassTransformers(inputArchiveURL,aclassloader);
    
Methods Summary
private voidbuildClassTransformers(java.net.URL inputArchiveURL, java.lang.ClassLoader aclassloader)
INTERNAL: The method creates classtransformer list corresponding to each persistence unit.

param
inputArchiveURL
param
aclassloader
throws
Exception

 
        if(classTransformers!=null) {
            return ;
        } else{
            classTransformers = new ArrayList<ClassTransformer>();
        }
        Archive archive =null;
        try{
           archive = (new ArchiveFactoryImpl()).createArchive(inputArchiveURL);
        }catch(ZipException e){
            throw StaticWeaveException.exceptionOpeningArchive(inputArchiveURL,e);
        }
            
        List<SEPersistenceUnitInfo> persistenceUnitsList = 
        PersistenceUnitProcessor.processPersistenceArchive(archive, aclassloader);
        if(persistenceUnitsList==null){
            throw PersistenceUnitLoadingException.couldNotGetUnitInfoFromUrl(inputArchiveURL);
        }
        Iterator<SEPersistenceUnitInfo> persistenceUnitsIterator = persistenceUnitsList.iterator();
        while (persistenceUnitsIterator.hasNext()){
            SEPersistenceUnitInfo unitInfo = (SEPersistenceUnitInfo)persistenceUnitsIterator.next();
            unitInfo.setNewTempClassLoader(aclassloader);
            //build class transformer.
            ClassTransformer transformer = buildTransformer(unitInfo,this.logWriter,this.logLevel);
            classTransformers.add(transformer);
        }
    
private javax.persistence.spi.ClassTransformerbuildTransformer(javax.persistence.spi.PersistenceUnitInfo unitInfo, java.io.Writer logWriter, int logLevel)
INTERNAL: This method builds the classtransformer for the specified perisistence unit.

param
unitInfo
param
logWriter
param
logLevel
return
a ClassTransformer

        //persistenceUnitInfo = unitInfo;
        ClassLoader privateClassLoader = unitInfo.getNewTempClassLoader();

        // create server session (it should be done before initializing ServerPlatform)
        ServerSession session = new ServerSession(new Project(new DatabaseLogin()));
        session.setLogLevel(logLevel);
        if(logWriter!=null){
            ((DefaultSessionLog)session.getSessionLog()).setWriter(logWriter);
         }
        
        session.getPlatform().setConversionManager(new EJB30ConversionManager());

        // Create an instance of MetadataProcessor for specified persistence unit info
        MetadataProcessor processor = new MetadataProcessor(unitInfo, session, privateClassLoader, true);
        // Process the Object/relational metadata from XML and annotations.
        PersistenceUnitProcessor.processORMetadata(processor,privateClassLoader, session, false);

        //Collection entities = buildEntityList(persistenceUnitInfo, privateClassLoader);
        Collection entities = PersistenceUnitProcessor.buildEntityList(processor,privateClassLoader);

        // The transformer is capable of altering domain classes to handle a LAZY hint for OneToOne mappings.  It will only
        // be returned if we we are mean to process these mappings
        return TransformerFactory.createTransformerAndModifyProject(session, entities, privateClassLoader);
    
public byte[]transform(java.lang.String originalClassName, java.lang.Class originalClass, byte[] originalClassBytes)
INTERNAL: The method performs weaving function on the given class.

param
originalClassName
param
originalClass
param
originalClassBytes
return
the converted(woven) class
throws
Exception

        byte[] newClassBytes = null;
        for(ClassTransformer transformer : classTransformers){
            newClassBytes=transformer.transform(aClassLoader, originalClassName,originalClass, null, originalClassBytes);
            if(newClassBytes!=null) {
                break;
            };
        }
        return newClassBytes;