use of org.apache.xbean.finder.archive.FileArchive 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.archive.FileArchive in project tomee by apache.
the class ScanJarService method ensureInit.
private void ensureInit() throws Exception {
if (beans != null) {
return;
}
synchronized (this) {
if (beans != null) {
return;
}
final ClassLoader loader = ParentClassLoaderFinder.Helper.get();
final CompositeBeans mergedModel = new CompositeBeans();
for (final File file : findFiles()) {
final URL url = file.toURI().toURL();
if (file.isDirectory()) {
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new FileArchive(loader, file));
mergedModel.getManagedClasses().put(url, finder.getAnnotatedClassNames());
final File beansXml = new File(file, "META-INF/beans.xml");
if (beansXml.exists()) {
try (final FileInputStream inputStream = new FileInputStream(beansXml)) {
final Beans beansModel = ReadDescriptors.readBeans(inputStream);
mergedModel.mergeClasses(url, beansModel);
}
}
} else {
final FinderFactory.OpenEJBAnnotationFinder finder = new FinderFactory.OpenEJBAnnotationFinder(new JarArchive(loader, url));
mergedModel.getManagedClasses().put(url, finder.getAnnotatedClassNames());
try (final URLClassLoader cl = new URLClassLoader(new URL[] { url })) {
try (final InputStream is = cl.getResourceAsStream("META-INF/beans.xml")) {
if (is != null) {
final Beans beansModel = ReadDescriptors.readBeans(is);
mergedModel.mergeClasses(url, beansModel);
}
}
}
}
}
beans = mergedModel;
}
}
use of org.apache.xbean.finder.archive.FileArchive in project tomee by apache.
the class ApplicationComposers method finderFromClasses.
private static IAnnotationFinder finderFromClasses(final DeploymentModule module, final Class<?>[] value, final Collection<File> others, final String[] excludes) {
final Collection<Archive> archives = new ArrayList<>(1 + (others == null ? 0 : others.size()));
final Filter filter = excludes == null || excludes.length == 0 ? null : Filters.invert(Filters.prefixes(excludes));
final Collection<Class<?>> classes = new ArrayList<>(asList(FinderFactory.ensureMinimalClasses(module)));
if (value != null) {
classes.addAll(asList(value));
}
final ClassesArchive classesArchive = new ClassesArchive(classes);
archives.add(filter == null ? classesArchive : new FilteredArchive(classesArchive, filter));
if (others != null) {
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
for (final File f : others) {
try {
final Archive archive = f.isDirectory() ? new FileArchive(classLoader, f) : new JarArchive(classLoader, f.toURI().toURL());
archives.add(filter == null ? archive : new FilteredArchive(archive, filter));
} catch (final MalformedURLException e) {
throw new IllegalArgumentException(e);
}
}
}
return new FinderFactory.OpenEJBAnnotationFinder(new CompositeArchive(archives)).link();
}
use of org.apache.xbean.finder.archive.FileArchive 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);
}
}
Aggregations