Search in sources :

Example 1 with EnhancementNucleusContextImpl

use of org.datanucleus.enhancer.EnhancementNucleusContextImpl in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataManager method addORMDataToClass.

/**
 * Load up and add the O/R mapping info for the specified class to the stored JDO ClassMetaData.
 * @param c The class
 * @param clr the ClassLoaderResolver
 */
public void addORMDataToClass(Class c, ClassLoaderResolver clr) {
    if (getNucleusContext() instanceof EnhancementNucleusContextImpl) {
        // We don't need ORM data when enhancing
        return;
    }
    if (!allowORM) {
        // StoreManager doesn't "map" to the datastore so don't use ORM info
        return;
    }
    // Get the JDO MetaData for this class/interface
    AbstractClassMetaData cmd = classMetaDataByClass.get(c.getName());
    // See if we already have a file registered with the ORM metadata for this class
    AbstractClassMetaData ormCmd = ormClassMetaDataByClass.get(c.getName());
    if (ormCmd != null) {
        // Merge the ORM class into the JDO class
        MetaDataMerger.mergeClassORMData(cmd, ormCmd, this);
        // Remove it from the map since no longer needed
        ormClassMetaDataByClass.remove(c.getName());
        return;
    }
    if (allowXML) {
        // No ORM loaded for this class, so find if there is any ORM metadata available
        FileMetaData filemdORM = loadXMLMetaDataForClass(c, clr, getORMMappingName(), getORMFileSuffix(), MetadataFileType.JDO_ORM_FILE, false);
        if (filemdORM != null) {
            // The ORM file has now been registered, so find the class and merge it into the JDO definition
            ormCmd = ormClassMetaDataByClass.get(c.getName());
            if (ormCmd != null) {
                // Merge the ORM file into the JDO file
                MetaDataMerger.mergeFileORMData((FileMetaData) cmd.getPackageMetaData().getParent(), (FileMetaData) ormCmd.getPackageMetaData().getParent());
                // Merge the ORM class into the JDO class
                MetaDataMerger.mergeClassORMData(cmd, ormCmd, this);
                // Remove it from the map since no longer needed
                ormClassMetaDataByClass.remove(c.getName());
            }
        }
    }
}
Also used : AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) FileMetaData(org.datanucleus.metadata.FileMetaData) EnhancementNucleusContextImpl(org.datanucleus.enhancer.EnhancementNucleusContextImpl)

Example 2 with EnhancementNucleusContextImpl

use of org.datanucleus.enhancer.EnhancementNucleusContextImpl in project tests by datanucleus.

the class JDOTestBase method getEnhancedClassesFromFile.

/**
 * Method to return a set of enhanced classes for all of those found in the specified JDO MetaData file.
 * @param resourceName Name of the MetaData file (relative to the CLASSPATH).
 * @return Set of enhanced classes
 * @throws IllegalArgumentException if an error occurs reading the file resource
 */
public Class[] getEnhancedClassesFromFile(String resourceName) {
    InputStream in = JDOTestBase.class.getClassLoader().getResourceAsStream(resourceName);
    if (in == null) {
        throw new IllegalArgumentException("Cannot load resource :" + resourceName);
    }
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(in));
        String buf = null;
        while ((buf = br.readLine()) != null) {
            sb.append(buf);
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Error reading MetaData file " + resourceName + ": " + e.getMessage(), e);
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String jdoXmlContents = sb.toString();
    if (jdoXmlContents == null) {
        throw new IllegalArgumentException("Contents of file is null");
    }
    NucleusContext context = new EnhancementNucleusContextImpl("JDO", null);
    MetaDataManager mgr = new JDOMetaDataManager(context);
    MetaDataParser parser = new MetaDataParser(mgr, context.getPluginManager(), true, true);
    ClassLoaderResolverImpl clr = new ClassLoaderResolverImpl();
    // Parse the MetaData
    FileMetaData filemd = (FileMetaData) parser.parseMetaDataStream(new ByteArrayInputStream(jdoXmlContents.getBytes()), null, "JDO");
    if (filemd == null) {
        return null;
    }
    mgr.registerFile("EnhancerTestXMLFile", filemd, clr);
    // Populate/Initialise the MetaData for the actual classes.
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = pmd.getClass(j);
            cmd.populate(clr, null, mgr);
            cmd.initialise(clr);
        }
    }
    // Enhance the classes
    TestClassLoader cl = new TestClassLoader();
    Class[] result;
    ArrayList<Class> resultList = new ArrayList<Class>();
    for (int i = 0; i < filemd.getNoOfPackages(); i++) {
        PackageMetaData pmd = filemd.getPackage(i);
        for (int j = 0; j < pmd.getNoOfClasses(); j++) {
            ClassMetaData cmd = (ClassMetaData) pmd.getClass(j);
            // Enhance the class using the MetaData
            ClassEnhancer enhancer = getClassEnhancer(cmd, mgr);
            enhancer.enhance();
            // Save the enhanced class
            resultList.add(cl.getClass(cmd.getFullClassName(), enhancer.getClassBytes()));
        }
    }
    result = (Class[]) resultList.toArray(new Class[resultList.size()]);
    return result;
}
Also used : NucleusContext(org.datanucleus.NucleusContext) ArrayList(java.util.ArrayList) FileMetaData(org.datanucleus.metadata.FileMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PackageMetaData(org.datanucleus.metadata.PackageMetaData) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) MetaDataManager(org.datanucleus.metadata.MetaDataManager) IOException(java.io.IOException) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) ClassEnhancer(org.datanucleus.enhancer.ClassEnhancer) EnhancementNucleusContextImpl(org.datanucleus.enhancer.EnhancementNucleusContextImpl) ClassLoaderResolverImpl(org.datanucleus.ClassLoaderResolverImpl)

Aggregations

EnhancementNucleusContextImpl (org.datanucleus.enhancer.EnhancementNucleusContextImpl)2 FileMetaData (org.datanucleus.metadata.FileMetaData)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)1 NucleusContext (org.datanucleus.NucleusContext)1 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)1 ClassEnhancer (org.datanucleus.enhancer.ClassEnhancer)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 ClassMetaData (org.datanucleus.metadata.ClassMetaData)1 MetaDataManager (org.datanucleus.metadata.MetaDataManager)1 PackageMetaData (org.datanucleus.metadata.PackageMetaData)1 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)1