Search in sources :

Example 1 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project motech by motech.

the class MetadataHolderTest method shouldReturnAndReloadMetadata.

@Test
public void shouldReturnAndReloadMetadata() {
    JDOMetadata jdoMetadata = mock(JDOMetadata.class);
    when(pmf.newMetadata()).thenReturn(jdoMetadata);
    // constructs initial metadata, then reloads
    assertEquals(jdoMetadata, metadataHolder.getJdoMetadata());
    assertEquals(jdoMetadata, metadataHolder.reloadMetadata());
    verify(pmf, times(2)).newMetadata();
    // retrieves existing metadata
    assertEquals(jdoMetadata, metadataHolder.getJdoMetadata());
    verifyNoMoreInteractions(pmf);
}
Also used : JDOMetadata(javax.jdo.metadata.JDOMetadata) Test(org.junit.Test)

Example 2 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.

the class JDOMetadataAPITest method testClassInPackageAndJDO.

/**
 * Simple test that checks that classes retrieved have a parent package etc
 */
public void testClassInPackageAndJDO() {
    TypeMetadata typemd = pmf.getMetadata(Computer.class.getName());
    PackageMetadata pmd = (PackageMetadata) typemd.getParent();
    assertNotNull("Package of class is null!", pmd);
    assertEquals("Package name is different to expected", Computer.class.getName().substring(0, Computer.class.getName().lastIndexOf('.')), pmd.getName());
    JDOMetadata jdomd = (JDOMetadata) pmd.getParent();
    assertNotNull("JDOMetadata of package is null!", jdomd);
}
Also used : TypeMetadata(javax.jdo.metadata.TypeMetadata) PackageMetadata(javax.jdo.metadata.PackageMetadata) JDOMetadata(javax.jdo.metadata.JDOMetadata) Computer(org.datanucleus.samples.annotations.embedded.Computer)

Example 3 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method schemaCreate.

public void schemaCreate(DynamicEnhanceSchemaToolClassLoader runtimeCL) throws Exception {
    Map props = getPropertiesForDatastore(runtimeCL);
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOHelper.getPersistenceManagerFactory(props);
    try {
        JDOMetadata filemd = pmf.newMetadata();
        createMetadata(filemd);
        pmf.registerMetadata(filemd);
        Set<String> classNames = new HashSet<>();
        classNames.add("test.Client");
        PersistenceNucleusContext nucCtx = pmf.getNucleusContext();
        StoreManager storeMgr = nucCtx.getStoreManager();
        if (!(storeMgr instanceof SchemaAwareStoreManager)) {
            // Can't create schema with this datastore
            return;
        }
        try {
            SchemaTool schematool = new SchemaTool();
            schematool.setDdlFile("target/schema.ddl");
            schematool.setCompleteDdl(true);
            SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) nucCtx.getStoreManager();
            schematool.createSchemaForClasses(schemaStoreMgr, classNames);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        pmf.close();
    }
}
Also used : JDOMetadata(javax.jdo.metadata.JDOMetadata) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) SchemaTool(org.datanucleus.store.schema.SchemaTool) HashMap(java.util.HashMap) Map(java.util.Map) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager) HashSet(java.util.HashSet) StoreManager(org.datanucleus.store.StoreManager) SchemaAwareStoreManager(org.datanucleus.store.schema.SchemaAwareStoreManager)

Example 4 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project tests by datanucleus.

the class DynamicEnhanceSchemaToolTest method testEnhance.

public void testEnhance() throws Exception {
    // Create an in-memory class
    String className = "test.Client";
    NucleusLogger.PERSISTENCE.info(">> Creating class in-memory");
    byte[] classBytes = createClass(className);
    // Add it to a CustomClassLoader
    DynamicEnhanceSchemaToolClassLoader workCL = new DynamicEnhanceSchemaToolClassLoader(Thread.currentThread().getContextClassLoader());
    workCL.defineClass("test.Client", classBytes);
    // Write the class to disk (debugging)
    NucleusLogger.PERSISTENCE.info(">> Writing in-memory class to target/generated/Client.class");
    File file = new File("target/generated");
    if (!file.exists()) {
        file.mkdirs();
    }
    FileOutputStream fos = new FileOutputStream("target/generated/Client.class");
    fos.write(classBytes);
    fos.close();
    // Create an enhancer (JDO, ASM)
    JDOEnhancer enhancer = JDOHelper.getEnhancer();
    enhancer.setClassLoader(workCL);
    // Create MetaData for the in-memory class and register it with the enhancer
    JDOMetadata jdomd = enhancer.newMetadata();
    createMetadata(jdomd);
    enhancer.registerMetadata(jdomd);
    enhancer.addClass(className, classBytes);
    // Enhance the in-memory bytes and obtain the enhanced bytes
    NucleusLogger.PERSISTENCE.info(">> Enhancing test.Client inmemory bytes");
    enhancer.enhance();
    byte[] enhancedBytes = enhancer.getEnhancedBytes(className);
    // Write the enhanced class to disk (debugging)
    NucleusLogger.PERSISTENCE.info(">> Writing enhanced in-memory class to target/enhancedClient.class");
    file = new File("target/enhanced");
    if (!file.exists()) {
        file.mkdirs();
    }
    fos = new FileOutputStream("target/enhanced/Client.class");
    fos.write(enhancedBytes);
    fos.close();
    // Create our runtime class loader, and load the enhanced class into it
    DynamicEnhanceSchemaToolClassLoader runtimeCL = new DynamicEnhanceSchemaToolClassLoader(Thread.currentThread().getContextClassLoader());
    runtimeCL.defineClass(className, enhancedBytes);
    // SchemaTool
    NucleusLogger.PERSISTENCE.info(">> Schema creation for dynamic type");
    schemaCreate(runtimeCL);
    // Persist an object of the new type
    NucleusLogger.PERSISTENCE.info(">> Persisting an object of dynamic type");
    persist(runtimeCL);
}
Also used : JDOEnhancer(javax.jdo.JDOEnhancer) FileOutputStream(java.io.FileOutputStream) JDOMetadata(javax.jdo.metadata.JDOMetadata) File(java.io.File)

