4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Collection;
10 import java.util.HashMap;
11 import java.util.LinkedHashMap;
12 import java.util.Iterator;
14 import java.lang.reflect.*;
15 import java.lang.annotation.Annotation;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Node;
36 import org.w3c.dom.NodeList;
37 import org.w3c.dom.Element;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 import javax.xml.namespace.QName;
42 import org.apache.axis2.jaxrs.JAXRSUtils;
43 import org.apache.axis2.jaxrs.JAXRSModel;
45 import org.apache.axis2.wsdl.WSDLConstants;
47 import org.apache.axis2.description.java2wsdl.SchemaGenerator;
48 import org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator;
49 import org.apache.axis2.description.java2wsdl.DocLitBareSchemaGenerator;
50 import org.apache.axis2.description.AxisOperation;
51 import org.apache.axis2.description.AxisService;
52 import org.apache.axis2.description.AxisMessage;
54 import org.apache.axis2.deployment.util.Utils;
56 import org.apache.ws.commons.schema.*;
58 import org.apache.axis2.jsr181.JSR181Helper;
59 import org.apache.axis2.jsr181.WebMethodAnnotation;
60 import org.apache.axis2.jsr181.WebParamAnnotation;
61 import org.apache.axis2.jsr181.WebResultAnnotation;
62 import org.apache.axis2.jsr181.WebServiceAnnotation;
67 new HashMap<String,QName>();
69 new HashMap<String,String>();
71 new HashMap<String,QName>();
77 String schemaTargetNamespace,
78 String schemaTargetNamespacePrefix,
79 Map<String,String> methodClassNameMapping,
80 Map<String,QName> dynamicTypeMapping,
81 Map<String,QName> typeMapping,
85 super(loader,className,schemaTargetNamespace,schemaTargetNamespacePrefix,
92 this.service = service;
96 WebServiceAnnotation wsa =
97 JSR181Helper.INSTANCE.getWebServiceAnnotation(serviceClass);
99 String tns = wsa.getTargetNamespace();
100 if (tns != null && !
"".equals(tns)) {
101 targetNamespace = tns;
102 if (this.schemaTargetNameSpace == null)
103 this.schemaTargetNameSpace = tns;
106 && (service.getName() == null || service.getName().equals(
"")))
107 service.setName(Utils.getAnnotatedServiceName(serviceClass,wsa));
110 if (schemaURL == null)
111 return super.generateSchema();
114 InputStream ris = schemaURL.openStream();
157 xmlSchemaCollection.setSchemaResolver(dr);
159 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
160 dbf.setNamespaceAware(
true);
161 DocumentBuilder db = dbf.newDocumentBuilder();
162 Document doc = db.parse(ris);
168 String toplevel[] =
new String[] {
"definitions",
"description" };
169 for (
int i = 0;
i < toplevel.length; ++
i) {
170 NodeList nl = doc.getElementsByTagName(toplevel[
i]);
171 for (
int j = 0; j < nl.getLength(); ++j) {
172 Element defE = (Element)nl.item(j);
174 NodeList defL = doc.getElementsByTagName(
"types");
175 for (
int k = 0; k < defL.getLength(); ++k) {
176 Element typesE = (Element)defL.item(k);
178 NodeList schemaL = doc.getElementsByTagName(
"schema");
179 for (
int l = 0; l < schemaL.getLength(); ++l) {
180 Element schemaE = (Element)schemaL.item(k);
185 XmlSchema schema = xmlSchemaCollection.read(schemaE);
186 schemaMap.put(schema.getTargetNamespace(),schema);
208 if (typeMapping != null) {
209 Set<Map.Entry<String,QName>> tms = typeMapping.entrySet();
210 Iterator<Map.Entry<String,QName>> tmi = tms.iterator();
211 while (tmi.hasNext()) {
212 Map.Entry<String,QName> me = tmi.next();
213 System.err.println(
"DEBUG: adding static mapping to"
215 + me.getKey() +
" -> " + me.getValue());
216 typeTable.addComplexSchema(me.getKey(),me.getValue());
219 }
catch (Exception e) {
225 myClassModel = JAXRSUtils.getClassModel(serviceClass);
228 for (String extraClassName : getExtraClasses()) {
229 Class<?> extraClass = Class.forName(extraClassName,
true, classLoader);
230 if (typeTable.getSimpleSchemaTypeName(extraClassName) == null) {
235 return schemaMap.values();
245 XmlSchema xmlSchema =
getXmlSchema(schemaTargetNameSpace);
247 ArrayList<Method> list =
new ArrayList<Method>();
249 Arrays.sort(declaredMethods,
new MathodComparator());
252 HashMap<String,Method> uniqueMethods =
new LinkedHashMap<String,Method>();
254 for (Method jMethod : declaredMethods) {
255 if (jMethod.isBridge())
258 WebMethodAnnotation methodAnnon =
259 JSR181Helper.INSTANCE.getWebMethodAnnotation(jMethod);
260 if (methodAnnon != null && methodAnnon.isExclude())
263 String methodName = jMethod.getName();
265 if (excludeMethods.contains(methodName))
267 if (uniqueMethods.get(methodName) != null)
269 if (!Modifier.isPublic(jMethod.getModifiers()))
271 if (nonRpcMethods.contains(methodName)) {
272 System.err.println(
"DEBUG: " + methodName +
" is a nonRpcMethod;"
273 +
" letting DefaultSchemaGenerator handle!");
274 Method tmplist[] =
new Method[] { jMethod };
275 Method retval[] = super.processMethods(tmplist);
276 if (retval != null && retval.length == 1) {
278 uniqueMethods.put(methodName,jMethod);
287 String methodClassName = methodName;
288 if (methodClassNameMapping.get(methodName) != null)
289 System.err.println(
"DEBUG: mapping method " + methodName
290 +
" to class " + methodClassName);
292 new QName(xmlSchema.getTargetNamespace(),methodName);
293 if (dynamicTypeMapping.get(methodName) != null) {
294 methodQName = dynamicTypeMapping.get(methodName);
295 System.err.println(
"DEBUG: mapping method " + methodName
296 +
" type to " + methodQName);
298 XmlSchemaComplexType methodSchemaType =
300 if (methodSchemaType == null) {
301 System.err.println(
"DEBUG: could not find schema type for"
302 +
" method " + methodName +
"; letting"
303 +
"DefaultSchemaGenerator handle!");
304 Method tmplist[] =
new Method[] { jMethod };
305 Method retval[] = super.processMethods(tmplist);
306 if (retval != null && retval.length == 1) {
308 uniqueMethods.put(methodName,jMethod);
312 boolean addToService =
false;
313 AxisOperation axisOperation = service.getOperation(methodQName);
314 if (axisOperation == null) {
315 axisOperation = Utils.getAxisOperationForJmethod(jMethod);
318 if (axisOperation != null) {
320 JAXRSUtils.getMethodModel(this.myClassModel,jMethod);
321 axisOperation.addParameter(
"JAXRSAnnotaion",model);
324 Class<?>[] parameters = jMethod.getParameterTypes();
325 String parameterNames[] = null;
326 if (parameters.length > 0)
327 parameterNames = methodTable.getParameterNames(methodName);
328 Annotation[][] parameterAnnotation = jMethod.getParameterAnnotations();
329 java.lang.reflect.Type[] genericParameterTypes = jMethod.getGenericParameterTypes();
330 for (
int j = 0; j < parameters.length; j++) {
331 Class<?> methodParameter = parameters[j];
332 String parameterName =
333 getParameterName(parameterAnnotation,j,parameterNames);
334 java.lang.reflect.Type genericParameterType = genericParameterTypes[j];
339 Class<?> returnType = jMethod.getReturnType();
340 String returnClassName = returnType.getName();
341 QName returnQName = null;
342 if ((returnQName = dynamicTypeMapping.get(returnClassName)) != null) {
343 System.err.println(
"DEBUG: mapping return type " + returnClassName
344 +
" to " + returnQName);
347 int idx = returnClassName.lastIndexOf(
'.');
349 returnQName =
new QName(xmlSchema.getTargetNamespace(),
350 returnClassName.substring(idx + 1));
352 returnQName =
new QName(xmlSchema.getTargetNamespace(),
355 XmlSchemaType returnSchemaType = null;
356 if (!
"void".equals(jMethod.getReturnType().getName())) {
360 if (returnSchemaType == null) {
361 System.err.println(
"DEBUG: could not find schema type for"
362 +
"return type " + returnType
363 +
" of method " + methodName +
";"
364 +
" letting DefaultSchemaGenerator handle!");
365 Method tmplist[] =
new Method[] { jMethod };
366 Method retval[] = super.processMethods(tmplist);
367 if (retval != null && retval.length == 1) {
369 uniqueMethods.put(methodName,jMethod);
380 uniqueMethods.put(methodName, jMethod);
383 typeTable.addComplexSchema(methodClassName,methodQName);
384 typeTable.addClassNameForQName(methodQName,methodClassName);
389 if (parameters.length > 0) {
390 parameterNames = methodTable.getParameterNames(methodName);
392 service.addParameter(methodName,parameterNames);
395 AxisMessage inMessage =
396 axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
397 inMessage.setElementQName(methodQName);
398 inMessage.setName(methodQName.getLocalPart());
399 service.addMessageElementQNameToOperationMapping(methodSchemaType.getQName(),
402 typeTable.addComplexSchema(returnClassName,returnQName);
403 typeTable.addClassNameForQName(returnQName,returnClassName);
405 if (returnSchemaType != null) {
406 AxisMessage outMessage =
407 axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
408 outMessage.setElementQName(returnQName);
409 outMessage.setName(returnQName.getLocalPart());
410 service.addMessageElementQNameToOperationMapping(returnSchemaType.getQName(),
414 processException(jMethod,axisOperation);
417 service.addOperation(axisOperation);
420 return list.toArray(
new Method[list.size()]);
425 System.err.println(
"DEBUG: getComplexTypeForElement(" + name +
")");
426 XmlSchemaComplexType retval =
427 super.getComplexTypeForElement(xmlSchema,name);
428 System.err.println(
"DEBUG: getComplexTypeForElement(" + name +
") = " + retval);
429 if (retval == null) {
430 String newLocalPart = name.getLocalPart();
431 char localPartChars[] = newLocalPart.toCharArray();
432 if (localPartChars != null && localPartChars.length > 0
433 && Character.isLowerCase(localPartChars[0])) {
434 localPartChars[0] = Character.toUpperCase(localPartChars[0]);
435 newLocalPart =
new String(localPartChars);
436 name =
new QName(name.getNamespaceURI(),newLocalPart,name.getPrefix());
437 System.err.println(
"DEBUG: (uppercase) getComplexTypeForElement(" + name +
")");
439 super.getComplexTypeForElement(xmlSchema,name);
440 System.err.println(
"DEBUG: (uppercase) getComplexTypeForElement(" + name +
") = " + retval);
447 System.err.println(
"DEBUG: getXmlSchema(" + targetNamespace +
")");
448 XmlSchema xmlSchema = super.getXmlSchema(targetNamespace);
449 System.err.println(
"DEBUG: getXmlSchema(" + targetNamespace +
") = " + xmlSchema);
Method[] processMethods(Method[] declaredMethods)
static uint64_t unsigned int i
Collection< XmlSchema > generateSchema()
XmlSchemaComplexType getComplexTypeForElement(XmlSchema xmlSchema, QName name)
Map< String, String > methodClassNameMapping
Map< String, QName > dynamicTypeMapping
Map< String, QName > typeMapping
XmlSchema getXmlSchema(String targetNamespace)
ResourceSchemaGenerator(ClassLoader loader, String className, URL schemaURL, String schemaTargetNamespace, String schemaTargetNamespacePrefix, Map< String, String > methodClassNameMapping, Map< String, QName > dynamicTypeMapping, Map< String, QName > typeMapping, AxisService service)