Search in sources :

Example 21 with ServletContainerInitializer

use of javax.servlet.ServletContainerInitializer in project tomcat70 by apache.

the class ContextConfig method processServletContainerInitializers.

/**
 * Scan JARs for ServletContainerInitializer implementations.
 */
protected void processServletContainerInitializers() {
    List<ServletContainerInitializer> detectedScis;
    try {
        WebappServiceLoader<ServletContainerInitializer> loader = new WebappServiceLoader<ServletContainerInitializer>(context);
        detectedScis = loader.load(ServletContainerInitializer.class);
    } catch (IOException e) {
        log.error(sm.getString("contextConfig.servletContainerInitializerFail", context.getName()), e);
        ok = false;
        return;
    }
    for (ServletContainerInitializer sci : detectedScis) {
        initializerClassMap.put(sci, new HashSet<Class<?>>());
        HandlesTypes ht;
        try {
            ht = sci.getClass().getAnnotation(HandlesTypes.class);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.info(sm.getString("contextConfig.sci.debug", sci.getClass().getName()), e);
            } else {
                log.info(sm.getString("contextConfig.sci.info", sci.getClass().getName()));
            }
            continue;
        }
        if (ht == null) {
            continue;
        }
        Class<?>[] types = ht.value();
        if (types == null) {
            continue;
        }
        for (Class<?> type : types) {
            if (type.isAnnotation()) {
                handlesTypesAnnotations = true;
            } else {
                handlesTypesNonAnnotations = true;
            }
            Set<ServletContainerInitializer> scis = typeInitializerMap.get(type);
            if (scis == null) {
                scis = new HashSet<ServletContainerInitializer>();
                typeInitializerMap.put(type, scis);
            }
            scis.add(sci);
        }
    }
}
Also used : IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) NameNotFoundException(javax.naming.NameNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) HandlesTypes(javax.servlet.annotation.HandlesTypes)

Example 22 with ServletContainerInitializer

use of javax.servlet.ServletContainerInitializer in project mysql_perf_analyzer by yahoo.

the class App method jspInitializers.

private List<ContainerInitializer> jspInitializers() {
    JettyJasperInitializer sci = new JettyJasperInitializer();
    ContainerInitializer initializer = new ContainerInitializer((ServletContainerInitializer) sci, null);
    List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
    initializers.add(initializer);
    return initializers;
}
Also used : ArrayList(java.util.ArrayList) JettyJasperInitializer(org.eclipse.jetty.apache.jsp.JettyJasperInitializer) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) ContainerInitializer(org.eclipse.jetty.plus.annotation.ContainerInitializer)

Example 23 with ServletContainerInitializer

use of javax.servlet.ServletContainerInitializer in project tomee by apache.

the class OpenEJBContextConfig method processServletContainerInitializers.

// called before processAnnotationsFile so using it as hook to init webInfClassesAnnotationsProcessed
@Override
protected void processServletContainerInitializers() {
    try {
        super.processServletContainerInitializers();
        final Iterator<Map.Entry<ServletContainerInitializer, Set<Class<?>>>> iterator = initializerClassMap.entrySet().iterator();
        while (iterator.hasNext()) {
            final Map.Entry<ServletContainerInitializer, Set<Class<?>>> entry = iterator.next();
            final ServletContainerInitializer sci = entry.getKey();
            final String classname = sci.getClass().getName();
            if (classname.equals("org.apache.myfaces.ee6.MyFacesContainerInitializer") || classname.equals("org.springframework.web.SpringServletContainerInitializer")) {
                for (final Map.Entry<Class<?>, Set<ServletContainerInitializer>> scanning : typeInitializerMap.entrySet()) {
                    final Set<ServletContainerInitializer> scis = scanning.getValue();
                    if (scis != null && scis.contains(sci)) {
                        scis.remove(sci);
                    }
                }
                iterator.remove();
            } else if ("org.apache.jasper.servlet.JasperInitializer".equals(classname)) {
                iterator.remove();
            }
        }
        initializerClassMap.put(new TomEEJasperInitializer(), new HashSet<>());
        final ClassLoader loader = context.getLoader().getClassLoader();
        // spring-web (not scanned)
        try {
            final Class<?> initializer = Class.forName("org.springframework.web.SpringServletContainerInitializer", true, loader);
            final ServletContainerInitializer instance = (ServletContainerInitializer) initializer.newInstance();
            typeInitializerMap.put(Class.forName("org.springframework.web.WebApplicationInitializer", true, loader), Collections.singleton(instance));
            initializerClassMap.put(instance, new HashSet<>());
        } catch (final Exception | NoClassDefFoundError ignored) {
        // no-op
        }
        // scanned SCIs
        if (typeInitializerMap.size() > 0 && finder != null) {
            for (final Map.Entry<Class<?>, Set<ServletContainerInitializer>> entry : typeInitializerMap.entrySet()) {
                if (entry.getValue() == null || entry.getValue().isEmpty()) {
                    continue;
                }
                final Class<?> annotation = entry.getKey();
                for (final ServletContainerInitializer sci : entry.getValue()) {
                    if (annotation.isAnnotation()) {
                        try {
                            final Class<? extends Annotation> reloadedAnnotation = Class.class.cast(tempLoader.loadClass(annotation.getName()));
                            addClassesWithRightLoader(loader, sci, finder.findAnnotatedClasses(reloadedAnnotation));
                        } catch (final Throwable th) {
                        // no-op
                        }
                    } else {
                        try {
                            final Class<?> reloadedClass = tempLoader.loadClass(annotation.getName());
                            final List<Class<?>> implementations;
                            if (annotation.isInterface()) {
                                implementations = List.class.cast(finder.findImplementations(reloadedClass));
                            } else {
                                implementations = List.class.cast(finder.findSubclasses(reloadedClass));
                            }
                            addClassesWithRightLoader(loader, sci, implementations);
                        } catch (final Throwable th) {
                        // no-op
                        }
                    }
                }
            }
        }
        // done
        finder = null;
        tempLoader = null;
    } catch (final RuntimeException e) {
        // if exception occurs we have to clear the threadlocal
        throw e;
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ClassFormatException(org.apache.tomcat.util.bcel.classfile.ClassFormatException) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) TomEEJasperInitializer(org.apache.tomee.jasper.TomEEJasperInitializer) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) List(java.util.List) ArrayList(java.util.ArrayList) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with ServletContainerInitializer

