Search in sources :

Example 16 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class ApplicationWideTest method testShouldCreateAResourceAndNotRemoveOnUndeploy.

public void testShouldCreateAResourceAndNotRemoveOnUndeploy() throws Exception {
    final EjbModule ejbModule = new EjbModule(new EjbJar(), new OpenejbJar());
    final EjbJar ejbJar = ejbModule.getEjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean(EchoBean.class));
    final AppModule appModule = new AppModule(ejbModule);
    final Container container = new Container();
    container.setId("My Container");
    container.setCtype("STATELESS");
    container.getProperties().setProperty("ApplicationWide", "true");
    appModule.getContainers().add(container);
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    {
        // setup the system
        assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
        assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    }
    final AppInfo appInfo = config.configureApplication(appModule);
    assembler.createApplication(appInfo);
    {
        final ContainerSystem containerSystem = assembler.getContainerSystem();
        final org.apache.openejb.Container appContainer = containerSystem.getContainer(ejbModule.getModuleId() + "/My Container");
        assertNotNull(appContainer);
    }
    assembler.destroyApplication(appInfo);
    {
        final ContainerSystem containerSystem = assembler.getContainerSystem();
        final org.apache.openejb.Container appContainer = containerSystem.getContainer(ejbModule.getModuleId() + "/My Container");
        assertNotNull(appContainer);
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) Container(org.apache.openejb.config.sys.Container) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) StatelessBean(org.apache.openejb.jee.StatelessBean) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJar(org.apache.openejb.jee.EjbJar) AppInfo(org.apache.openejb.assembler.classic.AppInfo)

Example 17 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar 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;
}
Also used : URLClassLoaderFirst(org.apache.openejb.util.classloader.URLClassLoaderFirst) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) EnterpriseArchive(org.jboss.shrinkwrap.api.spec.EnterpriseArchive) WebappAggregatedArchive(org.apache.openejb.config.WebappAggregatedArchive) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) Archive(org.jboss.shrinkwrap.api.Archive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) AppModule(org.apache.openejb.config.AppModule) HashMap(java.util.HashMap) Node(org.jboss.shrinkwrap.api.Node) ArrayList(java.util.ArrayList) EjbModule(org.apache.openejb.config.EjbModule) URL(java.net.URL) ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) URLClassLoader(java.net.URLClassLoader) Asset(org.jboss.shrinkwrap.api.asset.Asset) FileAsset(org.jboss.shrinkwrap.api.asset.FileAsset) ClassLoaderAsset(org.jboss.shrinkwrap.api.asset.ClassLoaderAsset) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) UrlAsset(org.jboss.shrinkwrap.api.asset.UrlAsset) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) IncludeRegExpPaths(org.jboss.shrinkwrap.impl.base.filter.IncludeRegExpPaths) EjbJar(org.apache.openejb.jee.EjbJar) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) ArchiveAsset(org.jboss.shrinkwrap.api.asset.ArchiveAsset) WebModule(org.apache.openejb.config.WebModule) FinderFactory(org.apache.openejb.config.FinderFactory) OpenEJBException(org.apache.openejb.OpenEJBException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) URLClassLoader(java.net.URLClassLoader) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class OpenEJBArchiveProcessor method addTestClassAsManagedBean.

private static EjbModule addTestClassAsManagedBean(Class<?> javaClass, URLClassLoader tempClassLoader, AppModule appModule) {
    final EjbJar ejbJar = new EjbJar();
    final OpenejbJar openejbJar = new OpenejbJar();
    final String ejbName = appModule.getModuleId() + "_" + javaClass.getName();
    final ManagedBean bean = ejbJar.addEnterpriseBean(new ManagedBean(ejbName, javaClass.getName(), true));
    bean.localBean();
    bean.setTransactionType(TransactionType.BEAN);
    final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
    ejbDeployment.setDeploymentId(ejbName);
    final EjbModule e = new EjbModule(ejbJar, openejbJar);
    e.getProperties().setProperty("openejb.cdi.activated", "false");
    e.getProperties().setProperty("openejb.test.module", "true");
    e.setBeans(new Beans());
    e.setClassLoader(tempClassLoader);
    appModule.getEjbModules().add(e);
    return e;
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) Beans(org.apache.openejb.jee.Beans) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) EjbModule(org.apache.openejb.config.EjbModule) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ManagedBean(org.apache.openejb.jee.ManagedBean) EjbJar(org.apache.openejb.jee.EjbJar)

Example 19 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class TestClassDiscoverer method discover.

