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:
020: package org.apache.axis2.tool.ant;
021:
022: import org.apache.axis2.util.CommandLineOption;
023: import org.apache.axis2.util.CommandLineOptionConstants;
024: import org.apache.axis2.util.CommandLineOptionParser;
025: import org.apache.axis2.util.URLProcessor;
026: import org.apache.axis2.wsdl.codegen.CodeGenerationEngine;
027: import org.apache.axis2.wsdl.util.ConfigPropertyFileLoader;
028: import org.apache.tools.ant.AntClassLoader;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031: import org.apache.tools.ant.types.Path;
032: import org.apache.tools.ant.types.Reference;
033: import org.apache.tools.ant.types.Parameter;
034:
035: import java.util.HashMap;
036: import java.util.Map;
037: import java.util.Properties;
038: import java.util.Iterator;
039:
040: public class AntCodegenTask extends Task {
041:
042: private String wsdlFileName = null;
043: private String output = ".";
044: private String language = ConfigPropertyFileLoader
045: .getDefaultLanguage();
046: private String packageName = URLProcessor.DEFAULT_PACKAGE;
047: private String databindingName = ConfigPropertyFileLoader
048: .getDefaultDBFrameworkName();
049: private String portName = null;
050: private String serviceName = null;
051:
052: private boolean asyncOnly = false;
053: private boolean syncOnly = false;
054: private boolean serverSide = false;
055: private boolean testcase = false;
056: private boolean generateServiceXml = false;
057: private boolean generateAllClasses = false;
058: private boolean unpackClasses = false;
059: private boolean serverSideInterface = false;
060:
061: private boolean allPorts = false;
062: private boolean backwardCompatible = false;
063: private boolean flattenFiles = false;
064: private boolean skipMessageReceiver = false;
065: private boolean skipBuildXML = false;
066: private boolean skipWSDL = false;
067: private boolean overWrite = false;
068: private boolean suppressPrefixes = false;
069: private Properties props = new Properties();
070:
071: private String repositoryPath = null;
072: private String externalMapping = null;
073: private String wsdlVersion = null;
074: private String targetSourceFolderLocation = null;
075: private String targetResourcesFolderLocation = null;
076: private boolean unwrap = false;
077:
078: private String namespaceToPackages = null;
079:
080: private Path classpath;
081:
082: /**
083: *
084: */
085: public AntCodegenTask() {
086: super ();
087: }
088:
089: /**
090: * Sets the classpath.
091: *
092: * @return Returns Path.
093: */
094: public Path createClasspath() {
095: if (classpath == null) {
096: classpath = new Path(getProject());
097: classpath = classpath.concatSystemClasspath();
098: }
099: return classpath.createPath();
100: }
101:
102: /**
103: * Set the reference to an optional classpath
104: *
105: * @param r the id of the Ant path instance to act as the classpath
106: */
107: public void setClasspathRef(Reference r) {
108: createClasspath().setRefid(r);
109: }
110:
111: /** @return */
112: public boolean isServerSideInterface() {
113: return serverSideInterface;
114: }
115:
116: /** @param serverSideInterface */
117: public void setServerSideInterface(boolean serverSideInterface) {
118: this .serverSideInterface = serverSideInterface;
119: }
120:
121: /** Fills the option map. This map is passed onto the code generation API to generate the code. */
122: private Map fillOptionMap() {
123: Map optionMap = new HashMap();
124:
125: ////////////////////////////////////////////////////////////////
126: //WSDL file name
127: optionMap
128: .put(
129: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
130: new CommandLineOption(
131: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
132: getStringArray(wsdlFileName)));
133:
134: //WSDL version
135: if (wsdlVersion != null) {
136: optionMap
137: .put(
138: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_VERSION_OPTION,
139: new CommandLineOption(
140: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_VERSION_OPTION,
141: getStringArray(wsdlVersion)));
142: }
143:
144: // repository path
145: if (repositoryPath != null) {
146: optionMap
147: .put(
148: CommandLineOptionConstants.WSDL2JavaConstants.REPOSITORY_PATH_OPTION,
149: new CommandLineOption(
150: CommandLineOptionConstants.WSDL2JavaConstants.REPOSITORY_PATH_OPTION,
151: getStringArray(repositoryPath)));
152: }
153:
154: // external mapping
155: if (externalMapping != null) {
156: optionMap
157: .put(
158: CommandLineOptionConstants.WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION,
159: new CommandLineOption(
160: CommandLineOptionConstants.WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION,
161: getStringArray(externalMapping)));
162: }
163:
164: // target source folder location
165: if (targetSourceFolderLocation != null) {
166: optionMap
167: .put(
168: CommandLineOptionConstants.WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION,
169: new CommandLineOption(
170: CommandLineOptionConstants.WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION,
171: getStringArray(targetSourceFolderLocation)));
172: }
173:
174: // target source folder location
175: if (targetResourcesFolderLocation != null) {
176: optionMap
177: .put(
178: CommandLineOptionConstants.WSDL2JavaConstants.RESOURCE_FOLDER_OPTION,
179: new CommandLineOption(
180: CommandLineOptionConstants.WSDL2JavaConstants.RESOURCE_FOLDER_OPTION,
181: getStringArray(targetResourcesFolderLocation)));
182: }
183:
184: // target source folder location
185: if (unwrap) {
186: optionMap
187: .put(
188: CommandLineOptionConstants.WSDL2JavaConstants.UNWRAP_PARAMETERS,
189: new CommandLineOption(
190: CommandLineOptionConstants.WSDL2JavaConstants.UNWRAP_PARAMETERS,
191: new String[0]));
192: }
193:
194: //output location
195: optionMap
196: .put(
197: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
198: new CommandLineOption(
199: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
200: getStringArray(output)));
201: //////////////////////////////////////////////////////////////////
202: // Databinding type
203: optionMap
204: .put(
205: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
206: new CommandLineOption(
207: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
208: getStringArray(databindingName)));
209:
210: // Async only option - forcing to generate async methods only
211: if (asyncOnly) {
212: optionMap
213: .put(
214: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
215: new CommandLineOption(
216: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
217: new String[0]));
218: }
219: // Sync only option - forcing to generate Sync methods only
220: if (syncOnly) {
221: optionMap
222: .put(
223: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
224: new CommandLineOption(
225: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
226: new String[0]));
227: }
228:
229: //Package
230: optionMap
231: .put(
232: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
233: new CommandLineOption(
234: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
235: getStringArray(packageName)));
236:
237: //stub language
238: optionMap
239: .put(
240: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
241: new CommandLineOption(
242: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
243: getStringArray(language)));
244:
245: //server side and generate services.xml options
246: if (serverSide) {
247: optionMap
248: .put(
249: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
250: new CommandLineOption(
251: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
252: new String[0]));
253:
254: //services XML generation - effective only when specified as the server side
255: if (generateServiceXml) {
256: optionMap
257: .put(
258: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
259: new CommandLineOption(
260: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
261: new String[0]));
262: }
263: //generate all option - Only valid when generating serverside code
264: if (generateAllClasses) {
265: optionMap
266: .put(
267: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
268: new CommandLineOption(
269: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
270: new String[0]));
271: }
272:
273: }
274:
275: //generate the test case
276: if (testcase) {
277: optionMap
278: .put(
279: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
280: new CommandLineOption(
281: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
282: new String[0]));
283: }
284:
285: //Unwrap classes option - this determines whether the generated classes are inside the stub/MR
286: //or gets generates as seperate classes
287: if (unpackClasses) {
288: optionMap
289: .put(
290: CommandLineOptionConstants.WSDL2JavaConstants.UNPACK_CLASSES_OPTION,
291: new CommandLineOption(
292: CommandLineOptionConstants.WSDL2JavaConstants.UNPACK_CLASSES_OPTION,
293: new String[0]));
294: }
295:
296: //server side interface option
297: if (serverSideInterface) {
298: optionMap
299: .put(
300: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
301: new CommandLineOption(
302: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
303: new String[0]));
304: }
305:
306: if (allPorts) {
307: optionMap
308: .put(
309: CommandLineOptionConstants.WSDL2JavaConstants.All_PORTS_OPTION,
310: new CommandLineOption(
311: CommandLineOptionConstants.WSDL2JavaConstants.All_PORTS_OPTION,
312: new String[0]));
313: }
314:
315: if (backwardCompatible) {
316: optionMap
317: .put(
318: CommandLineOptionConstants.WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION,
319: new CommandLineOption(
320: CommandLineOptionConstants.WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION,
321: new String[0]));
322: }
323:
324: if (flattenFiles) {
325: optionMap
326: .put(
327: CommandLineOptionConstants.WSDL2JavaConstants.FLATTEN_FILES_OPTION,
328: new CommandLineOption(
329: CommandLineOptionConstants.WSDL2JavaConstants.FLATTEN_FILES_OPTION,
330: new String[0]));
331: }
332:
333: if (skipMessageReceiver) {
334: optionMap
335: .put(
336: CommandLineOptionConstants.WSDL2JavaConstants.NO_MESSAGE_RECEIVER_OPTION_LONG,
337: new CommandLineOption(
338: CommandLineOptionConstants.WSDL2JavaConstants.NO_MESSAGE_RECEIVER_OPTION_LONG,
339: new String[0]));
340: }
341:
342: if (skipBuildXML) {
343: optionMap
344: .put(
345: CommandLineOptionConstants.WSDL2JavaConstants.NO_BUILD_XML_OPTION_LONG,
346: new CommandLineOption(
347: CommandLineOptionConstants.WSDL2JavaConstants.NO_BUILD_XML_OPTION_LONG,
348: new String[0]));
349: }
350:
351: if (skipWSDL) {
352: optionMap
353: .put(
354: CommandLineOptionConstants.WSDL2JavaConstants.NO_WSDLS_OPTION_LONG,
355: new CommandLineOption(
356: CommandLineOptionConstants.WSDL2JavaConstants.NO_WSDLS_OPTION_LONG,
357: new String[0]));
358: }
359:
360: if (overWrite) {
361: optionMap
362: .put(
363: CommandLineOptionConstants.WSDL2JavaConstants.OVERRIDE_OPTION,
364: new CommandLineOption(
365: CommandLineOptionConstants.WSDL2JavaConstants.OVERRIDE_OPTION,
366: new String[0]));
367: }
368:
369: if (suppressPrefixes) {
370: optionMap
371: .put(
372: CommandLineOptionConstants.WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION,
373: new CommandLineOption(
374: CommandLineOptionConstants.WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION,
375: new String[0]));
376: }
377:
378: Iterator iterator = props.entrySet().iterator();
379: while (iterator.hasNext()) {
380: Map.Entry entry = (Map.Entry) iterator.next();
381: String key = CommandLineOptionConstants.WSDL2JavaConstants.EXTRA_OPTIONTYPE_PREFIX
382: + entry.getKey();
383: optionMap.put(key, new CommandLineOption(key,
384: new String[] { (String) entry.getValue() }));
385: }
386:
387: optionMap
388: .put(
389: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
390: new CommandLineOption(
391: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
392: new String[] { serviceName }));
393:
394: optionMap
395: .put(
396: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
397: new CommandLineOption(
398: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
399: new String[] { portName }));
400: // set the namespaces
401: optionMap
402: .put(
403: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
404: new CommandLineOption(
405: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
406: new String[] { namespaceToPackages }));
407:
408: return optionMap;
409: }
410:
411: /**
412: * Utility method to convert a string into a single item string[]
413: *
414: * @param value
415: * @return Returns String[].
416: */
417: private String[] getStringArray(String value) {
418: String[] values = new String[1];
419: values[0] = value;
420: return values;
421: }
422:
423: public void execute() throws BuildException {
424: try {
425: /*
426: * This needs the ClassLoader we use to load the task
427: * have all the dependancies set, hope that
428: * is ok for now
429: */
430:
431: AntClassLoader cl = new AntClassLoader(getClass()
432: .getClassLoader(), getProject(),
433: classpath == null ? createClasspath() : classpath,
434: false);
435:
436: Thread.currentThread().setContextClassLoader(cl);
437:
438: Map commandLineOptions = this .fillOptionMap();
439: CommandLineOptionParser parser = new CommandLineOptionParser(
440: commandLineOptions);
441: new CodeGenerationEngine(parser).generate();
442: } catch (Throwable e) {
443: throw new BuildException(e);
444: }
445:
446: }
447:
448: public void setPortName(String portName) {
449: this .portName = portName;
450: }
451:
452: public void setServiceName(String serviceName) {
453: this .serviceName = serviceName;
454: }
455:
456: public void setGenerateAllClasses(boolean generateAllClasses) {
457: this .generateAllClasses = generateAllClasses;
458: }
459:
460: public void setUnpackClasses(boolean unpackClasses) {
461: this .unpackClasses = unpackClasses;
462: }
463:
464: public void setWsdlFileName(String wsdlFileName) {
465: this .wsdlFileName = wsdlFileName;
466: }
467:
468: public void setOutput(String output) {
469: this .output = output;
470: }
471:
472: public void setPackageName(String packageName) {
473: this .packageName = packageName;
474: }
475:
476: public void setLanguage(String language) {
477: this .language = language;
478: }
479:
480: public void setAsyncOnly(boolean asyncOnly) {
481: this .asyncOnly = asyncOnly;
482: }
483:
484: public void setSyncOnly(boolean syncOnly) {
485: this .syncOnly = syncOnly;
486: }
487:
488: public void setServerSide(boolean serverSide) {
489: this .serverSide = serverSide;
490: }
491:
492: public void setTestcase(boolean testcase) {
493: this .testcase = testcase;
494: }
495:
496: public void setGenerateServiceXml(boolean generateServiceXml) {
497: this .generateServiceXml = generateServiceXml;
498: }
499:
500: public void setRepositoryPath(String repositoryPath) {
501: this .repositoryPath = repositoryPath;
502: }
503:
504: public void setExternalMapping(String externalMapping) {
505: this .externalMapping = externalMapping;
506: }
507:
508: public void setWsdlVersion(String wsdlVersion) {
509: this .wsdlVersion = wsdlVersion;
510: }
511:
512: public void setTargetSourceFolderLocation(
513: String targetSourceFolderLocation) {
514: this .targetSourceFolderLocation = targetSourceFolderLocation;
515: }
516:
517: public void setTargetResourcesFolderLocation(
518: String targetResourcesFolderLocation) {
519: this .targetResourcesFolderLocation = targetResourcesFolderLocation;
520: }
521:
522: public void setUnwrap(boolean unwrap) {
523: this .unwrap = unwrap;
524: }
525:
526: /** @return Returns Path. */
527: public Path getClasspath() {
528: return classpath;
529: }
530:
531: /** @param path */
532: public void setClasspath(Path path) {
533: classpath = path;
534: }
535:
536: public String getDatabindingName() {
537: return databindingName;
538: }
539:
540: public void setDatabindingName(String databindingName) {
541: this .databindingName = databindingName;
542: }
543:
544: public String getNamespaceToPackages() {
545: return namespaceToPackages;
546: }
547:
548: public void setNamespaceToPackages(String namespaceToPackages) {
549: this .namespaceToPackages = namespaceToPackages;
550: }
551:
552: public void addConfiguredParameter(Parameter prop) {
553: props.setProperty(prop.getName(), prop.getValue());
554: }
555:
556: public void setSuppressPrefixes(boolean suppressPrefixes) {
557: this .suppressPrefixes = suppressPrefixes;
558: }
559:
560: public void setOverWrite(boolean overWrite) {
561: this .overWrite = overWrite;
562: }
563:
564: public void setSkipWSDL(boolean skipWSDL) {
565: this .skipWSDL = skipWSDL;
566: }
567:
568: public void setSkipBuildXML(boolean skipBuildXML) {
569: this .skipBuildXML = skipBuildXML;
570: }
571:
572: public void setSkipMessageReceiver(boolean skipMessageReceiver) {
573: this .skipMessageReceiver = skipMessageReceiver;
574: }
575:
576: public void setFlattenFiles(boolean flattenFiles) {
577: this .flattenFiles = flattenFiles;
578: }
579:
580: public void setBackwardCompatible(boolean backwardCompatible) {
581: this .backwardCompatible = backwardCompatible;
582: }
583:
584: public void setAllPorts(boolean allPorts) {
585: this.allPorts = allPorts;
586: }
587: }
|