javaclasstodbcconverter.java

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

JAVA
388
字号
/*
 * 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.description.builder.converter;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebFault;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceProvider;
import javax.xml.ws.WebServiceRef;
import javax.xml.ws.WebServiceRefs;

import org.apache.axis2.jaxws.description.builder.BindingTypeAnnot;
import org.apache.axis2.jaxws.description.builder.DescriptionBuilderComposite;
import org.apache.axis2.jaxws.description.builder.FieldDescriptionComposite;
import org.apache.axis2.jaxws.description.builder.MethodDescriptionComposite;
import org.apache.axis2.jaxws.description.builder.ServiceModeAnnot;
import org.apache.axis2.jaxws.description.builder.WebFaultAnnot;
import org.apache.axis2.jaxws.description.builder.WebServiceAnnot;
import org.apache.axis2.jaxws.description.builder.WebServiceProviderAnnot;
import org.apache.axis2.jaxws.description.builder.WebServiceRefAnnot;

public class JavaClassToDBCConverter {

    private Class serviceClass;

    private String seiClassName;

    private List<Class> classes;

    public JavaClassToDBCConverter(Class serviceClass) {
        this.serviceClass = serviceClass;
        classes = new ArrayList<Class>();
        establishClassHierarchy(serviceClass);
        establishInterfaceHierarchy(serviceClass.getInterfaces());
        establishExceptionClasses(serviceClass);
    }

    /**
     * The only method we will expose to users of this class. It will trigger the creation of the
     * <code>DescriptionBuilderComposite</code> based on our service class. It will also handle the
     * case of an impl class that references an SEI.
     *
     * @return - <code>DescriptionBuilderComposite</code>
     */
    public HashMap<String, DescriptionBuilderComposite> produceDBC() {
        HashMap<String, DescriptionBuilderComposite> dbcMap = new HashMap<String,
                DescriptionBuilderComposite>();
        for (int i = 0; i < classes.size(); i++) {
            buildDBC(dbcMap, classes.get(i));
            if (seiClassName != null && !seiClassName.equals("")) {
                try {
                    Class seiClass =
                            Thread.currentThread().getContextClassLoader().loadClass(seiClassName);
                    buildDBC(dbcMap, seiClass);
                    
                    // Also try to see if the SEI has any super interfaces  
                    Class[] interfaces = seiClass.getInterfaces();
                    for (int j = 0; j < interfaces.length; j++) {
                        buildDBC(dbcMap, interfaces[i]);                            
                    }
                }
                catch (ClassNotFoundException e) {
                    // TODO: (JLB) Make this an error log?
                    System.out
                            .println("Class not found exception caught for class: " + seiClassName);
                    e.printStackTrace();
                }
            }
        }
        return dbcMap;
    }

    private void buildDBC(HashMap<String, DescriptionBuilderComposite> dbcMap, Class clazz) {
        serviceClass = clazz;
        DescriptionBuilderComposite composite = new DescriptionBuilderComposite();
        introspectClass(composite);
        dbcMap.put(composite.getClassName(), composite);        
    }
    
    /**
     * This method will drive the introspection of the class-level information. It will store the
     * gathered information in the pertinent data members of the <code>DescriptionBuilderComposite</code>
     *
     * @param composite - <code>DescriptionBuilderComposite</code>
     */
    private void introspectClass(DescriptionBuilderComposite composite) {
        // Need to investigate this, probably want to specify
        composite.setClassLoader(serviceClass.getClassLoader());
        composite.setIsInterface(serviceClass.isInterface());
        composite.setSuperClassName(serviceClass.getSuperclass() != null ? serviceClass.
                getSuperclass().getName() : null);
        composite.setClassName(serviceClass.getName());
        setInterfaces(composite);
        setTypeTargettedAnnotations(composite);
        if (serviceClass.getFields().length > 0) {
            JavaFieldsToFDCConverter fieldConverter = new JavaFieldsToFDCConverter(
                    serviceClass.getFields());
            List<FieldDescriptionComposite> fdcList = fieldConverter.convertFields();
            ConverterUtils.attachFieldDescriptionComposites(composite, fdcList);
        }
        if (serviceClass.getMethods().length > 0) {
            // Inherited methods and constructors for superclasses will be in a seperate DBC for
            // the superclass.  We only need the ones actually declared in this class.
            JavaMethodsToMDCConverter methodConverter = new JavaMethodsToMDCConverter(
                    serviceClass.getDeclaredMethods(), serviceClass.getDeclaredConstructors(),
                    serviceClass.getName());
            List<MethodDescriptionComposite> mdcList = methodConverter.convertMethods();
            ConverterUtils.attachMethodDescriptionComposites(composite, mdcList);
        }
    }

    /**
     * This method is responsible for finding any interfaces implemented by the service class. We will
     * then set these as a list of fully qualified class names on the <code>DescriptionBuilderComposite</code>
     *
     * @param composite <code>DescriptionBuilderComposite</code>
     */
    private void setInterfaces(DescriptionBuilderComposite composite) {
        Type[] interfaces = serviceClass.getGenericInterfaces();
        List<String> interfaceList = interfaces.length > 0 ? new ArrayList<String>()
                : null;
        for (int i = 0; i < interfaces.length; i++) {
            interfaceList.add(getNameFromType(interfaces[i]));
        }
        // We only want to set this list if we found interfaces b/c the
        // DBC news up an interface list as part of its static initialization.
        // Thus, if we set this list to null we may cause an NPE
        if (interfaceList != null) {
            composite.setInterfacesList(interfaceList);
        }
    }

    private String getNameFromType(Type type) {
        String returnName = null;
        if (type instanceof Class) {
            returnName = ((Class)type).getName();
        } else if (type instanceof ParameterizedType) {
            returnName = ((ParameterizedType)type).toString();
        }
        return returnName;
    }


    /**
     * This method will drive the attachment of Type targetted annotations to the
     * <code>DescriptionBuilderComposite</code>
     *
     * @param composite - <code>DescriptionBuilderComposite</code>
     */
    private void setTypeTargettedAnnotations(DescriptionBuilderComposite composite) {
        attachBindingTypeAnnotation(composite);
        attachHandlerChainAnnotation(composite);
        attachServiceModeAnnotation(composite);
        attachSoapBindingAnnotation(composite);
        attachWebFaultAnnotation(composite);
        attachWebServiceAnnotation(composite);
        attachWebServiceClientAnnotation(composite);
        attachWebServiceProviderAnnotation(composite);
        attachWebServiceRefsAnnotation(composite);
        attachWebServiceRefAnnotation(composite);
    }

    /**
     * This method will be used to attach @WebService annotation data to the
     * <code>DescriptionBuildercomposite</code>
     *
     * @param composite - <code>DescriptionBuilderComposite</code>
     */
    private void attachWebServiceAnnotation(DescriptionBuilderComposite composite) {

⌨️ 快捷键说明

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