Search in sources :

Example 11 with AnnotationFinder

use of org.apache.xbean.finder.AnnotationFinder in project tomee by apache.

the class TomEEEmbeddedApplicationRunner method ensureAppInit.

// if app is not set then we'll check if -Dtomee.application-composer.application is set otherwise
// we'll try to find a single @Application class in the jar containing marker (case for tests).
private void ensureAppInit(final Class<?> marker) {
    if (app != null) {
        return;
    }
    final Class<?> type;
    final String typeStr = System.getProperty("tomee.application-composer.application");
    if (typeStr != null) {
        try {
            type = Thread.currentThread().getContextClassLoader().loadClass(typeStr);
        } catch (final ClassNotFoundException e) {
            throw new IllegalArgumentException(e);
        }
    } else if (marker == null) {
        throw new IllegalArgumentException("set tomee.application-composer.application system property or add a marker to the rule or runner");
    } else {
        final Iterator<Class<?>> descriptors = new AnnotationFinder(new FileArchive(Thread.currentThread().getContextClassLoader(), jarLocation(marker)), false).findAnnotatedClasses(Application.class).iterator();
        if (!descriptors.hasNext()) {
            throw new IllegalArgumentException("No descriptor class using @Application");
        }
        type = descriptors.next();
        if (descriptors.hasNext()) {
            throw new IllegalArgumentException("Ambiguous @Application: " + type + ", " + descriptors.next());
        }
    }
    try {
        app = type.newInstance();
    } catch (final InstantiationException | IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}
Also used : Iterator(java.util.Iterator) FileArchive(org.apache.xbean.finder.archive.FileArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

Example 12 with AnnotationFinder

use of org.apache.xbean.finder.AnnotationFinder in project tomee by apache.

the class MBeanDeployer method deploy.

// mbeans ObjectNames are stored in the app since they are global and that's easier
// mbean classes themself are stored in modules since they depend only on them
@Override
public AppModule deploy(final AppModule appModule) throws OpenEJBException {
    logger.debug("looking for annotated MBeans in " + appModule.getModuleId());
    final List<String> done = new ArrayList<String>();
    ClassLoader cl = appModule.getClassLoader();
    if (cl == null) {
        cl = Thread.currentThread().getContextClassLoader();
        if (cl == null) {
            cl = getClass().getClassLoader();
        }
    }
    final Collection<Class<? extends Annotation>> mbeanClasses = new ArrayList<Class<? extends Annotation>>(2);
    mbeanClasses.add(MBean.class);
    try {
        // for OSGi environment, javax.management is imported by the JRE
        mbeanClasses.add((Class<? extends Annotation>) cl.loadClass("javax.management.MBean"));
    } catch (final NoClassDefFoundError | ClassNotFoundException noClassDefFoundError) {
    // ignored
    }
    for (final EjbModule ejbModule : appModule.getEjbModules()) {
        if (ejbModule.getFinder() == null) {
            continue;
        }
        for (final Class<? extends Annotation> mclazz : mbeanClasses) {
            for (final Annotated<Class<?>> clazz : ejbModule.getFinder().findMetaAnnotatedClasses(mclazz)) {
                final Class<?> realClass = clazz.get();
                final String name = realClass.getName();
                if (done.contains(name)) {
                    continue;
                }
                ejbModule.getMbeans().add(name);
                done.add(name);
            }
        }
    }
    for (final ClientModule clientModule : appModule.getClientModules()) {
        if (clientModule.getFinder() == null) {
            continue;
        }
        for (final Class<? extends Annotation> mclazz : mbeanClasses) {
            for (final Annotated<Class<?>> clazz : clientModule.getFinder().findMetaAnnotatedClasses(mclazz)) {
                final String name = clazz.get().getName();
                if (done.contains(name)) {
                    continue;
                }
                clientModule.getMbeans().add(name);
            }
        }
    }
    final List<URL> libs = appModule.getAdditionalLibraries();
    final Iterator<URL> it = libs.iterator();
    while (it.hasNext()) {
        final URL url = it.next();
        for (final String location : done) {
            if (url.getFile().equals(location)) {
                it.remove();
            }
        }
    }
    if (libs.size() > 0) {
        // force descriptor for additinal libs since it shouldn't occur often and can save some time
        final IAnnotationFinder finder = new AnnotationFinder(new ConfigurableClasspathArchive(appModule.getClassLoader(), true, libs));
        for (final Class<? extends Annotation> mclazz : mbeanClasses) {
            for (final Annotated<Class<?>> clazz : finder.findMetaAnnotatedClasses(mclazz)) {
                final String name = clazz.get().getName();
                if (done.contains(name)) {
                    continue;
                }
                appModule.getAdditionalLibMbeans().add(name);
            }
        }
    }
    return appModule;
}
Also used : IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) URL(java.net.URL) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder)

