Search in sources :

Example 1 with ClassDependencyBuilder

use of org.glassfish.deployment.common.ClassDependencyBuilder 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<Class<? extends ServletContainerInitializer>, Set<Class<?>>>
 *                          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);
        }
    }
    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)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 ClassDependencyBuilder (org.glassfish.deployment.common.ClassDependencyBuilder)1