Search in sources :

Example 21 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class CheckClasses method validate.

public void validate(final EjbModule ejbModule) {
    final ClassLoader loader = ejbModule.getClassLoader();
    for (final EnterpriseBean bean : ejbModule.getEjbJar().getEnterpriseBeans()) {
        try {
            final Class<?> beanClass = check_hasEjbClass(loader, bean);
            // All the subsequent checks require the bean class
            if (beanClass == null) {
                continue;
            }
            if (!(bean instanceof RemoteBean)) {
                continue;
            }
            if (bean instanceof SessionBean && ((SessionBean) bean).getProxy() != null) {
                continue;
            }
            final RemoteBean b = (RemoteBean) bean;
            check_isEjbClass(b);
            check_hasDependentClasses(b, b.getEjbClass(), "ejb-class");
            check_hasInterface(b);
            if (b.getRemote() != null) {
                checkInterface(loader, b, beanClass, "remote", b.getRemote());
            }
            if (b.getHome() != null) {
                checkInterface(loader, b, beanClass, "home", b.getHome());
            }
            if (b.getLocal() != null) {
                checkInterface(loader, b, beanClass, "local", b.getLocal());
            }
            if (b.getLocalHome() != null) {
                checkInterface(loader, b, beanClass, "local-home", b.getLocalHome());
            }
            if (b instanceof SessionBean) {
                final SessionBean sessionBean = (SessionBean) b;
                for (final String interfce : sessionBean.getBusinessLocal()) {
                    checkInterface(loader, b, beanClass, "business-local", interfce);
                }
                for (final String interfce : sessionBean.getBusinessRemote()) {
                    checkInterface(loader, b, beanClass, "business-remote", interfce);
                }
            }
        } catch (final RuntimeException e) {
            throw new OpenEJBRuntimeException(bean.getEjbName(), e);
        }
    }
    for (final Interceptor interceptor : ejbModule.getEjbJar().getInterceptors()) {
        check_hasInterceptorClass(loader, interceptor);
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) EnterpriseBean(org.apache.openejb.jee.EnterpriseBean) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) RemoteBean(org.apache.openejb.jee.RemoteBean) SessionBean(org.apache.openejb.jee.SessionBean) Interceptor(org.apache.openejb.jee.Interceptor)

Example 22 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class LocalInitialContext method logout.

@SuppressWarnings("unchecked")
private void logout() {
    try {
        final SecurityService securityService = SystemInstance.get().getComponent(SecurityService.class);
        if (clientIdentity != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("Logging out: " + clientIdentity);
            }
            securityService.logout(clientIdentity);
            ClientSecurity.setIdentity(null);
        }
    } catch (final LoginException e) {
        throw new OpenEJBRuntimeException("User could not be logged out.", e);
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) SecurityService(org.apache.openejb.spi.SecurityService) LoginException(javax.security.auth.login.LoginException)

Example 23 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class ArquillianUtil method addClass.

private static void addClass(final Collection<Archive<?>> list, final ClassLoader loader, final String classname) {
    final String name = classname.trim();
    try {
        final Class<?> clazz = loader.loadClass(name);
        for (final Method m : clazz.getMethods()) {
            final int modifiers = m.getModifiers();
            if (Object.class.equals(m.getDeclaringClass()) || !Archive.class.isAssignableFrom(m.getReturnType()) || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
                continue;
            }
            for (final Annotation a : m.getAnnotations()) {
                if ("org.jboss.arquillian.container.test.api.Deployment".equals(a.annotationType().getName())) {
                    final Archive<?> archive = (Archive<?>) m.invoke(null);
                    list.add(archive);
                    break;
                }
            }
        }
    } catch (final Exception e) {
        throw new OpenEJBRuntimeException(e);
    }
}
Also used : OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) Archive(org.jboss.shrinkwrap.api.Archive) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException)

Example 24 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException in project tomee by apache.

the class OpenEJBEnricher method enrich.

public static void enrich(final Object testInstance, final AppContext appCtx) {
    // don't rely on arquillian since this enrichment should absolutely be done before the following ones
    new MockitoEnricher().enrich(testInstance);
    AppContext ctx = appCtx;
    if (ctx == null) {
        ctx = AppFinder.findAppContextOrWeb(Thread.currentThread().getContextClassLoader(), AppFinder.AppContextTransformer.INSTANCE);
        if (ctx == null) {
            return;
        }
    }
    final BeanContext context = SystemInstance.get().getComponent(ContainerSystem.class).getBeanContext(ctx.getId() + "_" + testInstance.getClass().getName());
    final WebBeansContext appWBC = ctx.getWebBeansContext();
    final BeanManagerImpl bm = appWBC == null ? null : appWBC.getBeanManagerImpl();
    boolean ok = false;
    for (final WebContext web : ctx.getWebContexts()) {
        final WebBeansContext webBeansContext = web.getWebBeansContext();
        if (webBeansContext == null) {
            continue;
        }
        final BeanManagerImpl webAppBm = webBeansContext.getBeanManagerImpl();
        if (webBeansContext != appWBC && webAppBm.isInUse()) {
            try {
                doInject(testInstance, context, webAppBm);
                ok = true;
                break;
            } catch (final Exception e) {
            // no-op, try next
            }
        }
    }
    if (bm != null && bm.isInUse() && !ok) {
        try {
            doInject(testInstance, context, bm);
        } catch (final Exception e) {
            LOGGER.log(Level.SEVERE, "Failed injection on: " + testInstance.getClass(), e);
            if (RuntimeException.class.isInstance(e)) {
                throw RuntimeException.class.cast(e);
            }
            throw new OpenEJBRuntimeException(e);
        }
    }
    if (context != null) {
        final ThreadContext callContext = new ThreadContext(context, null, Operation.INJECTION);
        final ThreadContext oldContext = ThreadContext.enter(callContext);
        try {
            final InjectionProcessor processor = new InjectionProcessor<>(testInstance, context.getInjections(), context.getJndiContext());
            processor.createInstance();
        } catch (final OpenEJBException e) {
        // ignored
        } finally {
            ThreadContext.exit(oldContext);
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) OpenEJBException(org.apache.openejb.OpenEJBException) WebContext(org.apache.openejb.core.WebContext) AppContext(org.apache.openejb.AppContext) ThreadContext(org.apache.openejb.core.ThreadContext) InjectionProcessor(org.apache.openejb.InjectionProcessor) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanContext(org.apache.openejb.BeanContext) WebBeansContext(org.apache.webbeans.config.WebBeansContext) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) MockitoEnricher(org.apache.openejb.arquillian.common.mockito.MockitoEnricher)

Example 25 with OpenEJBRuntimeException

use of org.apache.openejb.OpenEJBRuntimeException 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)

Aggregations

OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)55 IOException (java.io.IOException)20 OpenEJBException (org.apache.openejb.OpenEJBException)17 ArrayList (java.util.ArrayList)13 Properties (java.util.Properties)12 NamingException (javax.naming.NamingException)9 MalformedURLException (java.net.MalformedURLException)8 BeanContext (org.apache.openejb.BeanContext)8 ObjectStreamException (java.io.ObjectStreamException)6 URISyntaxException (java.net.URISyntaxException)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 File (java.io.File)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Method (java.lang.reflect.Method)4 URL (java.net.URL)4 ApplicationException (org.apache.openejb.ApplicationException)4 InvalidObjectException (java.io.InvalidObjectException)3 AccessException (java.rmi.AccessException)3 RemoteException (java.rmi.RemoteException)3