Search in sources :

Example 11 with WebApp$JAXB

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

the class AutoConfigPersistenceUnitsTest method testFromWebAppContextNonJta.

/**
 * Existing data source "orange-web", non-jta managed
 * <p/>
 * Application contains a web module with root context path as "orange-web"
 * <p/>
 * Persistence xml like so:
 * <p/>
 * <persistence-unit name="orange-unit" />
 * <p/>
 * The orange-unit app should automatically use orange-web data source and create a new non-JtaManaged datasource
 *
 * @throws Exception
 */
public void testFromWebAppContextNonJta() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", false);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    // Check results
    final ResourceInfo generated = resources.get(1);
    assertEquals(supplied.id + "Jta", generated.id);
    assertEquals(supplied.service, generated.service);
    assertEquals(supplied.className, generated.className);
    assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
    assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
    assertEquals("true", generated.properties.get("JtaManaged"));
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 12 with WebApp$JAXB

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

the class AutoConfigPersistenceUnitsTest method testFromWebAppContextThirdParty.

/**
 * Existing data source "orange-web", not controlled by us
 * <p/>
 * Application contains a web module with root context path as "orange-web"
 * <p/>
 * Persistence xml like so:
 * <p/>
 * <persistence-unit name="orange-unit" />
 * <p/>
 * The orange-unit app should automatically use orange-web data source and the non-jta-datasource should be null
 *
 * @throws Exception
 */
public void testFromWebAppContextThirdParty() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-web", OrangeDriver.class, "jdbc:orange-web:some:stuff", null);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-web"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    final PersistenceUnitInfo unitInfo = appInfo.persistenceUnits.get(0);
    // Check results
    assertEquals(supplied.id, unitInfo.jtaDataSource);
    assertNull(unitInfo.nonJtaDataSource);
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) PersistenceUnitInfo(org.apache.openejb.assembler.classic.PersistenceUnitInfo) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 13 with WebApp$JAXB

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

the class AutoConfigPersistenceUnitsTest method testFromWebAppIdJta.

/**
 * Existing data source "orange-web", jta managed
 * <p/>
 * Application contains a web module with id "orange-id"
 * <p/>
 * Persistence xml like so:
 * <p/>
 * <persistence-unit name="orange-unit" />
 * <p/>
 * The orange-unit app should automatically use orange-id data source and create a new non-JtaManaged datasource
 *
 * @throws Exception
 */
public void testFromWebAppIdJta() throws Exception {
    final ResourceInfo supplied = addDataSource("orange-id", OrangeDriver.class, "jdbc:orange-web:some:stuff", true);
    assertSame(supplied, resources.get(0));
    final PersistenceUnit persistenceUnit = new PersistenceUnit("orange-unit");
    final ClassLoader cl = this.getClass().getClassLoader();
    final AppModule app = new AppModule(cl, "orange-app");
    app.addPersistenceModule(new PersistenceModule("root", new Persistence(persistenceUnit)));
    final WebApp webApp = new WebApp();
    webApp.setMetadataComplete(true);
    app.getWebModules().add(new WebModule(webApp, "orange-web", cl, "war", "orange-id"));
    // Create app
    final AppInfo appInfo = config.configureApplication(app);
    assembler.createApplication(appInfo);
    // Check results
    final ResourceInfo generated = resources.get(1);
    assertEquals(supplied.id + "NonJta", generated.id);
    assertEquals(supplied.service, generated.service);
    assertEquals(supplied.className, generated.className);
    assertEquals(supplied.properties.get("JdbcDriver"), generated.properties.get("JdbcDriver"));
    assertEquals(supplied.properties.get("JdbcUrl"), generated.properties.get("JdbcUrl"));
    assertEquals("false", generated.properties.get("JtaManaged"));
}
Also used : Persistence(org.apache.openejb.jee.jpa.unit.Persistence) ResourceInfo(org.apache.openejb.assembler.classic.ResourceInfo) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) WebApp(org.apache.openejb.jee.WebApp) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 14 with WebApp$JAXB

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

