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;
}
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);
}
}
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());
}
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());
}
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());
}
Aggregations