Search in sources :

Example 1 with ScannerService

use of org.apache.webbeans.spi.ScannerService in project meecrowave by apache.

the class OWBJarScanner method scan.

@Override
public void scan(final JarScanType jarScanType, final ServletContext servletContext, final JarScannerCallback callback) {
    switch(jarScanType) {
        case PLUGGABILITY:
            final WebBeansContext owb = WebBeansContext.getInstance();
            final ScannerService scannerService = owb.getScannerService();
            if (!WebScannerService.class.isInstance(scannerService)) {
                return;
            }
            final OwbAnnotationFinder finder = WebScannerService.class.cast(scannerService).getFinder();
            if (finder == null) {
                return;
            }
            CdiArchive.class.cast(finder.getArchive()).classesByUrl().keySet().stream().filter(// not a fake in memory url
            u -> !"jar:file://!/".equals(u)).forEach(u -> {
                try {
                    final URL url = new URL(u);
                    final File asFile = Files.toFile(url);
                    if (!asFile.exists()) {
                        return;
                    }
                    if (filter != null && !filter.check(jarScanType, asFile.getName())) {
                        return;
                    }
                    if (asFile.getName().endsWith(Constants.JAR_EXT)) {
                        try (final Jar jar = JarFactory.newInstance(asFile.toURI().toURL())) {
                            callback.scan(jar, u, true);
                        }
                    } else if (asFile.isDirectory()) {
                        callback.scan(asFile, asFile.getAbsolutePath(), true);
                    }
                } catch (final MalformedURLException e) {
                // skip
                } catch (final IOException ioe) {
                    throw new IllegalArgumentException(ioe);
                }
            });
            return;
        case TLD:
        default:
    }
}
Also used : ScannerService(org.apache.webbeans.spi.ScannerService) Jar(org.apache.tomcat.Jar) OwbAnnotationFinder(org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) JarScannerCallback(org.apache.tomcat.JarScannerCallback) Constants(org.apache.tomcat.util.scan.Constants) JarFactory(org.apache.tomcat.util.scan.JarFactory) JarScanType(org.apache.tomcat.JarScanType) IOException(java.io.IOException) File(java.io.File) JarScanner(org.apache.tomcat.JarScanner) Files(org.apache.xbean.finder.util.Files) WebBeansContext(org.apache.webbeans.config.WebBeansContext) JarScanFilter(org.apache.tomcat.JarScanFilter) ServletContext(javax.servlet.ServletContext) WebScannerService(org.apache.webbeans.web.scanner.WebScannerService) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) MalformedURLException(java.net.MalformedURLException) WebBeansContext(org.apache.webbeans.config.WebBeansContext) WebScannerService(org.apache.webbeans.web.scanner.WebScannerService) OwbAnnotationFinder(org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) File(java.io.File) ScannerService(org.apache.webbeans.spi.ScannerService) WebScannerService(org.apache.webbeans.web.scanner.WebScannerService) URL(java.net.URL)

Example 2 with ScannerService

use of org.apache.webbeans.spi.ScannerService in project meecrowave by apache.

the class MeecrowaveContextConfig method webConfig.