the class Container method deploy.

public Container deploy(final DeploymentRequest request) {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final SystemInstance systemInstance = SystemInstance.get();
    String contextRoot = request.context == null ? "" : request.context;
    if (!contextRoot.isEmpty() && !contextRoot.startsWith("/")) {
        contextRoot = "/" + request.context;
    }
    File jarLocation = request.docBase == null || !request.docBase.isDirectory() ? fakeRootDir() : request.docBase;
    final WebModule webModule = new WebModule(new WebApp(), contextRoot, loader, jarLocation.getAbsolutePath(), contextRoot.replace("/", ""));
    if (request.docBase == null) {
        webModule.getProperties().put("fakeJarLocation", "true");
    }
    webModule.setUrls(request.jarList);
    webModule.setAddedUrls(Collections.<URL>emptyList());
    webModule.setRarUrls(Collections.<URL>emptyList());
    webModule.setScannableUrls(request.jarList);
    final AnnotationFinder finder;
    try {
        Filter filter = configuration.getClassesFilter();
        if (filter == null && (request.jarList.size() <= 4 || "true".equalsIgnoreCase(SystemInstance.get().getProperty("tomee.embedded.filter-container-classes")))) {
            filter = new ContainerClassesFilter(configuration.getProperties());
        }
        final Archive archive;
        if (request.archive == null) {
            archive = new WebappAggregatedArchive(webModule, request.jarList, // see org.apache.openejb.config.DeploymentsResolver.ClasspathSearcher.cleanUpUrlSet()
            filter);
        } else if (WebappAggregatedArchive.class.isInstance(request.archive)) {
            archive = request.archive;
        } else {
            archive = new WebappAggregatedArchive(request.archive, request.jarList);
        }
        finder = new FinderFactory.OpenEJBAnnotationFinder(archive).link();
        SystemInstance.get().fireEvent(new TomEEEmbeddedScannerCreated(finder));
        webModule.setFinder(finder);
    } catch (final Exception e) {
        throw new IllegalArgumentException(e);
    }
    final File beansXml = new File(request.docBase, "WEB-INF/beans.xml");
    if (beansXml.exists()) {
        // add it since it is not in the scanned path by default
        try {
            webModule.getAltDDs().put("beans.xml", beansXml.toURI().toURL());
        } catch (final MalformedURLException e) {
        // no-op
        }
    }
    // else no classpath finding since we'll likely find it
    DeploymentLoader.addBeansXmls(webModule);
    final AppModule app = new AppModule(loader, null);
    app.setStandloneWebModule();
    app.setStandaloneModule(true);
    app.setModuleId(webModule.getModuleId());
    try {
        final Map<String, URL> webDescriptors = DeploymentLoader.getWebDescriptors(jarLocation);
        if (webDescriptors.isEmpty()) {
            // likely so let's try to find them in the classpath
            final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            final Collection<String> metaDir = asList("META-INF/tomee/", "META-INF/");
            for (final String dd : asList("app-ctx.xml", "module.properties", "application.properties", "env-entries.properties", NewLoaderLogic.EXCLUSION_FILE, "web.xml", "ejb-jar.xml", "openejb-jar.xml", "validation.xml")) {
                if (Boolean.parseBoolean(SystemInstance.get().getProperty("tomee.embedded.descriptors.classpath." + dd + ".skip")) || webDescriptors.containsKey(dd)) {
                    continue;
                }
                for (final String meta : metaDir) {
                    final URL url = classLoader.getResource(meta + dd);
                    if (url != null) {
                        webDescriptors.put(dd, url);
                        break;
                    }
                }
            }
        }
        webDescriptors.remove("beans.xml");
        webModule.getAltDDs().putAll(webDescriptors);
        DeploymentLoader.addWebModule(webModule, app);
        DeploymentLoader.addWebModuleDescriptors(new File(webModule.getJarLocation()).toURI().toURL(), webModule, app);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    if (!SystemInstance.isInitialized() || Boolean.parseBoolean(SystemInstance.get().getProperty("tomee.embedded.add-callers", "true"))) {
        addCallersAsEjbModule(loader, app, request.additionalCallers);
    }
    systemInstance.addObserver(new StandardContextCustomizer(configuration, webModule, request.keepClassloader));
    if (systemInstance.getComponent(AnnotationDeployer.FolderDDMapper.class) == null) {
        systemInstance.setComponent(AnnotationDeployer.FolderDDMapper.class, new AnnotationDeployer.FolderDDMapper() {

            @Override
            public File getDDFolder(final File dir) {
                try {
                    return isMaven(dir) || isGradle(dir) ? new File(request.docBase, "WEB-INF") : null;
                } catch (final RuntimeException re) {
                    // folder doesn't exist -> test is stopped which is expected
                    return null;
                }
            }

            private boolean isGradle(final File dir) {
                return dir.getName().equals("classes") && dir.getParentFile().getName().equals("target");
            }

            private boolean isMaven(final File dir) {
                return dir.getName().equals("main") && dir.getParentFile().getName().equals("classes") && dir.getParentFile().getParentFile().getName().equals("build");
            }
        });
    }
    try {
        final AppInfo appInfo = configurationFactory.configureApplication(app);
        systemInstance.getComponent(Assembler.class).createApplication(appInfo, loader);
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
    return this;
}
Also used : TomEEEmbeddedScannerCreated(org.apache.tomee.embedded.event.TomEEEmbeddedScannerCreated) MalformedURLException(java.net.MalformedURLException) WebappAggregatedArchive(org.apache.openejb.config.WebappAggregatedArchive) Archive(org.apache.xbean.finder.archive.Archive) AppModule(org.apache.openejb.config.AppModule) ContainerClassesFilter(org.apache.openejb.util.ContainerClassesFilter) URL(java.net.URL) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) SystemInstance(org.apache.openejb.loader.SystemInstance) WebModule(org.apache.openejb.config.WebModule) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException) TomEERuntimeException(org.apache.tomee.catalina.TomEERuntimeException) NamingException(javax.naming.NamingException) UndeployException(org.apache.openejb.UndeployException) OpenEJBException(org.apache.openejb.OpenEJBException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) AppInfo(org.apache.openejb.assembler.classic.AppInfo) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) AnnotationDeployer(org.apache.openejb.config.AnnotationDeployer) StandardContextCustomizer(org.apache.tomee.embedded.internal.StandardContextCustomizer) Filter(org.apache.xbean.finder.filter.Filter) ContainerClassesFilter(org.apache.openejb.util.ContainerClassesFilter) WebappAggregatedArchive(org.apache.openejb.config.WebappAggregatedArchive) Assembler(org.apache.openejb.assembler.classic.Assembler) File(java.io.File) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) WebApp(org.apache.openejb.jee.WebApp)

Example 15 with WebApp$JAXB

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

the class OpenEJBArchiveProcessor method createWebApp.

private static WebApp createWebApp(final Archive<?> archive) {
    WebApp webApp;
    final Node webXml = archive.get(WEB_INF + "web.xml");
    if (webXml == null) {
        webApp = new WebApp();
    } else {
        InputStream inputStream = null;
        try {
            inputStream = webXml.getAsset().openStream();
            webApp = Sxc.unmarshalJavaee(new WebApp$JAXB(), inputStream);
        } catch (final Exception e) {
            webApp = new WebApp();
        } finally {
            IO.close(inputStream);
        }
    }
    return webApp;
}
Also used : InputStream(java.io.InputStream) Node(org.jboss.shrinkwrap.api.Node) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) WebApp(org.apache.openejb.jee.WebApp) WebApp$JAXB(org.apache.openejb.jee.WebApp$JAXB)

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