Search in sources :

Example 1 with ClassesArchive

use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.

the class Assembler method postConstructResources.

private void postConstructResources(final Set<String> resourceIds, final ClassLoader classLoader, final Context containerSystemContext, final AppContext appContext) throws NamingException, OpenEJBException {
    final Thread thread = Thread.currentThread();
    final ClassLoader oldCl = thread.getContextClassLoader();
    try {
        thread.setContextClassLoader(classLoader);
        final List<ResourceInfo> resourceList = config.facilities.resources;
        for (final ResourceInfo resourceInfo : resourceList) {
            if (!resourceIds.contains(resourceInfo.id)) {
                continue;
            }
            if (isTemplatizedResource(resourceInfo)) {
                continue;
            }
            try {
                Class<?> clazz;
                try {
                    clazz = classLoader.loadClass(resourceInfo.className);
                } catch (final ClassNotFoundException cnfe) {
                    // custom classpath
                    clazz = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id).getClass();
                }
                final boolean initialize = "true".equalsIgnoreCase(String.valueOf(resourceInfo.properties.remove("InitializeAfterDeployment")));
                final AnnotationFinder finder = Proxy.isProxyClass(clazz) ? null : new AnnotationFinder(new ClassesArchive(ancestors(clazz)));
                final List<Method> postConstructs = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PostConstruct.class);
                final List<Method> preDestroys = finder == null ? Collections.<Method>emptyList() : finder.findAnnotatedMethods(PreDestroy.class);
                resourceInfo.postConstructMethods = new ArrayList<>();
                resourceInfo.preDestroyMethods = new ArrayList<>();
                addMethodsToResourceInfo(resourceInfo.postConstructMethods, PostConstruct.class, postConstructs);
                addMethodsToResourceInfo(resourceInfo.preDestroyMethods, PreDestroy.class, preDestroys);
                CreationalContext<?> creationalContext = null;
                Object originalResource = null;
                if (!postConstructs.isEmpty() || initialize) {
                    originalResource = containerSystemContext.lookup(OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id);
                    Object resource = originalResource;
                    if (resource instanceof Reference) {
                        resource = unwrapReference(resource);
                        this.bindResource(resourceInfo.id, resource, true);
                    }
                    try {
                        // wire up CDI
                        if (appContext != null && appContext.getWebBeansContext() != null) {
                            final BeanManagerImpl beanManager = appContext.getWebBeansContext().getBeanManagerImpl();
                            if (beanManager.isInUse()) {
                                creationalContext = beanManager.createCreationalContext(null);
                                OWBInjector.inject(beanManager, resource, creationalContext);
                            }
                        }
                        if (!"none".equals(resourceInfo.postConstruct)) {
                            if (resourceInfo.postConstruct != null) {
                                final Method p = clazz.getDeclaredMethod(resourceInfo.postConstruct);
                                if (!p.isAccessible()) {
                                    SetAccessible.on(p);
                                }
                                p.invoke(resource);
                            }
                            for (final Method m : postConstructs) {
                                if (!m.isAccessible()) {
                                    SetAccessible.on(m);
                                }
                                m.invoke(resource);
                            }
                        }
                    } catch (final Exception e) {
                        logger.fatal("Error calling @PostConstruct method on " + resource.getClass().getName());
                        throw new OpenEJBException(e);
                    }
                }
                if (!"none".equals(resourceInfo.preDestroy)) {
                    if (resourceInfo.preDestroy != null) {
                        final Method p = clazz.getDeclaredMethod(resourceInfo.preDestroy);
                        if (!p.isAccessible()) {
                            SetAccessible.on(p);
                        }
                        preDestroys.add(p);
                    }
                    if (!preDestroys.isEmpty() || creationalContext != null) {
                        final String name = OPENEJB_RESOURCE_JNDI_PREFIX + resourceInfo.id;
                        if (originalResource == null) {
                            originalResource = containerSystemContext.lookup(name);
                        }
                        this.bindResource(resourceInfo.id, new ResourceInstance(name, originalResource, preDestroys, creationalContext), true);
                    }
                }
                // log unused now for these resources now we built the resource completely and @PostConstruct can have used injected properties
                if (resourceInfo.unsetProperties != null && !isPassthroughType(resourceInfo)) {
                    final Set<String> unsetKeys = resourceInfo.unsetProperties.stringPropertyNames();
                    for (final String key : unsetKeys) {
                        // don't use keySet to auto filter txMgr for instance and not real properties!
                        unusedProperty(resourceInfo.id, logger, key);
                    }
                }
            } catch (final Exception e) {
                logger.fatal("Error calling PostConstruct method on " + resourceInfo.id);
                logger.fatal("Resource " + resourceInfo.id + " could not be initialized. Application will be undeployed.");
                throw new OpenEJBException(e);
            }
        }
    } finally {
        thread.setContextClassLoader(oldCl);
    }
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) ConnectorReference(org.apache.openejb.core.ConnectorReference) ContextualJndiReference(org.apache.openejb.core.ivm.naming.ContextualJndiReference) JndiUrlReference(org.apache.openejb.core.ivm.naming.JndiUrlReference) Reference(org.apache.openejb.core.ivm.naming.Reference) ResourceReference(org.apache.webbeans.spi.api.ResourceReference) LazyObjectReference(org.apache.openejb.core.ivm.naming.LazyObjectReference) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) InvalidObjectException(java.io.InvalidObjectException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ObjectStreamException(java.io.ObjectStreamException) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException) URISyntaxException(java.net.URISyntaxException) UndeployException(org.apache.openejb.UndeployException) DefinitionException(javax.enterprise.inject.spi.DefinitionException) ConstructionException(org.apache.xbean.recipe.ConstructionException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) ValidationException(javax.validation.ValidationException) MalformedObjectNameException(javax.management.MalformedObjectNameException) DuplicateDeploymentIdException(org.apache.openejb.DuplicateDeploymentIdException) TimeoutException(java.util.concurrent.TimeoutException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) DeploymentException(javax.enterprise.inject.spi.DeploymentException) NoSuchApplicationException(org.apache.openejb.NoSuchApplicationException) MalformedURLException(java.net.MalformedURLException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) BeanManagerImpl(org.apache.webbeans.container.BeanManagerImpl) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) ContainerSystemPreDestroy(org.apache.openejb.assembler.classic.event.ContainerSystemPreDestroy) PreDestroy(javax.annotation.PreDestroy) PostConstruct(javax.annotation.PostConstruct) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

