LOGGER.log(Level.SEVERE, "can't add a library to the deployment", e);
}
} else if (ArchiveAsset.class.isInstance(asset)) {
final Archive<?> nestedArchive = ArchiveAsset.class.cast(asset).getArchive();
if (!isExcluded(nestedArchive.getName())) {
final Node bXmlNode = nestedArchive.get(META_INF + BEANS_XML);
if (bXmlNode != null) {
try {
beansXmlMerged.add(new AssetSource(bXmlNode.getAsset(), new URL("jar:file://!/WEB-INF/lib/" + nestedArchive.getName() + "!/META-INF/beans.xml")));
} catch (final MalformedURLException e) {
// shouldn't occur
}
}
archive.merge(nestedArchive);
}
}
}
} else {
if (isEar) { // mainly for CDi TCKs
earMap = new HashMap<>();
final Map<ArchivePath, Node> jars = archive.getContent(new IncludeRegExpPaths("/.*\\.jar"));
final List<org.apache.xbean.finder.archive.Archive> archives = new ArrayList<>(jars.size());
for (final Map.Entry<ArchivePath, Node> node : jars.entrySet()) {
final Asset asset = node.getValue().getAsset();
if (ArchiveAsset.class.isInstance(asset)) {
final Archive<?> libArchive = ArchiveAsset.class.cast(asset).getArchive();
if (!isExcluded(libArchive.getName())) {
final List<Class<?>> earClasses = new ArrayList<>();
final List<String> earClassNames = new ArrayList<>();
final Map<ArchivePath, Node> content = libArchive.getContent(new IncludeRegExpPaths(".*.class"));
for (final Map.Entry<ArchivePath, Node> classNode : content.entrySet()) {
final String classname = name(classNode.getKey().get());
try {
earClasses.add(parent.loadClass(classname));
earClassNames.add(classname);
} catch (final ClassNotFoundException e) {
LOGGER.fine("Can't load class " + classname);
} catch (final NoClassDefFoundError ncdfe) {
// no-op
}
}
try { // ends with !/META-INF/beans.xml to force it to be used as a cdi module
earMap.put(new URL("jar:file://!/lib/" + archive.getName() + (libArchive.get(META_INF + BEANS_XML) != null ? "!/META-INF/beans.xml" : "")), earClassNames);
} catch (final MalformedURLException e) {
// no-op
}
archives.add(new ClassesArchive(earClasses));
}
} // else TODO
}
earArchive = new CompositeArchive(archives);
}
prefix = META_INF;
}
final URL[] urls = additionalPaths.toArray(new URL[additionalPaths.size()]);
final ClassLoader loader;
if (!WEB_INF.equals(prefix)) {
loader = new SWClassLoader("", new URLClassLoader(urls, parent), archive);
} else {
loader = new SWClassLoader(WEB_INF_CLASSES, new URLClassLoaderFirst(urls, parent), archive);
}
final URLClassLoader tempClassLoader = ClassLoaderUtil.createTempClassLoader(loader);
final AppModule appModule = new AppModule(loader, archive.getName());
if (WEB_INF.equals(prefix)) {
appModule.setDelegateFirst(false);
appModule.setStandloneWebModule();
final WebModule webModule = new WebModule(new WebApp(), contextRoot(archive.getName()), loader, "", appModule.getModuleId());
webModule.setUrls(additionalPaths);
appModule.getWebModules().add(webModule);
} else if (isEar) { // mainly for CDi TCKs
final FinderFactory.OpenEJBAnnotationFinder earLibFinder = new FinderFactory.OpenEJBAnnotationFinder(new SimpleWebappAggregatedArchive(earArchive, earMap));
appModule.setEarLibFinder(earLibFinder);
final EjbModule earCdiModule = new EjbModule(appModule.getClassLoader(), DeploymentLoader.EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
earCdiModule.setBeans(new Beans());
earCdiModule.setFinder(earLibFinder);
earCdiModule.setEjbJar(new EmptyEjbJar());
appModule.getEjbModules().add(earCdiModule);
for (final Map.Entry<ArchivePath, Node> node : archive.getContent(new IncludeRegExpPaths("/.*\\.war")).entrySet()) {
final Asset asset = node.getValue().getAsset();
if (ArchiveAsset.class.isInstance(asset)) {
final Archive<?> webArchive = ArchiveAsset.class.cast(asset).getArchive();
if (WebArchive.class.isInstance(webArchive)) {
/* TODO: libs
final Map<ArchivePath, Node> libs = archive.getContent(new IncludeRegExpPaths("/WEB-INF/lib/.*\\.jar"));
*/
final Map<String, Object> altDD = new HashMap<String, Object>();
final Node beansXml = findBeansXml(webArchive, new ArrayList<AssetSource>(), WEB_INF, altDD);
final SWClassLoader webLoader = new SWClassLoader(WEB_INF_CLASSES, parent, webArchive);
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(
finderArchive(beansXml, webArchive, webLoader, Collections.<URL>emptyList()));
final WebModule webModule = new WebModule(new WebApp(), contextRoot(webArchive.getName()), loader, "", appModule.getModuleId());
webModule.setUrls(Collections.<URL>emptyList());
webModule.setScannableUrls(Collections.<URL>emptyList());
webModule.setFinder(finder);
final EjbModule ejbModule = new EjbModule(webLoader, webModule.getModuleId(), null, new EjbJar(), new OpenejbJar());
ejbModule.getAltDDs().putAll(altDD);
ejbModule.setFinder(finder);
ejbModule.setClassLoader(webLoader);
ejbModule.setWebapp(true);
appModule.getEjbModules().add(ejbModule);
appModule.getWebModules().add(webModule);
addPersistenceXml(archive, WEB_INF, appModule);
addOpenEJbJarXml(archive, WEB_INF, ejbModule);
addValidationXml(archive, WEB_INF, new HashMap<String, Object>(), ejbModule);
addResourcesXml(archive, WEB_INF, ejbModule);
addEnvEntries(archive, WEB_INF, appModule, ejbModule);
}
}
}
}
if (isEar) { // adding the test class as lib class can break test if tested against the web part of the ear
return appModule;
}
// add the test as a managed bean to be able to inject into it easily
final Map<String, Object> testDD;
if (javaClass != null) {
final EjbJar ejbJar = new EjbJar();
final OpenejbJar openejbJar = new OpenejbJar();
final String ejbName = appModule.getModuleId() + "_" + javaClass.getName();
final ManagedBean bean = ejbJar.addEnterpriseBean(new ManagedBean(ejbName, javaClass.getName(), true));
bean.localBean();
bean.setTransactionType(TransactionType.BEAN);
final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
ejbDeployment.setDeploymentId(ejbName);
final EjbModule e = new EjbModule(ejbJar, openejbJar);
e.getProperties().setProperty("openejb.cdi.activated", "false");
e.setBeans(new Beans());
e.setClassLoader(tempClassLoader);
appModule.getEjbModules().add(e);
testDD = e.getAltDDs();
} else {
testDD = new HashMap<>(); // ignore
}
final EjbJar ejbJar;
final Node ejbJarXml = archive.get(prefix.concat(EJB_JAR_XML));
if (ejbJarXml != null) {
try {
ejbJar = ReadDescriptors.readEjbJar(ejbJarXml.getAsset().openStream());
} catch (final OpenEJBException e) {
throw new OpenEJBRuntimeException(e);
}
} else {
ejbJar = new EjbJar();
}
if (ejbJar.getModuleName() == null) {
final String name = archive.getName();
if (name.endsWith("ar") && name.length() > 4) {
ejbJar.setModuleName(name.substring(0, name.length() - ".jar".length()));
} else {
ejbJar.setModuleName(name);
}
}
final EjbModule ejbModule = new EjbModule(ejbJar);
ejbModule.setClassLoader(tempClassLoader);
final Node beansXml = findBeansXml(archive, beansXmlMerged, prefix, ejbModule.getAltDDs());
final org.apache.xbean.finder.archive.Archive finderArchive = finderArchive(beansXml, archive, tempClassLoader, additionalPaths);
ejbModule.setFinder(new FinderFactory.ModuleLimitedFinder(new FinderFactory.OpenEJBAnnotationFinder(finderArchive)));
if (appModule.isWebapp()) { // war
appModule.getWebModules().iterator().next().setFinder(ejbModule.getFinder());
}