Search in sources :

Example 1 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class PersistenceFactoryBase method get.

/**
 * Gets existing persistence context or create new based on given parameters if it doesn't exist.
 */
@Override
public PersistenceContext get(String persistenceUnitName, URI defaultURI, String version, Map<String, Object> initializationProperties) {
    PersistenceContext persistenceContext = getDynamicPersistenceContext(persistenceUnitName, version);
    if (persistenceContext == null) {
        try {
            DynamicClassLoader dcl = new DynamicRestClassLoader(Thread.currentThread().getContextClassLoader());
            Map<String, Object> properties = new HashMap<>();
            properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
            if (initializationProperties != null) {
                properties.putAll(initializationProperties);
            }
            properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
            EntityManagerFactoryImpl factory = (EntityManagerFactoryImpl) Persistence.createEntityManagerFactory(persistenceUnitName, properties);
            ClassLoader sessionLoader = factory.getServerSession().getLoader();
            if (!DynamicClassLoader.class.isAssignableFrom(sessionLoader.getClass())) {
                properties = new HashMap<>();
                dcl = new DynamicRestClassLoader(sessionLoader);
                properties.put(PersistenceUnitProperties.CLASSLOADER, dcl);
                if (initializationProperties != null) {
                    properties.putAll(initializationProperties);
                }
                properties.putIfAbsent(PersistenceUnitProperties.WEAVING_REST, "true");
                factory.refreshMetadata(properties);
            }
            persistenceContext = bootstrapPersistenceContext(persistenceUnitName, factory, defaultURI, version, true);
        } catch (Exception e) {
            JPARSLogger.exception("exception_creating_persistence_context", new Object[] { persistenceUnitName, e.toString() }, e);
        }
    }
    if ((persistenceContext != null) && (!persistenceContext.isWeavingEnabled())) {
        JPARSLogger.error(persistenceContext.getServerSession().getSessionLog(), "weaving_required_for_relationships", new Object[] {});
        throw JPARSException.invalidConfiguration();
    }
    return persistenceContext;
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) HashMap(java.util.HashMap) DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) EntityManagerFactoryImpl(org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 2 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class CollectionWrapperBuilder method getRestCollectionProxy.

private CollectionProxy getRestCollectionProxy(final Collection<?> toProxy, final String entityName, final String fieldname) {
    try {
        final DynamicClassLoader classLoader = (DynamicClassLoader) context.getServerSession().getDatasourcePlatform().getConversionManager().getLoader();
        final CollectionProxyClassWriter writer = new CollectionProxyClassWriter(toProxy.getClass().getName(), entityName, fieldname);
        final String proxyClassName = writer.getClassName();
        if (classLoader.getClassWriter(proxyClassName) == null) {
            classLoader.addClass(proxyClassName, writer);
        }
        final Class<?> referenceAdaptorClass = Class.forName(proxyClassName, true, classLoader);
        final Class<?>[] argTypes = { Collection.class };
        final Constructor<?> referenceAdaptorConstructor = referenceAdaptorClass.getDeclaredConstructor(argTypes);
        final Object[] args = new Object[] { toProxy };
        return (CollectionProxy) referenceAdaptorConstructor.newInstance(args);
    } catch (Exception e) {
        throw JPARSException.exceptionOccurred(e);
    }
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) CollectionProxyClassWriter(org.eclipse.persistence.internal.jpa.rs.weaving.CollectionProxyClassWriter) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 3 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class SimpleTypes_MultiTable method setUp.

@BeforeClass
public static void setUp() {
    session = createSession();
    dynamicHelper = new DynamicHelper(session);
    DynamicClassLoader dcl = dynamicHelper.getDynamicClassLoader();
    Class<?> simpleTypeA = dcl.createDynamicClass("simple.mappings.SimpleA");
    DynamicTypeBuilder typeBuilder = new DynamicTypeBuilder(simpleTypeA, null, "SIMPLE_TYPE_A", "SIMPLE_TYPE_B", "SIMPLE_TYPE_C");
    typeBuilder.setPrimaryKeyFields("SIMPLE_TYPE_A.SID");
    typeBuilder.addDirectMapping("id", int.class, "SIMPLE_TYPE_A.SID");
    typeBuilder.addDirectMapping("value1", String.class, "SIMPLE_TYPE_A.VAL_1");
    typeBuilder.addDirectMapping("value2", boolean.class, "SIMPLE_TYPE_B.VAL_2");
    typeBuilder.addDirectMapping("value3", String.class, "SIMPLE_TYPE_B.VAL_3");
    typeBuilder.addDirectMapping("value4", double.class, "SIMPLE_TYPE_C.VAL_4");
    typeBuilder.addDirectMapping("value5", String.class, "SIMPLE_TYPE_C.VAL_5");
    dynamicHelper.addTypes(true, true, typeBuilder.getType());
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) DynamicHelper(org.eclipse.persistence.dynamic.DynamicHelper) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) BeforeClass(org.junit.BeforeClass)

Example 4 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class SimpleTypes_OneToMany method setUp.

