File: WSProvideTask.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 (298 lines) | stat: -rw-r--r-- 9,572 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
/*
* 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 org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
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.Path;
import org.apache.tools.ant.types.Reference;
import org.jboss.wsf.spi.tools.WSContractProvider;

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

/**
 * Ant task which invokes provides a Web Service contract and portable JAX-WS wrapper classes.
 * 
 * <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>destdir</td><td>The output directory for generated artifacts.</td><td>"output"</td></tr>
 *   <tr><td>resourcedestdir</td><td>The output directory for resource artifacts (WSDL/XSD).</td><td>value of destdir</td></tr>
 *   <tr><td>sourcedestdir</td><td>The output directory for Java source.</td><td>value of destdir</td></tr>
 *   <tr><td>genwsdl</td><td>Whether or not to generate WSDL.</td><td>false</td><tr>
 *   <tr><td>verbose</td><td>Enables more informational output about cmd progress.</td><td>false</td><tr>
 *   <tr><td>sei*</td><td>Service Endpoint Implementation.</td><td></td><tr>
 *   <tr><td>classpath</td><td>The classpath that contains the service endpoint implementation.</td><td>""</tr>
 * </table>
 * <b>* = required.</b>
 * 
 * <p>Example:
 * 
 * <pre>
 *  &lt;target name=&quot;test-wsproivde&quot; depends=&quot;init&quot;&gt;
 *    &lt;taskdef name=&quot;WSProvideTask&quot; classname=&quot;org.jboss.wsf.spi.tools.ant.WSProvideTask&quot;&gt;
 *      &lt;classpath refid=&quot;core.classpath&quot;/&gt;
 *    &lt;/taskdef&gt;
 *    &lt;WSProvideTask
 *      fork=&quot;false&quot;
 *      keep=&quot;true&quot;
 *      destdir=&quot;out&quot;
 *      resourcedestdir=&quot;out-resource&quot;
 *      sourcedestdir=&quot;out-source&quot;
 *      genwsdl=&quot;true&quot; 
 *      verbose=&quot;true&quot;
 *      sei=&quot;org.jboss.test.ws.jaxws.jsr181.soapbinding.DocWrappedServiceImpl&quot;&gt;
 *      &lt;classpath&gt;
 *        &lt;pathelement path=&quot;${tests.output.dir}/classes&quot;/&gt;
 *      &lt;/classpath&gt;
 *    &lt;/WSProvideTask&gt;
 *  &lt;/target&gt;
 * </pre>
 * 
 * @author <a href="mailto:jason.greene@jboss.com">Jason T. Greene</a>
 * @version $Revision: 4153 $
 */
public class WSProvideTask extends Task
{
   private Path classpath = new Path(getProject());
   private CommandlineJava command = new CommandlineJava();
   private String sei = null;
   private File destdir = null;
   private File resourcedestdir = null;
   private File sourcedestdir = null;
   private boolean keep = false;
   private boolean genwsdl = false;
   private boolean verbose = false;
   private boolean fork = false;
   private boolean debug = false;
   
   // Not actually used right now
   public void setDebug(boolean debug)
   {
      this.debug = debug;
   }
   
   public Commandline.Argument createJvmarg() 
   {
      return command.createVmArgument();
   }
   
   public void setClasspath(Path classpath)
   {
      this.classpath = classpath;
   }
   
   public void setClasspathRef(Reference ref)
   {
      createClasspath().setRefid(ref);
   }
   
   public Path createClasspath()
   {
      return classpath;
   }
   
   public void setDestdir(File destdir)
   {
      this.destdir = destdir;
   }

   public void setKeep(boolean keep)
   {
      this.keep = keep;
   }
   
   public void setSei(String sei)
   {
      this.sei = sei;
   }
   
   public void setFork(boolean fork)
   {
      this.fork = fork;
   }

   public void setResourcedestdir(File resourcedestdir)
   {
      this.resourcedestdir = resourcedestdir;
   }

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

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

   public void setGenwsdl(boolean genwsdl)
   {
      this.genwsdl = genwsdl;
   }
   
   private ClassLoader getClasspathLoader(ClassLoader parent)
   {
		AntClassLoader antLoader = new AntClassLoader(parent, getProject(), classpath, false);

		// It's necessary to wrap it into an URLLoader in order to extract that information
		// within the actual provider impl.
		// See SunRIProviderImpl for instance
		List<URL> urls = new ArrayList<URL>();
		StringTokenizer tok = new StringTokenizer(antLoader.getClasspath(), File.separator);
		while(tok.hasMoreTokens())
		{
			try
			{
            String path = tok.nextToken();
            if(!path.startsWith("file://"))
               path = "file://"+path;

            urls.add(new URL(path));
			}
			catch (MalformedURLException e)
			{
				throw new IllegalArgumentException("Failed to wrap classloader", e);
			}

		}

		ClassLoader wrapper = new URLClassLoader(urls.toArray(new URL[0]), antLoader);
		return wrapper;
   }
   
   public void executeNonForked()
   {
      ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
      ClassLoader antLoader = this.getClass().getClassLoader();
      Thread.currentThread().setContextClassLoader(antLoader);
      try
      {
         WSContractProvider gen = WSContractProvider.newInstance(
					getClasspathLoader(antLoader)
			);         
         if (verbose)
            gen.setMessageStream(new PrintStream(new LogOutputStream(this, Project.MSG_INFO)));
         gen.setGenerateSource(keep);
         gen.setGenerateWsdl(genwsdl);
         if (destdir != null)
            gen.setOutputDirectory(destdir);
         if (resourcedestdir != null)
            gen.setResourceDirectory(resourcedestdir);
         if (sourcedestdir != null)
            gen.setSourceDirectory(sourcedestdir);

         log("Generating from endpoint: " + sei, Project.MSG_INFO);
         
         gen.provide(sei);
      }
      finally
      {
         Thread.currentThread().setContextClassLoader(prevCL);
      }
   }
   
   public void execute() throws BuildException
   {
      if (sei == null)
         throw new BuildException("The sei 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 void executeForked() throws BuildException
   {
      command.setClassname(org.jboss.wsf.spi.tools.cmd.WSProvide.class.getName());
      
      Path path = command.createClasspath(getProject());
      path.append(getTaskClassPath());
      path.append(classpath);
     
      if (keep)
         command.createArgument().setValue("-k");
      
      if (genwsdl)
         command.createArgument().setValue("-w");
      
      if (destdir != null)
      {
         command.createArgument().setValue("-o");
         command.createArgument().setFile(destdir);
      }
      if (resourcedestdir != null)
      {
         command.createArgument().setValue("-r");
         command.createArgument().setFile(resourcedestdir);
      }
      if (sourcedestdir != null)
      {
         command.createArgument().setValue("-s");
         command.createArgument().setFile(sourcedestdir);
      }
      
      if (!verbose)
         command.createArgument().setValue("-q");
      
      // Always dump traces
      command.createArgument().setValue("-t");
      command.createArgument().setValue(sei);
      
      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 WSProvideTask", getLocation());
   }
}