Java Code Examples for org.apache.catalina.util.Introspection#loadClass()

The following examples show how to use org.apache.catalina.util.Introspection#loadClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebAnnotationSet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Process the annotations for the listeners.
 */
protected static void loadApplicationListenerAnnotations(Context context) {
    Class<?> classClass = null;
    String[] applicationListeners =
            context.findApplicationListeners();
    for (int i = 0; i < applicationListeners.length; i++) {
        classClass = Introspection.loadClass(context,
                applicationListeners[i]);
        if (classClass == null) {
            continue;
        }

        loadClassAnnotation(context, classClass);
        loadFieldsAnnotation(context, classClass);
        loadMethodsAnnotation(context, classClass);
    }
}
 
Example 2
Source File: WebAnnotationSet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * Process the annotations for the filters.
 */
protected static void loadApplicationFilterAnnotations(Context context) {
    Class<?> classClass = null;
    FilterDef[] filterDefs = context.findFilterDefs();
    for (int i = 0; i < filterDefs.length; i++) {
        classClass = Introspection.loadClass(context,
                (filterDefs[i]).getFilterClass());
        if (classClass == null) {
            continue;
        }

        loadClassAnnotation(context, classClass);
        loadFieldsAnnotation(context, classClass);
        loadMethodsAnnotation(context, classClass);
    }
}
 
Example 3
Source File: WebAnnotationSet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Process the annotations for the listeners.
 */
protected static void loadApplicationListenerAnnotations(Context context) {
    Class<?> classClass = null;
    String[] applicationListeners =
            context.findApplicationListeners();
    for (int i = 0; i < applicationListeners.length; i++) {
        classClass = Introspection.loadClass(context,
                applicationListeners[i]);
        if (classClass == null) {
            continue;
        }

        loadClassAnnotation(context, classClass);
        loadFieldsAnnotation(context, classClass);
        loadMethodsAnnotation(context, classClass);
    }
}
 
Example 4
Source File: WebAnnotationSet.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * Process the annotations for the filters.
 */
protected static void loadApplicationFilterAnnotations(Context context) {
    Class<?> classClass = null;
    FilterDef[] filterDefs = context.findFilterDefs();
    for (int i = 0; i < filterDefs.length; i++) {
        classClass = Introspection.loadClass(context,
                (filterDefs[i]).getFilterClass());
        if (classClass == null) {
            continue;
        }

        loadClassAnnotation(context, classClass);
        loadFieldsAnnotation(context, classClass);
        loadMethodsAnnotation(context, classClass);
    }
}
 
Example 5
Source File: WebAnnotationSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Process the annotations for the listeners.
 *
 * @param context The context which will have its annotations processed
 */
protected static void loadApplicationListenerAnnotations(Context context) {
    String[] applicationListeners = context.findApplicationListeners();
    for (String className : applicationListeners) {
        Class<?> clazz = Introspection.loadClass(context, className);
        if (clazz == null) {
            continue;
        }

        loadClassAnnotation(context, clazz);
        loadFieldsAnnotation(context, clazz);
        loadMethodsAnnotation(context, clazz);
    }
}
 
Example 6
Source File: WebAnnotationSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Process the annotations for the filters.
 *
 * @param context The context which will have its annotations processed
 */
protected static void loadApplicationFilterAnnotations(Context context) {
    FilterDef[] filterDefs = context.findFilterDefs();
    for (FilterDef filterDef : filterDefs) {
        Class<?> clazz = Introspection.loadClass(context, filterDef.getFilterClass());
        if (clazz == null) {
            continue;
        }

        loadClassAnnotation(context, clazz);
        loadFieldsAnnotation(context, clazz);
        loadMethodsAnnotation(context, clazz);
    }
}
 
Example 7
Source File: WebAnnotationSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Process the annotations for the servlets.
 *
 * @param context The context which will have its annotations processed
 */
