Search in sources :

Example 1 with FileArchive

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);
    }
}
Also used : Iterator(java.util.Iterator) FileArchive(org.apache.xbean.finder.archive.FileArchive) AnnotationFinder(org.apache.xbean.finder.AnnotationFinder)

Example 2 with FileArchive

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;
    }
}
Also used : CompositeBeans(org.apache.openejb.cdi.CompositeBeans) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FinderFactory(org.apache.openejb.config.FinderFactory) URL(java.net.URL) FileInputStream(java.io.FileInputStream) Beans(org.apache.openejb.jee.Beans) CompositeBeans(org.apache.openejb.cdi.CompositeBeans) JarArchive(org.apache.xbean.finder.archive.JarArchive) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) FileArchive(org.apache.xbean.finder.archive.FileArchive) File(java.io.File)

Example 3 with FileArchive

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) Archive(org.apache.xbean.finder.archive.Archive) ArrayList(java.util.ArrayList) FilteredArchive(org.apache.xbean.finder.archive.FilteredArchive) FinderFactory(org.apache.openejb.config.FinderFactory) Filter(org.apache.xbean.finder.filter.Filter) CompositeArchive(org.apache.xbean.finder.archive.CompositeArchive) JarArchive(org.apache.xbean.finder.archive.JarArchive) ClassesArchive(org.apache.xbean.finder.archive.ClassesArchive) FileArchive(org.apache.xbean.finder.archive.FileArchive) File(java.io.File)

Example 4 with FileArchive

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

Aggregations

FileArchive (org.apache.xbean.finder.archive.FileArchive)4 File (java.io.File)2 Iterator (java.util.Iterator)2 FinderFactory (org.apache.openejb.config.FinderFactory)2 AnnotationFinder (org.apache.xbean.finder.AnnotationFinder)2 JarArchive (org.apache.xbean.finder.archive.JarArchive)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 CompositeBeans (org.apache.openejb.cdi.CompositeBeans)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 Beans (org.apache.openejb.jee.Beans)1 Archive (org.apache.xbean.finder.archive.Archive)1 ClassesArchive (org.apache.xbean.finder.archive.ClassesArchive)1 CompositeArchive (org.apache.xbean.finder.archive.CompositeArchive)1 FilteredArchive (org.apache.xbean.finder.archive.FilteredArchive)1 Filter (org.apache.xbean.finder.filter.Filter)1