Search in sources :

Example 1 with AnnotationEntry

use of org.apache.tomcat.util.bcel.classfile.AnnotationEntry in project tomcat70 by apache.

the class ContextConfig method processAnnotationsStream.

protected void processAnnotationsStream(InputStream is, WebXml fragment, boolean handlesTypesOnly) throws ClassFormatException, IOException {
    ClassParser parser = new ClassParser(is);
    JavaClass clazz = parser.parse();
    checkHandlesTypes(clazz);
    if (handlesTypesOnly) {
        return;
    }
    String className = clazz.getClassName();
    AnnotationEntry[] annotationsEntries = clazz.getAnnotationEntries();
    if (annotationsEntries != null) {
        for (AnnotationEntry ae : annotationsEntries) {
            String type = ae.getAnnotationType();
            if ("Ljavax/servlet/annotation/WebServlet;".equals(type)) {
                processAnnotationWebServlet(className, ae, fragment);
            } else if ("Ljavax/servlet/annotation/WebFilter;".equals(type)) {
                processAnnotationWebFilter(className, ae, fragment);
            } else if ("Ljavax/servlet/annotation/WebListener;".equals(type)) {
                fragment.addListener(className);
            } else {
            // Unknown annotation - ignore
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) ClassParser(org.apache.tomcat.util.bcel.classfile.ClassParser)

Example 2 with AnnotationEntry

use of org.apache.tomcat.util.bcel.classfile.AnnotationEntry in project tomcat70 by apache.

the class ContextConfig method checkHandlesTypes.

/**
 * For classes packaged with the web application, the class and each
 * super class needs to be checked for a match with {@link HandlesTypes} or
 * for an annotation that matches {@link HandlesTypes}.
 * @param javaClass
 */
protected void checkHandlesTypes(JavaClass javaClass) {
    // Skip this if we can
    if (typeInitializerMap.size() == 0)
        return;
    if ((javaClass.getAccessFlags() & org.apache.tomcat.util.bcel.Const.ACC_ANNOTATION) != 0) {
        // Skip annotations.
        return;
    }
    String className = javaClass.getClassName();
    Class<?> clazz = null;
    if (handlesTypesNonAnnotations) {
        // This *might* be match for a HandlesType.
        populateJavaClassCache(className, javaClass);
        JavaClassCacheEntry entry = javaClassCache.get(className);
        if (entry.getSciSet() == null) {
            try {
                populateSCIsForCacheEntry(entry);
            } catch (StackOverflowError soe) {
                throw new IllegalStateException(sm.getString("contextConfig.annotationsStackOverflow", context.getName(), classHierarchyToString(className, entry)));
            }
        }
        if (!entry.getSciSet().isEmpty()) {
            // Need to try and load the class
            clazz = Introspection.loadClass(context, className);
            if (clazz == null) {
                // Can't load the class so no point continuing
                return;
            }
            for (ServletContainerInitializer sci : entry.getSciSet()) {
                Set<Class<?>> classes = initializerClassMap.get(sci);
                if (classes == null) {
                    classes = new HashSet<Class<?>>();
                    initializerClassMap.put(sci, classes);
                }
                classes.add(clazz);
            }
        }
    }
    if (handlesTypesAnnotations) {
        for (Map.Entry<Class<?>, Set<ServletContainerInitializer>> entry : typeInitializerMap.entrySet()) {
            if (entry.getKey().isAnnotation()) {
                AnnotationEntry[] annotationEntries = javaClass.getAnnotationEntries();
                if (annotationEntries != null) {
                    for (AnnotationEntry annotationEntry : annotationEntries) {
                        if (entry.getKey().getName().equals(getClassName(annotationEntry.getAnnotationType()))) {
                            if (clazz == null) {
                                clazz = Introspection.loadClass(context, className);
                                if (clazz == null) {
                                    // continuing
                                    return;
                                }
                            }
                            for (ServletContainerInitializer sci : entry.getValue()) {
                                initializerClassMap.get(sci).add(clazz);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) RuleSet(org.apache.tomcat.util.digester.RuleSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ServletContainerInitializer(javax.servlet.ServletContainerInitializer) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) FilterMap(org.apache.catalina.deploy.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with AnnotationEntry

use of org.apache.tomcat.util.bcel.classfile.AnnotationEntry in project tomcat by apache.

the class ContextConfig method checkHandlesTypes.

/**
 * For classes packaged with the web application, the class and each
 * super class needs to be checked for a match with {@link HandlesTypes} or
 * for an annotation that matches {@link HandlesTypes}.
 * @param javaClass the class to check
 * @param javaClassCache a class cache
 */
protected void checkHandlesTypes(JavaClass javaClass, Map<String, JavaClassCacheEntry> javaClassCache) {
    // Skip this if we can
    if (typeInitializerMap.size() == 0) {
        return;
    }
    if ((javaClass.getAccessFlags() & org.apache.tomcat.util.bcel.Const.ACC_ANNOTATION) != 0) {
        // Skip annotations.
        return;
    }
    String className = javaClass.getClassName();
    Class<?> clazz = null;
    if (handlesTypesNonAnnotations) {
        // This *might* be match for a HandlesType.
        populateJavaClassCache(className, javaClass, javaClassCache);
        JavaClassCacheEntry entry = javaClassCache.get(className);
        if (entry.getSciSet() == null) {
            try {
                populateSCIsForCacheEntry(entry, javaClassCache);
            } catch (StackOverflowError soe) {
                throw new IllegalStateException(sm.getString("contextConfig.annotationsStackOverflow", context.getName(), classHierarchyToString(className, entry, javaClassCache)));
            }
        }
        if (!entry.getSciSet().isEmpty()) {
            // Need to try and load the class
            clazz = Introspection.loadClass(context, className);
            if (clazz == null) {
                // Can't load the class so no point continuing
                return;
            }
            for (ServletContainerInitializer sci : entry.getSciSet()) {
                Set<Class<?>> classes = initializerClassMap.get(sci);
                if (classes == null) {
                    classes = new HashSet<>();
                    initializerClassMap.put(sci, classes);
                }
                classes.add(clazz);
            }
        }
    }
    if (handlesTypesAnnotations) {
        AnnotationEntry[] annotationEntries = javaClass.getAllAnnotationEntries();
        if (annotationEntries != null) {
            for (Map.Entry<Class<?>, Set<ServletContainerInitializer>> entry : typeInitializerMap.entrySet()) {
                if (entry.getKey().isAnnotation()) {
                    String entryClassName = entry.getKey().getName();
                    for (AnnotationEntry annotationEntry : annotationEntries) {
                        if (entryClassName.equals(getClassName(annotationEntry.getAnnotationType()))) {
                            if (clazz == null) {
                                clazz = Introspection.loadClass(context, className);
                                if (clazz == null) {
                                    // continuing
                                    return;
                                }
                            }
                            for (ServletContainerInitializer sci : entry.getValue()) {
                                initializerClassMap.get(sci).add(clazz);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
Also used : Set(java.util.Set) RuleSet(org.apache.tomcat.util.digester.RuleSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) JavaClass(org.apache.tomcat.util.bcel.classfile.JavaClass) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with AnnotationEntry

use of org.apache.tomcat.util.bcel.classfile.AnnotationEntry in project tomcat70 by apache.

the class ContextConfig method processAnnotationWebServlet.

protected void processAnnotationWebServlet(String className, AnnotationEntry ae, WebXml fragment) {
    String servletName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("name".equals(name)) {
            servletName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (servletName == null) {
        // classname is default servletName as annotation has no name!
        servletName = className;
    }
    ServletDef servletDef = fragment.getServlets().get(servletName);
    boolean isWebXMLservletDef;
    if (servletDef == null) {
        servletDef = new ServletDef();
        servletDef.setServletName(servletName);
        servletDef.setServletClass(className);
        isWebXMLservletDef = false;
    } else {
        isWebXMLservletDef = true;
    }
    boolean urlPatternsSet = false;
    String[] urlPatterns = null;
    // List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebServlet", className));
            }
            urlPatternsSet = true;
            urlPatterns = processAnnotationsStringArray(evp.getValue());
        } else if ("description".equals(name)) {
            if (servletDef.getDescription() == null) {
                servletDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (servletDef.getDisplayName() == null) {
                servletDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (servletDef.getLargeIcon() == null) {
                servletDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (servletDef.getSmallIcon() == null) {
                servletDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (servletDef.getAsyncSupported() == null) {
                servletDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("loadOnStartup".equals(name)) {
            if (servletDef.getLoadOnStartup() == null) {
                servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLservletDef) {
                Map<String, String> webXMLInitParams = servletDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        servletDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    servletDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLservletDef && urlPatterns != null) {
        fragment.addServlet(servletDef);
    }
    if (urlPatterns != null) {
        if (!fragment.getServletMappings().containsValue(servletName)) {
            for (String urlPattern : urlPatterns) {
                fragment.addServletMapping(urlPattern, servletName);
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) ServletDef(org.apache.catalina.deploy.ServletDef) FilterMap(org.apache.catalina.deploy.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with AnnotationEntry

use of org.apache.tomcat.util.bcel.classfile.AnnotationEntry in project tomcat by apache.

the class ContextConfig method processAnnotationWebServlet.

protected void processAnnotationWebServlet(String className, AnnotationEntry ae, WebXml fragment) {
    String servletName = null;
    // must search for name s. Spec Servlet API 3.0 - 8.2.3.3.n.ii page 81
    List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("name".equals(name)) {
            servletName = evp.getValue().stringifyValue();
            break;
        }
    }
    if (servletName == null) {
        // classname is default servletName as annotation has no name!
        servletName = className;
    }
    ServletDef servletDef = fragment.getServlets().get(servletName);
    boolean isWebXMLservletDef;
    if (servletDef == null) {
        servletDef = new ServletDef();
        servletDef.setServletName(servletName);
        servletDef.setServletClass(className);
        isWebXMLservletDef = false;
    } else {
        isWebXMLservletDef = true;
    }
    boolean urlPatternsSet = false;
    String[] urlPatterns = null;
    // List<ElementValuePair> evps = ae.getElementValuePairs();
    for (ElementValuePair evp : evps) {
        String name = evp.getNameString();
        if ("value".equals(name) || "urlPatterns".equals(name)) {
            if (urlPatternsSet) {
                throw new IllegalArgumentException(sm.getString("contextConfig.urlPatternValue", "WebServlet", className));
            }
            urlPatternsSet = true;
            urlPatterns = processAnnotationsStringArray(evp.getValue());
        } else if ("description".equals(name)) {
            if (servletDef.getDescription() == null) {
                servletDef.setDescription(evp.getValue().stringifyValue());
            }
        } else if ("displayName".equals(name)) {
            if (servletDef.getDisplayName() == null) {
                servletDef.setDisplayName(evp.getValue().stringifyValue());
            }
        } else if ("largeIcon".equals(name)) {
            if (servletDef.getLargeIcon() == null) {
                servletDef.setLargeIcon(evp.getValue().stringifyValue());
            }
        } else if ("smallIcon".equals(name)) {
            if (servletDef.getSmallIcon() == null) {
                servletDef.setSmallIcon(evp.getValue().stringifyValue());
            }
        } else if ("asyncSupported".equals(name)) {
            if (servletDef.getAsyncSupported() == null) {
                servletDef.setAsyncSupported(evp.getValue().stringifyValue());
            }
        } else if ("loadOnStartup".equals(name)) {
            if (servletDef.getLoadOnStartup() == null) {
                servletDef.setLoadOnStartup(evp.getValue().stringifyValue());
            }
        } else if ("initParams".equals(name)) {
            Map<String, String> initParams = processAnnotationWebInitParams(evp.getValue());
            if (isWebXMLservletDef) {
                Map<String, String> webXMLInitParams = servletDef.getParameterMap();
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    if (webXMLInitParams.get(entry.getKey()) == null) {
                        servletDef.addInitParameter(entry.getKey(), entry.getValue());
                    }
                }
            } else {
                for (Map.Entry<String, String> entry : initParams.entrySet()) {
                    servletDef.addInitParameter(entry.getKey(), entry.getValue());
                }
            }
        }
    }
    if (!isWebXMLservletDef && urlPatterns != null) {
        fragment.addServlet(servletDef);
    }
    if (urlPatterns != null) {
        if (!fragment.getServletMappings().containsValue(servletName)) {
            for (String urlPattern : urlPatterns) {
                fragment.addServletMapping(urlPattern, servletName);
            }
        }
    }
}
Also used : Entry(java.util.Map.Entry) AnnotationEntry(org.apache.tomcat.util.bcel.classfile.AnnotationEntry) ElementValuePair(org.apache.tomcat.util.bcel.classfile.ElementValuePair) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

AnnotationEntry (org.apache.tomcat.util.bcel.classfile.AnnotationEntry)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 JavaClass (org.apache.tomcat.util.bcel.classfile.JavaClass)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 FilterMap (org.apache.catalina.deploy.FilterMap)2 ElementValuePair (org.apache.tomcat.util.bcel.classfile.ElementValuePair)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 RuleSet (org.apache.tomcat.util.digester.RuleSet)2 ServletContainerInitializer (jakarta.servlet.ServletContainerInitializer)1 Entry (java.util.Map.Entry)1 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)1 ServletDef (org.apache.catalina.deploy.ServletDef)1 ClassParser (org.apache.tomcat.util.bcel.classfile.ClassParser)1 ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)1