protected static void loadApplicationServletAnnotations(Context context) {

    Container[] children = context.findChildren();
    for (Container child : children) {
        if (child instanceof Wrapper) {

            Wrapper wrapper = (Wrapper) child;
            if (wrapper.getServletClass() == null) {
                continue;
            }

            Class<?> clazz = Introspection.loadClass(context, wrapper.getServletClass());
            if (clazz == null) {
                continue;
            }

            loadClassAnnotation(context, clazz);
            loadFieldsAnnotation(context, clazz);
            loadMethodsAnnotation(context, clazz);

            /* Process RunAs annotation which can be only on servlets.
             * Ref JSR 250, equivalent to the run-as element in
             * the deployment descriptor
             */
            RunAs runAs = clazz.getAnnotation(RunAs.class);
            if (runAs != null) {
                wrapper.setRunAs(runAs.value());
            }

            // Process ServletSecurity annotation
            ServletSecurity servletSecurity = clazz.getAnnotation(ServletSecurity.class);
            if (servletSecurity != null) {
                context.addServletSecurity(
                        new ApplicationServletRegistration(wrapper, context),
                        new ServletSecurityElement(servletSecurity));
            }
        }
    }
}
 
Example 8
Source File: NamingResourcesImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Checks that the configuration of the type for the specified resource is
 * consistent with any injection targets and if the type is not specified,
 * tries to configure the type based on the injection targets
 *
 * @param resource  The resource to check
 *
 * @return  <code>true</code> if the type for the resource is now valid (if
 *          previously <code>null</code> this means it is now set) or
 *          <code>false</code> if the current resource type is inconsistent
 *          with the injection targets and/or cannot be determined
 */
private boolean checkResourceType(ResourceBase resource) {
    if (!(container instanceof Context)) {
        // Only Context's will have injection targets
        return true;
    }

    if (resource.getInjectionTargets() == null ||
            resource.getInjectionTargets().size() == 0) {
        // No injection targets so use the defined type for the resource
        return true;
    }

    Context context = (Context) container;

    String typeName = resource.getType();
    Class<?> typeClass = null;
    if (typeName != null) {
        typeClass = Introspection.loadClass(context, typeName);
        if (typeClass == null) {
            // Can't load the type - will trigger a failure later so don't
            // fail here
            return true;
        }
    }

    Class<?> compatibleClass =
            getCompatibleType(context, resource, typeClass);
    if (compatibleClass == null) {
        // Indicates that a compatible type could not be identified that
        // worked for all injection targets
        return false;
    }

    resource.setType(compatibleClass.getCanonicalName());
    return true;
}
 
Example 9
Source File: NamingResources.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the configuration of the type for the specified resource is
 * consistent with any injection targets and if the type is not specified,
 * tries to configure the type based on the injection targets
 *
 * @param resource  The resource to check
 *
 * @return  <code>true</code> if the type for the resource is now valid (if
 *          previously <code>null</code> this means it is now set) or
 *          <code>false</code> if the current resource type is inconsistent
 *          with the injection targets and/or cannot be determined
 */
private boolean checkResourceType(ResourceBase resource) {
    if (!(container instanceof Context)) {
        // Only Context's will have injection targets
        return true;
    }

    if (resource.getInjectionTargets() == null ||
            resource.getInjectionTargets().size() == 0) {
        // No injection targets so use the defined type for the resource
        return true;
    }

    Context context = (Context) container;

    String typeName = resource.getType();
    Class<?> typeClass = null;
    if (typeName != null) {
        typeClass = Introspection.loadClass(context, typeName);
        if (typeClass == null) {
            // Can't load the type - will trigger a failure later so don't
            // fail here
            return true;
        }
    }

    Class<?> compatibleClass =
            getCompatibleType(context, resource, typeClass);
    if (compatibleClass == null) {
        // Indicates that a compatible type could not be identified that
        // worked for all injection targets
        return false;
    }

    resource.setType(compatibleClass.getCanonicalName());
    return true;
}
 
