1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*
* 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.tuscany.tools.java2wsdl.generate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tuscany.sdo.util.DataObjectUtil;
import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaElement;
import org.apache.ws.commons.schema.XmlSchemaForm;
import org.apache.ws.commons.schema.XmlSchemaImport;
import org.apache.ws.commons.schema.XmlSchemaInclude;
import org.apache.ws.commons.schema.XmlSchemaSequence;
import org.apache.ws.commons.schema.utils.NamespaceMap;
import org.apache.ws.java2wsdl.Java2WSDLConstants;
import org.apache.ws.java2wsdl.SchemaGenerator;
import org.apache.ws.java2wsdl.bytecode.MethodTable;
import org.codehaus.jam.JClass;
import org.codehaus.jam.JMethod;
import org.codehaus.jam.JParameter;
import org.codehaus.jam.JamClassIterator;
import org.codehaus.jam.JamService;
import org.codehaus.jam.JamServiceFactory;
import org.codehaus.jam.JamServiceParams;
public class TuscanySchemaGenerator implements TuscanyJava2WSDLConstants
{
public static final String NAME_SPACE_PREFIX = "stn_";
public static final String PERIOD_SEPARATOR = ".";
private static int prefixCount = 1;
protected String attrFormDefault = null;
protected String elementFormDefault = null;
protected Hashtable targetNamespacePrefixMap = new Hashtable();
protected Hashtable schemaMap = new Hashtable();
protected Hashtable sdoAnnoMap = new Hashtable();
protected XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
private TuscanyTypeTable typeTable = new TuscanyTypeTable();
protected SchemaBuilder schemaBuilder = null;
protected Map schemaLocationMap = null;
private static final Log log = LogFactory.getLog(SchemaGenerator.class);
private ClassLoader classLoader;
private String className;
// to keep loadded method using JAM
private JMethod methods [];
//to store byte code method using Axis 1.x codes
private MethodTable methodTable;
private String schemaTargetNameSpace;
private String schema_namespace_prefix;
private Class clazz;
private ArrayList excludeMethods = new ArrayList();
public TuscanySchemaGenerator(ClassLoader loader,
String className,
String schematargetNamespace,
String schematargetNamespacePrefix,
Map schemaLocMap)
throws Exception
{
DataObjectUtil.initRuntime();
this.classLoader = loader;
this.className = className;
clazz = Class.forName(className, true, loader);
methodTable = new MethodTable(clazz);
this.schemaTargetNameSpace = schematargetNamespace;
this.schema_namespace_prefix = schematargetNamespacePrefix;
this.schemaLocationMap = schemaLocMap;
initializeSchemaMap(this.schemaTargetNameSpace, this.schema_namespace_prefix);
schemaBuilder = new SchemaBuilder(xmlSchemaCollection,
schemaMap,
targetNamespacePrefixMap,
typeTable,
getAttrFormDefault(),
getElementFormDefault(),
schemaLocMap,
this.classLoader );
}
/**
* 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 buildWSDLTypes() throws Exception
{
JamServiceFactory factory = JamServiceFactory.getInstance();
JamServiceParams jam_service_parms = factory.createServiceParams();
//setting the classLoder
// jam_service_parms.setParentClassLoader(factory.createJamClassLoader(classLoader));
//it can posible to add the classLoader as well
jam_service_parms.addClassLoader(classLoader);
jam_service_parms.includeClass(className);
JamService service = factory.createService(jam_service_parms);
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();
// serviceName = jclass.getSimpleName();
//todo in the future , when we support annotation we can use this
//JAnnotation[] annotations = jclass.getAnnotations();
/**
* 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
*/
methods = jclass.getDeclaredMethods();
// since we do not support overload
HashMap uniqueMethods = new HashMap();
XmlSchemaComplexType methodSchemaType = null;
XmlSchemaSequence sequence = null;
QName methodPartSchemaTypeName = null;
for (int i = 0; i < methods.length; i++)
{
String methodName = methods[i].getSimpleName();
JMethod jMethod = methods[i];
// no need to think abt this method , since that is system
// config method
if (excludeMethods.contains(jMethod.getSimpleName())) {
continue;
}
//if (jMethod.getSimpleName().equals("init")
// || "setOperationContext".equals(jMethod.getSimpleName())
// || "destroy".equals(jMethod.getSimpleName()))
// continue;
if (uniqueMethods.get(jMethod.getSimpleName()) != null) {
throw new Exception(
" Sorry we don't support methods overloading !!!! ");
}
if (!jMethod.isPublic()) {
// no need to generate Schema for non public methods
continue;
}
uniqueMethods.put(jMethod.getSimpleName(), jMethod);
JParameter [] paras = jMethod.getParameters();
String parameterNames [] = null;
if (paras.length > 0)
{
parameterNames = methodTable.getParameterNames(methodName);
sequence = new XmlSchemaSequence();
//create the schema type for the method wrapper
methodSchemaType = createSchemaTypeForMethodPart(jMethod.getSimpleName());
methodSchemaType.setParticle(sequence);
}
for (int j = 0; j < paras.length; j++)
{
JParameter methodParameter = paras[j];
JClass paraType = methodParameter.getType();
generateSchemaForType(sequence, paraType,
( parameterNames != null && parameterNames[j] != null )? parameterNames[j] : methodParameter.getSimpleName());
}
// for its return type
JClass returnType = jMethod.getReturnType();
QName methodReturnSchemaTypeName = null;
if (!returnType.isVoidType())
{
methodSchemaType = createSchemaTypeForMethodPart(jMethod.getSimpleName() + RESPONSE);
sequence = new XmlSchemaSequence();
methodSchemaType.setParticle(sequence);
generateSchemaForType(sequence, returnType, "return");
}
}
//generateWrapperElements(methods);
}
return schemaMap.values();
}
private QName generateSchemaForType(XmlSchemaSequence sequence, JClass type, String partName) throws Exception
{
boolean isArrayType = type.isArrayType();
if ( isArrayType )
{
type = type.getArrayComponentType();
}
String classTypeName = type.getQualifiedName();
QName schemaTypeName = typeTable.getSimpleSchemaTypeName(classTypeName);
if ( schemaTypeName == null )
{
schemaTypeName =
schemaBuilder.generateSchema(type);
addContentToMethodSchemaType(sequence,
schemaTypeName,
partName,
type.isArrayType());
addImportORInclude((XmlSchema)schemaMap.get(schemaTargetNameSpace), schemaTypeName);
}
else
{
addContentToMethodSchemaType(sequence,
schemaTypeName,
partName,
type.isArrayType());
}
return schemaTypeName;
}
private void addContentToMethodSchemaType(XmlSchemaSequence sequence,
QName schemaTypeName,
String paraName,
boolean isArray )
{
XmlSchemaElement elt1 = new XmlSchemaElement();
elt1.setName(paraName);
elt1.setSchemaTypeName(schemaTypeName);
sequence.getItems().add(elt1);
if ( isArray )
{
elt1.setMaxOccurs(Long.MAX_VALUE);
elt1.setMinOccurs(0);
}
}
private XmlSchemaComplexType createSchemaTypeForMethodPart(String localPartName)
{
XmlSchema xmlSchema = (XmlSchema)schemaMap.get(schemaTargetNameSpace);
QName elementName = new QName(this.schemaTargetNameSpace, localPartName, this.schema_namespace_prefix);
XmlSchemaComplexType complexType = new XmlSchemaComplexType(xmlSchema);
XmlSchemaElement globalElement = new XmlSchemaElement();
globalElement.setSchemaType(complexType);
globalElement.setName(formGlobalElementName(localPartName));
globalElement.setQName(elementName);
xmlSchema.getItems().add(globalElement);
xmlSchema.getElements().add(elementName, globalElement);
typeTable.addComplexSchemaType(this.schemaTargetNameSpace, globalElement.getName(), elementName);
return complexType;
}
private String formGlobalElementName(String typeName)
{
String firstChar = typeName.substring(0,1);
return typeName.replaceFirst(firstChar, firstChar.toLowerCase());
}
public TuscanyTypeTable getTypeTable() {
return typeTable;
}
public JMethod[] getMethods() {
return methods;
}
private String generatePrefix() {
return NAME_SPACE_PREFIX + prefixCount++;
}
public void setExcludeMethods(ArrayList excludeMethods) {
this.excludeMethods = excludeMethods;
}
private void initializeSchemaMap(String targetNamespace, String targetNamespacePrefix)
{
XmlSchema xmlSchema = new XmlSchema(targetNamespace, xmlSchemaCollection);
xmlSchema.setAttributeFormDefault(getAttrFormDefaultSetting());
xmlSchema.setElementFormDefault(getElementFormDefaultSetting());
targetNamespacePrefixMap.put(targetNamespace, targetNamespacePrefix);
schemaMap.put(targetNamespace, xmlSchema);
NamespaceMap prefixmap = new NamespaceMap();
prefixmap.put(TuscanyTypeTable.XS_URI_PREFIX, TuscanyTypeTable.XML_SCHEMA_URI);
prefixmap.put(targetNamespacePrefix, targetNamespace);
xmlSchema.setNamespaceContext(prefixmap);
}
private void setFormDefaults()
{
}
public Hashtable getSdoAnnoMap() {
return sdoAnnoMap;
}
public void setSdoAnnoMap(Hashtable sdoAnnoMap) {
this.sdoAnnoMap = sdoAnnoMap;
}
private void addImportORInclude(XmlSchema xmlSchema, QName schemaTypeName)
{
//decide whether there must be an import or an include
if ( xmlSchema.getTargetNamespace().equals(schemaTypeName.getNamespaceURI()) )
{
XmlSchema containingSchema = (XmlSchema)schemaMap.get(schemaTypeName.getNamespaceURI());
//if the type is not defined in the Schema then include
if ( containingSchema.getTypeByName(schemaTypeName) == null )
{
String schemaLocation = null;
if ( (schemaLocation = (String)schemaLocationMap.get(schemaTypeName.getNamespaceURI())) != null )
{
schemaLocation = DEFAULT_SCHEMA_LOCATION;
}
XmlSchemaInclude includeElement = new XmlSchemaInclude();
includeElement.setSchemaLocation(schemaLocation);
if ( !xmlSchema.getIncludes().contains(includeElement) )
{
xmlSchema.getIncludes().add(includeElement);
}
}
}
else
{
if (!((NamespaceMap)xmlSchema.getNamespaceContext()).values().
contains(schemaTypeName.getNamespaceURI()))
{
XmlSchemaImport importElement = new XmlSchemaImport();
importElement.setNamespace(schemaTypeName.getNamespaceURI());
xmlSchema.getItems().add(importElement);
((NamespaceMap)xmlSchema.getNamespaceContext()).
put(generatePrefix(), schemaTypeName.getNamespaceURI());
}
}
}
private XmlSchemaForm getAttrFormDefaultSetting()
{
if ( FORM_DEFAULT_UNQUALIFIED.equals(getAttrFormDefault()) )
{
return new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED);
}
else
{
return new XmlSchemaForm(XmlSchemaForm.QUALIFIED);
}
}
private XmlSchemaForm getElementFormDefaultSetting()
{
if ( FORM_DEFAULT_UNQUALIFIED.equals(getElementFormDefault()) )
{
return new XmlSchemaForm(XmlSchemaForm.UNQUALIFIED);
}
else
{
return new XmlSchemaForm(XmlSchemaForm.QUALIFIED);
}
}
public String getAttrFormDefault() {
return attrFormDefault;
}
public void setAttrFormDefault(String attrFormDefault) {
this.attrFormDefault = attrFormDefault;
}
public String getElementFormDefault() {
return elementFormDefault;
}
public void setElementFormDefault(String elementFormDefault) {
this.elementFormDefault = elementFormDefault;
}
}
|