File: WSConsumeTask.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,276 kB
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (323 lines) | stat: -rw-r--r-- 9,941 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.wsf.spi.tools.ant;

import java.io.File;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.ExecuteJava;
import org.apache.tools.ant.taskdefs.LogOutputStream;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.jboss.wsf.spi.tools.WSContractConsumer;

/**
 * Ant task which consumes a Web Service contract.
 *
 * <table border="1">
 *   <tr align="left" BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><th>Attribute</th><th>Description</th><th>Default</th></tr>
 *   <tr><td>fork</td><td>Whether or not to run the generation task in a separate VM.</td><td>true</td></tr>
 *   <tr><td>keep</td><td>Keep/Enable Java source code generation.</td><td>false</td></tr>
 *   <tr><td>catalog</td><td> Oasis XML Catalog file for entity resolution</td><td>none</td></tr>
 *   <tr><td>package</td><td> The target Java package for generated code.</td><td>generated</td></tr>
 *   <tr><td>binding</td><td>A JAX-WS or JAXB binding file</td><td>none</td></tr>
 *   <tr><td>wsdlLocation</td><td>Value to use for @@WebService.wsdlLocation</td><td>generated</td></tr>
 *   <tr><td>destdir</td><td>The output directory for generated artifacts.</td><td>"output"</td></tr>
 *   <tr><td>sourcedestdir</td><td>The output directory for Java source.</td><td>value of destdir</td></tr>
 *   <tr><td>target</td><td>The JAX-WS specification target</td><td>2.0 | 2.1</td></tr>
 *   <tr><td>verbose</td><td>Enables more informational output about cmd progress.</td><td>false</td><tr>
 *   <tr><td>wsdl*</td><td>The WSDL file or URL</td><td>n/a</td><tr>
 * </table>
 * <b>* = required.</b>
 *
 * <p>Example:
 *
 * <pre>
 * &lt;WSConsumeTask
 *   fork=&quot;true&quot;
 *   verbose=&quot;true&quot;
 *   destdir=&quot;output&quot;
 *   sourcedestdir=&quot;gen-src&quot;
 *   keep=&quot;true&quot;
 *   wsdllocation=&quot;handEdited.wsdl&quot;
 *   wsdl=&quot;foo.wsdl&quot;&gt;
 *   &lt;binding dir=&quot;binding-files&quot; includes=&quot;*.xml&quot; excludes=&quot;bad.xml&quot;/&gt;
 * &lt;/wsimport&gt;
 * </pre>
 *
 * @author <a href="mailto:jason.greene@jboss.com">Jason T. Greene</a>
 * @version $Revision: 3959 $
 */
public class WSConsumeTask extends Task
{
   private CommandlineJava command = new CommandlineJava();
   private String wsdl;
   private File destdir;
   private File sourcedestdir;
   private List<File> bindingFiles = new ArrayList<File>();
   private File catalog;
   private String wsdlLocation;
   private String targetPackage;
   private boolean keep;
   private boolean verbose;
   private boolean fork;
   private boolean debug;
   private String target;

   // Not actually used right now
   public void setDebug(boolean debug)
   {
      this.debug = debug;
   }

   public Commandline.Argument createJvmarg()
   {
      return command.createVmArgument();
   }

   public void setBinding(File bindingFile)
   {
      bindingFiles.add(bindingFile);
   }

   public void setCatalog(File catalog)
   {
      this.catalog = catalog;
   }

   public void setDestdir(File destdir)
   {
      this.destdir = destdir;
   }

   public void setFork(boolean fork)
   {
      this.fork = fork;
   }

   public void setKeep(boolean keep)
   {
      this.keep = keep;
   }

   public void setSourcedestdir(File sourcedestdir)
   {
      this.sourcedestdir = sourcedestdir;
   }

   public void setTarget(String target)
   {
      this.target = target;
   }

   public void setPackage(String targetPackage)
   {
      this.targetPackage = targetPackage;
   }

   public void setVerbose(boolean verbose)
   {
      this.verbose = verbose;
   }

   public void setWsdl(String wsdl)
   {
      this.wsdl = wsdl;
   }

   public void setWsdlLocation(String wsdlLocation)
   {
      this.wsdlLocation = wsdlLocation;
   }