@Override
public AppModule discover(final AppModule module) {
    final Set<Class<?>> testClasses = new HashSet<>();
    final Map<Class<?>, WebModule> webTestClasses = new HashMap<>();
    final Set<ClassLoader> saw = new HashSet<>();
    if (module.getClassLoader() != null) {
        addTests(findMarkers(module.getClassLoader()), findClassMarkers(module.getClassLoader()), module.getEarLibFinder(), testClasses);
        saw.add(module.getClassLoader());
    }
    for (final WebModule web : module.getWebModules()) {
        if (web.getClassLoader() != null && !saw.contains(web.getClassLoader())) {
            final Set<Class<?>> classes = new HashSet<>();
            addTests(findMarkers(web.getClassLoader()), findClassMarkers(web.getClassLoader()), web.getFinder(), classes);
            saw.add(web.getClassLoader());
            for (final Class<?> c : classes) {
                webTestClasses.put(c, web);
            }
            // in case of an ear if we find the same test class in a webapp we don't want it in lib part
            // this case can happen in tomee-embedded mainly
            final Iterator<Class<?>> c = testClasses.iterator();
            while (c.hasNext()) {
                final String cl = c.next().getName();
                for (final Class<?> wc : classes) {
                    if (cl.equals(wc.getName())) {
                        c.remove();
                        break;
                    }
                }
            }
            testClasses.addAll(classes);
        }
    }
    for (final EjbModule ejb : module.getEjbModules()) {
        if (ejb.getClassLoader() != null && !saw.contains(ejb.getClassLoader())) {
            addTests(findMarkers(ejb.getClassLoader()), findClassMarkers(ejb.getClassLoader()), ejb.getFinder(), testClasses);
            saw.add(ejb.getClassLoader());
        }
    }
    for (final ConnectorModule connector : module.getConnectorModules()) {
        if (connector.getClassLoader() != null && !saw.contains(connector.getClassLoader())) {
            addTests(findMarkers(connector.getClassLoader()), findClassMarkers(connector.getClassLoader()), connector.getFinder(), testClasses);
            saw.add(connector.getClassLoader());
        }
    }
    // keep it since CukeSpace doesn't rely on JUnit or TestNG @Test so it stays mandatory
    final File file = module.getFile();
    final String line = findTestName(file, module.getClassLoader());
    if (line != null) {
        String name;
        final int endIndex = line.indexOf('#');
        if (endIndex > 0) {
            name = line.substring(0, endIndex);
            if (file != null && !file.getName().equals(line.substring(endIndex + 1, line.length()))) {
                name = null;
            }
        } else {
            name = line;
        }
        if (name != null) {
            boolean found = false;
            for (final WebModule web : module.getWebModules()) {
                try {
                    testClasses.add(web.getClassLoader().loadClass(name));
                    found = true;
                    break;
                } catch (final Throwable e) {
                // no-op
                }
            }
            if (!found) {
                try {
                    testClasses.add(module.getClassLoader().loadClass(name));
                } catch (final Throwable e) {
                // no-op
                }
            }
        }
    }
    final Iterator<Class<?>> it = testClasses.iterator();
    while (it.hasNext()) {
        try {
            // call some reflection methods to make it fail if some dep are missing...
            Class<?> current = it.next();
            if (!AnnotationDeployer.isInstantiable(current)) {
                it.remove();
                continue;
            }
            while (current != null) {
                current.getDeclaredFields();
                current.getDeclaredMethods();
                current.getCanonicalName();
                current = current.getSuperclass();
            // TODO: more validations
            }
        } catch (final NoClassDefFoundError ncdfe) {
            it.remove();
        }
    }
    for (final Class<?> test : testClasses) {
        final EjbJar ejbJar = new EjbJar();
        final OpenejbJar openejbJar = new OpenejbJar();
        final String name = test.getName();
        final String ejbName = module.getModuleId() + "_" + name;
        final ManagedBean bean = ejbJar.addEnterpriseBean(new ManagedBean(ejbName, name, true));
        bean.localBean();
        bean.setTransactionType(TransactionType.BEAN);
        final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(bean);
        ejbDeployment.setDeploymentId(ejbName);
        final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
        ejbModule.setClassLoader(test.getClassLoader());
        final WebModule webModule = webTestClasses.get(test);
        if (webModule != null) {
            ejbModule.setWebapp(true);
            ejbModule.getProperties().put("openejb.ejbmodule.webappId", webModule.getModuleId());
        }
        ejbModule.getProperties().put("openejb.ejbmodule.MergeWebappJndiContext", "true");
        module.getEjbModules().add(ejbModule);
    }
    return module;
}
Also used : HashMap(java.util.HashMap) EjbModule(org.apache.openejb.config.EjbModule) WebModule(org.apache.openejb.config.WebModule) ConnectorModule(org.apache.openejb.config.ConnectorModule) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) ManagedBean(org.apache.openejb.jee.ManagedBean) File(java.io.File) HashSet(java.util.HashSet) EjbJar(org.apache.openejb.jee.EjbJar)

Example 20 with OpenejbJar

use of org.apache.openejb.jee.oejb3.OpenejbJar in project tomee by apache.

the class EjbModule method setModuleId.

public void setModuleId(final String moduleId) {
    if (openejbJar == null) {
        openejbJar = new OpenejbJar();
    }
    openejbJar.setModuleName(moduleId);
    this.id = new ID(openejbJar, ejbJar, moduleId, id.getLocation(), id.getUri(), this);
}
Also used : OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar)

Aggregations

OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)65 EjbJar (org.apache.openejb.jee.EjbJar)52 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)37 EjbModule (org.apache.openejb.config.EjbModule)30 Properties (java.util.Properties)20 StatelessBean (org.apache.openejb.jee.StatelessBean)19 Assembler (org.apache.openejb.assembler.classic.Assembler)18 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)16 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)12 ContainerSystem (org.apache.openejb.spi.ContainerSystem)12 SingletonBean (org.apache.openejb.jee.SingletonBean)11 Module (org.apache.openejb.testing.Module)11 AppModule (org.apache.openejb.config.AppModule)8 PojoDeployment (org.apache.openejb.jee.oejb3.PojoDeployment)8 InitialContext (javax.naming.InitialContext)7 OpenEJBException (org.apache.openejb.OpenEJBException)7 Resources (org.apache.openejb.config.sys.Resources)7 Service (org.apache.openejb.config.sys.Service)7 MessageDrivenBean (org.apache.openejb.jee.MessageDrivenBean)7 File (java.io.File)6