use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer in project tomee by apache.
the class TomEEWebappLoader method startInternal.
@Override
protected void startInternal() throws LifecycleException {
if (getClassLoader() != null) {
final TomEEWebappClassLoader webappClassLoader = TomEEWebappClassLoader.class.cast(getClassLoader());
if (webappClassLoader.isStopped()) {
webappClassLoader.internalStop();
}
}
final Context context = getContext();
ClassLoaderConfigurer configurer = ClassLoaderUtil.configurer(context.getName());
// WEB-INF/jars.xml
final File war = Contexts.warPath(Context.class.cast(context));
final File jarsXml = new File(war, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME);
final ClassLoaderConfigurer configurerTxt = QuickJarsTxtParser.parse(jarsXml);
if (configurerTxt != null) {
configurer = new CompositeClassLoaderConfigurer(configurer, configurerTxt);
}
TomEEWebappClassLoader.initContext(configurer);
TomEEWebappClassLoader.initContext(context);
try {
super.startInternal();
} finally {
TomEEWebappClassLoader.cleanContext();
}
if (forceSkip != null && WebAppFirstEarClassLoader.class.isInstance(getClassLoader())) {
WebAppFirstEarClassLoader.class.cast(getClassLoader()).setForceSkip(forceSkip.split(" *, *"));
}
}
use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer in project tomee by apache.
the class TomEEWebappClassLoader method start.
// embeddeding implementation of sthg (JPA, JSF) can lead to classloading issues if we don't enrich the webapp
// with our integration jars
// typically the class will try to be loaded by the common classloader
// but the interface implemented or the parent class
// will be in the webapp
@Override
public void start() throws LifecycleException {
// do it first otherwise we can't use this as classloader
super.start();
// mainly for tomee-maven-plugin
initAdditionalRepos();
if (additionalRepos != null && !additionalRepos.isEmpty()) {
for (final File f : additionalRepos) {
final DirResourceSet webResourceSet = new PremptiveDirResourceSet(resources, "/", f.getAbsolutePath(), "/");
resources.addPreResources(webResourceSet);
}
resources.setCachingAllowed(false);
}
// add configurer enrichments
if (configurer != null) {
// add now we removed all we wanted
final URL[] enrichment = configurer.additionalURLs();
for (final URL url : enrichment) {
super.addURL(url);
}
}
// add internal enrichments
for (final URL url : SystemInstance.get().getComponent(WebAppEnricher.class).enrichment(this)) {
super.addURL(url);
}
// WEB-INF/jars.xml
final File war = Contexts.warPath(CONTEXT.get());
final File jarsXml = new File(war, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME);
final ClassLoaderConfigurer configurerTxt = QuickJarsTxtParser.parse(jarsXml);
if (configurerTxt != null) {
configurer = new CompositeClassLoaderConfigurer(configurer, configurerTxt);
}
stopped = false;
}
use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer in project tomee by apache.
the class Assembler method createAppClassLoader.
public ClassLoader createAppClassLoader(final AppInfo appInfo) throws OpenEJBException, IOException {
if ("openejb".equals(appInfo.appId)) {
return ParentClassLoaderFinder.Helper.get();
}
final Set<URL> jars = new HashSet<>();
for (final EjbJarInfo info : appInfo.ejbJars) {
if (info.path != null) {
jars.add(toUrl(info.path));
}
}
for (final ClientInfo info : appInfo.clients) {
if (info.path != null) {
jars.add(toUrl(info.path));
}
}
for (final ConnectorInfo info : appInfo.connectors) {
for (final String jarPath : info.libs) {
jars.add(toUrl(jarPath));
}
}
for (final String jarPath : appInfo.libs) {
jars.add(toUrl(jarPath));
}
// add openejb-jpa-integration if the jpa provider is in lib/
if (appInfo.libs.size() > 0) {
// the test could be enhanced
try {
final File jpaIntegrationFile = JarLocation.jarLocation(MakeTxLookup.class);
final URL url = jpaIntegrationFile.toURI().toURL();
if (!jars.contains(url)) {
// could have been done before (webapp enrichment or manually for instance)
jars.add(url);
}
} catch (final RuntimeException re) {
logger.warning("Unable to find the open-jpa-integration jar");
}
}
final ClassLoaderEnricher component = SystemInstance.get().getComponent(ClassLoaderEnricher.class);
if (component != null) {
jars.addAll(Arrays.asList(component.applicationEnrichment()));
} else {
logger.warning("Unable to find open-jpa-integration jar");
}
// Create the class loader
final ClassLoader parent = ParentClassLoaderFinder.Helper.get();
final String prefix;
if (appInfo.webAppAlone) {
prefix = "WEB-INF/";
} else {
prefix = "META-INF/";
}
final ClassLoaderConfigurer configurer1 = QuickJarsTxtParser.parse(new File(appInfo.path, prefix + QuickJarsTxtParser.FILE_NAME));
final ClassLoaderConfigurer configurer2 = ClassLoaderUtil.configurer(appInfo.appId);
if (configurer1 != null || configurer2 != null) {
final ClassLoaderConfigurer configurer = new CompositeClassLoaderConfigurer(configurer1, configurer2);
ClassLoaderConfigurer.Helper.configure(jars, configurer);
}
final URL[] filtered = jars.toArray(new URL[jars.size()]);
// since we don't really need to create a classloader here when starting from classpath just let skip this step
if (skipLoaderIfPossible) {
// TODO: maybe use a boolean to know if all urls comes from the classpath to avoid this validation
if ("classpath.ear".equals(appInfo.appId)) {
return parent;
}
final Collection<File> urls = new HashSet<>();
for (final URL url : ClassLoaders.findUrls(parent)) {
// need to convert it to file since urls can be file:/xxx or jar:file:///xxx
try {
urls.add(URLs.toFile(url).getCanonicalFile());
} catch (final Exception error) {
if (logger.isDebugEnabled()) {
logger.debug("Can't determine url for: " + url.toExternalForm(), error);
}
}
}
boolean allIsIntheClasspath = true;
for (final URL url : filtered) {
try {
if (!urls.contains(URLs.toFile(url).getCanonicalFile())) {
allIsIntheClasspath = false;
if (logger.isDebugEnabled()) {
logger.debug(url.toExternalForm() + " (" + URLs.toFile(url) + ") is not in the classloader so we'll create a dedicated classloader for this app");
}
break;
}
} catch (final Exception ignored) {
allIsIntheClasspath = false;
if (logger.isDebugEnabled()) {
logger.debug(url.toExternalForm() + " (" + URLs.toFile(url) + ") is not in the classloader", ignored);
}
break;
}
}
if (allIsIntheClasspath) {
logger.info("Not creating another application classloader for " + appInfo.appId);
return parent;
} else if (logger.isDebugEnabled()) {
logger.debug("Logging all urls from the app since we don't skip the app classloader creation:");
for (final URL url : filtered) {
logger.debug(" -> " + url.toExternalForm());
}
logger.debug("Logging all urls from the classloader since we don't skip the app classloader creation:");
for (final File url : urls) {
logger.debug(" -> " + url.getAbsolutePath());
}
}
}
logger.info("Creating dedicated application classloader for " + appInfo.appId);
if (!appInfo.delegateFirst) {
return ClassLoaderUtil.createClassLoader(appInfo.path, filtered, parent);
}
return ClassLoaderUtil.createClassLoaderFirst(appInfo.path, filtered, parent);
}
use of org.apache.openejb.classloader.CompositeClassLoaderConfigurer in project tomee by apache.
the class ClassLoaderUtil method createTempClassLoader.
public static URLClassLoader createTempClassLoader(final String appId, final URL[] rawUrls, final ClassLoader parent) {
String updatedAppId = appId;
if (appId != null) {
// here we often get the full path of the app as id where later it is simply the name of the file/dir
final File file = new File(appId);
if (file.exists()) {
updatedAppId = file.getName();
if (updatedAppId.endsWith(".war") || updatedAppId.endsWith(".ear")) {
updatedAppId = updatedAppId.substring(0, updatedAppId.length() - ".war".length());
}
}
}
// from the app
final ClassLoaderConfigurer configurer1 = QuickJarsTxtParser.parse(new File(appId, "META-INF/" + QuickJarsTxtParser.FILE_NAME));
final ClassLoaderConfigurer configurer2 = QuickJarsTxtParser.parse(new File(appId, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME));
// external config
ClassLoaderConfigurer configurer3 = ClassLoaderUtil.configurer(updatedAppId);
if (configurer3 == null) {
// try the complete path
configurer3 = ClassLoaderUtil.configurer(appId);
}
final URL[] urls;
if (configurer1 == null && configurer2 == null && configurer3 == null) {
urls = rawUrls;
} else {
final CompositeClassLoaderConfigurer configurer = new CompositeClassLoaderConfigurer(configurer1, configurer2, configurer3);
final Collection<URL> list = new ArrayList<URL>();
list.addAll(Arrays.asList(rawUrls));
ClassLoaderConfigurer.Helper.configure(list, configurer);
urls = list.toArray(new URL[list.size()]);
}
return new TempClassLoader(createClassLoader(appId, urls, parent));
}
Aggregations