Map<String, Persistence.PersistenceUnit> overrides = new HashMap<String, Persistence.PersistenceUnit>();
try {
for (XmlObject raw : raws) {
Persistence persistence = fromXmlObject(raw);
for (Persistence.PersistenceUnit unit : persistence.getPersistenceUnit()) {
overrides.put(unit.getName().trim(), unit);
}
}
} catch (JAXBException e) {
throw new DeploymentException("Parse Persistence configuration file failed", e);
}
try {
final Collection<String> manifestcpCopy = new LinkedHashSet<String> ();
boolean resolveWARcp = false;
// resolve the classpath for non-standalone war file since module.getClassPath
// returns the classpath relative to the war file
if (!module.isStandAlone() && module.getType() == ConfigurationModuleType.WAR) {
resolveWARcp = true;
}
final Collection<String> manifestcp = module.getClassPath();
for (String classpath : manifestcp) {
if (resolveWARcp) {
manifestcpCopy.add(module.resolve(classpath).toString());
} else {
manifestcpCopy.add(classpath);
}
}
// add "" into manifestcpCopy to make META-INF/persistence.xml in standalone ejb be processed
if (module.isStandAlone() && module.getType() == ConfigurationModuleType.EJB) {
manifestcpCopy.add("");
}
BundleResourceFinder finder = new BundleResourceFinder(packageAdmin, bundle, "", "META-INF/persistence.xml", new ResourceDiscoveryFilter() {
@Override
public boolean rangeDiscoveryRequired(DiscoveryRange discoveryRange) {
return discoveryRange == DiscoveryRange.BUNDLE_CLASSPATH || discoveryRange == DiscoveryRange.FRAGMENT_BUNDLES;
}
@Override
public boolean zipFileDiscoveryRequired(String s) {
return manifestcpCopy.contains(s);
}
@Override
public boolean directoryDiscoveryRequired(String s) {
boolean found = false;
if (manifestcpCopy.contains(s)){
found=true;
} else if(s.endsWith("/") && manifestcpCopy.contains(s.substring(0,s.length()-1))){
found=true;
}
return found;
}
});
final Map<URL, String> persistenceURLs = new HashMap<URL, String>();
finder.find(new BundleResourceFinder.ResourceFinderCallback() {
public boolean foundInDirectory(Bundle bundle, String baseDir, URL url) throws Exception {
persistenceURLs.put(url, baseDir);
return true;
}
public boolean foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream inputStream) throws Exception {
URL jarURL = bundle.getEntry(jarName);
URL url = new URL("jar:" + jarURL.toString() + "!/" + entry.getName());
persistenceURLs.put(url, jarName);
return true;
}
});
if (raws.length > 0 || persistenceURLs.size() > 0) {
EnvironmentBuilder.mergeEnvironments(module.getEnvironment(), defaultEnvironment);
}
for (Map.Entry<URL, String> entry : persistenceURLs.entrySet()) {
URL persistenceUrl = entry.getKey();
String persistenceLocation = entry.getValue();
Persistence persistence;
InputStream in = persistenceUrl.openStream();
try {
persistence = (Persistence) JaxbJavaee.unmarshal(Persistence.class, in, false);
} catch (JAXBException e) {
throw new DeploymentException("Could not parse persistence.xml file: " + persistenceUrl, e);