   public void addConfiguredBinding(FileSet fs)
   {
      DirectoryScanner ds = fs.getDirectoryScanner(getProject());
      File baseDir = ds.getBasedir();
      for (String file : ds.getIncludedFiles())
      {
         bindingFiles.add(new File(baseDir, file));
      }
   }

   public void executeNonForked()
   {
      ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
      ClassLoader antLoader = this.getClass().getClassLoader();
      Thread.currentThread().setContextClassLoader(antLoader);
      try
      {
         WSContractConsumer importer = WSContractConsumer.newInstance();
         importer.setGenerateSource(keep);
         if (destdir != null)
            importer.setOutputDirectory(destdir);
         if (sourcedestdir != null)
            importer.setSourceDirectory(sourcedestdir);
         if (targetPackage != null)
            importer.setTargetPackage(targetPackage);
         if (wsdlLocation != null)
            importer.setWsdlLocation(wsdlLocation);
         if (catalog != null)
            importer.setCatalog(catalog);
         if (bindingFiles != null && bindingFiles.size() > 0)
            importer.setBindingFiles(bindingFiles);
         if (target != null)
            importer.setTarget(target);

         log("Consuming wsdl: " + wsdl, Project.MSG_INFO);

         if (verbose)
         {
            importer.setMessageStream(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)));
         }

         try
         {
            importer.setAdditionalCompilerClassPath(getTaskClassPathStrings());
            importer.consume(wsdl);
         }
         catch (MalformedURLException e)
         {
            throw new BuildException(e, getLocation());
         }
      }
      finally
      {
         Thread.currentThread().setContextClassLoader(prevCL);
      }
   }

   public void execute() throws BuildException
   {
      if (wsdl == null)
         throw new BuildException("The wsdl attribute must be specified!", getLocation());

      if (fork)
         executeForked();
      else executeNonForked();
   }

   private Path getTaskClassPath()
   {
      // Why is everything in the Ant API a big hack???
      ClassLoader cl = this.getClass().getClassLoader();
      if (cl instanceof AntClassLoader)
      {
         return new Path(getProject(), ((AntClassLoader)cl).getClasspath());
      }

      return new Path(getProject());
   }

   private List<String> getTaskClassPathStrings()
   {
      // Why is everything in the Ant API a big hack???
      List<String> strings = new ArrayList<String>();
      ClassLoader cl = this.getClass().getClassLoader();
      if (cl instanceof AntClassLoader)
      {
         for (String string : ((AntClassLoader)cl).getClasspath().split(File.pathSeparator))
            strings.add(string);
      }

      return strings;
   }

   private void executeForked() throws BuildException
   {
      command.setClassname(org.jboss.wsf.spi.tools.cmd.WSConsume.class.getName());

      Path path = command.createClasspath(getProject());
      path.append(getTaskClassPath());

      if (keep)
         command.createArgument().setValue("-k");

      for (File file : bindingFiles)
      {
         command.createArgument().setValue("-b");
         command.createArgument().setFile(file);
      }

      if (catalog != null)
      {
         command.createArgument().setValue("-c");
         command.createArgument().setFile(catalog);
      }

      if (targetPackage != null)
      {
         command.createArgument().setValue("-p");
         command.createArgument().setValue(targetPackage);
      }

      if (wsdlLocation != null)
      {
         command.createArgument().setValue("-w");
         command.createArgument().setValue(wsdlLocation);
      }

      if (destdir != null)
      {
         command.createArgument().setValue("-o");
         command.createArgument().setFile(destdir);
      }

      if (sourcedestdir != null)
      {
         command.createArgument().setValue("-s");
         command.createArgument().setFile(sourcedestdir);
      }

      if (target != null)
      {
         command.createArgument().setValue("-t");
         command.createArgument().setValue(target);
      }

      if (verbose)
         command.createArgument().setValue("-v");

      command.createArgument().setValue(wsdl);

      log("Consuming wsdl: " + wsdl, Project.MSG_INFO);
      
      if (verbose)
         log("Command invoked: " + command.getJavaCommand().toString());

      ExecuteJava execute = new ExecuteJava();
      execute.setClasspath(path);
      execute.setJavaCommand(command.getJavaCommand());
      if (execute.fork(this) != 0)
         throw new BuildException("Could not invoke WSConsumeTask", getLocation());
   }
}