use of org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths in project tomee by apache.
the class OpenEJBArchiveProcessor method createModule.
public static AppModule createModule(final Archive<?> archive, final TestClass testClass, final Closeables closeables) {
final Class<?> javaClass;
if (testClass != null) {
javaClass = testClass.getJavaClass();
} else {
javaClass = null;
}
final ClassLoader parent;
if (javaClass == null) {
parent = Thread.currentThread().getContextClassLoader();
} else {
parent = javaClass.getClassLoader();
}
final List<URL> additionalPaths = new ArrayList<>();
CompositeArchive scannedArchive = null;
final Map<URL, List<String>> earMap = new HashMap<>();
final Map<String, Object> altDD = new HashMap<>();
final List<Archive> earLibsArchives = new ArrayList<>();
final CompositeBeans earBeans = new CompositeBeans();
final boolean isEar = EnterpriseArchive.class.isInstance(archive);
final boolean isWebApp = WebArchive.class.isInstance(archive);
final String prefix = isWebApp ? WEB_INF : META_INF;
if (isEar || isWebApp) {
final Map<ArchivePath, Node> jars = archive.getContent(new IncludeRegExpPaths(isEar ? "/.*\\.jar" : "/WEB-INF/lib/.*\\.jar"));
scannedArchive = analyzeLibs(parent, additionalPaths, earMap, earLibsArchives, earBeans, jars, altDD);
}
final URL[] urls = additionalPaths.toArray(new URL[additionalPaths.size()]);
final URLClassLoaderFirst swParent = new URLClassLoaderFirst(urls, parent);
closeables.add(swParent);
if (!isEar) {
earLibsArchives.add(archive);
}
final SWClassLoader loader = new SWClassLoader(swParent, earLibsArchives.toArray(new Archive<?>[earLibsArchives.size()]));
closeables.add(loader);
final URLClassLoader tempClassLoader = ClassLoaderUtil.createTempClassLoader(loader);
closeables.add(tempClassLoader);
final AppModule appModule = new AppModule(loader, archive.getName());
if (WEB_INF.equals(prefix)) {
appModule.setDelegateFirst(false);
appModule.setStandloneWebModule();
final WebModule webModule = new WebModule(createWebApp(archive), 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(tempClassLoader, scannedArchive, earMap));
appModule.setEarLibFinder(earLibFinder);
final EjbModule earCdiModule = new EjbModule(appModule.getClassLoader(), DeploymentLoader.EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
earCdiModule.setBeans(earBeans);
earCdiModule.setFinder(earLibFinder);
final EjbJar ejbJar;
final AssetSource ejbJarXml = AssetSource.class.isInstance(altDD.get(EJB_JAR_XML)) ? AssetSource.class.cast(altDD.get(EJB_JAR_XML)) : null;
if (ejbJarXml != null) {
try {
ejbJar = ReadDescriptors.readEjbJar(ejbJarXml.get());
} catch (final Exception e) {
throw new OpenEJBRuntimeException(e);
}
} else {
ejbJar = new EjbJar();
}
// EmptyEjbJar would prevent to add scanned EJBs but this is *here* an aggregator so we need to be able to do so
earCdiModule.setEjbJar(ejbJar);
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)) {
final Map<String, Object> webAltDD = new HashMap<>();
final Node beansXml = findBeansXml(webArchive, WEB_INF);
final List<URL> webappAdditionalPaths = new LinkedList<>();
final CompositeBeans webAppBeansXml = new CompositeBeans();
final List<Archive> webAppArchives = new LinkedList<Archive>();
final Map<URL, List<String>> webAppClassesByUrl = new HashMap<URL, List<String>>();
final CompositeArchive webAppArchive = analyzeLibs(parent, webappAdditionalPaths, webAppClassesByUrl, webAppArchives, webAppBeansXml, webArchive.getContent(new IncludeRegExpPaths("/WEB-INF/lib/.*\\.jar")), webAltDD);
webAppArchives.add(webArchive);
final SWClassLoader webLoader = new SWClassLoader(parent, webAppArchives.toArray(new Archive<?>[webAppArchives.size()]));
closeables.add(webLoader);
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, webArchive, webLoader, webAppArchive, webAppClassesByUrl, webAppBeansXml));
final String contextRoot = contextRoot(webArchive.getName());
final WebModule webModule = new WebModule(createWebApp(webArchive), contextRoot, webLoader, "", appModule.getModuleId() + "_" + contextRoot);
webModule.setUrls(Collections.<URL>emptyList());
webModule.setScannableUrls(Collections.<URL>emptyList());
webModule.setFinder(finder);
final EjbJar webEjbJar = createEjbJar(prefix, webArchive);
final EjbModule ejbModule = new EjbModule(webLoader, webModule.getModuleId(), null, webEjbJar, new OpenejbJar());
ejbModule.setBeans(webAppBeansXml);
ejbModule.getAltDDs().putAll(webAltDD);
ejbModule.getAltDDs().put("beans.xml", webAppBeansXml);
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
addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
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 EjbModule testEjbModule = addTestClassAsManagedBean(javaClass, tempClassLoader, appModule);
testDD = testEjbModule.getAltDDs();
} else {
// ignore
testDD = new HashMap<>();
}
final EjbJar ejbJar = createEjbJar(prefix, archive);
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, prefix);
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(finderArchive(beansXml, archive, loader, scannedArchive, earMap, earBeans));
ejbModule.setFinder(finder);
ejbModule.setBeans(earBeans);
ejbModule.getAltDDs().put("beans.xml", earBeans);
if (appModule.isWebapp()) {
// war
appModule.getWebModules().iterator().next().setFinder(ejbModule.getFinder());
}
appModule.getEjbModules().add(ejbModule);
addPersistenceXml(archive, prefix, appModule);
addOpenEJbJarXml(archive, prefix, ejbModule);
addValidationXml(archive, prefix, testDD, ejbModule);
addResourcesXml(archive, prefix, ejbModule);
addEnvEntries(archive, prefix, appModule, ejbModule);
if (!appModule.isWebapp()) {
appModule.getAdditionalLibraries().addAll(additionalPaths);
}
if (archive.getName().endsWith(".jar")) {
// otherwise global naming will be broken
appModule.setStandloneWebModule();
}
return appModule;
}
use of org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths in project tomee by apache.
the class OpenEJBArchiveProcessor method analyzeLibs.
private static CompositeArchive analyzeLibs(final ClassLoader parent, final List<URL> additionalPaths, final Map<URL, List<String>> earMap, final List<Archive> earLibsArchives, final CompositeBeans earBeans, final Map<ArchivePath, Node> jars, final Map<String, Object> altDD) {
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())) {
earLibsArchives.add(libArchive);
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 *with bda*
final Node beansNode = libArchive.get(META_INF + BEANS_XML);
final URL arUrl = new URL("jar:file://!/lib/" + libArchive.getName() + (beansNode != null ? "!/META-INF/beans.xml" : ""));
if (beansNode != null) {
try {
DeploymentLoader.doMerge(arUrl, earBeans, ReadDescriptors.readBeans(beansNode.getAsset().openStream()));
} catch (final OpenEJBException e) {
throw new IllegalArgumentException(e);
}
}
earMap.put(arUrl, earClassNames);
} catch (final MalformedURLException e) {
// no-op
}
archives.add(new ClassesArchive(earClasses));
}
final Node ejbJarXml = libArchive.get(META_INF + EJB_JAR_XML);
if (ejbJarXml != null) {
// not super, we should merge them surely but ok for use cases we met until today
altDD.put("ejb-jar.xml", new AssetSource(ejbJarXml.getAsset(), null));
}
}
if (UrlAsset.class.isInstance(asset) || FileAsset.class.isInstance(asset)) {
try {
final URL url = UrlAsset.class.isInstance(asset) ? get(URL.class, "url", asset) : get(File.class, "file", asset).toURI().toURL();
final List<String> classes = new ArrayList<String>();
archives.add(new FilteredArchive(new JarArchive(parent, url), new WebappAggregatedArchive.ScanXmlSaverFilter(false, null, classes, null)));
additionalPaths.add(url);
final URLClassLoader loader = new URLClassLoader(new URL[] { url }, EMPTY_LOADER);
for (final String beans : asList("META-INF/beans.xml", "/META-INF/beans.xml")) {
final URL u = loader.getResource(beans);
if (u != null) {
try {
DeploymentLoader.doMerge(u, earBeans, ReadDescriptors.readBeans(u.openStream()));
} catch (final OpenEJBException e) {
throw new IllegalArgumentException(e);
} catch (final IOException e) {
// no-op
}
earMap.put(u, classes);
}
}
try {
loader.close();
} catch (final IOException e) {
// no-op
}
} catch (final MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
}
return new CompositeArchive(archives);
}
use of org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths in project tomee by apache.
the class OpenEJBArchiveProcessor method finderArchive.
private static org.apache.xbean.finder.archive.Archive finderArchive(final Node beansXml, final Archive<?> archive, final ClassLoader cl, final CompositeArchive libs, final Map<URL, List<String>> webAppClassesByUrl, final CompositeBeans compositeBeans) {
final List<Class<?>> classes = new ArrayList<>();
final Map<ArchivePath, Node> content = archive.getContent(new IncludeRegExpPaths(".*.class"));
for (final Map.Entry<ArchivePath, Node> node : content.entrySet()) {
final String classname = name(node.getKey().get());
try {
classes.add(cl.loadClass(classname));
} catch (final ClassNotFoundException e) {
LOGGER.fine("Can't load class " + classname);
if (LOGGER.isLoggable(Level.FINEST)) {
e.printStackTrace(System.err);
}
} catch (final NoClassDefFoundError ncdfe) {
// no-op
}
}
final List<org.apache.xbean.finder.archive.Archive> archives = new ArrayList<>();
archives.add(new ClassesArchive(classes));
if (beansXml != null) {
try {
// no host avoid host resolution in hashcode()
final URL key = new URL("jar:file://!/WEB-INF/beans.xml");
try {
DeploymentLoader.doMerge(key, compositeBeans, ReadDescriptors.readBeans(beansXml.getAsset().openStream()));
} catch (final OpenEJBException e) {
throw new IllegalArgumentException(e);
}
final List<String> mainClasses = new ArrayList<>();
for (final Class<?> clazz : classes) {
mainClasses.add(clazz.getName());
}
webAppClassesByUrl.put(key, mainClasses);
} catch (final MalformedURLException mue) {
// no-op
}
}
if (libs != null) {
archives.add(libs);
}
return new SimpleWebappAggregatedArchive(cl, new CompositeArchive(archives), webAppClassesByUrl);
}
Aggregations