KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > maven_plugin > WSDL2JavaMojo


1 package org.objectweb.celtix.maven_plugin;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.apache.maven.plugin.AbstractMojo;
9 import org.apache.maven.plugin.MojoExecutionException;
10 import org.apache.maven.project.MavenProject;
11 import org.apache.tools.ant.ExitException;
12 import org.apache.tools.ant.util.optional.NoExitSecurityManager;
13 import org.objectweb.celtix.tools.WSDLToJava;
14 /**
15  * @goal wsdl2java
16  * @description Celtix WSDL To Java Tool
17  */

18 public class WSDL2JavaMojo extends AbstractMojo {
19     /**
20      * @parameter
21      */

22     String JavaDoc testSourceRoot;
23     
24     /**
25      * @parameter expression="${project.build.directory}/generated/src/main/java"
26      * @required
27      */

28     String JavaDoc sourceRoot;
29     
30     /**
31      * @parameter expression="${project.build.outputDirectory}"
32      * @required
33      */

34     String JavaDoc classesDirectory;
35     
36     /**
37      * @parameter expression="${project.compileClasspathElements}"
38      * @required
39      */

40     List JavaDoc classpathElements;
41
42     /**
43      * @parameter expression="${project}"
44      * @required
45      */

46     MavenProject project;
47     
48     
49     /**
50      * @parameter
51      */

52     WsdlOption wsdlOptions[];
53
54     public void execute() throws MojoExecutionException {
55         String JavaDoc outputDir = testSourceRoot == null ? sourceRoot : testSourceRoot;
56         File JavaDoc outputDirFile = new File JavaDoc(outputDir);
57         outputDirFile.mkdirs();
58         
59         File JavaDoc classesDir = new File JavaDoc(classesDirectory);
60         classesDir.mkdirs();
61         
62         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
63         Iterator JavaDoc it = classpathElements.iterator();
64         while (it.hasNext()) {
65             buf.append(it.next().toString());
66             buf.append(File.pathSeparatorChar);
67         }
68         String JavaDoc newCp = buf.toString();
69         
70         String JavaDoc cp = System.getProperty("java.class.path");
71         SecurityManager JavaDoc oldSm = System.getSecurityManager();
72         long timestamp = CodegenUtils.getCodegenTimestamp();
73         try {
74             System.setProperty("java.class.path", newCp);
75             System.setSecurityManager(new NoExitSecurityManager());
76         
77             for (int x = 0; x < wsdlOptions.length; x++) {
78                 processWsdl(wsdlOptions[x], outputDirFile, timestamp);
79             }
80         } finally {
81             System.setSecurityManager(oldSm);
82             System.setProperty("java.class.path", cp);
83         }
84         if (project != null && sourceRoot != null) {
85             project.addCompileSourceRoot(sourceRoot);
86         }
87         if (project != null && testSourceRoot != null) {
88             project.addTestCompileSourceRoot(testSourceRoot);
89         }
90     }
91     
92     private void processWsdl(WsdlOption wsdlOption,
93                              File JavaDoc outputDirFile,
94                              long cgtimestamp) throws MojoExecutionException {
95         File JavaDoc file = new File JavaDoc(wsdlOption.getWsdl());
96         File JavaDoc doneFile = new File JavaDoc(outputDirFile, "." + file.getName() + ".DONE");
97         boolean doWork = cgtimestamp > doneFile.lastModified();
98         if (!doneFile.exists()) {
99             doWork = true;
100         } else if (file.lastModified() > doneFile.lastModified()) {
101             doWork = true;
102         } else {
103             File JavaDoc files[] = wsdlOption.getDependencies();
104             if (files != null) {
105                 for (int z = 0; z < files.length; ++z) {
106                     if (files[z].lastModified() > doneFile.lastModified()) {
107                         doWork = true;
108                     }
109                 }
110             }
111         }
112         
113         if (doWork) {
114             
115             List JavaDoc list = new ArrayList JavaDoc();
116             if (wsdlOption.getPackagenames() != null) {
117                 Iterator JavaDoc it = wsdlOption.getPackagenames().iterator();
118                 while (it.hasNext()) {
119                     list.add("-p");
120                     list.add(it.next().toString());
121                 }
122             }
123             // -d specify the dir for generated source code
124
//list.add("-verbose");
125
list.add("-d");
126             list.add(outputDirFile.toString());
127             
128             if (wsdlOption.getExtraargs() != null) {
129                 Iterator JavaDoc it = wsdlOption.getExtraargs().iterator();
130                 while (it.hasNext()) {
131                     list.add(it.next().toString());
132                 }
133             }
134             list.add(wsdlOption.getWsdl());
135             
136             
137             try {
138                 try {
139                     WSDLToJava.main((String JavaDoc[])list.toArray(new String JavaDoc[list.size()]));
140                     doneFile.delete();
141                     doneFile.createNewFile();
142                 } catch (ExitException e) {
143                     if (e.getStatus() == 0) {
144                         doneFile.delete();
145                         doneFile.createNewFile();
146                     } else {
147                         throw e;
148                     }
149                 }
150             } catch (Throwable JavaDoc e) {
151                 e.printStackTrace();
152                 throw new MojoExecutionException(e.getMessage(), e);
153             }
154         }
155     }
156 }
157
Popular Tags