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 | |
package org.codehaus.mojo.jaxws; |
38 | |
|
39 | |
import java.io.File; |
40 | |
import java.io.IOException; |
41 | |
import java.net.URL; |
42 | |
import java.net.URLClassLoader; |
43 | |
import java.util.ArrayList; |
44 | |
import java.util.HashSet; |
45 | |
import java.util.Set; |
46 | |
|
47 | |
import javax.jws.WebService; |
48 | |
|
49 | |
import org.apache.maven.model.Resource; |
50 | |
import org.apache.maven.plugin.MojoExecutionException; |
51 | |
import org.apache.maven.plugin.MojoFailureException; |
52 | |
import org.apache.maven.plugins.annotations.Parameter; |
53 | |
import org.codehaus.plexus.util.FileUtils; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | 0 | abstract class AbstractWsGenMojo |
62 | |
extends AbstractJaxwsMojo |
63 | |
{ |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
@Parameter( defaultValue = "false" ) |
69 | |
protected boolean genWsdl; |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
@Parameter |
75 | |
private String sei; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
@Parameter |
84 | |
private String protocol; |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
@Parameter |
91 | |
private String servicename; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
@Parameter |
98 | |
private String portname; |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
@Parameter( defaultValue = "false" ) |
105 | |
private boolean inlineSchemas; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
@Parameter( defaultValue = "false" ) |
112 | |
private boolean xnocompile; |
113 | |
|
114 | |
|
115 | |
|
116 | |
@Parameter( defaultValue = "false" ) |
117 | |
private boolean xdonotoverwrite; |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
@Parameter |
129 | |
private File metadata; |
130 | |
|
131 | |
protected abstract File getResourceDestDir(); |
132 | |
|
133 | |
protected abstract File getClassesDir(); |
134 | |
|
135 | |
@Override |
136 | |
public void executeJaxws() |
137 | |
throws MojoExecutionException, MojoFailureException |
138 | |
{ |
139 | 0 | Set<String> seis = new HashSet<>(); |
140 | 0 | if ( sei != null ) |
141 | |
{ |
142 | 0 | seis.add( sei ); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | |
|
147 | 0 | seis.addAll( getSEIs( getClassesDir() ) ); |
148 | |
} |
149 | 0 | if ( seis.isEmpty() ) |
150 | |
{ |
151 | 0 | throw new MojoFailureException( "No @javax.jws.WebService found." ); |
152 | |
} |
153 | 0 | for ( String aSei : seis ) |
154 | |
{ |
155 | 0 | processSei( aSei ); |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | |
protected void processSei( String aSei ) |
160 | |
throws MojoExecutionException |
161 | |
{ |
162 | 0 | getLog().info( "Processing: " + aSei ); |
163 | 0 | ArrayList<String> args = getWsGenArgs( aSei ); |
164 | 0 | getLog().info( "jaxws:wsgen args: " + args ); |
165 | 0 | exec( args ); |
166 | 0 | if ( metadata != null ) |
167 | |
{ |
168 | |
try |
169 | |
{ |
170 | 0 | FileUtils.copyFileToDirectory( metadata, getClassesDir() ); |
171 | |
} |
172 | 0 | catch ( IOException ioe ) |
173 | |
{ |
174 | 0 | throw new MojoExecutionException( ioe.getMessage(), ioe ); |
175 | 0 | } |
176 | |
} |
177 | 0 | } |
178 | |
|
179 | |
@Override |
180 | |
protected String getMain() |
181 | |
{ |
182 | 0 | return "com.sun.tools.ws.wscompile.WsgenTool"; |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
protected String getToolName() |
187 | |
{ |
188 | 0 | return "wsgen"; |
189 | |
} |
190 | |
|
191 | |
@Override |
192 | |
protected String getExtraClasspath() |
193 | |
{ |
194 | 0 | return String.join( File.pathSeparator, |
195 | 0 | getClassesDir().getAbsolutePath(), |
196 | 0 | getCPasString( project.getArtifacts() ) ); |
197 | |
} |
198 | |
|
199 | |
@Override |
200 | |
protected boolean isXnocompile() |
201 | |
{ |
202 | 0 | return xnocompile; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
private ArrayList<String> getWsGenArgs( String aSei ) |
209 | |
throws MojoExecutionException |
210 | |
{ |
211 | 0 | ArrayList<String> args = new ArrayList<>(); |
212 | 0 | args.addAll( getCommonArgs() ); |
213 | |
|
214 | 0 | if ( this.genWsdl ) |
215 | |
{ |
216 | 0 | if ( this.protocol != null ) |
217 | |
{ |
218 | 0 | args.add( "-wsdl:" + this.protocol ); |
219 | |
} |
220 | |
else |
221 | |
{ |
222 | 0 | args.add( "-wsdl" ); |
223 | |
} |
224 | |
|
225 | 0 | if ( inlineSchemas ) |
226 | |
{ |
227 | 0 | maybeUnsupportedOption( "-inlineSchemas", null, args ); |
228 | |
} |
229 | |
|
230 | 0 | if ( servicename != null ) |
231 | |
{ |
232 | 0 | args.add( "-servicename" ); |
233 | 0 | args.add( servicename ); |
234 | |
} |
235 | |
|
236 | 0 | if ( portname != null ) |
237 | |
{ |
238 | 0 | args.add( "-portname" ); |
239 | 0 | args.add( portname ); |
240 | |
} |
241 | |
|
242 | 0 | File resourceDir = getResourceDestDir(); |
243 | 0 | if ( !resourceDir.mkdirs() && !resourceDir.exists() ) |
244 | |
{ |
245 | 0 | getLog().warn( "Cannot create directory: " + resourceDir.getAbsolutePath() ); |
246 | |
} |
247 | 0 | args.add( "-r" ); |
248 | 0 | args.add( "'" + resourceDir.getAbsolutePath() + "'" ); |
249 | 0 | if ( !"war".equals( project.getPackaging() ) ) |
250 | |
{ |
251 | 0 | Resource r = new Resource(); |
252 | 0 | r.setDirectory( getRelativePath( project.getBasedir(), getResourceDestDir() ) ); |
253 | 0 | project.addResource( r ); |
254 | |
} |
255 | |
} |
256 | |
|
257 | 0 | if ( xdonotoverwrite ) |
258 | |
{ |
259 | 0 | args.add( "-Xdonotoverwrite" ); |
260 | |
} |
261 | |
|
262 | 0 | if ( metadata != null && isArgSupported( "-x" ) ) |
263 | |
{ |
264 | 0 | maybeUnsupportedOption( "-x", "'" + metadata.getAbsolutePath() + "'", args ); |
265 | |
} |
266 | |
|
267 | 0 | args.add( aSei ); |
268 | |
|
269 | 0 | return args; |
270 | |
} |
271 | |
|
272 | |
private String getRelativePath( File root, File f ) |
273 | |
{ |
274 | 0 | return root.toURI().relativize( f.toURI() ).getPath(); |
275 | |
} |
276 | |
|
277 | |
private Set<String> getSEIs( File directory ) |
278 | |
throws MojoExecutionException |
279 | |
{ |
280 | 0 | Set<String> seis = new HashSet<>(); |
281 | 0 | if ( !directory.exists() || directory.isFile() ) |
282 | |
{ |
283 | 0 | return seis; |
284 | |
} |
285 | 0 | try ( URLClassLoader cl = new URLClassLoader( new URL[] { directory.toURI().toURL() }, getClass().getClassLoader() ) ) |
286 | |
{ |
287 | 0 | for ( String s : FileUtils.getFileAndDirectoryNames( directory, "**/*.class", null, false, true, true, |
288 | |
false ) ) |
289 | |
{ |
290 | |
try |
291 | |
{ |
292 | 0 | String clsName = s.replace( File.separator, "." ); |
293 | 0 | Class<?> c = cl.loadClass( clsName.substring( 0, clsName.length() - 6 ) ); |
294 | 0 | WebService ann = c.getAnnotation( WebService.class ); |
295 | 0 | if ( !c.isInterface() && ann != null ) |
296 | |
{ |
297 | |
|
298 | 0 | seis.add( c.getName() ); |
299 | |
} |
300 | |
} |
301 | 0 | catch ( ClassNotFoundException ex ) |
302 | |
{ |
303 | 0 | throw new MojoExecutionException( ex.getMessage(), ex ); |
304 | 0 | } |
305 | 0 | } |
306 | 0 | } |
307 | 0 | catch ( IOException ex ) |
308 | |
{ |
309 | 0 | throw new MojoExecutionException( ex.getMessage(), ex ); |
310 | 0 | } |
311 | 0 | return seis; |
312 | |
} |
313 | |
} |