use of javax.servlet.ServletContainerInitializer in project Payara by payara.

the class ServletContainerInitializerUtil method getInitializerList.

/**
 * Given an interestlist that was built above, and a class loader, scan the entire web app's classes and libraries
 * looking for classes that extend/implement/use the annotations of a class present in the interest list
 *
 * @param initializers Iterable over all ServletContainerInitializers that
 * were discovered
 * @param interestList The interestList built by the previous util method
 * @param cl The classloader to be used to load classes in WAR
 * @return Map&lt;Class&lt;? extends ServletContainerInitializer&gt;, Set&lt;Class&lt;?&gt;&gt;&gt;
 *                          A Map of ServletContainerInitializer classes to be called and arguments to be passed
 *                          to them
 */
public static Map<Class<? extends ServletContainerInitializer>, Set<Class<?>>> getInitializerList(Iterable<ServletContainerInitializer> initializers, Map<Class<?>, List<Class<? extends ServletContainerInitializer>>> interestList, Types types, ClassLoader cl, boolean isStandalone) {
    if (interestList == null) {
        return null;
    }
    // This contains the final list of initializers and the set of
    // classes to be passed to them as arg
    Map<Class<? extends ServletContainerInitializer>, Set<Class<?>>> initializerList = null;
    // must be called with a null set of classes
    if (interestList.containsKey(ServletContainerInitializerUtil.class)) {
        initializerList = new HashMap<Class<? extends ServletContainerInitializer>, Set<Class<?>>>();
        List<Class<? extends ServletContainerInitializer>> initializersWithoutHandleTypes = interestList.get(ServletContainerInitializerUtil.class);
        for (Class<? extends ServletContainerInitializer> c : initializersWithoutHandleTypes) {
            initializerList.put(c, null);
        }
    }
    /*
         * Now scan every class in this app's WEB-INF/classes and WEB-INF/lib
         * to see if any class uses the annotation or extends/implements a
         * class in our interest list.
         * Do this scanning only if we have ServletContainerinitializers that
         * have expressed specific interest
         */
    if ((interestList.keySet().size() > 1) || ((interestList.keySet().size() == 1) && (!interestList.containsKey(ServletContainerInitializerUtil.class)))) {
        /*
             * Create an instance of ClassDependencyBuilder that looks at the byte code and keeps
             * the information for every class in this app
             *
             */
        if (types == null || Boolean.getBoolean("org.glassfish.web.parsing")) {
            ClassDependencyBuilder classInfo = new ClassDependencyBuilder();
            if (cl instanceof URLClassLoader) {
                URLClassLoader ucl = (URLClassLoader) cl;
                for (URL u : ucl.getURLs()) {
                    String path = u.getPath();
                    try {
                        if (path.endsWith(".jar")) {
                            JarFile jf = new JarFile(path);
                            try {
                                Enumeration<JarEntry> entries = jf.entries();
                                while (entries.hasMoreElements()) {
                                    JarEntry anEntry = entries.nextElement();
                                    if (anEntry.isDirectory())
                                        continue;
                                    if (!anEntry.getName().endsWith(".class"))
                                        continue;
                                    InputStream jarInputStream = null;
                                    try {
                                        jarInputStream = jf.getInputStream(anEntry);
                                        int size = (int) anEntry.getSize();
                                        byte[] classData = new byte[size];
                                        for (int bytesRead = 0; bytesRead < size; ) {
                                            int r2 = jarInputStream.read(classData, bytesRead, size - bytesRead);
                                            bytesRead += r2;
                                        }
                                        classInfo.loadClassData(classData);
                                    } catch (Throwable t) {
                                        if (log.isLoggable(Level.FINE)) {
                                            log.log(Level.FINE, LogFacade.CLASS_LOADING_ERROR, new Object[] { anEntry.getName(), t.toString() });
                                        }
                                        continue;
                                    } finally {
                                        if (jarInputStream != null) {
                                            jarInputStream.close();
                                        }
                                    }
                                }
                            } finally {
                                jf.close();
                            }
                        } else {
                            File file = new File(path);
                            if (file.exists()) {
                                if (file.isDirectory()) {
                                    scanDirectory(file, classInfo);
                                } else {
                                    log.log(Level.WARNING, LogFacade.INVALID_URL_CLASS_LOADER_PATH, path);
                                }
                            }
                        }
                    } catch (IOException ioex) {
                        String msg = rb.getString(LogFacade.IO_ERROR);
                        msg = MessageFormat.format(msg, new Object[] { path });
                        log.log(Level.SEVERE, msg, ioex);
                        return null;
                    }
                }
            }
            initializerList = checkAgainstInterestList(classInfo, interestList, initializerList, cl, isStandalone);
        } else {
            initializerList = checkAgainstInterestList(types, interestList, initializerList, cl, isStandalone);
        }
    }
    /*
         * If a ServletContainerInitializer was annotated with HandlesTypes,
         * but none of the application classes match, we must still invoke
         * it at its onStartup method, passing in a null Set of classes
         */
    for (ServletContainerInitializer initializer : initializers) {
        if (!initializerList.containsKey(initializer.getClass())) {
            initializerList.put(initializer.getClass(), null);
        }
    }
    // Remove initializers we aren't interested in
    initializerList = checkAgainstBlacklist(initializerList);
    return initializerList;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ClassDependencyBuilder(org.glassfish.deployment.common.ClassDependencyBuilder) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) URLClassLoader(java.net.URLClassLoader) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 25 with ServletContainerInitializer

