001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.ws.java2wsdl;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.context.ConfigurationContext;
023: import org.apache.axis2.context.ConfigurationContextFactory;
024: import org.apache.axis2.deployment.util.Utils;
025: import org.apache.axis2.description.AxisService;
026: import org.apache.axis2.description.AxisService2WSDL11;
027: import org.apache.axis2.description.AxisService2WSDL20;
028: import org.apache.axis2.description.WSDL2Constants;
029: import org.apache.axis2.description.java2wsdl.DefaultNamespaceGenerator;
030: import org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator;
031: import org.apache.axis2.description.java2wsdl.DocLitBareSchemaGenerator;
032: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
033: import org.apache.axis2.description.java2wsdl.Java2WSDLUtils;
034: import org.apache.axis2.description.java2wsdl.NamespaceGenerator;
035: import org.apache.axis2.description.java2wsdl.SchemaGenerator;
036: import org.apache.axis2.engine.AxisConfiguration;
037: import org.apache.axis2.engine.MessageReceiver;
038: import org.apache.axis2.util.Loader;
039: import org.apache.axis2.util.XMLPrettyPrinter;
040:
041: import java.io.OutputStream;
042: import java.lang.reflect.Constructor;
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: public class Java2WSDLBuilder implements Java2WSDLConstants {
048:
049: public static final String ALL = "all";
050: private OutputStream out;
051: private String className;
052: private ClassLoader classLoader;
053: private String wsdlPrefix = "wsdl";
054:
055: private String serviceName = null;
056:
057: //these apply for the WSDL
058: private String targetNamespace = null;
059: private String targetNamespacePrefix = null;
060:
061: private String attrFormDefault = null;
062: private String elementFormDefault = null;
063: private String schemaTargetNamespace = null;
064: private String schemaTargetNamespacePrefix = null;
065: private String style = Java2WSDLConstants.DOCUMENT;
066: private String use = Java2WSDLConstants.LITERAL;
067: private String locationUri;
068: private ArrayList extraClasses;
069:
070: private String nsGenClassName = null;
071: private Map pkg2nsMap = null;
072: private boolean pretty = true;
073: private String wsdlVersion = WSDL_VERSION_1;
074: private String schemaGenClassName = null;
075: private boolean generateDocLitBare = false;
076: private AxisConfiguration axisConfig;
077:
078: public Java2WSDLBuilder() {
079: try {
080: ConfigurationContext configCtx = ConfigurationContextFactory
081: .createDefaultConfigurationContext();
082: axisConfig = configCtx.getAxisConfiguration();
083: } catch (Exception e) {
084: throw new RuntimeException(e);
085: }
086: }
087:
088: public Java2WSDLBuilder(AxisConfiguration axisConfig) {
089: this .axisConfig = axisConfig;
090: }
091:
092: public Java2WSDLBuilder(OutputStream out, String className,
093: ClassLoader classLoader) {
094: try {
095: ConfigurationContext configCtx = ConfigurationContextFactory
096: .createDefaultConfigurationContext();
097: axisConfig = configCtx.getAxisConfiguration();
098: this .out = out;
099: this .className = className;
100: this .classLoader = classLoader;
101: } catch (Exception e) {
102: throw new RuntimeException(e);
103: }
104: }
105:
106: public String getSchemaTargetNamespace() throws Exception {
107: if (schemaTargetNamespace == null) {
108: schemaTargetNamespace = Java2WSDLUtils
109: .schemaNamespaceFromClassName(className,
110: classLoader, resolveNSGen()).toString();
111: }
112: return schemaTargetNamespace;
113: }
114:
115: public String getStyle() {
116: return style;
117: }
118:
119: public String getLocationUri() {
120: return locationUri;
121: }
122:
123: public void setLocationUri(String locationUri) {
124: this .locationUri = locationUri;
125: }
126:
127: public void setStyle(String style) {
128: this .style = style;
129: }
130:
131: public String getUse() {
132: return use;
133: }
134:
135: public void setUse(String use) {
136: this .use = use;
137: }
138:
139: public void setSchemaTargetNamespace(String schemaTargetNamespace) {
140: this .schemaTargetNamespace = schemaTargetNamespace;
141: }
142:
143: public String getSchemaTargetNamespacePrefix() {
144: if (schemaTargetNamespacePrefix == null) {
145: this .schemaTargetNamespacePrefix = SCHEMA_NAMESPACE_PRFIX;
146: }
147: return schemaTargetNamespacePrefix;
148: }
149:
150: public void setSchemaTargetNamespacePrefix(
151: String schemaTargetNamespacePrefix) {
152: this .schemaTargetNamespacePrefix = schemaTargetNamespacePrefix;
153: }
154:
155: public String getTargetNamespace() {
156: return targetNamespace;
157: }
158:
159: public void setTargetNamespace(String targetNamespace) {
160: this .targetNamespace = targetNamespace;
161: }
162:
163: public String getTargetNamespacePrefix() {
164: return targetNamespacePrefix;
165: }
166:
167: public void setTargetNamespacePrefix(String targetNamespacePrefix) {
168: this .targetNamespacePrefix = targetNamespacePrefix;
169: }
170:
171: public String getServiceName() {
172: return serviceName;
173: }
174:
175: public void setServiceName(String serviceName) {
176: this .serviceName = serviceName;
177: }
178:
179: public String getWsdlPrefix() {
180: return wsdlPrefix;
181: }
182:
183: public void setWsdlPrefix(String wsdlPrefix) {
184: this .wsdlPrefix = wsdlPrefix;
185: }
186:
187: public boolean isGenerateDocLitBare() {
188: return generateDocLitBare;
189: }
190:
191: public void setGenerateDocLitBare(boolean generateDocLitBare) {
192: this .generateDocLitBare = generateDocLitBare;
193: }
194:
195: public void generateWSDL() throws Exception {
196: SchemaGenerator schemaGenerator = resolveSchemaGen(classLoader,
197: className, getSchemaTargetNamespace(),
198: getSchemaTargetNamespacePrefix());
199:
200: ArrayList excludedOperation = new ArrayList();
201: Utils.addExcludeMethods(excludedOperation);
202: schemaGenerator.setExcludeMethods(excludedOperation);
203: schemaGenerator.setAttrFormDefault(getAttrFormDefault());
204: schemaGenerator.setElementFormDefault(getElementFormDefault());
205: schemaGenerator.setExtraClasses(getExtraClasses());
206: schemaGenerator.setNsGen(resolveNSGen());
207: schemaGenerator.setPkg2nsmap(getPkg2nsMap());
208: if (getPkg2nsMap() != null
209: && !getPkg2nsMap().isEmpty()
210: && (getPkg2nsMap().containsKey(ALL) || getPkg2nsMap()
211: .containsKey(ALL.toUpperCase()))) {
212: schemaGenerator.setUseWSDLTypesNamespace(true);
213: }
214:
215: HashMap messageReciverMap = new HashMap();
216: Class inOnlyMessageReceiver = Loader
217: .loadClass("org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver");
218: MessageReceiver messageReceiver = (MessageReceiver) inOnlyMessageReceiver
219: .newInstance();
220: messageReciverMap.put(WSDL2Constants.MEP_URI_IN_ONLY,
221: messageReceiver);
222: Class inoutMessageReceiver = Loader
223: .loadClass("org.apache.axis2.rpc.receivers.RPCMessageReceiver");
224: MessageReceiver inOutmessageReceiver = (MessageReceiver) inoutMessageReceiver
225: .newInstance();
226: messageReciverMap.put(WSDL2Constants.MEP_URI_IN_OUT,
227: inOutmessageReceiver);
228: AxisService service = new AxisService();
229: schemaGenerator.setAxisService(service);
230: AxisService axisService = AxisService.createService(className,
231: serviceName == null ? Java2WSDLUtils
232: .getSimpleClassName(className) : serviceName,
233: axisConfig, messageReciverMap,
234: targetNamespace == null ? Java2WSDLUtils
235: .namespaceFromClassName(className, classLoader,
236: resolveNSGen()).toString()
237: : targetNamespace, classLoader,
238: schemaGenerator, service);
239: schemaGenerator.setAxisService(axisService);
240: axisService.setTargetNamespacePrefix(targetNamespacePrefix);
241: axisService
242: .setSchemaTargetNamespace(getSchemaTargetNamespace());
243: axisService
244: .setSchematargetNamespacePrefix(getSchemaTargetNamespacePrefix());
245: String uri = locationUri;
246: if (uri == null) {
247: uri = DEFAULT_LOCATION_URL
248: + (serviceName == null ? Java2WSDLUtils
249: .getSimpleClassName(className)
250: : serviceName);
251: }
252: axisService.setEPRs(new String[] { uri });
253: axisConfig.addService(axisService);
254:
255: if (WSDL_VERSION_1.equals(wsdlVersion)) {
256: AxisService2WSDL11 g = new AxisService2WSDL11(axisService);
257: g.setStyle(this .style);
258: g.setUse(this .use);
259: OMElement wsdlElement = g.generateOM();
260: if (!isPretty()) {
261: wsdlElement.serialize(out);
262: } else {
263: XMLPrettyPrinter.prettify(wsdlElement, out);
264: }
265: } else {
266: AxisService2WSDL20 g = new AxisService2WSDL20(axisService);
267: OMElement wsdlElement = g.generateOM();
268: if (!isPretty()) {
269: wsdlElement.serialize(out);
270: } else {
271: XMLPrettyPrinter.prettify(wsdlElement, out);
272: }
273: }
274:
275: out.flush();
276: out.close();
277: }
278:
279: public String getAttrFormDefault() {
280: return attrFormDefault;
281: }
282:
283: public void setAttrFormDefault(String attrFormDefault) {
284: this .attrFormDefault = attrFormDefault;
285: }
286:
287: public String getElementFormDefault() {
288: return elementFormDefault;
289: }
290:
291: public void setElementFormDefault(String elementFormDefault) {
292: this .elementFormDefault = elementFormDefault;
293: }
294:
295: public ArrayList getExtraClasses() {
296: return extraClasses;
297: }
298:
299: public void setExtraClasses(ArrayList extraClasses) {
300: this .extraClasses = extraClasses;
301: }
302:
303: public String getNsGenClassName() {
304: return nsGenClassName;
305: }
306:
307: public void setNsGenClassName(String nsGenClassName) {
308: this .nsGenClassName = nsGenClassName;
309: }
310:
311: public String getSchemaGenClassName() {
312: return schemaGenClassName;
313: }
314:
315: public void setSchemaGenClassName(String schemaGenClassName) {
316: this .schemaGenClassName = schemaGenClassName;
317: }
318:
319: public Map getPkg2nsMap() {
320: return pkg2nsMap;
321: }
322:
323: public void setPkg2nsMap(Map pkg2nsMap) {
324: this .pkg2nsMap = pkg2nsMap;
325: }
326:
327: private NamespaceGenerator resolveNSGen() {
328: NamespaceGenerator nsGen;
329: if (this .nsGenClassName == null) {
330: nsGen = new DefaultNamespaceGenerator();
331: } else {
332: try {
333: nsGen = (NamespaceGenerator) Class.forName(
334: this .nsGenClassName).newInstance();
335: } catch (Exception e) {
336: nsGen = new DefaultNamespaceGenerator();
337: }
338: }
339: return nsGen;
340: }
341:
342: private SchemaGenerator resolveSchemaGen(ClassLoader loader,
343: String className, String schematargetNamespace,
344: String schematargetNamespacePrefix) throws Exception {
345: SchemaGenerator schemaGen;
346: if (this .schemaGenClassName == null) {
347: if (generateDocLitBare) {
348: schemaGen = new DocLitBareSchemaGenerator(loader,
349: className, schematargetNamespace,
350: schematargetNamespacePrefix, null);
351: } else {
352: schemaGen = new DefaultSchemaGenerator(loader,
353: className, schematargetNamespace,
354: schematargetNamespacePrefix, null);
355: }
356:
357: } else {
358: try {
359: Class clazz = Class.forName(this .schemaGenClassName);
360: Constructor constructor = clazz
361: .getConstructor(new Class[] {
362: ClassLoader.class, String.class,
363: String.class, String.class });
364: schemaGen = (SchemaGenerator) constructor
365: .newInstance(new Object[] { loader, className,
366: schematargetNamespace,
367: schematargetNamespacePrefix });
368: } catch (Exception e) {
369: if (generateDocLitBare) {
370: schemaGen = new DocLitBareSchemaGenerator(loader,
371: className, schematargetNamespace,
372: schematargetNamespacePrefix, null);
373: } else {
374: schemaGen = new DefaultSchemaGenerator(loader,
375: className, schematargetNamespace,
376: schematargetNamespacePrefix, null);
377: }
378:
379: }
380: }
381: return schemaGen;
382: }
383:
384: public boolean isPretty() {
385: return pretty;
386: }
387:
388: public void setPretty(boolean pretty) {
389: this .pretty = pretty;
390: }
391:
392: public void setWSDLVersion(String wsdlVersion) {
393: this.wsdlVersion = wsdlVersion;
394: }
395: }
|