Example 2 with ClassesArchive

use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.

the class ApplicationComposerDeployer method configureClasses.

private void configureClasses(final WebModule web, final EjbModule ejbModule, final ApplicationComposer applicationComposer, final Classes classes) {
    ejbModule.getEjbJar().setMetadataComplete(applicationComposer.metadataComplete());
    final Collection<Archive> archives = new LinkedList<>();
    if (classes.value().length > 0) {
        archives.add(new ClassesArchive(classes.value()));
    }
    if (classes.cdi()) {
        final Beans beans = new Beans();
        for (final Class<?> c : classes.cdiAlternatives()) {
            beans.addAlternativeClass(c);
        }
        for (final Class<?> c : classes.cdiDecorators()) {
            beans.addDecorator(c);
        }
        for (final Class<?> c : classes.cdiInterceptors()) {
            beans.addInterceptor(c);
        }
        ejbModule.setBeans(beans);
        if (applicationComposer.metadataComplete()) {
            for (final Class<?> c : classes.value()) {
                beans.addManagedClass(null, c.getName());
            }
            final String name = BeanContext.Comp.openejbCompName(web.getModuleId());
            final org.apache.openejb.jee.ManagedBean managedBean = new CompManagedBean(name, BeanContext.Comp.class);
            managedBean.setTransactionType(TransactionType.BEAN);
            ejbModule.getEjbJar().addEnterpriseBean(managedBean);
        }
    }
    final CompositeArchive archive = new CompositeArchive(archives);
    final Archive finalArchive = classes.excludes().length > 0 ? new FilteredArchive(archive, Filters.invert(Filters.prefixes(classes.excludes()))) : archive;
    ejbModule.setFinder(new FinderFactory.OpenEJBAnnotationFinder(finalArchive).link());
    web.setFinder(ejbModule.getFinder());
    web.getWebApp().setMetadataComplete(ejbModule.getEjbJar().isMetadataComplete());
}
Also used : FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) LinkedList(java.util.LinkedList) BeanContext(org.apache.openejb.BeanContext) Beans(org.apache.openejb.jee.Beans) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive)

Example 3 with ClassesArchive

use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.

the class ApplicationComposerDeployer method deploy.

