Search in sources :

Example 1 with EnhanceScannableUrlsEvent

use of org.apache.openejb.config.event.EnhanceScannableUrlsEvent in project tomee by apache.

the class DeploymentLoader method addBeansXmls.

private void addBeansXmls(final AppModule appModule) {
    final List<URL> urls = appModule.getAdditionalLibraries();
    final URLClassLoader loader = new URLClassLoader(urls.toArray(new URL[urls.size()]));
    final ArrayList<URL> xmls;
    try {
        xmls = Collections.list(loader.getResources("META-INF/beans.xml"));
    } catch (final IOException e) {
        return;
    }
    final CompositeBeans complete = new CompositeBeans();
    for (final URL url : xmls) {
        if (url == null) {
            continue;
        }
        mergeBeansXml(complete, url);
    }
    if (complete.getDiscoveryByUrl().isEmpty()) {
        return;
    }
    complete.removeDuplicates();
    ensureContainerUrls();
    final List<URL> scannableUrls = new ArrayList<>(this.containerUrls);
    SystemInstance.get().fireEvent(new EnhanceScannableUrlsEvent(scannableUrls));
    appModule.getScannableContainerUrls().addAll(scannableUrls);
    IAnnotationFinder finder;
    try {
        finder = FinderFactory.createFinder(appModule);
    } catch (final Exception e) {
        finder = new FinderFactory.ModuleLimitedFinder(new FinderFactory.OpenEJBAnnotationFinder(new WebappAggregatedArchive(appModule.getClassLoader(), appModule.getAltDDs(), xmls)));
    }
    appModule.setEarLibFinder(finder);
    final EjbModule ejbModule = new EjbModule(appModule.getClassLoader(), EAR_SCOPED_CDI_BEANS + appModule.getModuleId(), new EjbJar(), new OpenejbJar());
    ejbModule.setBeans(complete);
    ejbModule.setFinder(finder);
    ejbModule.setEjbJar(new EmptyEjbJar());
    appModule.getEjbModules().add(ejbModule);
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) EnhanceScannableUrlsEvent(org.apache.openejb.config.event.EnhanceScannableUrlsEvent) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) OpenEJBException(org.apache.openejb.OpenEJBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) URLClassLoader(java.net.URLClassLoader) EjbJar(org.apache.openejb.jee.EjbJar)

Example 2 with EnhanceScannableUrlsEvent

use of org.apache.openejb.config.event.EnhanceScannableUrlsEvent in project tomee by apache.

the class DeploymentLoader method createWebModule.

