*/
private void iterateInitializedPUsAtApplicationPrepare(final DeploymentContext context) {
final DeployCommandParameters deployCommandParameters = context.getCommandParameters(DeployCommandParameters.class);
String appName = deployCommandParameters.name;
final ApplicationInfo appInfo = applicationRegistry.get(appName);
//iterate through all the PersistenceUnitDescriptor for this bundle.
PersistenceUnitDescriptorIterator pudIterator = new PersistenceUnitDescriptorIterator() {
@Override void visitPUD(PersistenceUnitDescriptor pud, DeploymentContext context) {
//PersistenceUnitsDescriptor corresponds to persistence.xml. A bundle can only have one persitence.xml except
// when the bundle is an application which can have multiple persitence.xml under jars in root of ear and lib.
PersistenceUnitLoader puLoader = context.getTransientAppMetaData(getUniquePuIdentifier(pud), PersistenceUnitLoader.class);
if (puLoader != null) { // We have initialized PU
boolean saveEMF = true;
if(isDas()) { //We do validation and execute Java2DB only on DAS
if(deployCommandParameters.origin.isDeploy()) { //APPLICATION_PREPARED will be called for create-application-ref also. We should perform java2db only on first deploy
//Create EM to trigger validation on PU
EntityManagerFactory emf = puLoader.getEMF();
EntityManager em = null;
try {
// Create EM to trigger any validations that are lazily performed by the provider
// EM creation also triggers DDL generation by provider.
em = emf.createEntityManager();
} catch (PersistenceException e) {
// Exception indicates something went wrong while performing validation. Clean up and rethrow to fail deployment
emf.close();
throw e;
} finally {
if (em != null) {
em.close();
}
}
puLoader.doJava2DB();
boolean enabled = deployCommandParameters.enabled;
boolean isTargetDas = isTargetDas(deployCommandParameters);
if(logger.isLoggable(Level.FINER)) {
logger.finer("iterateInitializedPUsAtApplicationPrepare(): enabled: " + enabled + " isTargetDas: " + isTargetDas);
}
if(!isTargetDas || !enabled) {
// we are on DAS but target != das or app is not enabled on das => The EMF was just created for Java2Db. Close it.
puLoader.getEMF().close();
saveEMF = false; // Do not save EMF. We have already closed it
}
}
}
if(saveEMF) {
// Save emf in ApplicationInfo so that it can be retrieved and closed for cleanup
@SuppressWarnings("unchecked") //Suppress warning required as there is no way to pass equivalent of List<EMF>.class to the method
List<EntityManagerFactory> emfsCreatedForThisApp = appInfo.getTransientAppMetaData(EMF_KEY, List.class );
if(emfsCreatedForThisApp == null) {
//First EMF for this app, initialize
emfsCreatedForThisApp = new ArrayList<EntityManagerFactory>();
appInfo.addTransientAppMetaData(EMF_KEY, emfsCreatedForThisApp);
}
emfsCreatedForThisApp.add(puLoader.getEMF());
} // if (saveEMF)
} // if(puLoader != null)
}