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.axis2.maven2.java2wsdl;
020:
021: import java.io.File;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026: import java.util.Properties;
027: import java.util.ArrayList;
028:
029: import org.apache.maven.artifact.Artifact;
030: import org.apache.maven.plugin.AbstractMojo;
031: import org.apache.maven.plugin.MojoExecutionException;
032: import org.apache.maven.plugin.MojoFailureException;
033: import org.apache.maven.project.MavenProject;
034: import org.apache.ws.java2wsdl.Java2WSDLCodegenEngine;
035: import org.apache.ws.java2wsdl.utils.Java2WSDLCommandLineOption;
036: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
037:
038: /**
039: * Takes a Java class as input and converts it into an equivalent
040: * WSDL file.
041: *
042: * @goal java2wsdl
043: * @phase process-classes
044: * @requiresDependencyResolution compile
045: */
046: public class Java2WSDLMojo extends AbstractMojo {
047: public static final String OPEN_BRACKET = "[";
048: public static final String CLOSE_BRACKET = "]";
049: public static final String COMMA = ",";
050:
051: /**
052: * The maven project.
053: * @parameter expression="${project}"
054: * @read-only
055: * @required
056: */
057: private MavenProject project;
058:
059: /**
060: * Fully qualified name of the class, which is being inspected.
061: * @parameter expression="${axis2.java2wsdl.className}"
062: * @required
063: */
064: private String className;
065:
066: /**
067: * Target namespace of the generated WSDL.
068: * @parameter expression="${axis2.java2wsdl.targetNamespace}"
069: */
070: private String targetNamespace;
071:
072: /**
073: * The namespace prefix, which is being used for the WSDL's
074: * target namespace.
075: * @parameter expression="${axis2.java2wsdl.targetNamespacePrefix}"
076: */
077: private String targetNamespacePrefix;
078:
079: /**
080: * The generated schemas target namespace.
081: * @parameter expression="${axis2.java2wsdl.schemaTargetNamespace}"
082: */
083: private String schemaTargetNamespace;
084:
085: /**
086: * The generated schemas target namespace prefix.
087: * @parameter expression="${axis2.java2wsdl.schemaTargetNamespacePrefix}"
088: */
089: private String schemaTargetNamespacePrefix;
090:
091: /**
092: * Name of the generated service.
093: * @parameter expression="${axis2.java2wsdl.serviceName}"
094: */
095: private String serviceName;
096:
097: /**
098: * Name of the service file, which is being generated.
099: * @parameter expression="${axis2.java2wsdl.outputFileName}" default-value="${project.build.directory}/generated-resources/service.wsdl"
100: */
101: private String outputFileName;
102:
103: /**
104: * Style for the wsdl
105: * @parameter expression="${axis2.java2wsdl.style}"
106: */
107: private String style;
108:
109: /**
110: * Use for the wsdl
111: * @parameter expression="${axis2.java2wsdl.use}"
112: */
113: private String use;
114:
115: /**
116: * Version for the wsdl
117: * @parameter expression="${axis2.java2wsdl.wsdlVersion}"
118: */
119: private String wsdlVersion;
120:
121: /**
122: * Namespace Generator
123: * @parameter expression="${axis2.java2wsdl.nsGenClassName}"
124: */
125: private String nsGenClassName;
126:
127: /**
128: * Schema Generator
129: * @parameter expression="${axis2.java2wsdl.nsGenClassName}"
130: */
131: private String schemaGenClassName;
132:
133: /**
134: * Location URI in the wsdl
135: * @parameter expression="${axis2.java2wsdl.locationUri}"
136: */
137: private String locationUri;
138:
139: /**
140: * attrFormDefault setting for the schema
141: * @parameter expression="${axis2.java2wsdl.attrFormDefault}"
142: */
143: private String attrFormDefault;
144:
145: /**
146: * elementFormDefault setting for the schema
147: * @parameter expression="${axis2.java2wsdl.elementFormDefault}"
148: */
149: private String elementFormDefault;
150:
151: /**
152: * Switch on the Doc/Lit/Bare style schema
153: * @parameter expression="${axis2.java2wsdl.docLitBare}"
154: */
155: private String docLitBare;
156:
157: /**
158: * Additional classes for which we need to generate schema
159: * @parameter expression="${axis2.java2wsdl.extraClasses}"
160: */
161: private String[] extraClasses;
162:
163: /**
164: * Specify namespaces explicitly for packages
165: * @parameter expression="${axis2.java2wsdl.package2Namespace}"
166: */
167: private Properties package2Namespace;
168:
169: private void addToOptionMap(Map map, String option, String value) {
170: addToOptionMap(map, option, new String[] { value });
171: }
172:
173: private void addToOptionMap(Map map, String option, String[] value) {
174: if (value != null) {
175: map.put(option, new Java2WSDLCommandLineOption(option,
176: value));
177: }
178: }
179:
180: private void addToOptionMap(Map map, String option, ArrayList values) {
181: if (values != null && !values.isEmpty()) {
182: map.put(option, new Java2WSDLCommandLineOption(option,
183: values));
184: }
185: }
186:
187: /**
188: * Fills the option map. This map is passed onto
189: * the code generation API to generate the code.
190: */
191: private Map fillOptionMap() throws MojoFailureException {
192: Map optionMap = new HashMap();
193:
194: if (className == null) {
195: throw new MojoFailureException(
196: "You must specify a classname");
197: }
198: addToOptionMap(optionMap, Java2WSDLConstants.CLASSNAME_OPTION,
199: className);
200: addToOptionMap(optionMap,
201: Java2WSDLConstants.TARGET_NAMESPACE_OPTION,
202: targetNamespace);
203: addToOptionMap(optionMap,
204: Java2WSDLConstants.TARGET_NAMESPACE_PREFIX_OPTION,
205: targetNamespacePrefix);
206: addToOptionMap(optionMap,
207: Java2WSDLConstants.SCHEMA_TARGET_NAMESPACE_OPTION,
208: schemaTargetNamespace);
209: addToOptionMap(
210: optionMap,
211: Java2WSDLConstants.SCHEMA_TARGET_NAMESPACE_PREFIX_OPTION,
212: schemaTargetNamespacePrefix);
213: addToOptionMap(optionMap,
214: Java2WSDLConstants.SERVICE_NAME_OPTION, serviceName);
215: File outputFile = new File(outputFileName);
216: if (!outputFile.isAbsolute()) {
217: outputFile = new File(project.getBasedir(), outputFileName);
218: }
219: File dir = outputFile.getParentFile();
220: if (!dir.isDirectory()) {
221: dir.mkdirs();
222: }
223: addToOptionMap(optionMap,
224: Java2WSDLConstants.OUTPUT_LOCATION_OPTION, dir
225: .getPath());
226: addToOptionMap(optionMap,
227: Java2WSDLConstants.OUTPUT_FILENAME_OPTION, outputFile
228: .getName());
229:
230: Set artifacts = project.getArtifacts();
231: String[] artifactFileNames = new String[artifacts.size() + 1];
232: int j = 0;
233: for (Iterator i = artifacts.iterator(); i.hasNext(); j++) {
234: artifactFileNames[j] = ((Artifact) i.next()).getFile()
235: .getAbsolutePath();
236: }
237: artifactFileNames[j] = project.getArtifact().getFile()
238: .getAbsolutePath();
239:
240: addToOptionMap(optionMap, Java2WSDLConstants.CLASSPATH_OPTION,
241: artifactFileNames);
242:
243: if (style != null) {
244: addToOptionMap(optionMap, Java2WSDLConstants.STYLE_OPTION,
245: style);
246: }
247:
248: if (use != null) {
249: addToOptionMap(optionMap, Java2WSDLConstants.USE_OPTION,
250: use);
251: }
252:
253: if (wsdlVersion != null) {
254: addToOptionMap(optionMap,
255: Java2WSDLConstants.WSDL_VERSION_OPTION, wsdlVersion);
256: }
257:
258: if (docLitBare != null) {
259: addToOptionMap(optionMap, Java2WSDLConstants.DOC_LIT_BARE,
260: docLitBare);
261: }
262:
263: if (locationUri != null) {
264: addToOptionMap(optionMap,
265: Java2WSDLConstants.LOCATION_OPTION, locationUri);
266: }
267:
268: if (nsGenClassName != null) {
269: addToOptionMap(optionMap,
270: Java2WSDLConstants.NAMESPACE_GENERATOR_OPTION,
271: nsGenClassName);
272: }
273:
274: if (schemaGenClassName != null) {
275: addToOptionMap(optionMap,
276: Java2WSDLConstants.SCHEMA_GENERATOR_OPTION,
277: schemaGenClassName);
278: }
279:
280: if (attrFormDefault != null) {
281: addToOptionMap(optionMap,
282: Java2WSDLConstants.ATTR_FORM_DEFAULT_OPTION,
283: attrFormDefault);
284: }
285:
286: if (elementFormDefault != null) {
287: addToOptionMap(optionMap,
288: Java2WSDLConstants.ELEMENT_FORM_DEFAULT_OPTION,
289: elementFormDefault);
290: }
291:
292: if (extraClasses != null && extraClasses.length > 0) {
293: addToOptionMap(optionMap,
294: Java2WSDLConstants.EXTRA_CLASSES_DEFAULT_OPTION,
295: extraClasses);
296: }
297:
298: ArrayList list = new ArrayList();
299: Iterator iterator = package2Namespace.entrySet().iterator();
300:
301: while (iterator.hasNext()) {
302: Map.Entry entry = (Map.Entry) iterator.next();
303: String packageName = (String) entry.getKey();
304: String namespace = (String) entry.getValue();
305: list.add(OPEN_BRACKET + packageName + COMMA + namespace
306: + CLOSE_BRACKET);
307: }
308: addToOptionMap(optionMap,
309: Java2WSDLConstants.JAVA_PKG_2_NSMAP_OPTION, list);
310:
311: return optionMap;
312: }
313:
314: public void execute() throws MojoExecutionException,
315: MojoFailureException {
316: Map commandLineOptions = fillOptionMap();
317: try {
318: new Java2WSDLCodegenEngine(commandLineOptions).generate();
319: } catch (Exception e) {
320: e.printStackTrace();
321: throw new MojoExecutionException(e.getMessage(), e);
322: }
323: }
324: }
|