Search in sources :

Example 1 with ManagedBean

use of org.apache.openejb.jee.ManagedBean 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 2 with ManagedBean

use of org.apache.openejb.jee.ManagedBean in project tomee by apache.

the class EjbJarInfoBuilder method initSessionBean.

private EnterpriseBeanInfo initSessionBean(final SessionBean s, final EjbJarInfo ejbJar, final Map m) throws OpenEJBException {
    EnterpriseBeanInfo bean;
    if (s.getSessionType() == SessionType.STATEFUL) {
        bean = new StatefulBeanInfo();
        bean.passivable = s.getPassivationCapable() == null || s.getPassivationCapable();
        final StatefulBeanInfo stateful = (StatefulBeanInfo) bean;
        copyCallbacks(s.getPostActivate(), stateful.postActivate);
        copyCallbacks(s.getPrePassivate(), stateful.prePassivate);
        copyCallbacks(s.getAfterBegin(), stateful.afterBegin);
        copyCallbacks(s.getBeforeCompletion(), stateful.beforeCompletion);
        copyCallbacks(s.getAfterCompletion(), stateful.afterCompletion);
        for (final InitMethod initMethod : s.getInitMethod()) {
            final InitMethodInfo init = new InitMethodInfo();
            init.beanMethod = toInfo(initMethod.getBeanMethod());
            init.createMethod = toInfo(initMethod.getCreateMethod());
            stateful.initMethods.add(init);
        }
        for (final RemoveMethod removeMethod : s.getRemoveMethod()) {
            final RemoveMethodInfo remove = new RemoveMethodInfo();
            remove.beanMethod = toInfo(removeMethod.getBeanMethod());
            remove.retainIfException = removeMethod.getRetainIfException();
            stateful.removeMethods.add(remove);
        }
        copyConcurrentMethods(s, ejbJar, m);
    } else if (s.getSessionType() == SessionType.MANAGED) {
        bean = new ManagedBeanInfo();
        final ManagedBeanInfo managed = (ManagedBeanInfo) bean;
        // this way we support managed beans in ejb-jar.xml (not in the spec but can be useful)
        managed.hidden = !(s instanceof ManagedBean) || ((ManagedBean) s).isHidden();
        copyCallbacks(s.getPostActivate(), managed.postActivate);
        copyCallbacks(s.getPrePassivate(), managed.prePassivate);
        for (final RemoveMethod removeMethod : s.getRemoveMethod()) {
            final RemoveMethodInfo remove = new RemoveMethodInfo();
            remove.beanMethod = toInfo(removeMethod.getBeanMethod());
            remove.retainIfException = removeMethod.getRetainIfException();
            managed.removeMethods.add(remove);
        }
    } else if (s.getSessionType() == SessionType.SINGLETON) {
        bean = new SingletonBeanInfo();
        final ConcurrencyManagementType type = s.getConcurrencyManagementType();
        bean.concurrencyType = type != null ? type.toString() : ConcurrencyManagementType.CONTAINER.toString();
        bean.loadOnStartup = s.getInitOnStartup();
        copyCallbacks(s.getAroundTimeout(), bean.aroundTimeout);
        copySchedules(s.getTimer(), bean.methodScheduleInfos);
        // See JndiEncInfoBuilder.buildDependsOnRefs for processing of DependsOn
        // bean.dependsOn.addAll(s.getDependsOn());
        copyConcurrentMethods(s, ejbJar, m);
    } else {
        bean = new StatelessBeanInfo();
        copySchedules(s.getTimer(), bean.methodScheduleInfos);
    }
    if (s.getSessionType() != SessionType.STATEFUL) {
        copyCallbacks(s.getAroundTimeout(), bean.aroundTimeout);
    }
    bean.localbean = s.getLocalBean() != null;
    bean.timeoutMethod = toInfo(s.getTimeoutMethod());
    copyCallbacks(s.getAroundInvoke(), bean.aroundInvoke);
    copyCallbacks(s.getPostConstruct(), bean.postConstruct);
    copyCallbacks(s.getPreDestroy(), bean.preDestroy);
    copyAsynchronous(s.getAsyncMethod(), bean.asynchronous);
    bean.asynchronousClasses.addAll(s.getAsynchronousClasses());
    final EjbDeployment d = (EjbDeployment) m.get(s.getEjbName());
    if (d == null) {
        throw new OpenEJBException("No deployment information in openejb-jar.xml for bean " + s.getEjbName() + ". Please redeploy the jar");
    }
    bean.ejbDeploymentId = d.getDeploymentId();
    bean.containerId = d.getContainerId();
    final Icon icon = s.getIcon();
    bean.largeIcon = icon == null ? null : icon.getLargeIcon();
    bean.smallIcon = icon == null ? null : icon.getSmallIcon();
    bean.description = s.getDescription();
    bean.displayName = s.getDisplayName();
    bean.ejbClass = s.getEjbClass();
    bean.ejbName = s.getEjbName();
    bean.home = s.getHome();
    bean.remote = s.getRemote();
    bean.localHome = s.getLocalHome();
    bean.local = s.getLocal();
    bean.proxy = s.getProxy();
    bean.parents.addAll(s.getParents());
    bean.businessLocal.addAll(s.getBusinessLocal());
    bean.businessRemote.addAll(s.getBusinessRemote());
    final TransactionType txType = s.getTransactionType();
    bean.transactionType = txType != null ? txType.toString() : TransactionType.CONTAINER.toString();
    bean.serviceEndpoint = s.getServiceEndpoint();
    bean.properties.putAll(d.getProperties());
    bean.statefulTimeout = toInfo(s.getStatefulTimeout());
    bean.restService = s.isRestService() && !(s instanceof StatefulBean);
    return bean;
}
Also used : InitMethod(org.apache.openejb.jee.InitMethod) InitMethodInfo(org.apache.openejb.assembler.classic.InitMethodInfo) OpenEJBException(org.apache.openejb.OpenEJBException) TransactionType(org.apache.openejb.jee.TransactionType) ConcurrencyManagementType(org.apache.openejb.jee.ConcurrencyManagementType) StatefulBean(org.apache.openejb.jee.StatefulBean) RemoveMethodInfo(org.apache.openejb.assembler.classic.RemoveMethodInfo) StatelessBeanInfo(org.apache.openejb.assembler.classic.StatelessBeanInfo) ManagedBeanInfo(org.apache.openejb.assembler.classic.ManagedBeanInfo) SingletonBeanInfo(org.apache.openejb.assembler.classic.SingletonBeanInfo) EnterpriseBeanInfo(org.apache.openejb.assembler.classic.EnterpriseBeanInfo) StatefulBeanInfo(org.apache.openejb.assembler.classic.StatefulBeanInfo) RemoveMethod(org.apache.openejb.jee.RemoveMethod) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Icon(org.apache.openejb.jee.Icon) ManagedBean(org.apache.openejb.jee.ManagedBean)