Example 10
Source File: NamingResources.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Checks that the configuration of the type for the specified resource is
 * consistent with any injection targets and if the type is not specified,
 * tries to configure the type based on the injection targets
 *
 * @param resource  The resource to check
 *
 * @return  <code>true</code> if the type for the resource is now valid (if
 *          previously <code>null</code> this means it is now set) or
 *          <code>false</code> if the current resource type is inconsistent
 *          with the injection targets and/or cannot be determined
 */
private boolean checkResourceType(ResourceBase resource) {
    if (!(container instanceof Context)) {
        // Only Context's will have injection targets
        return true;
    }

    if (resource.getInjectionTargets() == null ||
            resource.getInjectionTargets().size() == 0) {
        // No injection targets so use the defined type for the resource
        return true;
    }

    Context context = (Context) container;

    String typeName = resource.getType();
    Class<?> typeClass = null;
    if (typeName != null) {
        typeClass = Introspection.loadClass(context, typeName);
        if (typeClass == null) {
            // Can't load the type - will trigger a failure later so don't
            // fail here
            return true;
        }
    }

    Class<?> compatibleClass =
            getCompatibleType(context, resource, typeClass);
    if (compatibleClass == null) {
        // Indicates that a compatible type could not be identified that
        // worked for all injection targets
        return false;
    }

    resource.setType(compatibleClass.getCanonicalName());
    return true;
}
 
Example 11
Source File: NamingResourcesImpl.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private Class<?> getCompatibleType(Context context,
        ResourceBase resource, Class<?> typeClass) {

    Class<?> result = null;

    for (InjectionTarget injectionTarget : resource.getInjectionTargets()) {
        Class<?> clazz = Introspection.loadClass(
                context, injectionTarget.getTargetClass());
        if (clazz == null) {
            // Can't load class - therefore ignore this target
            continue;
        }

        // Look for a match
        String targetName = injectionTarget.getTargetName();
        // Look for a setter match first
        Class<?> targetType = getSetterType(clazz, targetName);
        if (targetType == null) {
            // Try a field match if no setter match
            targetType = getFieldType(clazz,targetName);
        }
        if (targetType == null) {
            // No match - ignore this injection target
            continue;
        }
        targetType = Introspection.convertPrimitiveType(targetType);

        if (typeClass == null) {
            // Need to find a common type amongst the injection targets
            if (result == null) {
                result = targetType;
            } else if (targetType.isAssignableFrom(result)) {
                // NO-OP - This will work
            } else if (result.isAssignableFrom(targetType)) {
                // Need to use more specific type
                result = targetType;
            } else {
                // Incompatible types
                return null;
            }
        } else {
            // Each injection target needs to be consistent with the defined
            // type
            if (targetType.isAssignableFrom(typeClass)) {
                result = typeClass;
            } else {
                // Incompatible types
                return null;
            }
        }
    }
    return result;
}
 
Example 12
Source File: ApplicationContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
private ServletRegistration.Dynamic addServlet(String servletName, String servletClass,
        Servlet servlet, Map<String,String> initParams) throws IllegalStateException {

    if (servletName == null || servletName.equals("")) {
        throw new IllegalArgumentException(sm.getString(
                "applicationContext.invalidServletName", servletName));
    }

    if (!context.getState().equals(LifecycleState.STARTING_PREP)) {
        //TODO Spec breaking enhancement to ignore this restriction
        throw new IllegalStateException(
                sm.getString("applicationContext.addServlet.ise",
                        getContextPath()));
    }

    Wrapper wrapper = (Wrapper) context.findChild(servletName);

    // Assume a 'complete' ServletRegistration is one that has a class and
    // a name
    if (wrapper == null) {
        wrapper = context.createWrapper();
        wrapper.setName(servletName);
        context.addChild(wrapper);
    } else {
        if (wrapper.getName() != null &&
                wrapper.getServletClass() != null) {
            if (wrapper.isOverridable()) {
                wrapper.setOverridable(false);
            } else {
                return null;
            }
        }
    }

    ServletSecurity annotation = null;
    if (servlet == null) {
        wrapper.setServletClass(servletClass);
        Class<?> clazz = Introspection.loadClass(context, servletClass);
        if (clazz != null) {
            annotation = clazz.getAnnotation(ServletSecurity.class);
        }
    } else {
        wrapper.setServletClass(servlet.getClass().getName());
        wrapper.setServlet(servlet);
        if (context.wasCreatedDynamicServlet(servlet)) {
            annotation = servlet.getClass().getAnnotation(ServletSecurity.class);
        }
    }

    if (initParams != null) {
        for (Map.Entry<String, String> initParam: initParams.entrySet()) {
            wrapper.addInitParameter(initParam.getKey(), initParam.getValue());
        }
    }

    ServletRegistration.Dynamic registration =
            new ApplicationServletRegistration(wrapper, context);
    if (annotation != null) {
        registration.setServletSecurity(new ServletSecurityElement(annotation));
    }
    return registration;
}
 
