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);
}
}
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;
}
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;
}
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);
}
}
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");
}
Aggregations