Example 5 with JDOMetadata

use of javax.jdo.metadata.JDOMetadata in project motech by motech.

the class MDSConstructorImpl method constructEntities.

@Override
public synchronized boolean constructEntities(SchemaHolder schemaHolder) {
    // To be able to register updated class, we need to reload class loader
    // and therefore add all the classes again
    MotechClassPool.clearEnhancedData();
    MDSClassLoader.reloadClassLoader();
    // we need an jdo enhancer and a temporary classLoader
    // to define classes in before enhancement
    MDSClassLoader tmpClassLoader = MDSClassLoader.getStandaloneInstance();
    MdsJDOEnhancer enhancer = createEnhancer(tmpClassLoader);
    JavassistLoader loader = new JavassistLoader(tmpClassLoader);
    // process only entities that are not drafts
    List<EntityDto> entities = schemaHolder.getAllEntities();
    filterEntities(entities);
    sortEntities(entities, schemaHolder);
    // create enum for appropriate combobox fields
    for (EntityDto entity : entities) {
        buildEnum(loader, enhancer, entity, schemaHolder);
    }
    // load entities interfaces
    for (EntityDto entity : entities) {
        buildInterfaces(loader, enhancer, entity);
    }
    // generate jdo metadata from scratch for our entities
    JDOMetadata jdoMetadata = metadataHolder.reloadMetadata();
    // since we just fetch fields from existing definition
    for (EntityDto entity : entities) {
        if (entity.isRecordHistory()) {
            entityBuilder.prepareHistoryClass(entity);
        }
        entityBuilder.prepareTrashClass(entity);
    }
    // Build classes
    Map<String, ClassData> classDataMap = buildClasses(entities, schemaHolder);
    List<Class> classes = new ArrayList<>();
    // the temporary ClassLoader and enhancer
    for (EntityDto entity : entities) {
        String className = entity.getClassName();
        Class<?> definition = addClassData(loader, enhancer, classDataMap.get(className));
        if (entity.isRecordHistory()) {
            addClassData(loader, enhancer, classDataMap.get(ClassName.getHistoryClassName(className)));
        }
        addClassData(loader, enhancer, classDataMap.get(ClassName.getTrashClassName(className)));
        classes.add(definition);
        LOGGER.debug("Generated classes for {}", entity.getClassName());
    }
    for (Class<?> definition : classes) {
        loader.loadFieldsAndMethodsOfClass(definition);
    }
    // Prepare metadata
    buildMetadata(entities, jdoMetadata, classDataMap, classes, schemaHolder);
    // after the classes are defined, we register their metadata
    enhancer.registerMetadata(jdoMetadata);
    // then, we commence with enhancement
    enhancer.enhance();
    // we register the enhanced class bytes
    // and build the infrastructure classes
    registerEnhancedClassBytes(entities, enhancer, schemaHolder);
    metadataBuilder.fixEnhancerIssuesInMetadata(jdoMetadata, schemaHolder);
    return CollectionUtils.isNotEmpty(entities);
}
Also used : MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) JDOMetadata(javax.jdo.metadata.JDOMetadata) ArrayList(java.util.ArrayList) JavassistLoader(org.motechproject.mds.javassist.JavassistLoader) EntityDto(org.motechproject.mds.dto.EntityDto) MdsJDOEnhancer(org.motechproject.mds.enhancer.MdsJDOEnhancer) ClassData(org.motechproject.mds.domain.ClassData) CtClass(javassist.CtClass)

Aggregations

JDOMetadata (javax.jdo.metadata.JDOMetadata)7 HashMap (java.util.HashMap)2 Map (java.util.Map)2 PackageMetadata (javax.jdo.metadata.PackageMetadata)2 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CtClass (javassist.CtClass)1 JDOEnhancer (javax.jdo.JDOEnhancer)1 PersistenceManager (javax.jdo.PersistenceManager)1 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)1 Transaction (javax.jdo.Transaction)1 TypeMetadata (javax.jdo.metadata.TypeMetadata)1 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JDOMetadataImpl (org.datanucleus.api.jdo.metadata.JDOMetadataImpl)1 FileMetaData (org.datanucleus.metadata.FileMetaData)1 Computer (org.datanucleus.samples.annotations.embedded.Computer)1