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: */package org.apache.cxf.maven_plugin;
019:
020: import java.io.File;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import org.apache.cxf.helpers.FileUtils;
025: import org.apache.cxf.tools.java2wsdl.JavaToWSDL;
026: import org.apache.maven.plugin.AbstractMojo;
027: import org.apache.maven.plugin.MojoExecutionException;
028: import org.apache.maven.project.MavenProject;
029: import org.apache.tools.ant.ExitException;
030: import org.apache.tools.ant.util.optional.NoExitSecurityManager;
031:
032: /**
033: * @goal java2wsdl
034: * @description CXF Java To WSDL Tool
035: */
036: public class Java2WSDLMojo extends AbstractMojo {
037: /**
038: * @parameter
039: * @required
040: */
041: String className;
042:
043: /**
044: * @parameter expression="${project.build.outputDirectory}"
045: * @required
046: */
047: String classpath;
048:
049: /**
050: * @parameter
051: */
052: String outputFile;
053:
054: /**
055: * @parameter
056: */
057: Boolean soap12;
058:
059: /**
060: * @parameter
061: */
062: String targetNamespace;
063:
064: /**
065: * @parameter
066: */
067: String serviceName;
068:
069: /**
070: * @parameter
071: */
072: Boolean verbose;
073:
074: /**
075: * @parameter
076: */
077: Boolean quiet;
078:
079: /**
080: * @parameter expression="${project.compileClasspathElements}"
081: * @required
082: */
083: List classpathElements;
084:
085: /**
086: * @parameter expression="${project}"
087: * @required
088: */
089: MavenProject project;
090:
091: public void execute() throws MojoExecutionException {
092: File classesDir = new File(classpath);
093: FileUtils.mkDir(classesDir);
094:
095: StringBuffer buf = new StringBuffer();
096: for (Object classpathElement : classpathElements) {
097: buf.append(classpathElement.toString());
098: buf.append(File.pathSeparatorChar);
099: }
100: String newCp = buf.toString();
101:
102: String cp = System.getProperty("java.class.path");
103: SecurityManager oldSm = System.getSecurityManager();
104: try {
105: System.setProperty("java.class.path", newCp);
106: System.setSecurityManager(new NoExitSecurityManager());
107: processJavaClass();
108: } finally {
109: System.setSecurityManager(oldSm);
110: System.setProperty("java.class.path", cp);
111: }
112:
113: System.gc();
114: }
115:
116: private void processJavaClass() throws MojoExecutionException {
117: List<String> args = new ArrayList<String>();
118:
119: // outputfile arg
120: if (outputFile == null && project != null) {
121: // Put the wsdl in target/generated/wsdl
122: int i = className.lastIndexOf('.');
123: // Prone to OoBE, but then it's wrong anyway
124: String name = className.substring(i + 1);
125: outputFile = (project.getBuild().getDirectory()
126: + "/generated/wsdl/" + name + ".wsdl").replace("/",
127: File.separator);
128: }
129: if (outputFile != null) {
130: // JavaToWSDL freaks out if the directory of the outputfile doesn't exist, so lets
131: // create it since there's no easy way for the user to create it beforehand in maven
132: FileUtils.mkDir(new File(outputFile).getParentFile());
133: args.add("-o");
134: args.add(outputFile);
135:
136: /*
137: Contributor's comment:
138: Sometimes JavaToWSDL creates Java code for the wrappers. I don't *think* this is
139: needed by the end user.
140: */
141:
142: // Commiter's comment:
143: // Yes, it's required, it's defined in the JAXWS spec.
144: if (project != null) {
145: project.addCompileSourceRoot(new File(outputFile)
146: .getParentFile().getAbsolutePath());
147: }
148: }
149:
150: // classpath arg
151: args.add("-cp");
152: args.add(classpath);
153:
154: // soap12 arg
155: if (soap12 != null && soap12.booleanValue()) {
156: args.add("-soap12");
157: }
158:
159: // target namespace arg
160: if (targetNamespace != null) {
161: args.add("-t");
162: args.add(targetNamespace);
163: }
164:
165: // servicename arg
166: if (serviceName != null) {
167: args.add("-servicename");
168: args.add(serviceName);
169: }
170:
171: // verbose arg
172: if (verbose != null && verbose.booleanValue()) {
173: args.add("-verbose");
174: }
175:
176: // quiet arg
177: if (quiet != null && quiet.booleanValue()) {
178: args.add("-quiet");
179: }
180:
181: // classname arg
182: args.add(className);
183:
184: String exitOnFinish = System.getProperty("exitOnFinish", "");
185: try {
186: JavaToWSDL.main(args.toArray(new String[args.size()]));
187: } catch (ExitException e) {
188: if (e.getStatus() != 0) {
189: throw e;
190: }
191: } finally {
192: System.setProperty("exitOnFinish", exitOnFinish);
193: }
194: }
195: }
|