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.Iterator;
023: import java.util.List;
024:
025: import org.apache.cxf.tools.wsdlto.WSDLToJava;
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 wsdl2java
034: * @description CXF WSDL To Java Tool
035: */
036: public class WSDL2JavaMojo extends AbstractMojo {
037: /**
038: * @parameter
039: */
040: String testSourceRoot;
041:
042: /**
043: * @parameter expression="${project.build.directory}/generated/src/main/java"
044: * @required
045: */
046: String sourceRoot;
047:
048: /**
049: * @parameter expression="${project.build.outputDirectory}"
050: * @required
051: */
052: String classesDirectory;
053:
054: /**
055: * @parameter expression="${project.compileClasspathElements}"
056: * @required
057: */
058: List classpathElements;
059:
060: /**
061: * @parameter expression="${project}"
062: * @required
063: */
064: MavenProject project;
065:
066: /**
067: * @parameter
068: */
069: WsdlOption wsdlOptions[];
070:
071: public void execute() throws MojoExecutionException {
072: String outputDir = testSourceRoot == null ? sourceRoot
073: : testSourceRoot;
074: File outputDirFile = new File(outputDir);
075: outputDirFile.mkdirs();
076:
077: File classesDir = new File(classesDirectory);
078: classesDir.mkdirs();
079:
080: if (wsdlOptions == null) {
081: throw new MojoExecutionException("Must specify wsdlOptions");
082: }
083:
084: StringBuffer buf = new StringBuffer();
085: Iterator it = classpathElements.iterator();
086: while (it.hasNext()) {
087: buf.append(it.next().toString());
088: buf.append(File.pathSeparatorChar);
089: }
090: String newCp = buf.toString();
091:
092: String cp = System.getProperty("java.class.path");
093: SecurityManager oldSm = System.getSecurityManager();
094: long timestamp = CodegenUtils.getCodegenTimestamp();
095: boolean result = true;
096: try {
097: System.setProperty("java.class.path", newCp);
098: System.setSecurityManager(new NoExitSecurityManager());
099: for (int x = 0; x < wsdlOptions.length; x++) {
100: processWsdl(wsdlOptions[x], outputDirFile, timestamp);
101:
102: File dirs[] = wsdlOptions[x].getDeleteDirs();
103: if (dirs != null) {
104: for (int idx = 0; idx < dirs.length; ++idx) {
105: result = result && deleteDir(dirs[idx]);
106: }
107: }
108: }
109: } finally {
110: System.setSecurityManager(oldSm);
111: System.setProperty("java.class.path", cp);
112: }
113: if (project != null && sourceRoot != null) {
114: project.addCompileSourceRoot(sourceRoot);
115: }
116: if (project != null && testSourceRoot != null) {
117: project.addTestCompileSourceRoot(testSourceRoot);
118: }
119:
120: System.gc();
121: }
122:
123: private void processWsdl(WsdlOption wsdlOption, File outputDirFile,
124: long cgtimestamp) throws MojoExecutionException {
125: File file = new File(wsdlOption.getWsdl());
126: // If URL to WSDL, replace ? and & since they're invalid chars for file names
127: File doneFile = new File(outputDirFile, "."
128: + file.getName().replace('?', '_').replace('&', '_')
129: + ".DONE");
130: boolean doWork = cgtimestamp > doneFile.lastModified();
131: if (!doneFile.exists()) {
132: doWork = true;
133: } else if (file.lastModified() > doneFile.lastModified()) {
134: doWork = true;
135: } else if (isDefServiceName(wsdlOption)) {
136: doWork = true;
137: } else {
138: File files[] = wsdlOption.getDependencies();
139: if (files != null) {
140: for (int z = 0; z < files.length; ++z) {
141: if (files[z].lastModified() > doneFile
142: .lastModified()) {
143: doWork = true;
144: }
145: }
146: }
147: }
148:
149: if (doWork) {
150:
151: List<String> list = new ArrayList<String>();
152: if (wsdlOption.getPackagenames() != null) {
153: Iterator it = wsdlOption.getPackagenames().iterator();
154: while (it.hasNext()) {
155: list.add("-p");
156: list.add(it.next().toString());
157: }
158: }
159: // -d specify the dir for generated source code
160: //list.add("-verbose");
161: list.add("-d");
162: list.add(outputDirFile.toString());
163:
164: if (wsdlOption.getExtraargs() != null) {
165: Iterator it = wsdlOption.getExtraargs().iterator();
166: while (it.hasNext()) {
167: list.add(it.next().toString());
168: }
169: }
170: list.add(wsdlOption.getWsdl());
171:
172: try {
173: String exitOnFinish = System.getProperty(
174: "exitOnFinish", "");
175:
176: try {
177: StringBuffer strBuffer = new StringBuffer();
178: for (int i = 0; i < list.size(); i++) {
179: strBuffer.append(list.get(i));
180: strBuffer.append(" ");
181: }
182: System.setProperty("exitOnFinish", "YES");
183: WSDLToJava.main((String[]) list
184: .toArray(new String[list.size()]));
185: doneFile.delete();
186: doneFile.createNewFile();
187: } catch (ExitException e) {
188: if (e.getStatus() == 0) {
189: doneFile.delete();
190: doneFile.createNewFile();
191: } else {
192: throw e;
193: }
194: } finally {
195: System.setProperty("exitOnFinish", exitOnFinish);
196: }
197: } catch (Throwable e) {
198: getLog().debug(e);
199: throw new MojoExecutionException(e.getMessage(), e);
200: }
201: }
202: }
203:
204: private boolean deleteDir(File f) {
205: if (f.isDirectory()) {
206: File files[] = f.listFiles();
207: for (int idx = 0; idx < files.length; ++idx) {
208: deleteDir(files[idx]);
209: }
210: }
211:
212: if (f.exists()) {
213: return f.delete();
214: }
215:
216: return true;
217: }
218:
219: private boolean isDefServiceName(WsdlOption wsdlOption) {
220: List args = wsdlOption.extraargs;
221: if (args == null) {
222: return false;
223: }
224: for (int i = 0; i < args.size(); i++) {
225: if ("-sn".equalsIgnoreCase((String) args.get(i))) {
226: return true;
227: }
228: }
229: return false;
230:
231: }
232:
233: }
|