@Override
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    if (!appModule.isStandaloneModule()) {
        return appModule;
    }
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        if (ejbModule.getFinder() == null) {
            continue;
        }
        WebModule webModule = null;
        for (final WebModule web : appModule.getWebModules()) {
            if (!web.getModuleId().equals(ejbModule.getModuleId())) {
                continue;
            }
            webModule = web;
            break;
        }
        if (webModule == null) {
            continue;
        }
        for (final Class<?> clazz : ejbModule.getFinder().findAnnotatedClasses(ApplicationComposer.class)) {
            final ApplicationComposer applicationComposer = clazz.getAnnotation(ApplicationComposer.class);
            final Descriptor descriptor = clazz.getAnnotation(Descriptor.class);
            if (descriptor != null) {
                configureDescriptor(appModule, descriptor);
            }
            final Descriptors descriptors = clazz.getAnnotation(Descriptors.class);
            if (descriptors != null) {
                for (final Descriptor d : descriptors.value()) {
                    configureDescriptor(appModule, descriptor);
                }
            }
            final Classes classes = clazz.getAnnotation(Classes.class);
            if (classes != null) {
                configureClasses(webModule, ejbModule, applicationComposer, classes);
            }
            Object instance = null;
            final AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(org.apache.openejb.util.Classes.ancestors(clazz)));
            for (final Method m : finder.findAnnotatedMethods(org.apache.openejb.testing.Module.class)) {
                instance = configureModule(appModule, ejbModule, clazz, instance, m);
            }
            for (final Method m : finder.findAnnotatedMethods(Configuration.class)) {
                instance = configureConfiguration(appModule, clazz, instance, m);
            }
            final JaxrsProviders jaxrsProviders = clazz.getAnnotation(JaxrsProviders.class);
            if (jaxrsProviders != null) {
                for (final Class<?> c : jaxrsProviders.value()) {
                    webModule.getJaxrsProviders().add(c.getName());
                }
            }
        }
    }
    return appModule;
}
Also used : Method(java.lang.reflect.Method) JaxrsProviders(org.apache.openejb.testing.JaxrsProviders) ApplicationComposer(org.apache.openejb.api.configuration.ApplicationComposer) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) Descriptor(org.apache.openejb.testing.Descriptor) Descriptors(org.apache.openejb.testing.Descriptors) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) Classes(org.apache.openejb.testing.Classes)

Example 4 with ClassesArchive

use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.

the class FinderFactory method fallbackAnnotationFinder.

private OpenEJBAnnotationFinder fallbackAnnotationFinder(DeploymentModule module) {
    final OpenEJBAnnotationFinder finder = new OpenEJBAnnotationFinder(new ClassesArchive(ensureMinimalClasses(module)));
    finder.enableMetaAnnotations();
    return finder;
}
Also used : ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive)

Example 5 with ClassesArchive

use of org.apache.xbean.finder.archive.ClassesArchive in project tomee by apache.

the class AnnotationDeployerTest method findRestClasses.

@Test
public void findRestClasses() throws Exception {
    final WebApp webApp = new WebApp();
    webApp.setContextRoot("/");
    webApp.setId("web");
    webApp.setVersion("2.5");
    WebModule webModule = new WebModule(webApp, webApp.getContextRoot(), Thread.currentThread().getContextClassLoader(), "myapp", webApp.getId());
    webModule.setFinder(new AnnotationFinder(new ClassesArchive(RESTClass.class, RESTMethod.class, RESTApp.class)).link());
    final AnnotationDeployer annotationDeployer = new AnnotationDeployer();
    webModule = annotationDeployer.deploy(webModule);
    final Set<String> classes = webModule.getRestClasses();
    final Set<String> applications = webModule.getRestApplications();
    assertEquals(1, classes.size());
    assertTrue(classes.contains(RESTClass.class.getName()));
    // assertTrue(classes.contains(RESTMethod.class.getName()));
    assertEquals(1, applications.size());
    assertEquals(RESTApp.class.getName(), applications.iterator().next());
}
Also used : ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) WebApp(org.apache.openejb.jee.WebApp) Test(org.junit.Test)

Aggregations

ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)33 Test (org.junit.Test)14 AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)12 NotAnnotated (org.acme.NotAnnotated)10 ClassAnnotatedClass (org.acme.ClassAnnotatedClass)8 Method (java.lang.reflect.Method)7 URL (java.net.URL)7 CompositeArchive (org.apache.xbean.finder.archive.CompositeArchive)7 FilteredArchive (org.apache.xbean.finder.archive.FilteredArchive)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 EjbModule (org.apache.openejb.config.EjbModule)6 EjbJar (org.apache.openejb.jee.EjbJar)6 Archive (org.apache.xbean.finder.archive.Archive)6 MalformedURLException (java.net.MalformedURLException)5 AppModule (org.apache.openejb.config.AppModule)5 File (java.io.File)4 Field (java.lang.reflect.Field)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4