@BeforeClass
public static void setUp() {
    session = createSession();
    dynamicHelper = new DynamicHelper(session);
    DynamicClassLoader dcl = dynamicHelper.getDynamicClassLoader();
    // Create Dynamic Classes
    Class<?> simpleTypeA = dcl.createDynamicClass("simple.mappings.SimpleA");
    Class<?> simpleTypeB = dcl.createDynamicClass("simple.mappings.SimpleB");
    // Build dynamic types with mappings
    DynamicTypeBuilder aTypeBuilder = new DynamicTypeBuilder(simpleTypeA, null, "SIMPLE_TYPE_A");
    aTypeBuilder.setPrimaryKeyFields("SID");
    DynamicTypeBuilder bTypeBuilder = new DynamicTypeBuilder(simpleTypeB, null, "SIMPLE_TYPE_B");
    bTypeBuilder.setPrimaryKeyFields("SID");
    bTypeBuilder.addDirectMapping("id", int.class, "SID");
    bTypeBuilder.addDirectMapping("value1", String.class, "VAL_1");
    bTypeBuilder.addOneToOneMapping("a", aTypeBuilder.getType(), "A_FK");
    aTypeBuilder.addDirectMapping("id", int.class, "SID");
    aTypeBuilder.addDirectMapping("value1", String.class, "VAL_1");
    aTypeBuilder.addOneToManyMapping("b", bTypeBuilder.getType(), "A_FK");
    dynamicHelper.addTypes(true, true, aTypeBuilder.getType(), bTypeBuilder.getType());
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) DynamicHelper(org.eclipse.persistence.dynamic.DynamicHelper) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) BeforeClass(org.junit.BeforeClass)

Example 5 with DynamicClassLoader

use of org.eclipse.persistence.dynamic.DynamicClassLoader in project eclipselink by eclipse-ee4j.

the class SimpleTypes_AggregateObject method setUp.

@BeforeClass
public static void setUp() {
    session = createSession();
    dynamicHelper = new DynamicHelper(session);
    DynamicClassLoader dcl = dynamicHelper.getDynamicClassLoader();
    Class<?> simpleTypeB = dcl.createDynamicClass("simple.mappings.SimpleB");
    DynamicTypeBuilder bTypeBuilder = new DynamicTypeBuilder(simpleTypeB, null);
    bTypeBuilder.addDirectMapping("value2", boolean.class, "VAL_2");
    bTypeBuilder.addDirectMapping("value3", String.class, "VAL_3");
    Class<?> simpleTypeC = dcl.createDynamicClass("simple.mappings.SimpleC");
    DynamicTypeBuilder cTypeBuilder = new DynamicTypeBuilder(simpleTypeC, null);
    cTypeBuilder.addDirectMapping("value4", double.class, "VAL_4");
    cTypeBuilder.addDirectMapping("value5", String.class, "VAL_5");
    Class<?> simpleTypeA = dcl.createDynamicClass("simple.mappings.SimpleA");
    DynamicTypeBuilder aTypeBuilder = new DynamicTypeBuilder(simpleTypeA, null, "SIMPLE_TYPE_A");
    aTypeBuilder.setPrimaryKeyFields("SID");
    aTypeBuilder.addDirectMapping("id", int.class, "SID");
    aTypeBuilder.addDirectMapping("value1", String.class, "VAL_1");
    aTypeBuilder.addAggregateObjectMapping("b", bTypeBuilder.getType(), true);
    aTypeBuilder.addAggregateObjectMapping("c", cTypeBuilder.getType(), false);
    dynamicHelper.addTypes(true, true, aTypeBuilder.getType(), bTypeBuilder.getType(), cTypeBuilder.getType());
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) DynamicHelper(org.eclipse.persistence.dynamic.DynamicHelper) DynamicTypeBuilder(org.eclipse.persistence.dynamic.DynamicTypeBuilder) BeforeClass(org.junit.BeforeClass)

Aggregations

DynamicClassLoader (org.eclipse.persistence.dynamic.DynamicClassLoader)58 BeforeClass (org.junit.BeforeClass)27 DynamicTypeBuilder (org.eclipse.persistence.dynamic.DynamicTypeBuilder)26 DynamicHelper (org.eclipse.persistence.dynamic.DynamicHelper)17 Test (org.junit.Test)17 JPADynamicHelper (org.eclipse.persistence.jpa.dynamic.JPADynamicHelper)15 JPADynamicTypeBuilder (org.eclipse.persistence.jpa.dynamic.JPADynamicTypeBuilder)13 HashMap (java.util.HashMap)7 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)6 DynamicType (org.eclipse.persistence.dynamic.DynamicType)5 IOException (java.io.IOException)4 TableSequence (org.eclipse.persistence.sequencing.TableSequence)4 EntityManager (jakarta.persistence.EntityManager)3 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 Project (org.eclipse.persistence.sessions.Project)3 EclipseLinkClassWriter (org.eclipse.persistence.dynamic.EclipseLinkClassWriter)2 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)2