Search in sources :

Example 16 with WebApp$JAXB

use of org.apache.openejb.jee.WebApp$JAXB in project tomee by apache.

the class WebAppEnvEntryTest method war.

@Module
@Classes(cdi = true, value = { CdiBean.class })
public WebApp war() {
    final WebApp webApp = new WebApp().contextRoot("/myapp");
    webApp.getEnvEntry().add(new EnvEntry("foo", String.class.getName(), "bar"));
    return webApp;
}
Also used : WebApp(org.apache.openejb.jee.WebApp) EnvEntry(org.apache.openejb.jee.EnvEntry)

Example 17 with WebApp$JAXB

use of org.apache.openejb.jee.WebApp$JAXB 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
    final List<URL> webUrls = new ArrayList<>();
    ensureContainerUrls();
    webUrls.addAll(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<URL>();
        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<URL>();
    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);
            }
        }
    }
    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.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) 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)

Example 18 with WebApp$JAXB

use of org.apache.openejb.jee.WebApp$JAXB in project tomee by apache.

the class AppInfoBuilder method buildWebModules.

private void buildWebModules(final AppModule appModule, final JndiEncInfoBuilder jndiEncInfoBuilder, final AppInfo appInfo) throws OpenEJBException {
    for (final WebModule webModule : appModule.getWebModules()) {
        final WebApp webApp = webModule.getWebApp();
        final WebAppInfo webAppInfo = new WebAppInfo();
        webAppInfo.description = webApp.getDescription();
        webAppInfo.displayName = webApp.getDisplayName();
        webAppInfo.path = webModule.getJarLocation();
        webAppInfo.moduleId = webModule.getModuleId();
        webAppInfo.watchedResources.addAll(webModule.getWatchedResources());
        webAppInfo.validationInfo = ValidatorBuilder.getInfo(webModule.getValidationConfig());
        webAppInfo.uniqueId = webModule.getUniqueId();
        webAppInfo.restApplications.addAll(webModule.getRestApplications());
        webAppInfo.restClass.addAll(webModule.getRestClasses());
        webAppInfo.ejbWebServices.addAll(webModule.getEjbWebServices());
        webAppInfo.ejbRestServices.addAll(webModule.getEjbRestServices());
        webAppInfo.jaxRsProviders.addAll(webModule.getJaxrsProviders());
        for (final Map.Entry<String, Set<String>> entry : webModule.getWebAnnotatedClasses().entrySet()) {
            final ClassListInfo info = new ClassListInfo();
            info.name = entry.getKey();
            info.list.addAll(entry.getValue());
            webAppInfo.webAnnotatedClasses.add(info);
        }
        for (final Map.Entry<String, Set<String>> entry : webModule.getJsfAnnotatedClasses().entrySet()) {
            final ClassListInfo info = new ClassListInfo();
            info.name = entry.getKey();
            info.list.addAll(entry.getValue());
            webAppInfo.jsfAnnotatedClasses.add(info);
        }
        webAppInfo.host = webModule.getHost();
        if (!webModule.isStandaloneModule() && USE_EAR_AS_CONTEXT_ROOT_BASE) {
            webAppInfo.contextRoot = appModule.getModuleId() + "/" + webModule.getContextRoot();
        } else {
            webAppInfo.contextRoot = webModule.getContextRoot();
        }
        webAppInfo.sessionTimeout = 30;
        if (webModule.getWebApp() != null && webModule.getWebApp().getSessionConfig() != null) {
            for (final SessionConfig sessionConfig : webModule.getWebApp().getSessionConfig()) {
                if (sessionConfig.getSessionTimeout() != null) {
                    webAppInfo.sessionTimeout = sessionConfig.getSessionTimeout();
                    break;
                }
            }
        }
        jndiEncInfoBuilder.build(webApp, webModule.getJarLocation(), webAppInfo.moduleId, webModule.getModuleUri(), webAppInfo.jndiEnc, webAppInfo.jndiEnc);
        webAppInfo.portInfos.addAll(this.configureWebservices(webModule.getWebservices()));
        for (final Servlet servlet : webModule.getWebApp().getServlet()) {
            final ServletInfo servletInfo = new ServletInfo();
            servletInfo.servletName = servlet.getServletName();
            servletInfo.servletClass = servlet.getServletClass();
            servletInfo.mappings = webModule.getWebApp().getServletMappings(servletInfo.servletName);
            for (final ParamValue pv : servlet.getInitParam()) {
                final ParamValueInfo pvi = new ParamValueInfo();
                pvi.name = pv.getParamName();
                pvi.value = pv.getParamValue();
                servletInfo.initParams.add(pvi);
            }
            webAppInfo.servlets.add(servletInfo);
        }
        for (final Listener listener : webModule.getWebApp().getListener()) {
            final ListenerInfo listenerInfo = new ListenerInfo();
            listenerInfo.classname = listener.getListenerClass();
            webAppInfo.listeners.add(listenerInfo);
        }
        for (final Filter filter : webModule.getWebApp().getFilter()) {
            final FilterInfo filterInfo = new FilterInfo();
            filterInfo.name = filter.getFilterName();
            filterInfo.classname = filter.getFilterClass();
            filterInfo.mappings = webModule.getWebApp().getFilterMappings(filter.getFilterName());
            for (final ParamValue pv : filter.getInitParam()) {
                filterInfo.initParams.put(pv.getParamName(), pv.getParamValue());
            }
            webAppInfo.filters.add(filterInfo);
        }
        appInfo.webApps.add(webAppInfo);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) MessageListener(org.apache.openejb.jee.MessageListener) Listener(org.apache.openejb.jee.Listener) SessionConfig(org.apache.openejb.jee.SessionConfig) ParamValue(org.apache.openejb.jee.ParamValue) ClassListInfo(org.apache.openejb.assembler.classic.ClassListInfo) ServletInfo(org.apache.openejb.assembler.classic.ServletInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) ListenerInfo(org.apache.openejb.assembler.classic.ListenerInfo) ParamValueInfo(org.apache.openejb.assembler.classic.ParamValueInfo) Filter(org.apache.openejb.jee.Filter) Servlet(org.apache.openejb.jee.Servlet) Map(java.util.Map) HashMap(java.util.HashMap) FilterInfo(org.apache.openejb.assembler.classic.FilterInfo) WebApp(org.apache.openejb.jee.WebApp)

Example 19 with WebApp$JAXB

use of org.apache.openejb.jee.WebApp$JAXB in project tomee by apache.

the class EjbJarXmlTest method testEjbJar.

/**
 * TODO Doesn't seem there are any asserts here
 *
 * @throws Exception
 */
public void testEjbJar() throws Exception {
    final String fileName = "ejb-jar-example1.xml";
    final Event test = Event.start("Test");
    final URL resource = this.getClass().getClassLoader().getResource(fileName);
    final String expected = IO.slurp(resource);
    final Event ejbJarJAXBCreate = Event.start("EjbJarJAXBCreate");
    ejbJarJAXBCreate.stop();
    final Event unmarshalEvent = Event.start("unmarshal");
    final Object value;
    final EjbJar$JAXB jaxbType = new EjbJar$JAXB();
    value = Sxc.unmarshalJavaee(resource, jaxbType);
    unmarshalEvent.stop();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Event marshall = Event.start("marshall");
    Sxc.marshall(jaxbType, value, baos);
    marshall.stop();
    final String result = new String(baos.toByteArray(), "UTF-8");
    XMLUnit.setIgnoreComments(Boolean.TRUE);
    XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
    XMLUnit.setIgnoreAttributeOrder(Boolean.TRUE);
    XMLUnit.setIgnoreDiffBetweenTextAndCDATA(Boolean.TRUE);
    final Diff diff = new Diff(expected.trim(), result.trim());
    final Diff myDiff = new DetailedDiff(diff);
    // just to get an int wrapper for the test
    final AtomicInteger differenceNumber = new AtomicInteger(0);
    myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener() {

        @Override
        public int differenceFound(final Difference difference) {
            if (!difference.isRecoverable()) {
                differenceNumber.incrementAndGet();
                System.err.println(">>> " + difference.toString());
            }
            return 0;
        }
    });
    assertTrue("Files are not identical", myDiff.identical());
    test.stop();
}
Also used : DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) DetailedDiff(org.custommonkey.xmlunit.DetailedDiff) Diff(org.custommonkey.xmlunit.Diff) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgnoreTextAndAttributeValuesDifferenceListener(org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Difference(org.custommonkey.xmlunit.Difference) URL(java.net.URL) EjbJar$JAXB(org.apache.openejb.jee.EjbJar$JAXB)

Example 20 with WebApp$JAXB

use of org.apache.openejb.jee.WebApp$JAXB in project tomee by apache.

the class WsJMXTest method war.

@Module
@Classes({ AnEjbEndpoint.class, AnPojoEndpoint.class })
public WebApp war() {
    final WebApp webapp = new WebApp().contextRoot("app");
    Servlet servlet = new Servlet();
    servlet.setServletName("toto");
    servlet.setServletClass(AnPojoEndpoint.class.getName());
    ServletMapping servletMapping = new ServletMapping();
    servletMapping.setServletName("pojo");
    servletMapping.getUrlPattern().add("/toto");
    webapp.getServlet().add(servlet);
    webapp.getServletMapping().add(servletMapping);
    return webapp;
}
Also used : ServletMapping(org.apache.openejb.jee.ServletMapping) Servlet(org.apache.openejb.jee.Servlet) WebApp(org.apache.openejb.jee.WebApp) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Aggregations

WebApp (org.apache.openejb.jee.WebApp)24 AppInfo (org.apache.openejb.assembler.classic.AppInfo)7 Persistence (org.apache.openejb.jee.jpa.unit.Persistence)7 PersistenceUnit (org.apache.openejb.jee.jpa.unit.PersistenceUnit)7 File (java.io.File)6 IOException (java.io.IOException)6 URL (java.net.URL)6 ResourceInfo (org.apache.openejb.assembler.classic.ResourceInfo)6 WebModule (org.apache.openejb.config.WebModule)5 MalformedURLException (java.net.MalformedURLException)4 LinkedHashSet (java.util.LinkedHashSet)4 JarFile (java.util.jar.JarFile)4 HashSet (java.util.HashSet)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 WebAppInfo (org.apache.openejb.assembler.classic.WebAppInfo)3 EjbJar (org.apache.openejb.jee.EjbJar)3 InputStream (java.io.InputStream)2 URLClassLoader (java.net.URLClassLoader)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2