use of javax.servlet.ServletContainerInitializer in project Payara by payara.

the class ServletContainerInitializerUtil method getInterestList.

/**
 * Builds a mapping of classes to the list of ServletContainerInitializers
 * interested in them
 *
 * @param initializers an Iterable over all ServletContainerInitializers
 * that need to be considered
 *
 * @return Mapping of classes to list of ServletContainerInitializers
 * interested in them
 */
public static Map<Class<?>, List<Class<? extends ServletContainerInitializer>>> getInterestList(Iterable<ServletContainerInitializer> initializers) {
    if (null == initializers) {
        return null;
    }
    Map<Class<?>, List<Class<? extends ServletContainerInitializer>>> interestList = null;
    // initializers are interested
    for (ServletContainerInitializer sc : initializers) {
        if (interestList == null) {
            interestList = new HashMap<Class<?>, List<Class<? extends ServletContainerInitializer>>>();
        }
        Class<? extends ServletContainerInitializer> sciClass = sc.getClass();
        HandlesTypes ann = (HandlesTypes) sciClass.getAnnotation(HandlesTypes.class);
        if (ann == null) {
            // This initializer does not contain @HandlesTypes
            // This means it should always be called for all web apps
            // So map it with a special token
            List<Class<? extends ServletContainerInitializer>> currentInitializerList = interestList.get(ServletContainerInitializerUtil.class);
            if (currentInitializerList == null) {
                List<Class<? extends ServletContainerInitializer>> arr = new ArrayList<Class<? extends ServletContainerInitializer>>();
                arr.add(sciClass);
                interestList.put(ServletContainerInitializerUtil.class, arr);
            } else {
                currentInitializerList.add(sciClass);
            }
        } else {
            Class[] interestedClasses = ann.value();
            if ((interestedClasses != null) && (interestedClasses.length != 0)) {
                for (Class c : interestedClasses) {
                    List<Class<? extends ServletContainerInitializer>> currentInitializerList = interestList.get(c);
                    if (currentInitializerList == null) {
                        List<Class<? extends ServletContainerInitializer>> arr = new ArrayList<Class<? extends ServletContainerInitializer>>();
                        arr.add(sciClass);
                        interestList.put(c, arr);
                    } else {
                        currentInitializerList.add(sciClass);
                    }
                }
            }
        }
    }
    return interestList;
}
Also used : ServletContainerInitializer(javax.servlet.ServletContainerInitializer) HandlesTypes(javax.servlet.annotation.HandlesTypes)

Aggregations

ServletContainerInitializer (javax.servlet.ServletContainerInitializer)25 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 HashSet (java.util.HashSet)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 Set (java.util.Set)8 File (java.io.File)7 MalformedURLException (java.net.MalformedURLException)6 HandlesTypes (javax.servlet.annotation.HandlesTypes)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 ServletContext (javax.servlet.ServletContext)5 LinkedHashMap (java.util.LinkedHashMap)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Context (org.apache.catalina.Context)4 FilterMap (org.apache.catalina.deploy.FilterMap)4 Tomcat (org.apache.catalina.startup.Tomcat)4 InputStream (java.io.InputStream)3 TreeMap (java.util.TreeMap)3