001: package org.objectweb.celtix.maven_plugin;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.Iterator;
006: import java.util.List;
007:
008: import org.apache.maven.plugin.AbstractMojo;
009: import org.apache.maven.plugin.MojoExecutionException;
010: import org.apache.maven.project.MavenProject;
011: import org.apache.tools.ant.ExitException;
012: import org.apache.tools.ant.util.optional.NoExitSecurityManager;
013: import org.objectweb.celtix.tools.WSDLToJava;
014:
015: /**
016: * @goal wsdl2java
017: * @description Celtix WSDL To Java Tool
018: */
019: public class WSDL2JavaMojo extends AbstractMojo {
020: /**
021: * @parameter
022: */
023: String testSourceRoot;
024:
025: /**
026: * @parameter expression="${project.build.directory}/generated/src/main/java"
027: * @required
028: */
029: String sourceRoot;
030:
031: /**
032: * @parameter expression="${project.build.outputDirectory}"
033: * @required
034: */
035: String classesDirectory;
036:
037: /**
038: * @parameter expression="${project.compileClasspathElements}"
039: * @required
040: */
041: List classpathElements;
042:
043: /**
044: * @parameter expression="${project}"
045: * @required
046: */
047: MavenProject project;
048:
049: /**
050: * @parameter
051: */
052: WsdlOption wsdlOptions[];
053:
054: public void execute() throws MojoExecutionException {
055: String outputDir = testSourceRoot == null ? sourceRoot
056: : testSourceRoot;
057: File outputDirFile = new File(outputDir);
058: outputDirFile.mkdirs();
059:
060: File classesDir = new File(classesDirectory);
061: classesDir.mkdirs();
062:
063: StringBuffer buf = new StringBuffer();
064: Iterator it = classpathElements.iterator();
065: while (it.hasNext()) {
066: buf.append(it.next().toString());
067: buf.append(File.pathSeparatorChar);
068: }
069: String newCp = buf.toString();
070:
071: String cp = System.getProperty("java.class.path");
072: SecurityManager oldSm = System.getSecurityManager();
073: long timestamp = CodegenUtils.getCodegenTimestamp();
074: try {
075: System.setProperty("java.class.path", newCp);
076: System.setSecurityManager(new NoExitSecurityManager());
077:
078: for (int x = 0; x < wsdlOptions.length; x++) {
079: processWsdl(wsdlOptions[x], outputDirFile, timestamp);
080: }
081: } finally {
082: System.setSecurityManager(oldSm);
083: System.setProperty("java.class.path", cp);
084: }
085: if (project != null && sourceRoot != null) {
086: project.addCompileSourceRoot(sourceRoot);
087: }
088: if (project != null && testSourceRoot != null) {
089: project.addTestCompileSourceRoot(testSourceRoot);
090: }
091: }
092:
093: private void processWsdl(WsdlOption wsdlOption, File outputDirFile,
094: long cgtimestamp) throws MojoExecutionException {
095: File file = new File(wsdlOption.getWsdl());
096: File doneFile = new File(outputDirFile, "." + file.getName()
097: + ".DONE");
098: boolean doWork = cgtimestamp > doneFile.lastModified();
099: if (!doneFile.exists()) {
100: doWork = true;
101: } else if (file.lastModified() > doneFile.lastModified()) {
102: doWork = true;
103: } else {
104: File files[] = wsdlOption.getDependencies();
105: if (files != null) {
106: for (int z = 0; z < files.length; ++z) {
107: if (files[z].lastModified() > doneFile
108: .lastModified()) {
109: doWork = true;
110: }
111: }
112: }
113: }
114:
115: if (doWork) {
116:
117: List list = new ArrayList();
118: if (wsdlOption.getPackagenames() != null) {
119: Iterator it = wsdlOption.getPackagenames().iterator();
120: while (it.hasNext()) {
121: list.add("-p");
122: list.add(it.next().toString());
123: }
124: }
125: // -d specify the dir for generated source code
126: //list.add("-verbose");
127: list.add("-d");
128: list.add(outputDirFile.toString());
129:
130: if (wsdlOption.getExtraargs() != null) {
131: Iterator it = wsdlOption.getExtraargs().iterator();
132: while (it.hasNext()) {
133: list.add(it.next().toString());
134: }
135: }
136: list.add(wsdlOption.getWsdl());
137:
138: try {
139: try {
140: WSDLToJava.main((String[]) list
141: .toArray(new String[list.size()]));
142: doneFile.delete();
143: doneFile.createNewFile();
144: } catch (ExitException e) {
145: if (e.getStatus() == 0) {
146: doneFile.delete();
147: doneFile.createNewFile();
148: } else {
149: throw e;
150: }
151: }
152: } catch (Throwable e) {
153: e.printStackTrace();
154: throw new MojoExecutionException(e.getMessage(), e);
155: }
156: }
157: }
158: }
|