Example 3 with ManagedBean

use of org.apache.openejb.jee.ManagedBean in project tomee by apache.

the class ApplicationComposers method deployApp.

public void deployApp(final Object inputTestInstance) throws Exception {
    final ClassFinder testClassFinder = fixFakeClassFinder(inputTestInstance);
    final ClassLoader loader = testClass.getClassLoader();
    AppModule appModule = new AppModule(loader, testClass.getSimpleName());
    // Add the test case as an @ManagedBean
    final ManagedBean testBean;
    {
        final EjbJar ejbJar = new EjbJar();
        final OpenejbJar openejbJar = new OpenejbJar();
        testBean = ejbJar.addEnterpriseBean(new ManagedBean(testClass.getSimpleName(), testClass.getName(), true));
        testBean.localBean();
        testBean.setTransactionType(TransactionType.BEAN);
        final EjbDeployment ejbDeployment = openejbJar.addEjbDeployment(testBean);
        ejbDeployment.setDeploymentId(testClass.getName());
        final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
        ejbModule.getProperties().setProperty("openejb.cdi.activated", "false");
        final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new ClassesArchive(ancestors(testClass)));
        ejbModule.setFinder(finder);
        if (finder.findMetaAnnotatedFields(Inject.class).size() + finder.findMetaAnnotatedMethods(Inject.class).size() > 0) {
            // "activate" cdi to avoid WARNINGs
            ejbModule.setBeans(new Beans());
        }
        appModule.getEjbModules().add(ejbModule);
    }
    final Map<String, URL> additionalDescriptors = descriptorsToMap(testClass.getAnnotation(org.apache.openejb.junit.Descriptors.class));
    final Map<String, URL> additionalDescriptorsNew = descriptorsToMap(testClass.getAnnotation(Descriptors.class));
    additionalDescriptors.putAll(additionalDescriptorsNew);
    Application application = null;
    int webModulesNb = 0;
    final Jars globalJarsAnnotation = testClass.getAnnotation(Jars.class);
    // Invoke the @Module producer methods to build out the AppModule
    int moduleNumber = 0;
    // we dont consider resources.xml to set an app as standalone or not
    int notBusinessModuleNumber = 0;
    final Map<Object, List<Method>> moduleMethods = new HashMap<>();
    findAnnotatedMethods(moduleMethods, Module.class);
    findAnnotatedMethods(moduleMethods, org.apache.openejb.junit.Module.class);
    for (final Map.Entry<Object, List<Method>> methods : moduleMethods.entrySet()) {
        moduleNumber += methods.getValue().size();
        for (final Method method : methods.getValue()) {
            final Object obj = method.invoke(methods.getKey());
            final Jars jarsAnnotation = method.getAnnotation(Jars.class);
            final Classes classesAnnotation = method.getAnnotation(Classes.class);
            final org.apache.openejb.junit.Classes classesAnnotationOld = method.getAnnotation(org.apache.openejb.junit.Classes.class);
            final boolean defaultConfig = method.getAnnotation(Default.class) != null;
            Class<?>[] classes = null;
            String[] excludes = null;
            Class<?>[] cdiInterceptors = null;
            Class<?>[] cdiAlternatives = null;
            Class<?>[] cdiStereotypes = null;
            Class<?>[] cdiDecorators = null;
            boolean cdi = false;
            boolean innerClassesAsBean = false;
            if (classesAnnotation != null) {
                classes = classesAnnotation.value();
                excludes = classesAnnotation.excludes();
                innerClassesAsBean = classesAnnotation.innerClassesAsBean();
                cdiInterceptors = classesAnnotation.cdiInterceptors();
                cdiDecorators = classesAnnotation.cdiDecorators();
                cdiAlternatives = classesAnnotation.cdiAlternatives();
                cdiStereotypes = classesAnnotation.cdiStereotypes();
                cdi = isCdi(classesAnnotation.cdi(), cdiInterceptors, cdiAlternatives, cdiStereotypes, cdiDecorators);
            } else if (classesAnnotationOld != null) {
                classes = classesAnnotationOld.value();
            }
            if (obj instanceof WebApp) {
                // will add the ejbmodule too
                final WebApp webApp = WebApp.class.cast(obj);
                if (webApp.getContextRoot() == null && classesAnnotation != null) {
                    webApp.contextRoot(classesAnnotation.context());
                }
                webModulesNb++;
                addWebApp(appModule, testBean, additionalDescriptors, method.getAnnotation(Descriptors.class), method.getAnnotation(JaxrsProviders.class), webApp, globalJarsAnnotation, jarsAnnotation, classes, excludes, cdiInterceptors, cdiAlternatives, cdiDecorators, cdiStereotypes, cdi, innerClassesAsBean, defaultConfig);
            } else if (obj instanceof WebModule) {
                // will add the ejbmodule too
                webModulesNb++;
                final WebModule webModule = (WebModule) obj;
                webModule.getAltDDs().putAll(additionalDescriptors);
                webModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                final EjbModule ejbModule = DeploymentLoader.addWebModule(webModule, appModule);
                ejbModule.getProperties().put(CdiScanner.OPENEJB_CDI_FILTER_CLASSLOADER, "false");
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                webModule.setFinder(finderFromClasses(webModule, classes, files, excludes));
                ejbModule.setFinder(webModule.getFinder());
            } else if (obj instanceof EjbModule) {
                final EjbModule ejbModule = (EjbModule) obj;
                ejbModule.getAltDDs().putAll(additionalDescriptors);
                ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                ejbModule.initAppModule(appModule);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof EjbJar) {
                final EjbJar ejbJar = (EjbJar) obj;
                setId(ejbJar, method);
                final EjbModule ejbModule = new EjbModule(ejbJar);
                ejbModule.getAltDDs().putAll(additionalDescriptors);
                ejbModule.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof EnterpriseBean) {
                final EnterpriseBean bean = (EnterpriseBean) obj;
                final EjbJar ejbJar = new EjbJar(method.getName());
                ejbJar.addEnterpriseBean(bean);
                final EjbModule ejbModule = new EjbModule(ejbJar);
                final Beans beans = new Beans();
                beans.addManagedClass(bean.getEjbClass());
                ejbModule.setBeans(beans);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(new Beans(), cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof Application) {
                application = (Application) obj;
                setId(application, method);
            } else if (obj instanceof Connector) {
                final Connector connector = (Connector) obj;
                setId(connector, method);
                appModule.getConnectorModules().add(new ConnectorModule(connector));
            } else if (obj instanceof Persistence) {
                final Persistence persistence = (Persistence) obj;
                appModule.addPersistenceModule(new PersistenceModule(appModule, implicitRootUrl(method.getAnnotation(PersistenceRootUrl.class)), persistence));
                notBusinessModuleNumber++;
            } else if (obj instanceof PersistenceUnit) {
                final PersistenceUnit unit = (PersistenceUnit) obj;
                appModule.addPersistenceModule(new PersistenceModule(appModule, implicitRootUrl(method.getAnnotation(PersistenceRootUrl.class)), new Persistence(unit)));
                notBusinessModuleNumber++;
            } else if (obj instanceof Beans) {
                final Beans beans = (Beans) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setBeans(beans);
                appModule.getEjbModules().add(ejbModule);
                if (cdi) {
                    ejbModule.setBeans(beans(beans, cdiDecorators, cdiInterceptors, cdiAlternatives, cdiStereotypes));
                }
                Collection<File> files = findFiles(jarsAnnotation);
                if (defaultConfig) {
                    (files == null ? files = new LinkedList<>() : files).add(jarLocation(testClass));
                }
                ejbModule.setFinder(finderFromClasses(ejbModule, classes, files, excludes));
            } else if (obj instanceof Class[]) {
                final Class[] beans = (Class[]) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder(new ClassesArchive(beans)).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof Class) {
                final Class bean = (Class) obj;
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder(new ClassesArchive(bean)).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof IAnnotationFinder) {
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder((IAnnotationFinder) obj);
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof ClassesArchive) {
                final EjbModule ejbModule = new EjbModule(new EjbJar(method.getName()));
                ejbModule.setFinder(new AnnotationFinder((Archive) obj).link());
                ejbModule.setBeans(new Beans());
                appModule.getEjbModules().add(ejbModule);
            } else if (obj instanceof Resources) {
                final Resources asResources = Resources.class.cast(obj);
                appModule.getResources().addAll(asResources.getResource());
                appModule.getContainers().addAll(asResources.getContainer());
                notBusinessModuleNumber++;
            } else if (obj instanceof AppModule) {
                // we can probably go further here
                final AppModule module = (AppModule) obj;
                module.getAltDDs().putAll(additionalDescriptors);
                module.getAltDDs().putAll(descriptorsToMap(method.getAnnotation(Descriptors.class)));
                if (module.getWebModules().size() > 0) {
                    webModulesNb++;
                }
                appModule.getEjbModules().addAll(module.getEjbModules());
                appModule.getPersistenceModules().addAll(module.getPersistenceModules());
                appModule.getAdditionalLibMbeans().addAll(module.getAdditionalLibMbeans());
                appModule.getWebModules().addAll(module.getWebModules());
                appModule.getConnectorModules().addAll(module.getConnectorModules());
                appModule.getResources().addAll(module.getResources());
                appModule.getServices().addAll(module.getServices());
                appModule.getPojoConfigurations().putAll(module.getPojoConfigurations());
                appModule.getAdditionalLibraries().addAll(module.getAdditionalLibraries());
                appModule.getAltDDs().putAll(module.getAltDDs());
                appModule.getProperties().putAll(module.getProperties());
            } else {
                moduleNumber--;
            }
        }
    }
    final Classes classClasses = testClass.getAnnotation(Classes.class);
    if (classClasses != null) {
        final WebApp webapp = new WebApp();
        webapp.setContextRoot(classClasses.context());
        addWebApp(appModule, testBean, additionalDescriptors, null, null, webapp, globalJarsAnnotation, null, classClasses.value(), classClasses.excludes(), classClasses.cdiInterceptors(), classClasses.cdiAlternatives(), classClasses.cdiDecorators(), classClasses.cdiStereotypes(), classClasses.cdi(), classClasses.innerClassesAsBean(), testClass.getAnnotation(Default.class) != null);
        webModulesNb++;
        moduleNumber++;
    }
    // Application is final in AppModule, which is fine, so we'll create a new one and move everything
    if (application != null) {
        final AppModule newModule = new AppModule(appModule.getClassLoader(), appModule.getModuleId(), application, false);
        newModule.getClientModules().addAll(appModule.getClientModules());
        newModule.addPersistenceModules(appModule.getPersistenceModules());
        newModule.getEjbModules().addAll(appModule.getEjbModules());
        newModule.getConnectorModules().addAll(appModule.getConnectorModules());
        appModule = newModule;
    }
    // config for the app
    for (final Map.Entry<Object, List<Method>> method : findAnnotatedMethods(new HashMap<Object, List<Method>>(), ApplicationConfiguration.class).entrySet()) {
        for (final Method m : method.getValue()) {
            final Object o = m.invoke(method.getKey());
            if (Properties.class.isInstance(o)) {
                appModule.getProperties().putAll(Properties.class.cast(o));
            }
        }
    }
    // copy ejb into beans if cdi is activated and init finder
    for (final EjbModule ejb : appModule.getEjbModules()) {
        final EnterpriseBean[] enterpriseBeans = ejb.getEjbJar().getEnterpriseBeans();
        final Beans beans = ejb.getBeans();
        if (beans != null && ejb.getEjbJar() != null) {
            for (final EnterpriseBean bean : enterpriseBeans) {
                boolean found = false;
                for (final List<String> mc : beans.getManagedClasses().values()) {
                    if (mc.contains(bean.getEjbClass())) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    beans.addManagedClass(bean.getEjbClass());
                }
            }
        }
    }
    if (moduleNumber - notBusinessModuleNumber == 1 && webModulesNb == 1) {
        appModule.setStandloneWebModule();
    }
    if (webModulesNb > 0 && SystemInstance.get().getComponent(WebAppBuilder.class) == null) {
        SystemInstance.get().setComponent(WebAppBuilder.class, new LightweightWebAppBuilder());
    }
    final Context jndiContext = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
    for (final EnvEntry entry : testBean.getEnvEntry()) {
        // set it in global jndi context since that's "app" entries and otherwise when we are no more in test bean context lookup fails
        final String name = entry.getName();
        final String jndi;
        if (name.startsWith("java:") || name.startsWith("comp/env")) {
            jndi = name;
        } else {
            jndi = "java:comp/env/" + name;
        }
        jndiContext.bind(jndi, entry.getEnvEntryValue());
    }
    appInfo = SystemInstance.get().getComponent(ConfigurationFactory.class).configureApplication(appModule);
    appContext = assembler.createApplication(appInfo);
    if (mockCdiContexts() && appContext.getWebBeansContext() != null) {
        ScopeHelper.startContexts(appContext.getWebBeansContext().getContextsService(), servletContext, session);
    }
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(testClass.getName());
    enrich(inputTestInstance, context);
    JavaSecurityManagers.setSystemProperty(Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    JavaSecurityManagers.setSystemProperty(OPENEJB_APPLICATION_COMPOSER_CONTEXT, appContext.getGlobalJndiContext());
    final List<Field> fields = new ArrayList<>(testClassFinder.findAnnotatedFields(AppResource.class));
    fields.addAll(testClassFinder.findAnnotatedFields(org.apache.openejb.junit.AppResource.class));
    for (final Field field : fields) {
        final Class<?> type = field.getType();
        if (AppModule.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, appModule);
        } else if (Context.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, new InitialContext(new Properties() {

                {
                    setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
                }
            }));
        } else if (ApplicationComposers.class.isAssignableFrom(type)) {
            field.setAccessible(true);
            field.set(inputTestInstance, this);
        } else if (ContextProvider.class.isAssignableFrom(type)) {
            RESTResourceFinder finder = SystemInstance.get().getComponent(RESTResourceFinder.class);
            if (finder == null || !ContextProvider.class.isInstance(finder)) {
                finder = new ContextProvider(finder);
                SystemInstance.get().setComponent(RESTResourceFinder.class, finder);
            }
            field.setAccessible(true);
            field.set(inputTestInstance, finder);
        } else {
            throw new IllegalArgumentException("can't find value for type " + type.getName());
        }
    }
    previous = ThreadContext.enter(new ThreadContext(context, null, Operation.BUSINESS));
    // switch back since next test will use another instance
    testClassFinders.put(this, testClassFinder);
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) AppModule(org.apache.openejb.config.AppModule) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) HashMap(java.util.HashMap) EjbModule(org.apache.openejb.config.EjbModule) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) RESTResourceFinder(org.apache.openejb.rest.RESTResourceFinder) Method(java.lang.reflect.Method) WebModule(org.apache.openejb.config.WebModule) ContextProvider(org.apache.openejb.testing.rest.ContextProvider) LinkedList(java.util.LinkedList) Persistence(org.apache.openejb.jee.jpa.unit.Persistence) BeanContext(org.apache.openejb.BeanContext) Collection(java.util.Collection) EjbDeployment(org.apache.openejb.jee.oejb3.EjbDeployment) Resources(org.apache.openejb.config.sys.Resources) ManagedBean(org.apache.openejb.jee.ManagedBean) Application(org.apache.openejb.jee.Application) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) Connector(org.apache.openejb.jee.Connector) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) Properties(java.util.Properties) URL(java.net.URL) ConnectorModule(org.apache.openejb.config.ConnectorModule) Field(java.lang.reflect.Field) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) PersistenceUnit(org.apache.openejb.jee.jpa.unit.PersistenceUnit) ClassFinder(org.apache.xbean.finder.ClassFinder) EjbJar(org.apache.openejb.jee.EjbJar) EnvEntry(org.apache.openejb.jee.EnvEntry) WebContext(org.apache.openejb.core.WebContext) InitialContext(javax.naming.InitialContext) MockServletContext(org.apache.webbeans.web.lifecycle.test.MockServletContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.naming.Context) ThreadContext(org.apache.openejb.core.ThreadContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) FinderFactory(org.apache.openejb.config.FinderFactory) PersistenceModule(org.apache.openejb.config.PersistenceModule) InitialContext(javax.naming.InitialContext) Beans(org.apache.openejb.jee.Beans) LightweightWebAppBuilder(org.apache.openejb.web.LightweightWebAppBuilder) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) WebApp(org.apache.openejb.jee.WebApp)

