defaultschemagenerator.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 972 行 · 第 1/3 页
JAVA
972 行
/*
* 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.description.java2wsdl;
import org.apache.axis2.AxisFault;
import org.apache.axis2.deployment.util.Utils;
import org.apache.axis2.description.AxisMessage;
import org.apache.axis2.description.AxisOperation;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.WSDL2Constants;
import org.apache.axis2.description.java2wsdl.bytecode.MethodTable;
import org.apache.axis2.wsdl.WSDLConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.commons.schema.*;
import org.apache.ws.commons.schema.utils.NamespaceMap;
import org.apache.ws.commons.schema.utils.NamespacePrefixList;
import org.codehaus.jam.*;
import javax.xml.namespace.QName;
import javax.activation.DataHandler;
import java.util.*;
public class DefaultSchemaGenerator implements Java2WSDLConstants, SchemaGenerator {
private static final Log log = LogFactory.getLog(DefaultSchemaGenerator.class);
public static final String NAME_SPACE_PREFIX = "ax2";// axis2 name space
private static int prefixCount = 1;
protected Map targetNamespacePrefixMap = new Hashtable();
protected Map schemaMap = new Hashtable();
protected XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
protected ClassLoader classLoader;
protected String className;
protected TypeTable typeTable = new TypeTable();
// to keep loadded method using JAM
protected JMethod methods[];
//to store byte code method using Axis 1.x codes
protected MethodTable methodTable;
protected String schemaTargetNameSpace;
protected String schema_namespace_prefix;
protected String attrFormDefault = null;
protected String elementFormDefault = null;
protected ArrayList excludeMethods = new ArrayList();
protected ArrayList extraClasses = null;
protected boolean useWSDLTypesNamespace = false;
protected Map pkg2nsmap = null;
protected NamespaceGenerator nsGen = null;
protected String targetNamespace = null;
//to keep the list of operation which uses MR other than RPC MR
protected ArrayList nonRpcMethods = new ArrayList();
protected Class serviceClass = null;
protected AxisService service;
//To check whether we need to generate Schema element for Exception
protected boolean generateBaseException ;
public NamespaceGenerator getNsGen() throws Exception {
if (nsGen == null) {
nsGen = new DefaultNamespaceGenerator();
}
return nsGen;
}
public void setNsGen(NamespaceGenerator nsGen) {
this.nsGen = nsGen;
}
public DefaultSchemaGenerator(ClassLoader loader, String className,
String schematargetNamespace,
String schematargetNamespacePrefix,
AxisService service)
throws Exception {
this.classLoader = loader;
this.className = className;
this.service = service;
serviceClass = Class.forName(className, true, loader);
methodTable = new MethodTable(serviceClass);
this.targetNamespace = Java2WSDLUtils.targetNamespaceFromClassName(
className, loader, getNsGen()).toString();
if (schematargetNamespace != null
&& schematargetNamespace.trim().length() != 0) {
this.schemaTargetNameSpace = schematargetNamespace;
} else {
this.schemaTargetNameSpace =
Java2WSDLUtils.schemaNamespaceFromClassName(className, loader, getNsGen())
.toString();
}
if (schematargetNamespacePrefix != null
&& schematargetNamespacePrefix.trim().length() != 0) {
this.schema_namespace_prefix = schematargetNamespacePrefix;
} else {
this.schema_namespace_prefix = SCHEMA_NAMESPACE_PRFIX;
}
}
/**
* Generates schema for all the parameters in method. First generates schema for all different
* parameter type and later refers to them.
*
* @return Returns XmlSchema.
* @throws Exception
*/
public Collection generateSchema() throws Exception {
JamServiceFactory factory = JamServiceFactory.getInstance();
JamServiceParams jam_service_parms = factory.createServiceParams();
//setting the classLoder
//it can posible to add the classLoader as well
jam_service_parms.addClassLoader(classLoader);
jam_service_parms.includeClass(className);
for (int count = 0; count < getExtraClasses().size(); ++count) {
jam_service_parms.includeClass((String) getExtraClasses().get(count));
}
JamService service = factory.createService(jam_service_parms);
QName extraSchemaTypeName;
JamClassIterator jClassIter = service.getClasses();
//all most all the time the ittr will have only one class in it
while (jClassIter.hasNext()) {
JClass jclass = (JClass) jClassIter.next();
if (getQualifiedName(jclass).equals(className)) {
/**
* Schema genertaion done in two stage 1. Load all the methods and
* create type for methods parameters (if the parameters are Bean
* then it will create Complex types for those , and if the
* parameters are simple type which decribe in SimpleTypeTable
* nothing will happen) 2. In the next stage for all the methods
* messages and port types will be creteated
*/
JAnnotation annotation = jclass.getAnnotation(AnnotationConstants.WEB_SERVICE);
if (annotation != null) {
String tns =
annotation.getValue(AnnotationConstants.TARGETNAMESPACE).asString();
if (tns != null && !"".equals(tns)) {
targetNamespace = tns;
schemaTargetNameSpace = tns;
}
}
methods = processMethods(jclass.getDeclaredMethods());
} else {
//generate the schema type for extra classes
extraSchemaTypeName = typeTable.getSimpleSchemaTypeName(getQualifiedName(jclass));
if (extraSchemaTypeName == null) {
generateSchema(jclass);
}
}
}
return schemaMap.values();
}
protected JMethod[] processMethods(JMethod[] declaredMethods) throws Exception {
ArrayList list = new ArrayList();
//short the elements in the array
Arrays.sort(declaredMethods);
// since we do not support overload
HashMap uniqueMethods = new HashMap();
XmlSchemaComplexType methodSchemaType;
XmlSchemaSequence sequence = null;
for (int i = 0; i < declaredMethods.length; i++) {
JMethod jMethod = declaredMethods[i];
JAnnotation methodAnnon = jMethod.getAnnotation(AnnotationConstants.WEB_METHOD);
if (methodAnnon != null) {
if (methodAnnon.getValue(AnnotationConstants.EXCLUDE).asBoolean()) {
continue;
}
}
String methodName = getSimpleName(jMethod);
// no need to think abt this method , since that is system
// config method
if (excludeMethods.contains(methodName)) {
continue;
}
if (uniqueMethods.get(methodName) != null) {
log.warn("We don't support method overloading. Ignoring [" +
jMethod.getQualifiedName() + "]");
continue;
}
if (!jMethod.isPublic()) {
// no need to generate Schema for non public methods
continue;
}
boolean addToService = false;
AxisOperation axisOperation = service.getOperation(new QName(methodName));
if (axisOperation == null) {
axisOperation = Utils.getAxisOperationForJmethod(jMethod);
if (WSDL2Constants.MEP_URI_ROBUST_IN_ONLY.equals(
axisOperation.getMessageExchangePattern())){
AxisMessage outMessage = axisOperation.getMessage(
WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
if (outMessage !=null ){
outMessage.setName(methodName + RESPONSE);
}
}
addToService = true;
}
// Maintain a list of methods we actually work with
list.add(jMethod);
processException(jMethod,axisOperation);
uniqueMethods.put(methodName, jMethod);
JParameter[] paras = jMethod.getParameters();
String parameterNames[] = null;
AxisMessage inMessage = axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
if (inMessage != null) {
inMessage.setName(methodName + Java2WSDLConstants.MESSAGE_SUFFIX);
}
if (paras.length > 0) {
parameterNames = methodTable.getParameterNames(methodName);
sequence = new XmlSchemaSequence();
methodSchemaType = createSchemaTypeForMethodPart(methodName);
methodSchemaType.setParticle(sequence);
inMessage.setElementQName(typeTable.getQNamefortheType(methodName));
service.addMessageElementQNameToOperationMapping(methodSchemaType.getQName(),
axisOperation);
}
for (int j = 0; j < paras.length; j++) {
JParameter methodParameter = paras[j];
String parameterName = null;
JAnnotation paramterAnnon =
methodParameter.getAnnotation(AnnotationConstants.WEB_PARAM);
if (paramterAnnon != null) {
parameterName =
paramterAnnon.getValue(AnnotationConstants.NAME).asString();
}
if (parameterName == null || "".equals(parameterName)) {
parameterName = (parameterNames != null && parameterNames[j] != null) ?
parameterNames[j] : getSimpleName(methodParameter);
}
JClass paraType = methodParameter.getType();
if (nonRpcMethods.contains(getSimpleName(jMethod))) {
generateSchemaForType(sequence, null, getSimpleName(jMethod));
break;
} else {
generateSchemaForType(sequence, paraType, parameterName);
}
}
// for its return type
JClass returnType = jMethod.getReturnType();
if (!returnType.isVoidType()) {
String partQname = methodName + RESPONSE;
methodSchemaType =
createSchemaTypeForMethodPart(partQname);
sequence = new XmlSchemaSequence();
methodSchemaType.setParticle(sequence);
JAnnotation returnAnnon =
jMethod.getAnnotation(AnnotationConstants.WEB_RESULT);
String returnName = "return";
if (returnAnnon != null) {
returnName = returnAnnon.getValue(AnnotationConstants.NAME).asString();
if (returnName != null && !"".equals(returnName)) {
returnName = "return";
}
}
if (nonRpcMethods.contains(getSimpleName(jMethod))) {
generateSchemaForType(sequence, null, returnName);
} else {
generateSchemaForType(sequence, returnType, returnName);
}
AxisMessage outMessage = axisOperation.getMessage(
WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
outMessage.setElementQName(typeTable.getQNamefortheType(partQname));
outMessage.setName(partQname);
service.addMessageElementQNameToOperationMapping(methodSchemaType.getQName(),
axisOperation);
}
if (addToService) {
service.addOperation(axisOperation);
}
}
return (JMethod[]) list.toArray(new JMethod[list.size()]);
}
/**
* This method will generate Schema element for all the excetion types in a given JMethod
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?