private void addGeronimmoOpenEJBPersistenceUnit(EjbModule ejbModule) {
GeronimoEjbJarType geronimoEjbJarType = (GeronimoEjbJarType) ejbModule.getEjbModule().getAltDDs().get("geronimo-openejb.xml");
// search for the cmp persistence unit
PersistenceUnit persistenceUnit = null;
for (Persistence persistence : geronimoEjbJarType.getPersistence()) {
for (PersistenceUnit unit : persistence.getPersistenceUnit()) {
if ("cmp".equals(unit.getName())) {
persistenceUnit = unit;
break;
}
}
}
// if not found create one
if (persistenceUnit == null) {
String jtaDataSource = null;
// todo Persistence Unit Data Sources need to be global JNDI names
Object altDD = ejbModule.getEjbModule().getAltDDs().get("openejb-jar.xml");
if (altDD instanceof OpenejbJarType) {
ResourceLocatorType cmpConnectionFactory = ((OpenejbJarType) altDD).getCmpConnectionFactory();
if (cmpConnectionFactory != null) {
String datasourceName = cmpConnectionFactory.getResourceLink();
if (datasourceName != null) {
jtaDataSource = datasourceName.trim();
}
}
}
persistenceUnit = new PersistenceUnit();
persistenceUnit.setName("cmp");
persistenceUnit.setTransactionType(TransactionType.JTA);
if (jtaDataSource != null) {
persistenceUnit.setJtaDataSource(jtaDataSource);
} else {
persistenceUnit.setJtaDataSource(defaultCmpJTADataSource);
}
persistenceUnit.setNonJtaDataSource(defaultCmpNonJTADataSource);
persistenceUnit.setExcludeUnlistedClasses(true);
Persistence persistence = new Persistence();
persistence.setVersion("1.0");
persistence.getPersistenceUnit().add(persistenceUnit);
geronimoEjbJarType.getPersistence().add(persistence);
}
persistenceUnit.getMappingFile().add("META-INF/openejb-cmp-generated-orm.xml");
}