Example 4 with ManagedBean

use of org.apache.openejb.jee.ManagedBean in project tomee by apache.

the class OutputGeneratedDescriptorsTest method testOutputDescriptor.

@Test
public void testOutputDescriptor() throws Exception {
    final OutputGeneratedDescriptors dynamicDeployer = new OutputGeneratedDescriptors();
    final EjbJar ejbJar = new EjbJar();
    final StatelessBean redBean = new StatelessBean();
    redBean.setEjbClass("com.foo.Red");
    redBean.setEjbName("Red");
    redBean.setRemote("com.foo.Color");
    final ManagedBean orangeBean = new ManagedBean("Orange", "com.foo.Orange");
    final StatefulBean yellowBean = new StatefulBean();
    yellowBean.setEjbClass("com.foo.Yellow");
    yellowBean.setEjbName("Yellow");
    yellowBean.setRemote("com.foo.Color");
    final SingletonBean greenBean = new SingletonBean();
    greenBean.setEjbClass("com.foo.Green");
    greenBean.setEjbName("Green");
    greenBean.setRemote("com.foo.Color");
    ejbJar.addEnterpriseBean(redBean);
    ejbJar.addEnterpriseBean(orangeBean);
    ejbJar.addEnterpriseBean(yellowBean);
    ejbJar.addEnterpriseBean(greenBean);
    final OpenejbJar openejbJar = new OpenejbJar();
    final EjbModule ejbModule = new EjbModule(ejbJar, openejbJar);
    final AppModule appModule = new AppModule(ejbModule);
    File tempFolder = File.createTempFile("tmp", "ogd");
    tempFolder.delete();
    Assert.assertTrue("unable to create temp folder", tempFolder.mkdirs());
    try {
        Properties properties = ejbModule.getOpenejbJar().getProperties();
        properties.setProperty(OutputGeneratedDescriptors.OUTPUT_DESCRIPTORS, "true");
        properties.setProperty(OutputGeneratedDescriptors.OUTPUT_DESCRIPTORS_FOLDER, tempFolder.getAbsolutePath());
        SystemInstance.get().setProperty(OutputGeneratedDescriptors.OUTPUT_DESCRIPTORS_FOLDER, tempFolder.getAbsolutePath());
        dynamicDeployer.deploy(appModule);
        boolean seenEjbJarXml = false;
        boolean seenOpenejbJarXml = false;
        File[] listFiles = tempFolder.listFiles();
        for (File file : listFiles) {
            if (file.getName().startsWith("ejb-jar-")) {
                seenEjbJarXml = true;
                assertEjbFileCorrect(file);
            }
            if (file.getName().startsWith("openejb-jar-")) {
                seenOpenejbJarXml = true;
            }
        }
        Assert.assertTrue("No ejb-jar.xml file produced", seenEjbJarXml);
        Assert.assertTrue("No openejb-jar.xml file produced", seenOpenejbJarXml);
    } finally {
        // clean up temporary folder
        Files.delete(tempFolder);
    }
}
Also used : StatefulBean(org.apache.openejb.jee.StatefulBean) Properties(java.util.Properties) SingletonBean(org.apache.openejb.jee.SingletonBean) OpenejbJar(org.apache.openejb.jee.oejb3.OpenejbJar) StatelessBean(org.apache.openejb.jee.StatelessBean) ManagedBean(org.apache.openejb.jee.ManagedBean) File(java.io.File) EjbJar(org.apache.openejb.jee.EjbJar) Test(org.junit.Test)