Example 13 with AnnotationFinder

use of org.apache.xbean.finder.AnnotationFinder in project tomee by apache.

the class CheckAnnotationTest method testWebServiceWithStateful.

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

Example 14 with AnnotationFinder

use of org.apache.xbean.finder.AnnotationFinder in project tomee by apache.

the class SingleApplicationComposerRunner method start.

private static void start(final Class<?> marker) throws Exception {
    if (APP.get() == null) {
        final Class<?> type;
        final String typeStr = JavaSecurityManagers.getSystemProperty("tomee.application-composer.application");
        if (typeStr != null) {
            try {
                type = Thread.currentThread().getContextClassLoader().loadClass(typeStr);
            } catch (final ClassNotFoundException e) {
                throw new IllegalArgumentException(e);
            }
        } else if (marker == null) {
            throw new IllegalArgumentException("set tomee.application-composer.application system property or add a marker to the rule or runner");
        } else {
            final Iterator<Class<?>> descriptors = new AnnotationFinder(new FileArchive(Thread.currentThread().getContextClassLoader(), jarLocation(marker)), false).findAnnotatedClasses(Application.class).iterator();
            if (!descriptors.hasNext()) {
                throw new IllegalArgumentException("No descriptor class using @Application");
            }
            type = descriptors.next();
            if (descriptors.hasNext()) {
                throw new IllegalArgumentException("Ambiguous @Application: " + type + ", " + descriptors.next());
            }
        }
        try {
            APP.compareAndSet(null, type.newInstance());
        } catch (final InstantiationException | IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
    if (!started) {
        final Object app = APP.get();
        final ApplicationComposers composers = new ApplicationComposers(app.getClass()) {

            @Override
            public void deployApp(final Object inputTestInstance) throws Exception {
                super.deployApp(inputTestInstance);
                if (!started) {
                    // done here for logging
                    final ThreadContext previous = ThreadContext.getThreadContext();
                    final ApplicationComposers comp = this;
                    final Thread hook = new Thread() {

                        @Override
                        public void run() {
                            try {
                                comp.after();
                            } catch (final Exception e) {
                                ThreadContext.exit(previous);
                                throw new IllegalStateException(e);
                            }
                        }
                    };
                    HOOK.set(hook);
                    Runtime.getRuntime().addShutdownHook(hook);
                    started = true;
                }
            }
        };
        composers.before(app);
        composers.handleLifecycle(app.getClass(), app);
    }
}
Also used : ThreadContext(org.apache.openejb.core.ThreadContext) Iterator(java.util.Iterator) FileArchive(org.apache.xbean.finder.archive.FileArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

Example 15 with AnnotationFinder

use of org.apache.xbean.finder.AnnotationFinder in project tomee by apache.

the class AbstractXmlAnnotationFinderTest method initFinder.

@Before
public void initFinder() throws Exception {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    System.setProperty(SCAN_XML_PROPERTY, scanXml());
    finder = new AnnotationFinder(new ConfigurableClasspathArchive(loader, Arrays.asList(new URL(loader.getResource(scanXml()).toExternalForm().replace(scanXml(), "")))));
    System.clearProperty("openejb.scan.xml.name");
}
Also used : ConfigurableClasspathArchive(org.apache.openejb.config.ConfigurableClasspathArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder) IAnnotationFinder(org.apache.xbean.finder.IAnnotationFinder) URL(java.net.URL) Before(org.junit.Before)

Aggregations

AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)18 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)13 URL (java.net.URL)6 AppModule (org.apache.openejb.config.AppModule)6 EjbModule (org.apache.openejb.config.EjbModule)6 EjbJar (org.apache.openejb.jee.EjbJar)6 Method (java.lang.reflect.Method)4 ArrayList (java.util.ArrayList)4 IAnnotationFinder (org.apache.xbean.finder.IAnnotationFinder)4 Archive (org.apache.xbean.finder.archive.Archive)4 FileArchive (org.apache.xbean.finder.archive.FileArchive)4 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Test (org.junit.Test)3 Field (java.lang.reflect.Field)2 MalformedURLException (java.net.MalformedURLException)2 Iterator (java.util.Iterator)2 List (java.util.List)2