OPENJPA-2813 PersistenceProvider#generateSchema (misses the boolean state)

This commit is contained in:
Romain Manni-Bucau 2020-04-29 20:47:12 +02:00
parent a975a3d1c6
commit bf418eb1b3
3 changed files with 60 additions and 11 deletions

View File

@ -147,17 +147,17 @@ public class JDBCBrokerFactory extends AbstractBrokerFactory {
/** /**
* Synchronize the mappings of the classes listed in the configuration. * Synchronize the mappings of the classes listed in the configuration.
*/ */
protected void synchronizeMappings(ClassLoader loader, protected boolean synchronizeMappings(ClassLoader loader,
JDBCConfiguration conf) { JDBCConfiguration conf) {
mapSchemaGenerationToSynchronizeMappings(conf); mapSchemaGenerationToSynchronizeMappings(conf);
String action = conf.getSynchronizeMappings(); String action = conf.getSynchronizeMappings();
if (StringUtil.isEmpty(action)) if (StringUtil.isEmpty(action))
return; return false;
MappingRepository repo = conf.getMappingRepositoryInstance(); MappingRepository repo = conf.getMappingRepositoryInstance();
Collection<Class<?>> classes = repo.loadPersistentTypes(false, loader); Collection<Class<?>> classes = repo.loadPersistentTypes(false, loader);
if (classes.isEmpty()) if (classes.isEmpty())
return; return false;
String props = Configurations.getProperties(action); String props = Configurations.getProperties(action);
action = Configurations.getClassName(action); action = Configurations.getClassName(action);
@ -175,10 +175,11 @@ public class JDBCBrokerFactory extends AbstractBrokerFactory {
} }
} }
tool.record(); tool.record();
return true; // todo: check?
} }
protected void synchronizeMappings(ClassLoader loader) { protected boolean synchronizeMappings(ClassLoader loader) {
synchronizeMappings(loader, (JDBCConfiguration) getConfiguration()); return synchronizeMappings(loader, (JDBCConfiguration) getConfiguration());
} }
private void mapSchemaGenerationToSynchronizeMappings(JDBCConfiguration conf) { private void mapSchemaGenerationToSynchronizeMappings(JDBCConfiguration conf) {

View File

@ -20,6 +20,8 @@ package org.apache.openjpa.persistence;
import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException; import java.lang.instrument.IllegalClassFormatException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.ProtectionDomain; import java.security.ProtectionDomain;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -36,6 +38,7 @@ import org.apache.openjpa.conf.OpenJPAConfiguration;
import org.apache.openjpa.conf.OpenJPAConfigurationImpl; import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
import org.apache.openjpa.enhance.PCClassFileTransformer; import org.apache.openjpa.enhance.PCClassFileTransformer;
import org.apache.openjpa.enhance.PCEnhancerAgent; import org.apache.openjpa.enhance.PCEnhancerAgent;
import org.apache.openjpa.kernel.AbstractBrokerFactory;
import org.apache.openjpa.kernel.Bootstrap; import org.apache.openjpa.kernel.Bootstrap;
import org.apache.openjpa.kernel.BrokerFactory; import org.apache.openjpa.kernel.BrokerFactory;
import org.apache.openjpa.kernel.ConnectionRetainModes; import org.apache.openjpa.kernel.ConnectionRetainModes;
@ -223,13 +226,56 @@ public class PersistenceProviderImpl
} }
@Override @Override
public void generateSchema(PersistenceUnitInfo info, Map map) { public void generateSchema(final PersistenceUnitInfo info, final Map map) {
throw new UnsupportedOperationException("JPA 2.1"); final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
runMap.put("javax.persistence.schema-generation.database.action", "create");
final OpenJPAEntityManagerFactory factory = createContainerEntityManagerFactory(info, runMap);
try {
synchronizeMappings(factory);
} finally {
factory.close();
}
} }
@Override @Override
public boolean generateSchema(String persistenceUnitName, Map map) { public boolean generateSchema(final String persistenceUnitName, final Map map) {
throw new UnsupportedOperationException("JPA 2.1"); final Map runMap = map == null ? new HashMap<>() : new HashMap<>(map);
runMap.put("javax.persistence.schema-generation.database.action", "create");
final OpenJPAEntityManagerFactory factory = createEntityManagerFactory(persistenceUnitName, runMap);
try {
final Object obj = synchronizeMappings(factory);
return Boolean.class.cast(obj) ? Boolean.class.cast(obj) : true;
} finally {
factory.close();
}
}
private Object synchronizeMappings(final OpenJPAEntityManagerFactory factory) {
if (EntityManagerFactoryImpl.class.isInstance(factory)) {
final EntityManagerFactoryImpl entityManagerFactory = EntityManagerFactoryImpl.class.cast(factory);
final BrokerFactory brokerFactory = entityManagerFactory.getBrokerFactory();
if (!AbstractBrokerFactory.class.isInstance(brokerFactory)) {
throw new IllegalArgumentException("expected AbstractBrokerFactory but got " + brokerFactory);
}
try {
final Method synchronizeMappings = brokerFactory.getClass()
.getDeclaredMethod("synchronizeMappings", ClassLoader.class);
if (!synchronizeMappings.isAccessible()) {
synchronizeMappings.setAccessible(true);
}
return synchronizeMappings.invoke(brokerFactory, Thread.currentThread().getContextClassLoader());
} catch (final NoSuchMethodException | IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (final InvocationTargetException e) {
final Throwable targetException = e.getTargetException();
if (RuntimeException.class.isInstance(targetException)) {
throw RuntimeException.class.cast(targetException);
}
throw new IllegalStateException(targetException);
}
} else {
throw new IllegalArgumentException("expected EntityManagerFactoryImpl but got " + factory);
}
} }
public void setPersistenceEnvironmentInfo(OpenJPAConfiguration conf, PersistenceUnitInfo pui) { public void setPersistenceEnvironmentInfo(OpenJPAConfiguration conf, PersistenceUnitInfo pui) {

View File

@ -120,11 +120,13 @@ public class DistributedJDBCBrokerFactory extends JDBCBrokerFactory
} }
@Override @Override
protected void synchronizeMappings(ClassLoader loader) { protected boolean synchronizeMappings(ClassLoader loader) {
List<Slice> slices = getConfiguration().getSlices(Slice.Status.ACTIVE); List<Slice> slices = getConfiguration().getSlices(Slice.Status.ACTIVE);
boolean result = false;
for (Slice slice : slices) { for (Slice slice : slices) {
synchronizeMappings(loader, (JDBCConfiguration) slice.getConfiguration()); result = synchronizeMappings(loader, (JDBCConfiguration) slice.getConfiguration()) || result;
} }
return result;
} }
@Override @Override