Search in sources :

Example 1 with ReloadOnChangeController

use of org.apache.meecrowave.watching.ReloadOnChangeController 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", 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 OWBTomcatWebScannerService scannerService = OWBTomcatWebScannerService.class.cast(WebBeansContext.getInstance().getScannerService());
        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());
            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)));
    } finally {
        thread.setContextClassLoader(old);
    }
    try {
        super.webConfig();
    } finally {
        webClasses.clear();
        finder = null;
    }
}
Also used : OWBTomcatWebScannerService(org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService) LifecycleEvent(org.apache.catalina.LifecycleEvent) WebXml(org.apache.tomcat.util.descriptor.web.WebXml) OwbAnnotationFinder(org.apache.webbeans.corespi.scanner.xbean.OwbAnnotationFinder) Lifecycle(org.apache.catalina.Lifecycle) URL(java.net.URL) WebResource(org.apache.catalina.WebResource) HandlesTypes(javax.servlet.annotation.HandlesTypes) ReloadOnChangeController(org.apache.meecrowave.watching.ReloadOnChangeController) HashMap(java.util.HashMap) JarScanner(org.apache.tomcat.JarScanner) HashSet(java.util.HashSet) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) InputSource(org.xml.sax.InputSource) LogFacade(org.apache.meecrowave.logging.tomcat.LogFacade) Collections.emptySet(java.util.Collections.emptySet) Optional.ofNullable(java.util.Optional.ofNullable) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Context(org.apache.catalina.Context) WebListener(javax.servlet.annotation.WebListener) StandardCharsets(java.nio.charset.StandardCharsets) WebBeansContext(org.apache.webbeans.config.WebBeansContext) WebServlet(javax.servlet.annotation.WebServlet) Stream(java.util.stream.Stream) WebFilter(javax.servlet.annotation.WebFilter) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Meecrowave(org.apache.meecrowave.Meecrowave) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) InputStream(java.io.InputStream) WebFilter(javax.servlet.annotation.WebFilter) WebListener(javax.servlet.annotation.WebListener) WebServlet(javax.servlet.annotation.WebServlet) CdiArchive(org.apache.webbeans.corespi.scanner.xbean.CdiArchive) JarScanner(org.apache.tomcat.JarScanner) OWBTomcatWebScannerService(org.apache.meecrowave.openwebbeans.OWBTomcatWebScannerService) ReloadOnChangeController(org.apache.meecrowave.watching.ReloadOnChangeController) HashSet(java.util.HashSet)

Example 2 with ReloadOnChangeController

use of org.apache.meecrowave.watching.ReloadOnChangeController 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

ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Annotation (java.lang.annotation.Annotation)2 Modifier (java.lang.reflect.Modifier)2 URL (java.net.URL)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Collection (java.util.Collection)2 Collections.emptySet (java.util.Collections.emptySet)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Set (java.util.Set)2 Stream (java.util.stream.Stream)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 HandlesTypes (javax.servlet.annotation.HandlesTypes)2 WebFilter (javax.servlet.annotation.WebFilter)2 WebListener (javax.servlet.annotation.WebListener)2 WebServlet (javax.servlet.annotation.WebServlet)2