Example 5 with ManagedBean

use of org.apache.openejb.jee.ManagedBean in project tomee by apache.

the class CheckAnnotationTest method testWebServiceWithManagedBean.

@Keys({ @Key(value = "annotation.invalid.managedbean.webservice", type = KeyType.WARNING) })
public AppModule testWebServiceWithManagedBean() {
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new ManagedBean(Red.class));
    final EjbModule ejbModule = new EjbModule(ejbJar);
    ejbModule.setFinder(new AnnotationFinder(new ClassesArchive(Red.class)).link());
    final AppModule appModule = new AppModule(ejbModule);
    return appModule;
}
Also used : Red(org.apache.openejb.test.annotated.Red) AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) ManagedBean(org.apache.openejb.jee.ManagedBean) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

ManagedBean (org.apache.openejb.jee.ManagedBean)9 EjbJar (org.apache.openejb.jee.EjbJar)7 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)6 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)6 EjbModule (org.apache.openejb.config.EjbModule)5 File (java.io.File)3 Beans (org.apache.openejb.jee.Beans)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 AppModule (org.apache.openejb.config.AppModule)2 ConnectorModule (org.apache.openejb.config.ConnectorModule)2 WebModule (org.apache.openejb.config.WebModule)2 StatefulBean (org.apache.openejb.jee.StatefulBean)2 AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)2 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1