Search in sources :

Example 1 with EjbModule

use of org.apache.openejb.config.EjbModule in project aries by apache.

the class EJBExtender method startEJBs.

private void startEJBs(final Bundle bundle) {
    try {
        //If there is another thread adding or removing then stop here
        Object o = processingMap.put(bundle, PROCESSING_OBJECT);
        if (o == REMOVING_OBJECT || o == PROCESSING_OBJECT) {
            return;
        }
        //If already running then avoid
        if (runningApps.get(bundle) != null)
            return;
        //Broken validation for persistence :(
        EjbModule ejbModule = new EjbModule(AriesFrameworkUtil.getClassLoaderForced(bundle), null, null, null);
        try {
            Field f = EjbModule.class.getDeclaredField("validation");
            f.setAccessible(true);
            f.set(ejbModule, new ValidationProofValidationContext(ejbModule));
        } catch (Exception e) {
        // Hmmm
        }
        addAltDDs(ejbModule, bundle);
        //We build our own because we can't trust anyone to get the classpath right otherwise!
        ejbModule.setFinder(new OSGiFinder(bundle));
        ConfigurationFactory configurationFactory = new ConfigurationFactory();
        EjbJarInfo ejbInfo = null;
        //Avoid yet another ClassLoading problem
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(new ClassLoader(OpenEjbVersion.class.getClassLoader()) {

                protected Class<?> findClass(String name) throws ClassNotFoundException {
                    for (Bundle b : bundle.getBundleContext().getBundles()) {
                        if (b.getSymbolicName().contains("jaxb-impl"))
                            return b.loadClass(name);
                    }
                    throw new ClassNotFoundException(name);
                }
            });
            ejbInfo = configurationFactory.configureApplication(ejbModule);
            //Another oddity here
            ejbInfo.validationInfo = null;
        } finally {
            Thread.currentThread().setContextClassLoader(cl);
        }
        processJPAMappings(ejbInfo);
        Assembler assembler = (Assembler) SystemInstance.get().getComponent(Assembler.class);
        RunningApplication app = null;
        try {
            SystemInstance.get().setProperty("openejb.geronimo", "true");
            cl = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(OpenEjbVersion.class.getClassLoader());
                app = new RunningApplication(assembler.createApplication(ejbInfo, new AppClassLoader(ejbModule.getClassLoader())), bundle, ejbInfo.enterpriseBeans);
            } finally {
                Thread.currentThread().setContextClassLoader(cl);
            }
        } finally {
            SystemInstance.get().getProperties().remove("openejb.geronimo");
        }
        runningApps.put(bundle, app);
        app.init();
    } catch (OpenEJBException oee) {
        // TODO Auto-generated catch block
        oee.printStackTrace();
    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (processingMap.remove(bundle) == REMOVING_OBJECT) {
            stopEJBs(bundle);
        }
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) OpenEjbVersion(org.apache.openejb.util.OpenEjbVersion) Bundle(org.osgi.framework.Bundle) EjbModule(org.apache.openejb.config.EjbModule) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) IOException(java.io.IOException) Field(java.lang.reflect.Field) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) NamingException(javax.naming.NamingException) Assembler(org.apache.openejb.assembler.classic.Assembler) EjbJarInfo(org.apache.openejb.assembler.classic.EjbJarInfo)

Example 2 with EjbModule

use of org.apache.openejb.config.EjbModule 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 3 with EjbModule

use of org.apache.openejb.config.EjbModule in project tomee by apache.

the class CdiDecoratorMultipleDelegateCallsTest method classes.

@Module
@Classes({ ServiceImpl.class })
public EjbModule classes() {
    final Beans beans = new Beans();
    beans.addDecorator(ServiceDecorator.class);
    final EjbModule jar = new EjbModule(new EjbJar());
    jar.setBeans(beans);
    return jar;
}
Also used : Beans(org.apache.openejb.jee.Beans) EjbModule(org.apache.openejb.config.EjbModule) EjbJar(org.apache.openejb.jee.EjbJar) EjbModule(org.apache.openejb.config.EjbModule) Module(org.apache.openejb.testing.Module) Classes(org.apache.openejb.testing.Classes)

Example 4 with EjbModule

use of org.apache.openejb.config.EjbModule in project tomee by apache.

the class CdiDecoratorTest method setUp.