@Override
protected void webConfig() {
    if (context.getServletContext().getAttribute("meecrowave.configuration") == null) {
        // redeploy
        context.getServletContext().setAttribute("meecrowave.configuration", Meecrowave.Builder.class.isInstance(configuration) ? configuration : new Meecrowave.Builder(configuration));
        context.addServletContainerInitializer(intializer, emptySet());
    }
    if (!configuration.isTomcatScanning()) {
        super.webConfig();
        return;
    }
    // eagerly start CDI to scan only once and not twice (tomcat+CDI)
    // should already be started at that point
    final ClassLoader loader = context.getLoader().getClassLoader();
    final Thread thread = Thread.currentThread();
    final ClassLoader old = thread.getContextClassLoader();
    thread.setContextClassLoader(loader);
    try {
        final ScannerService service = WebBeansContext.getInstance().getScannerService();
        if (OWBTomcatWebScannerService.class.isInstance(service)) {
            final OWBTomcatWebScannerService scannerService = OWBTomcatWebScannerService.class.cast(service);
            scannerService.setFilter(ofNullable(context.getJarScanner()).map(JarScanner::getJarScanFilter).orElse(null), context.getServletContext());
            scannerService.setDocBase(context.getDocBase());
            scannerService.setShared(configuration.getSharedLibraries());
            if (configuration.getWatcherBouncing() > 0) {
                // note that caching should be disabled with this config in most of the times
                watcher = new ReloadOnChangeController(context, configuration.getWatcherBouncing(), redeployCallback);
                scannerService.setFileVisitor(f -> watcher.register(f));
            }
            scannerService.scan();
            finder = scannerService.getFinder();
            finder.link();
            final CdiArchive archive = CdiArchive.class.cast(finder.getArchive());
            Stream.of(WebServlet.class, WebFilter.class, WebListener.class).forEach(marker -> finder.findAnnotatedClasses(marker).stream().filter(c -> !Modifier.isAbstract(c.getModifiers()) && Modifier.isPublic(c.getModifiers())).forEach(webComponent -> webClasses.computeIfAbsent(archive.classesByUrl().entrySet().stream().filter(e -> e.getValue().getClassNames().contains(webComponent.getName())).findFirst().get().getKey(), k -> new HashSet<>()).add(webComponent)));
        }
        super.webConfig();
    } finally {
        thread.setContextClassLoader(old);
        webClasses.clear();
        finder = null;
    }
}
Also used : OWBTomcatWebScannerService(org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService) LifecycleEvent(org.apache.catalina.LifecycleEvent) ScannerService(org.apache.webbeans.spi.ScannerService) WebXml(org.apache.tomcat.util.descriptor.web.WebXml) OwbAnnotationFinder(org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder) URL(java.net.URL) Lifecycle(org.apache.catalina.Lifecycle) HandlesTypes(javax.servlet.annotation.HandlesTypes) WebappServiceLoader(org.apache.catalina.startup.WebappServiceLoader) HashMap(java.util.HashMap) ReloadOnChangeController(org.apache.meecrowave.watching.ReloadOnChangeController) JarScanner(org.apache.tomcat.JarScanner) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser) HashSet(java.util.HashSet) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) Collections.singleton(java.util.Collections.singleton) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) InputSource(org.xml.sax.InputSource) Collections.emptySet(java.util.Collections.emptySet) LogFacade(org.apache.meecrowave.logging.tomcat.LogFacade) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Set(java.util.Set) Configuration(org.apache.meecrowave.configuration.Configuration) IOException(java.io.IOException) Context(org.apache.catalina.Context) StandardCharsets(java.nio.charset.StandardCharsets) WebListener(javax.servlet.annotation.WebListener) Consumer(java.util.function.Consumer) WebServlet(javax.servlet.annotation.WebServlet) WebBeansContext(org.apache.webbeans.config.WebBeansContext) Stream(java.util.stream.Stream) WebFilter(javax.servlet.annotation.WebFilter) Modifier(java.lang.reflect.Modifier) ContextConfig(org.apache.catalina.startup.ContextConfig) Annotation(java.lang.annotation.Annotation) Meecrowave(org.apache.meecrowave.Meecrowave) InputStream(java.io.InputStream) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) WebFilter(javax.servlet.annotation.WebFilter) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) JarScanner(org.apache.tomcat.JarScanner) OWBTomcatWebScannerService(org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService) ScannerService(org.apache.webbeans.spi.ScannerService) OWBTomcatWebScannerService(org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService) WebListener(javax.servlet.annotation.WebListener) WebServlet(javax.servlet.annotation.WebServlet) Meecrowave(org.apache.meecrowave.Meecrowave) ReloadOnChangeController(org.apache.meecrowave.watching.ReloadOnChangeController) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)2 URL (java.net.URL)2 JarScanner (org.apache.tomcat.JarScanner)2 WebBeansContext (org.apache.webbeans.config.WebBeansContext)2 CdiArchive (org.apache.webbeans.corespi.scanner.xbean.CdiArchive)2 OwbAnnotationFinder (org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder)2 ScannerService (org.apache.webbeans.spi.ScannerService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 Modifier (java.lang.reflect.Modifier)1 MalformedURLException (java.net.MalformedURLException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Collection (java.util.Collection)1 Collections.emptySet (java.util.Collections.emptySet)1 Collections.singleton (java.util.Collections.singleton)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1