Example 13
Source File: NamingResources.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
private Class<?> getCompatibleType(Context context,
        ResourceBase resource, Class<?> typeClass) {

    Class<?> result = null;

    for (InjectionTarget injectionTarget : resource.getInjectionTargets()) {
        Class<?> clazz = Introspection.loadClass(
                context, injectionTarget.getTargetClass());
        if (clazz == null) {
            // Can't load class - therefore ignore this target
            continue;
        }

        // Look for a match
        String targetName = injectionTarget.getTargetName();
        // Look for a setter match first
        Class<?> targetType = getSetterType(clazz, targetName);
        if (targetType == null) {
            // Try a field match if no setter match
            targetType = getFieldType(clazz,targetName);
        }
        if (targetType == null) {
            // No match - ignore this injection target
            continue;
        }
        targetType = Introspection.convertPrimitiveType(targetType);

        if (typeClass == null) {
            // Need to find a common type amongst the injection targets
            if (result == null) {
                result = targetType;
            } else if (targetType.isAssignableFrom(result)) {
                // NO-OP - This will work
            } else if (result.isAssignableFrom(targetType)) {
                // Need to use more specific type
                result = targetType;
            } else {
                // Incompatible types
                return null;
            }
        } else {
            // Each injection target needs to be consistent with the defined
            // type
            if (targetType.isAssignableFrom(typeClass)) {
                result = typeClass;
            } else {
                // Incompatible types
                return null;
            }
        }
    }
    return result;
}
 
Example 14
Source File: NamingResources.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
private Class<?> getCompatibleType(Context context,
        ResourceBase resource, Class<?> typeClass) {

    Class<?> result = null;

    for (InjectionTarget injectionTarget : resource.getInjectionTargets()) {
        Class<?> clazz = Introspection.loadClass(
                context, injectionTarget.getTargetClass());
        if (clazz == null) {
            // Can't load class - therefore ignore this target
            continue;
        }

        // Look for a match
        String targetName = injectionTarget.getTargetName();
        // Look for a setter match first
        Class<?> targetType = getSetterType(clazz, targetName);
        if (targetType == null) {
            // Try a field match if no setter match
            targetType = getFieldType(clazz,targetName);
        }
        if (targetType == null) {
            // No match - ignore this injection target
            continue;
        }
        targetType = Introspection.convertPrimitiveType(targetType);

        if (typeClass == null) {
            // Need to find a common type amongst the injection targets
            if (result == null) {
                result = targetType;
            } else if (targetType.isAssignableFrom(result)) {
                // NO-OP - This will work
            } else if (result.isAssignableFrom(targetType)) {
                // Need to use more specific type
                result = targetType;
            } else {
                // Incompatible types
                return null;
            }
        } else {
            // Each injection target needs to be consistent with the defined
            // type
            if (targetType.isAssignableFrom(typeClass)) {
                result = typeClass;
            } else {
                // Incompatible types
                return null;
            }
        }
    }
    return result;
}