@Before
public void setUp() throws Exception {
    final ConfigurationFactory config = new ConfigurationFactory();
    final Assembler assembler = new Assembler();
    assembler.createProxyFactory(config.configureService(ProxyFactoryInfo.class));
    assembler.createTransactionManager(config.configureService(TransactionServiceInfo.class));
    assembler.createSecurityService(config.configureService(SecurityServiceInfo.class));
    assembler.createContainer(config.configureService(StatelessSessionContainerInfo.class));
    final EjbJar ejbJar = new EjbJar();
    ejbJar.addEnterpriseBean(new StatelessBean("HelloOne", RedBean.class));
    ejbJar.addEnterpriseBean(new StatelessBean("HelloTwo", RedBean.class));
    ejbJar.addEnterpriseBean(new StatelessBean(OrangeBean.class));
    final Beans beans = new Beans();
    beans.addInterceptor(OrangeCdiInterceptor.class);
    beans.addDecorator(OrangeOneDecorator.class);
    beans.addDecorator(OrangeTwoDecorator.class);
    beans.addManagedClass(YellowBean.class);
    final EjbModule module = new EjbModule(ejbJar);
    module.setBeans(beans);
    assembler.createApplication(config.configureApplication(module));
    final Properties properties = new Properties(System.getProperties());
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, InitContextFactory.class.getName());
    ctx = new InitialContext(properties);
}
Also used : StatelessSessionContainerInfo(org.apache.openejb.assembler.classic.StatelessSessionContainerInfo) EjbModule(org.apache.openejb.config.EjbModule) InitContextFactory(org.apache.openejb.core.ivm.naming.InitContextFactory) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext) ProxyFactoryInfo(org.apache.openejb.assembler.classic.ProxyFactoryInfo) Beans(org.apache.openejb.jee.Beans) TransactionServiceInfo(org.apache.openejb.assembler.classic.TransactionServiceInfo) StatelessBean(org.apache.openejb.jee.StatelessBean) ConfigurationFactory(org.apache.openejb.config.ConfigurationFactory) Assembler(org.apache.openejb.assembler.classic.Assembler) SecurityServiceInfo(org.apache.openejb.assembler.classic.SecurityServiceInfo) EjbJar(org.apache.openejb.jee.EjbJar) Before(org.junit.Before)

Example 5 with EjbModule

use of org.apache.openejb.config.EjbModule in project tomee by apache.

the class EjbRefTest method ear.

private void ear(final EjbJar... ejbJars) throws OpenEJBException, NamingException, IOException {
    final AppModule app = new AppModule(this.getClass().getClassLoader(), "classpath-" + ejbJars.hashCode());
    for (final EjbJar ejbJar : ejbJars) {
        app.getEjbModules().add(new EjbModule(ejbJar));
    }
    assembler.createApplication(config.configureApplication(app));
}
Also used : AppModule(org.apache.openejb.config.AppModule) EjbModule(org.apache.openejb.config.EjbModule) EjbJar(org.apache.openejb.jee.EjbJar)

Aggregations

EjbModule (org.apache.openejb.config.EjbModule)88 EjbJar (org.apache.openejb.jee.EjbJar)78 AppModule (org.apache.openejb.config.AppModule)40 StatelessBean (org.apache.openejb.jee.StatelessBean)37 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)29 OpenejbJar (org.apache.openejb.jee.oejb3.OpenejbJar)29 Assembler (org.apache.openejb.assembler.classic.Assembler)26 Properties (java.util.Properties)23 SingletonBean (org.apache.openejb.jee.SingletonBean)21 InitialContext (javax.naming.InitialContext)19 Module (org.apache.openejb.testing.Module)16 EjbDeployment (org.apache.openejb.jee.oejb3.EjbDeployment)15 SecurityServiceInfo (org.apache.openejb.assembler.classic.SecurityServiceInfo)12 TransactionServiceInfo (org.apache.openejb.assembler.classic.TransactionServiceInfo)12 Beans (org.apache.openejb.jee.Beans)12 AppInfo (org.apache.openejb.assembler.classic.AppInfo)10 ContainerSystem (org.apache.openejb.spi.ContainerSystem)10 ArrayList (java.util.ArrayList)9 InitContextFactory (org.apache.openejb.core.ivm.naming.InitContextFactory)9 EnterpriseBean (org.apache.openejb.jee.EnterpriseBean)9