public WebModule createWebModule(final String appId, final String warPath, final ClassLoader parentClassLoader, final String contextRoot, final String moduleName, final ExternalConfiguration config) throws OpenEJBException {
    File warFile = new File(warPath);
    if (!warFile.isDirectory()) {
        warFile = unpack(warFile);
    }
    // read web.xml file
    final Map<String, URL> descriptors;
    try {
        descriptors = getWebDescriptors(warFile);
    } catch (final IOException e) {
        throw new OpenEJBException("Unable to collect descriptors in web module: " + contextRoot, e);
    }
    final WebApp webApp;
    final URL webXmlUrl = descriptors.get("web.xml");
    if (webXmlUrl != null) {
        webApp = ReadDescriptors.readWebApp(webXmlUrl);
    } else {
        // no web.xml webapp - possible since Servlet 3.0
        webApp = new WebApp();
    }
    // determine war class path
    ensureContainerUrls();
    final List<URL> webUrls = new ArrayList<>(containerUrls);
    final SystemInstance systemInstance = SystemInstance.get();
    // add these urls first to ensure we load classes from here first
    final String externalRepos = systemInstance.getProperty("tomee." + warFile.getName().replace(".war", "") + ".externalRepositories");
    List<URL> externalUrls = null;
    if (externalRepos != null) {
        externalUrls = new ArrayList<>();
        for (final String additional : externalRepos.split(",")) {
            final String trim = additional.trim();
            if (!trim.isEmpty()) {
                try {
                    externalUrls.add(new File(trim).toURI().toURL());
                } catch (final MalformedURLException e) {
                    LOGGER.error(e.getMessage());
                }
            }
        }
        webUrls.addAll(externalUrls);
    }
    final Map<String, URL[]> urls = getWebappUrlsAndRars(warFile);
    webUrls.addAll(Arrays.asList(urls.get(URLS_KEY)));
    final List<URL> addedUrls = new ArrayList<>();
    for (final URL url : urls.get(RAR_URLS_KEY)) {
        // eager unpack to be able to use it in classloader
        final File[] files = unpack(URLs.toFile(url)).listFiles();
        if (files != null) {
            for (final File f : files) {
                if (f.getName().endsWith(".jar")) {
                    try {
                        addedUrls.add(f.toURI().toURL());
                    } catch (final MalformedURLException e) {
                        LOGGER.warning("War path bad: " + f.getAbsolutePath(), e);
                    }
                }
            }
        }
    }
    webUrls.addAll(addedUrls);
    // context.xml can define some additional libraries
    if (config != null) {
        // we don't test all !=null inline to show that config will get extra params in the future and that it is hierarchic
        if (config.getClasspath() != null && config.getClasspath().length > 0) {
            final Set<URL> contextXmlUrls = new LinkedHashSet<>();
            for (final String location : config.getClasspath()) {
                try {
                    webUrls.add(new File(location).toURI().toURL());
                } catch (final MalformedURLException e) {
                    throw new IllegalArgumentException(e);
                }
            }
            webUrls.addAll(contextXmlUrls);
        }
    }
    final ClassLoaderConfigurer configurer = QuickJarsTxtParser.parse(new File(warFile, "WEB-INF/" + QuickJarsTxtParser.FILE_NAME));
    if (configurer != null) {
        ClassLoaderConfigurer.Helper.configure(webUrls, configurer);
    }
    final URL[] webUrlsArray = webUrls.toArray(new URL[webUrls.size()]);
    // in TomEE this is done in init hook since we don't manage tomee webapp classloader
    // so here is not the best idea for tomee
    // if we want to manage it in a generic way
    // simply add a boolean shared between tomcat and openejb world
    // to know if we should fire it or not
    systemInstance.fireEvent(new BeforeDeploymentEvent(webUrlsArray, parentClassLoader));
    final ClassLoader warClassLoader = ClassLoaderUtil.createTempClassLoader(appId, webUrlsArray, parentClassLoader);
    // create web module
    final List<URL> scannableUrls = filterWebappUrls(webUrlsArray, config == null ? null : config.customerFilter, descriptors.get(NewLoaderLogic.EXCLUSION_FILE));
    // executable war will add war in scannable urls, we don't want it since it will surely contain tomee, cxf, ...
    if (Boolean.parseBoolean(systemInstance.getProperty("openejb.core.skip-war-in-loader", "true"))) {
        File archive = warFile;
        if (!archive.getName().endsWith(".war")) {
            archive = new File(warFile.getParentFile(), warFile.getName() + ".war");
            final String unpackDir = systemInstance.getProperty("tomee.unpack.dir");
            if (unpackDir != null && !archive.isFile()) {
                try {
                    archive = new File(systemInstance.getBase().getDirectory(unpackDir, false), warFile.getName());
                } catch (final IOException e) {
                // no-op
                }
            }
        }
        if (archive.isFile()) {
            try {
                scannableUrls.remove(archive.toURI().toURL());
            } catch (final MalformedURLException e) {
            // no-op
            }
        }
    }
    if (externalUrls != null) {
        for (final URL url : externalUrls) {
            if (scannableUrls.contains(url)) {
                scannableUrls.remove(url);
                scannableUrls.add(0, url);
            }
        }
    }
    SystemInstance.get().fireEvent(new EnhanceScannableUrlsEvent(scannableUrls));
    final WebModule webModule = new WebModule(webApp, contextRoot, warClassLoader, warFile.getAbsolutePath(), moduleName);
    webModule.setUrls(webUrls);
    webModule.setAddedUrls(addedUrls);
    webModule.setRarUrls(Arrays.asList(urls.get(RAR_URLS_KEY)));
    webModule.setScannableUrls(scannableUrls);
    webModule.setDefaultContextPath(webApp.getDefaultContextPath());
    webModule.getAltDDs().putAll(descriptors);
    webModule.getWatchedResources().add(warPath);
    webModule.getWatchedResources().add(warFile.getAbsolutePath());
    if (webXmlUrl != null && "file".equals(webXmlUrl.getProtocol())) {
        webModule.getWatchedResources().add(URLs.toFilePath(webXmlUrl));
    }
    // If webModule object is loaded by ejbModule or persitenceModule, no need to load tag libraries, web service and JSF related staffs.
    addTagLibraries(webModule);
    // load webservices descriptor
    addWebservices(webModule);
    // load faces configuration files
    addFacesConfigs(webModule);
    addBeansXmls(webModule);
    return webModule;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) EnhanceScannableUrlsEvent(org.apache.openejb.config.event.EnhanceScannableUrlsEvent) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ClassLoaderConfigurer(org.apache.openejb.classloader.ClassLoaderConfigurer) URL(java.net.URL) SystemInstance(org.apache.openejb.loader.SystemInstance) URLClassLoader(java.net.URLClassLoader) EmptyResourcesClassLoader(org.apache.openejb.core.EmptyResourcesClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File) BeforeDeploymentEvent(org.apache.openejb.config.event.BeforeDeploymentEvent) WebApp(org.apache.openejb.jee.WebApp)

Aggregations

IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 URLClassLoader (java.net.URLClassLoader)2 ArrayList (java.util.ArrayList)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 EnhanceScannableUrlsEvent (org.apache.openejb.config.event.EnhanceScannableUrlsEvent)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedHashSet (java.util.LinkedHashSet)1 JarFile (java.util.jar.JarFile)1 CompositeBeans (org.apache.openejb.cdi.CompositeBeans)1 ClassLoaderConfigurer (org.apache.openejb.classloader.ClassLoaderConfigurer)1 BeforeDeploymentEvent (org.apache.openejb.config.event.BeforeDeploymentEvent)1 EmptyResourcesClassLoader (org.apache.openejb.core.EmptyResourcesClassLoader)1 EjbJar (org.apache.openejb.jee.EjbJar)1 WebApp (org.apache.openejb.jee.WebApp)1 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)1 SystemInstance (org.apache.openejb.loader.SystemInstance)1 IAnnotationFinder (org.apache.xbean.finder.IAnnotationFinder)1