1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.mojo.jaxws;
18
19 import java.io.File;
20 import java.io.FileFilter;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27
28 import com.sun.tools.ws.wscompile.WsimportTool;
29
30
31
32
33
34
35
36
37
38 abstract class WsImportMojo extends AbstractJaxwsMojo
39 {
40
41
42
43
44
45
46 private String packageName;
47
48
49
50
51
52
53
54 private File catalog;
55
56
57
58
59
60
61 private String httpproxy;
62
63
64
65
66
67
68 private File wsdlDirectory;
69
70
71
72
73
74
75
76 protected List wsdlFiles;
77
78
79
80
81
82
83 private List wsdlUrls;
84
85
86
87
88
89
90 protected File bindingDirectory;
91
92
93
94
95
96
97
98 protected List bindingFiles;
99
100
101
102
103
104
105
106 private String wsdlLocation;
107
108
109
110
111
112
113
114
115 private String target;
116
117
118
119
120
121
122 protected File sourceDestDir;
123
124
125
126
127
128
129
130 private List xjcArgs;
131
132
133
134
135
136 private File staleFile;
137
138 public void execute()
139 throws MojoExecutionException
140 {
141
142 if (getWSDLFiles().length > 1 && wsdlLocation != null) {
143 getLog().warn("If 'wsdlLocation' is set, only one wsdl can be properly processed "
144 + "per maven execution. Define multiple maven executions having different ids!");
145 getLog().warn("Also define different staleFiles "
146 + "(see http://blog.darevay.com/2009/03/importing-multiple-wsdls-with-maven/)");
147 }
148
149
150 ClassLoader parent = this.getClass().getClassLoader();
151 String originalSystemClasspath = this.initClassLoader( parent );
152
153 try
154 {
155
156 sourceDestDir.mkdirs();
157 getDestDir().mkdirs();
158
159 this.processWsdlViaUrls();
160
161 this.processLocalWsdlFiles();
162
163
164
165 project.addCompileSourceRoot( sourceDestDir.getAbsolutePath() );
166
167 }
168 catch ( MojoExecutionException e )
169 {
170 throw e;
171 }
172 catch ( Exception e )
173 {
174 throw new MojoExecutionException( e.getMessage(), e );
175 }
176 finally
177 {
178
179 Thread.currentThread().setContextClassLoader( parent );
180 System.setProperty( "java.class.path", originalSystemClasspath );
181 }
182
183 }
184
185
186
187
188
189
190 private void processLocalWsdlFiles()
191 throws MojoExecutionException, IOException
192 {
193
194 if ( isOutputStale() )
195 {
196 File[] wsdls = getWSDLFiles();
197 for ( int i = 0; i < wsdls.length; i++ )
198 {
199 getLog().info( "Processing: " + wsdls[i].getAbsolutePath() );
200 ArrayList<String> args = getWsImportArgs();
201 args.add( wsdls[i].getAbsolutePath() );
202 getLog().info( "jaxws:wsimport args: " + args );
203 wsImport( args );
204
205 }
206 touchStaleFile();
207 }
208
209 }
210
211
212
213
214
215 private void processWsdlViaUrls()
216 throws MojoExecutionException
217 {
218
219
220 for ( int i = 0; wsdlUrls != null && i < wsdlUrls.size(); i++ )
221 {
222 String wsdlUrl = wsdlUrls.get( i ).toString();
223
224 getLog().info( "Processing: " + wsdlUrl );
225 ArrayList<String> args = getWsImportArgs();
226 args.add( wsdlUrl );
227 getLog().info( "jaxws:wsimport args: " + args );
228 wsImport( args );
229 }
230 }
231
232
233
234
235
236
237 private void wsImport( ArrayList<String> args )
238 throws MojoExecutionException
239 {
240 WsimportTool compTool = new WsimportTool( System.out );
241 if ( !compTool.run( args.toArray( new String[args.size()] ) ) )
242 {
243 throw new MojoExecutionException( "Error executing: wsimport " + args );
244 }
245 }
246
247
248
249
250
251
252 private ArrayList<String> getWsImportArgs()
253 throws MojoExecutionException
254 {
255 ArrayList<String> args = new ArrayList<String>();
256
257 args.add( "-s" );
258 args.add( sourceDestDir.getAbsolutePath() );
259
260 args.add( "-d" );
261 args.add( getDestDir().getAbsolutePath() );
262
263 if ( verbose )
264 {
265 args.add( "-verbose" );
266 }
267
268 if ( httpproxy != null )
269 {
270 args.add( "-httpproxy" );
271 args.add( httpproxy );
272 }
273
274 if ( packageName != null )
275 {
276 args.add( "-p" );
277 args.add( packageName );
278 }
279
280 if ( catalog != null )
281 {
282 args.add( "-catalog" );
283 args.add( catalog.getAbsolutePath() );
284 }
285
286 if ( wsdlLocation != null )
287 {
288 args.add( "-wsdllocation" );
289 args.add( wsdlLocation );
290 }
291
292 if ( target != null )
293 {
294 args.add( "-target" );
295 args.add( target );
296 }
297
298 if ( extension )
299 {
300 args.add( "-extension" );
301 }
302
303
304 if (xjcArgs != null)
305 {
306 Iterator xjcArgsIter = xjcArgs.iterator();
307 while ( xjcArgsIter.hasNext() )
308 {
309 String xjcArg = (String)xjcArgsIter.next();
310 args.add( "-B" + xjcArg );
311 }
312 }
313
314
315 File bindings[] = getBindingFiles();
316 for ( int i = 0; i < bindings.length; i++ )
317 {
318 args.add( "-b" );
319 args.add( bindings[i].getAbsolutePath() );
320 }
321
322 getLog().debug( "jaxws:wsimport args: " + args );
323
324 return args;
325 }
326
327
328
329
330
331
332 public final File[] getBindingFiles()
333 {
334 File [] bindings;
335
336 if ( bindingFiles != null )
337 {
338 bindings = new File[bindingFiles.size()];
339 for ( int i = 0 ; i < bindingFiles.size(); ++i )
340 {
341 String schemaName = (String) bindingFiles.get( i );
342 File file = new File( schemaName );
343 if (!file.isAbsolute()) {
344 file = new File( bindingDirectory, schemaName );
345 }
346 bindings[i] = file;
347 }
348 }
349 else
350 {
351 getLog().debug( "The binding Directory is " + bindingDirectory );
352 bindings = bindingDirectory.listFiles( new XMLFile() );
353 if ( bindings == null )
354 {
355 bindings = new File[0];
356 }
357 }
358
359 return bindings;
360 }
361
362
363
364
365
366
367 public final File[] getWSDLFiles()
368 {
369 File [] files;
370
371 if ( wsdlFiles != null )
372 {
373 files = new File[ wsdlFiles.size() ];
374 for ( int i = 0 ; i < wsdlFiles.size(); ++i )
375 {
376 String schemaName = (String) wsdlFiles.get( i );
377 files[i] = new File( wsdlDirectory, schemaName ) ;
378 }
379 }
380 else
381 {
382 getLog().debug( "The wsdl Directory is " + wsdlDirectory );
383 files = wsdlDirectory.listFiles( new WSDLFile() );
384 if ( files == null )
385 {
386 files = new File[0];
387 }
388 }
389
390 return files;
391 }
392
393
394
395
396 private final class XMLFile
397 implements FileFilter
398 {
399
400
401
402
403
404
405
406 public boolean accept( final java.io.File file )
407 {
408 return file.getName().endsWith( ".xml" );
409 }
410 }
411
412
413
414
415 private final class WSDLFile
416 implements FileFilter
417 {
418
419
420
421
422
423
424
425
426 public boolean accept( final java.io.File file )
427 {
428 return file.getName().endsWith( ".wsdl" );
429 }
430
431 }
432
433
434
435
436
437
438 private boolean isOutputStale()
439 {
440 File[] sourceWsdls = getWSDLFiles();
441 File[] sourceBindings = getBindingFiles();
442 boolean stale = !staleFile.exists();
443 if ( !stale )
444 {
445 getLog().debug( "Stale flag file exists, comparing to wsdls and bindings." );
446 long staleMod = staleFile.lastModified();
447
448 for ( int i = 0; i < sourceWsdls.length; i++ )
449 {
450 if ( sourceWsdls[i].lastModified() > staleMod )
451 {
452 getLog().debug( sourceWsdls[i].getName() + " is newer than the stale flag file." );
453 stale = true;
454 }
455 }
456
457 for ( int i = 0; i < sourceBindings.length; i++ )
458 {
459 if ( sourceBindings[i].lastModified() > staleMod )
460 {
461 getLog().debug( sourceBindings[i].getName() + " is newer than the stale flag file." );
462 stale = true;
463 }
464 }
465 }
466 return stale;
467 }
468
469 private void touchStaleFile()
470 throws IOException
471 {
472 if ( !staleFile.exists() )
473 {
474 staleFile.getParentFile().mkdirs();
475 staleFile.createNewFile();
476 getLog().debug( "Stale flag file created." );
477 }
478 else
479 {
480 staleFile.setLastModified( System.currentTimeMillis() );
481 }
482 }
483
484 }