webservicecontextinjectorimpl.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 391 行 · 第 1/2 页

JAVA
391
字号


    }

    /**
     * Set accessible.  This method must remain private
     *
     * @param obj   AccessibleObject
     * @param value true or false
     */
    private static void setAccessible(final AccessibleObject obj, final boolean value) {
        AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                obj.setAccessible(value);
                return null;
            }
        });

    }

    /*
      * Search for Field with @Resource Annotation.
      */
    private Field searchFieldsForResourceAnnotation(Class bean) {
        if (bean == null) {
            return null;
        }
        List<Field> fields = getFields(bean);
        for (Field field : fields) {
            Annotation[] annotations = field.getAnnotations();
            for (Annotation an : annotations) {
                if (Resource.class.isAssignableFrom(an.getClass())) {
                    //check to make sure it is a @Resource for WebServiceContext.
                    Resource atResource = (Resource)an;
                    Class type = atResource.type();
                    if (isWebServiceContextResource(atResource, field)) {
                        return field;
                    }
                }
            }
        }
        return null;
    }

    /**
     * Gets all of the fields in this class and the super classes
     *
     * @param beanClass
     * @return
     */
    static private List<Field> getFields(final Class beanClass) {
        // This class must remain private due to Java 2 Security concerns
        List<Field> fields;
        fields = (List<Field>)AccessController.doPrivileged(
                new PrivilegedAction() {
                    public Object run() {
                        List<Field> fields = new ArrayList<Field>();
                        Class cls = beanClass;
                        while (cls != null) {
                            Field[] fieldArray = cls.getDeclaredFields();
                            for (Field field : fieldArray) {
                                fields.add(field);
                            }
                            cls = cls.getSuperclass();
                        }
                        return fields;
                    }
                }
        );

        return fields;
    }

    /**
     * Gets all of the fields in this class and the super classes
     *
     * @param beanClass
     * @return
     */
    static private List<Method> getMethods(final Class beanClass) {
        // This class must remain private due to Java 2 Security concerns
        List<Method> methods;
        methods = (List<Method>)AccessController.doPrivileged(
                new PrivilegedAction() {
                    public Object run() {
                        List<Method> methods = new ArrayList<Method>();
                        Class cls = beanClass;
                        while (cls != null) {
                            Method[] methodArray = cls.getDeclaredMethods();
                            for (Method method : methodArray) {
                                methods.add(method);
                            }
                            cls = cls.getSuperclass();
                        }
                        return methods;
                    }
                }
        );

        return methods;
    }

    /*
      * Search for Method with @Resource Annotation
      */
    private Method searchMethodsResourceAnnotation(Class bean) {
        if (bean == null) {
            return null;
        }
        List<Method> methods = getMethods(bean);
        for (Method method : methods) {
            Annotation[] annotations = method.getAnnotations();
            for (Annotation an : annotations) {
                if (Resource.class.isAssignableFrom(an.getClass())) {
                    //check to make sure it is a @Resource for WebServiceContext.
                    Resource atResource = (Resource)an;
                    if (isWebServiceContextResource(atResource, method)) {
                        return method;
                    }
                }
            }
        }
        return null;
    }

    private boolean isWebServiceContextResource(Resource atResource, Field field) {
        Class type = atResource.type();
        if (type == java.lang.Object.class) {
            if (field != null && field.getType() == WebServiceContext.class) {
                return true;
            }
        } else if (type == WebServiceContext.class) {
            //TODO: Should I check if the field declared type is assignable from WebServiceContext. Spec is not clear about this.
            return true;
        }
        if (log.isDebugEnabled()) {
            log.debug(
                    "Invalid Field type or Resource Type found, cannot inject WebServiceContext on this field");
        }
        return false;
    }

    private boolean isWebServiceContextResource(Resource atResource, Method method) {
        //As per JSR-250 the method injection is nothing but setter based injection,
        //Such a injection can happen if method name starts with "set" returns a void and
        //only has one parameter and the type of this parameter must be compatible with
        //resource.

        Class type = atResource.type();
        Class[] paramTypes = method.getParameterTypes();
        for (Class paramType : paramTypes)
            if (type == java.lang.Object.class) {
                if (paramType == WebServiceContext.class ||
                        paramType.isAssignableFrom(WebServiceContext.class)) {
                    return true;
                }
            } else if (type == WebServiceContext.class) {
                //TODO: Should I check if the field declared type is assignable from WebServiceContext. Spec is not clear about this.
                return true;
            }
        if (log.isDebugEnabled()) {
            log.debug(
                    "Invalid Field type or Resource Type found, cannot inject WebServiceContext on this method");
        }
        return false;
    }

    private boolean isValidMethod(Method method) {
        //As per JSR-250 the method injection is nothing but setter based injection,
        //Such a injection can happen if method name starts with "set" returns a void and
        //only has one parameter and the type of this parameter must be compatible with
        //resource.
        String name = method.getName();
        Class returnType = method.getReturnType();
        Class[] types = method.getParameterTypes();
        int noOfDeclaredParameter = 0;
        if (types != null) {
            noOfDeclaredParameter = types.length;
        }

        if (name.startsWith(METHOD_NAME) && noOfDeclaredParameter == NUMBER_OF_PARAMETERS &&
                returnType.getName().equals(RETURN_TYPE)) {
            return true;
        }
        if (log.isDebugEnabled()) {
            log.debug(
                    "Method found with @Resource annotaion and input param to set WebServiceContext Object, However method did not meet the criteria for injection as per JSR-250");
        }
        return false;

    }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?