webservicecontextinjectorimpl.java

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

JAVA
391
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.jaxws.server.endpoint.injection.impl;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;

import org.apache.axis2.java.security.AccessController;
import org.apache.axis2.jaxws.context.WebServiceContextImpl;
import org.apache.axis2.jaxws.i18n.Messages;
import org.apache.axis2.jaxws.injection.ResourceInjectionException;
import org.apache.axis2.jaxws.server.endpoint.injection.WebServiceContextInjector;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WebServiceContextInjectorImpl implements WebServiceContextInjector {
    private static final Log log = LogFactory.getLog(WebServiceContextInjectorImpl.class);
    private static String METHOD_NAME = "set";
    private static int NUMBER_OF_PARAMETERS = 1;
    private static String RETURN_TYPE = "void";

    public WebServiceContextInjectorImpl() {
        super();

    }

    /* (non-Javadoc)
      * @see org.apache.axis2.jaxws.server.endpoint.injection.WebServiceContextInjection#addMessageContext(javax.xml.ws.WebServiceContext, javax.xml.ws.handler.MessageContext)
      */
    public void addMessageContext(WebServiceContext wc, MessageContext mc) {
        WebServiceContextImpl wsContext = (WebServiceContextImpl)wc;
        wsContext.setSoapMessageContext(mc);

    }

    /* (non-Javadoc)
      * @see org.apache.axis2.jaxws.server.endpoint.injection.ResourceInjection#inject(java.lang.Object, java.lang.Object)
      */
    public void inject(Object resource, Object instance) throws ResourceInjectionException {
        if (instance == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot inject Resource on a null Service Instance.");
            }
            throw new ResourceInjectionException(
                    Messages.getMessage("WebServiceContextInjectionImplErr1"));
        }

        Class serviceClazz = instance.getClass();

        /*Look for @Resource annotation on Field. If found then look for type on the annotation. If found, then
           * if type is java.lang.Object then make sure type on declared field in javax.xml.WebServiceContext and assign the resource value
           * if type is javax.xml.WebServiceContext then assign the resource value
          */
        Field resourceField = searchFieldsForResourceAnnotation(serviceClazz);
        if (resourceField != null) {
            if (log.isDebugEnabled()) {
                log.debug("Attempting to inject Resource on Field");
            }
            //Found field that has a @Resource for WebServiceContext
            //Inject Resource.
            injectOnField(resource, instance, resourceField);
            if (log.isDebugEnabled()) {
                log.debug("Resource Injected on Field");
            }
            return;
        }

        /* If @Resource annotation not found on declared Fileds, then look for it on Methods. If found then,
           * if @Resource type is java.lang.Object then,
           * look for PropertyDescriptor who's write Method or setter is the Method on which you found the annotation and
           * make sure that the declared type of that property is javax.xml.WebServiceContext and invoke the Method with Resource.
           * if @Resource type is javax.xml.ws.WebServiceContext, Invoke the Method with Resource.
           */
        Method method = searchMethodsResourceAnnotation(serviceClazz);
        if (method != null) {
            if (log.isDebugEnabled()) {
                log.debug("Attempting to inject resource on Method");
            }
            if (!isValidMethod(method)) {
                if (log.isDebugEnabled()) {
                    log.debug(
                            "injection can happen using method 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.");
                }
                throw new ResourceInjectionException(
                        Messages.getMessage("WebServiceContextInjectionImplErr6"));
            }
            injectOnMethod(resource, instance, method);
            if (log.isDebugEnabled()) {
                log.debug("Resource Injected");
            }
            return;
        }

        //Nothing to inject
    }

    /* (non-Javadoc)
      * @see org.apache.axis2.jaxws.server.endpoint.injection.ResourceInjection#injectOnClass(java.lang.Object, java.lang.Object, java.lang.Class)
      */
    public void injectOnClass(Object resource, Object instance, Class clazz)
            throws ResourceInjectionException {
        throw new UnsupportedOperationException(
                "WebServiceContext Injeciton on Class not yet supported");

    }

    /* (non-Javadoc)
      * @see org.apache.axis2.jaxws.server.endpoint.injection.ResourceInjection#injectOnField(java.lang.Object, java.lang.Object, java.lang.reflect.Field)
      */
    public void injectOnField(Object resource, Object instance, Field field)
            throws ResourceInjectionException {
        if (instance == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot inject Resource on a null Service Instance.");
            }
            throw new ResourceInjectionException(
                    Messages.getMessage("WebServiceContextInjectionImplErr1"));
        }
        if (field == null) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Cannot inject WebServiceContext on ServiceInstance Field, field cannot be NULL");
            }
            throw new ResourceInjectionException(
                    Messages.getMessage("WebServiceContextInjectionImplErr3"));
        }
        try {
            if (!Modifier.isPublic(field.getModifiers())) {
                setAccessible(field, true);
            }
            //Inject Resource.
            field.set(instance, resource);
        } catch (IllegalAccessException e) {
            throw new ResourceInjectionException(e);
        }

    }

    /* (non-Javadoc)
      * @see org.apache.axis2.jaxws.server.endpoint.injection.ResourceInjection#injectOnMethod(java.lang.Object, java.lang.Object, java.lang.reflect.Method)
      */
    public void injectOnMethod(Object resource, Object instance, Method method)
            throws ResourceInjectionException {
        if (instance == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot inject Resource on a null Service Instance.");
            }
            throw new ResourceInjectionException(
                    Messages.getMessage("WebServiceContextInjectionImplErr1"));
        }
        if (method == null) {
            if (log.isDebugEnabled()) {
                log.debug(
                        "Cannot inject WebServiceContext on ServiceInstance Method, method cannot be NULL");
            }
            throw new ResourceInjectionException(
                    Messages.getMessage("WebServiceContextInjectionImplErr3"));
        }
        try {
            if (!Modifier.isPublic(method.getModifiers())) {
                setAccessible(method, true);
            }
            method.invoke(instance, resource);
            return;
        } catch (IllegalAccessException e) {
            throw new ResourceInjectionException(e);
        } catch (InvocationTargetException e) {
            throw new ResourceInjectionException(e);
        }

⌨️ 快捷键说明

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