1 | |
package org.apache.maven.plugin.javadoc; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.BufferedReader; |
23 | |
import java.io.ByteArrayOutputStream; |
24 | |
import java.io.File; |
25 | |
import java.io.FileInputStream; |
26 | |
import java.io.FileNotFoundException; |
27 | |
import java.io.FileOutputStream; |
28 | |
import java.io.IOException; |
29 | |
import java.io.InputStream; |
30 | |
import java.io.InputStreamReader; |
31 | |
import java.io.OutputStream; |
32 | |
import java.io.OutputStreamWriter; |
33 | |
import java.io.PrintStream; |
34 | |
import java.io.UnsupportedEncodingException; |
35 | |
import java.lang.reflect.Modifier; |
36 | |
import java.net.SocketTimeoutException; |
37 | |
import java.net.URL; |
38 | |
import java.net.URLClassLoader; |
39 | |
import java.util.ArrayList; |
40 | |
import java.util.Arrays; |
41 | |
import java.util.Collection; |
42 | |
import java.util.List; |
43 | |
import java.util.Locale; |
44 | |
import java.util.NoSuchElementException; |
45 | |
import java.util.Properties; |
46 | |
import java.util.Set; |
47 | |
import java.util.StringTokenizer; |
48 | |
import java.util.jar.JarEntry; |
49 | |
import java.util.jar.JarInputStream; |
50 | |
import java.util.regex.Matcher; |
51 | |
import java.util.regex.Pattern; |
52 | |
import java.util.regex.PatternSyntaxException; |
53 | |
|
54 | |
import org.apache.commons.httpclient.Credentials; |
55 | |
import org.apache.commons.httpclient.HttpClient; |
56 | |
import org.apache.commons.httpclient.HttpStatus; |
57 | |
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; |
58 | |
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
59 | |
import org.apache.commons.httpclient.auth.AuthScope; |
60 | |
import org.apache.commons.httpclient.methods.GetMethod; |
61 | |
import org.apache.commons.httpclient.params.HttpClientParams; |
62 | |
import org.apache.commons.httpclient.params.HttpMethodParams; |
63 | |
import org.apache.commons.lang.SystemUtils; |
64 | |
import org.apache.maven.artifact.Artifact; |
65 | |
import org.apache.maven.plugin.logging.Log; |
66 | |
import org.apache.maven.project.MavenProject; |
67 | |
import org.apache.maven.settings.Proxy; |
68 | |
import org.apache.maven.settings.Settings; |
69 | |
import org.apache.maven.shared.invoker.DefaultInvocationRequest; |
70 | |
import org.apache.maven.shared.invoker.DefaultInvoker; |
71 | |
import org.apache.maven.shared.invoker.InvocationOutputHandler; |
72 | |
import org.apache.maven.shared.invoker.InvocationRequest; |
73 | |
import org.apache.maven.shared.invoker.InvocationResult; |
74 | |
import org.apache.maven.shared.invoker.Invoker; |
75 | |
import org.apache.maven.shared.invoker.MavenInvocationException; |
76 | |
import org.apache.maven.shared.invoker.PrintStreamHandler; |
77 | |
import org.apache.maven.wagon.proxy.ProxyInfo; |
78 | |
import org.apache.maven.wagon.proxy.ProxyUtils; |
79 | |
import org.codehaus.plexus.util.FileUtils; |
80 | |
import org.codehaus.plexus.util.IOUtil; |
81 | |
import org.codehaus.plexus.util.Os; |
82 | |
import org.codehaus.plexus.util.StringUtils; |
83 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
84 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
85 | |
import org.codehaus.plexus.util.cli.Commandline; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | 0 | public class JavadocUtil |
95 | |
{ |
96 | |
|
97 | |
public static final int DEFAULT_TIMEOUT = 2000; |
98 | |
|
99 | |
|
100 | |
protected static final String ERROR_INIT_VM = |
101 | |
"Error occurred during initialization of VM, try to reduce the Java heap size for the MAVEN_OPTS " |
102 | |
+ "environnement variable using -Xms:<size> and -Xmx:<size>."; |
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public static List<String> pruneDirs( MavenProject project, List<String> dirs ) |
114 | |
{ |
115 | 99 | List<String> pruned = new ArrayList<String>( dirs.size() ); |
116 | 99 | for ( String dir : dirs ) |
117 | |
{ |
118 | 108 | if ( dir == null ) |
119 | |
{ |
120 | 0 | continue; |
121 | |
} |
122 | |
|
123 | 108 | File directory = new File( dir ); |
124 | 108 | if ( !directory.isAbsolute() ) |
125 | |
{ |
126 | 0 | directory = new File( project.getBasedir(), directory.getPath() ); |
127 | |
} |
128 | |
|
129 | 108 | if ( directory.isDirectory() && !pruned.contains( directory.getAbsolutePath() ) ) |
130 | |
{ |
131 | 98 | pruned.add( directory.getAbsolutePath() ); |
132 | |
} |
133 | 108 | } |
134 | |
|
135 | 99 | return pruned; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
protected static List<String> pruneFiles( List<String> files ) |
146 | |
{ |
147 | 41 | List<String> pruned = new ArrayList<String>( files.size() ); |
148 | 41 | for ( String f : files ) |
149 | |
{ |
150 | 2 | if ( !shouldPruneFile( f, pruned ) ) |
151 | |
{ |
152 | 2 | pruned.add( f ); |
153 | |
} |
154 | |
} |
155 | |
|
156 | 41 | return pruned; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
public static boolean shouldPruneFile( String f, List<String> pruned ) |
164 | |
{ |
165 | 2 | if ( f != null ) |
166 | |
{ |
167 | 2 | File file = new File( f ); |
168 | 2 | if ( file.isFile() && ( isEmpty( pruned ) || !pruned.contains( f ) ) ) |
169 | |
{ |
170 | 2 | return false; |
171 | |
} |
172 | |
} |
173 | |
|
174 | 0 | return true; |
175 | |
} |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
protected static List<String> getExcludedNames( List<String> sourcePaths, String[] subpackagesList, |
187 | |
String[] excludedPackages ) |
188 | |
{ |
189 | 1 | List<String> excludedNames = new ArrayList<String>(); |
190 | 1 | for ( String path : sourcePaths ) |
191 | |
{ |
192 | 2 | for ( int j = 0; j < subpackagesList.length; j++ ) |
193 | |
{ |
194 | 1 | List<String> excludes = getExcludedPackages( path, excludedPackages ); |
195 | 1 | excludedNames.addAll( excludes ); |
196 | |
} |
197 | |
} |
198 | |
|
199 | 1 | return excludedNames; |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
protected static List<Artifact> getCompileArtifacts( Set<Artifact> artifacts ) |
209 | |
{ |
210 | 0 | return getCompileArtifacts( artifacts, false ); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
protected static List<Artifact> getCompileArtifacts( Set<Artifact> artifacts, boolean withTestScope ) |
220 | |
{ |
221 | 0 | List<Artifact> list = new ArrayList<Artifact>( artifacts.size() ); |
222 | |
|
223 | 0 | for ( Artifact a : artifacts ) |
224 | |
{ |
225 | |
|
226 | 0 | if ( a.getArtifactHandler().isAddedToClasspath() ) |
227 | |
{ |
228 | |
|
229 | 0 | if ( withTestScope ) |
230 | |
{ |
231 | 0 | if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) |
232 | |
|| Artifact.SCOPE_PROVIDED.equals( a.getScope() ) |
233 | |
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) |
234 | |
|| Artifact.SCOPE_TEST.equals( a.getScope() ) ) |
235 | |
{ |
236 | 0 | list.add( a ); |
237 | |
} |
238 | |
} |
239 | |
else |
240 | |
{ |
241 | 0 | if ( Artifact.SCOPE_COMPILE.equals( a.getScope() ) || Artifact.SCOPE_PROVIDED.equals( a.getScope() ) |
242 | |
|| Artifact.SCOPE_SYSTEM.equals( a.getScope() ) ) |
243 | |
{ |
244 | 0 | list.add( a ); |
245 | |
} |
246 | |
} |
247 | |
} |
248 | |
} |
249 | |
|
250 | 0 | return list; |
251 | |
} |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
protected static String quotedArgument( String value ) |
263 | |
{ |
264 | 520 | String arg = value; |
265 | |
|
266 | 520 | if ( StringUtils.isNotEmpty( arg ) ) |
267 | |
{ |
268 | 185 | if ( arg.indexOf( "'" ) != -1 ) |
269 | |
{ |
270 | 0 | arg = StringUtils.replace( arg, "'", "\\'" ); |
271 | |
} |
272 | 185 | arg = "'" + arg + "'"; |
273 | |
|
274 | |
|
275 | 185 | arg = StringUtils.replace( arg, "\n", " " ); |
276 | |
} |
277 | |
|
278 | 520 | return arg; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
protected static String quotedPathArgument( String value ) |
289 | |
{ |
290 | 382 | String path = value; |
291 | |
|
292 | 382 | if ( StringUtils.isNotEmpty( path ) ) |
293 | |
{ |
294 | 168 | path = path.replace( '\\', '/' ); |
295 | 168 | if ( path.indexOf( "\'" ) != -1 ) |
296 | |
{ |
297 | 4 | String split[] = path.split( "\'" ); |
298 | 4 | path = ""; |
299 | |
|
300 | 12 | for ( int i = 0; i < split.length; i++ ) |
301 | |
{ |
302 | 8 | if ( i != split.length - 1 ) |
303 | |
{ |
304 | 4 | path = path + split[i] + "\\'"; |
305 | |
} |
306 | |
else |
307 | |
{ |
308 | 4 | path = path + split[i]; |
309 | |
} |
310 | |
} |
311 | |
} |
312 | 168 | path = "'" + path + "'"; |
313 | |
} |
314 | |
|
315 | 382 | return path; |
316 | |
} |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
protected static void copyJavadocResources( File outputDirectory, File javadocDir ) |
328 | |
throws IOException |
329 | |
{ |
330 | 0 | copyJavadocResources( outputDirectory, javadocDir, null ); |
331 | 0 | } |
332 | |
|
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
protected static void copyJavadocResources( File outputDirectory, File javadocDir, String excludedocfilessubdir ) |
344 | |
throws IOException |
345 | |
{ |
346 | 8 | if ( !javadocDir.isDirectory() ) |
347 | |
{ |
348 | 1 | return; |
349 | |
} |
350 | |
|
351 | 7 | List<String> excludes = new ArrayList<String>(); |
352 | 7 | excludes.addAll( Arrays.asList( FileUtils.getDefaultExcludes() ) ); |
353 | |
|
354 | 7 | if ( StringUtils.isNotEmpty( excludedocfilessubdir ) ) |
355 | |
{ |
356 | 2 | StringTokenizer st = new StringTokenizer( excludedocfilessubdir, ":" ); |
357 | |
String current; |
358 | 5 | while ( st.hasMoreTokens() ) |
359 | |
{ |
360 | 3 | current = st.nextToken(); |
361 | 3 | excludes.add( "**/" + current + "/**" ); |
362 | |
} |
363 | |
} |
364 | |
|
365 | 7 | List<String> docFiles = |
366 | |
FileUtils.getDirectoryNames( javadocDir, "resources,**/doc-files", |
367 | |
StringUtils.join( excludes.iterator(), "," ), false, true ); |
368 | 7 | for ( String docFile : docFiles ) |
369 | |
{ |
370 | 10 | File docFileOutput = new File( outputDirectory, docFile ); |
371 | 10 | FileUtils.mkdir( docFileOutput.getAbsolutePath() ); |
372 | 10 | FileUtils.copyDirectoryStructure( new File( javadocDir, docFile ), docFileOutput ); |
373 | 10 | List<String> files = |
374 | |
FileUtils.getFileAndDirectoryNames( docFileOutput, StringUtils.join( excludes.iterator(), "," ), |
375 | |
null, true, true, true, true ); |
376 | 10 | for ( String filename : files ) |
377 | |
{ |
378 | 307 | File file = new File( filename ); |
379 | |
|
380 | 307 | if ( file.isDirectory() ) |
381 | |
{ |
382 | 25 | FileUtils.deleteDirectory( file ); |
383 | |
} |
384 | |
else |
385 | |
{ |
386 | 282 | file.delete(); |
387 | |
} |
388 | 307 | } |
389 | 10 | } |
390 | 7 | } |
391 | |
|
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
protected static List<String> getIncludedFiles( File sourceDirectory, String[] fileList, String[] excludePackages ) |
402 | |
{ |
403 | 43 | List<String> files = new ArrayList<String>(); |
404 | |
|
405 | 119 | for ( int j = 0; j < fileList.length; j++ ) |
406 | |
{ |
407 | 76 | boolean include = true; |
408 | 85 | for ( int k = 0; k < excludePackages.length && include; k++ ) |
409 | |
{ |
410 | |
|
411 | 9 | String[] excludeName = excludePackages[k].split( "[*]" ); |
412 | |
|
413 | 9 | if ( excludeName.length == 0 ) |
414 | |
{ |
415 | 0 | continue; |
416 | |
} |
417 | |
|
418 | 9 | if ( excludeName.length > 1 ) |
419 | |
{ |
420 | 5 | int u = 0; |
421 | 15 | while ( include && u < excludeName.length ) |
422 | |
{ |
423 | 10 | if ( !"".equals( excludeName[u].trim() ) && fileList[j].indexOf( excludeName[u] ) != -1 ) |
424 | |
{ |
425 | 1 | include = false; |
426 | |
} |
427 | 10 | u++; |
428 | |
} |
429 | 5 | } |
430 | |
else |
431 | |
{ |
432 | 4 | if ( fileList[j].startsWith( sourceDirectory.toString() + File.separatorChar + excludeName[0] ) ) |
433 | |
{ |
434 | 2 | if ( excludeName[0].endsWith( String.valueOf( File.separatorChar ) ) ) |
435 | |
{ |
436 | 2 | int i = fileList[j].lastIndexOf( File.separatorChar ); |
437 | 2 | String packageName = fileList[j].substring( 0, i + 1 ); |
438 | 2 | File currentPackage = new File( packageName ); |
439 | 2 | File excludedPackage = new File( sourceDirectory, excludeName[0] ); |
440 | 2 | if ( currentPackage.equals( excludedPackage ) |
441 | |
&& fileList[j].substring( i ).indexOf( ".java" ) != -1 ) |
442 | |
{ |
443 | 1 | include = true; |
444 | |
} |
445 | |
else |
446 | |
{ |
447 | 1 | include = false; |
448 | |
} |
449 | 2 | } |
450 | |
else |
451 | |
{ |
452 | 0 | include = false; |
453 | |
} |
454 | |
} |
455 | |
} |
456 | |
} |
457 | |
|
458 | 76 | if ( include ) |
459 | |
{ |
460 | 74 | files.add( quotedPathArgument( fileList[j] ) ); |
461 | |
} |
462 | |
} |
463 | |
|
464 | 43 | return files; |
465 | |
} |
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
protected static List<String> getExcludedPackages( String sourceDirectory, String[] excludePackagenames ) |
476 | |
{ |
477 | 1 | List<String> files = new ArrayList<String>(); |
478 | 3 | for ( int i = 0; i < excludePackagenames.length; i++ ) |
479 | |
{ |
480 | 2 | String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory, new String[] { "java" } ); |
481 | 14 | for ( int j = 0; j < fileList.length; j++ ) |
482 | |
{ |
483 | 12 | String[] excludeName = excludePackagenames[i].split( "[*]" ); |
484 | 12 | int u = 0; |
485 | 24 | while ( u < excludeName.length ) |
486 | |
{ |
487 | 12 | if ( !"".equals( excludeName[u].trim() ) && fileList[j].indexOf( excludeName[u] ) != -1 |
488 | |
&& sourceDirectory.indexOf( excludeName[u] ) == -1 ) |
489 | |
{ |
490 | 2 | files.add( fileList[j] ); |
491 | |
} |
492 | 12 | u++; |
493 | |
} |
494 | |
} |
495 | |
} |
496 | |
|
497 | 1 | List<String> excluded = new ArrayList<String>(); |
498 | 1 | for ( String file : files ) |
499 | |
{ |
500 | 2 | int idx = file.lastIndexOf( File.separatorChar ); |
501 | 2 | String tmpStr = file.substring( 0, idx ); |
502 | 2 | tmpStr = tmpStr.replace( '\\', '/' ); |
503 | 2 | String[] srcSplit = tmpStr.split( sourceDirectory.replace( '\\', '/' ) + '/' ); |
504 | 2 | String excludedPackage = srcSplit[1].replace( '/', '.' ); |
505 | |
|
506 | 2 | if ( !excluded.contains( excludedPackage ) ) |
507 | |
{ |
508 | 2 | excluded.add( excludedPackage ); |
509 | |
} |
510 | 2 | } |
511 | |
|
512 | 1 | return excluded; |
513 | |
} |
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
protected static void addFilesFromSource( List<String> files, File sourceDirectory, String[] excludePackages ) |
523 | |
{ |
524 | 47 | String[] fileList = FileUtils.getFilesFromExtension( sourceDirectory.getPath(), new String[] { "java" } ); |
525 | 47 | if ( fileList != null && fileList.length != 0 ) |
526 | |
{ |
527 | 43 | List<String> tmpFiles = getIncludedFiles( sourceDirectory, fileList, excludePackages ); |
528 | 43 | files.addAll( tmpFiles ); |
529 | |
} |
530 | 47 | } |
531 | |
|
532 | |
|
533 | |
|
534 | |
|
535 | |
|
536 | |
|
537 | |
|
538 | |
|
539 | |
|
540 | |
|
541 | |
|
542 | |
|
543 | |
|
544 | |
|
545 | |
|
546 | |
protected static float getJavadocVersion( File javadocExe ) |
547 | |
throws IOException, CommandLineException, IllegalArgumentException, PatternSyntaxException |
548 | |
{ |
549 | 42 | if ( ( javadocExe == null ) || ( !javadocExe.exists() ) || ( !javadocExe.isFile() ) ) |
550 | |
{ |
551 | 0 | throw new IOException( "The javadoc executable '" + javadocExe + "' doesn't exist or is not a file. " ); |
552 | |
} |
553 | |
|
554 | 42 | Commandline cmd = new Commandline(); |
555 | 42 | cmd.setExecutable( javadocExe.getAbsolutePath() ); |
556 | 42 | cmd.setWorkingDirectory( javadocExe.getParentFile() ); |
557 | 42 | cmd.createArg().setValue( "-J-version" ); |
558 | |
|
559 | 42 | CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer(); |
560 | 42 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
561 | |
|
562 | 42 | int exitCode = CommandLineUtils.executeCommandLine( cmd, out, err ); |
563 | |
|
564 | 42 | if ( exitCode != 0 ) |
565 | |
{ |
566 | 0 | StringBuffer msg = new StringBuffer( "Exit code: " + exitCode + " - " + err.getOutput() ); |
567 | 0 | msg.append( '\n' ); |
568 | 0 | msg.append( "Command line was:" + CommandLineUtils.toString( cmd.getCommandline() ) ); |
569 | 0 | throw new CommandLineException( msg.toString() ); |
570 | |
} |
571 | |
|
572 | 42 | if ( StringUtils.isNotEmpty( err.getOutput() ) ) |
573 | |
{ |
574 | 42 | return parseJavadocVersion( err.getOutput() ); |
575 | |
} |
576 | 0 | else if ( StringUtils.isNotEmpty( out.getOutput() ) ) |
577 | |
{ |
578 | 0 | return parseJavadocVersion( out.getOutput() ); |
579 | |
} |
580 | |
|
581 | 0 | throw new IllegalArgumentException( "No output found from the command line 'javadoc -J-version'" ); |
582 | |
} |
583 | |
|
584 | |
|
585 | |
|
586 | |
|
587 | |
|
588 | |
|
589 | |
|
590 | |
|
591 | |
|
592 | |
|
593 | |
|
594 | |
|
595 | |
|
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
|
603 | |
|
604 | |
|
605 | |
|
606 | |
|
607 | |
|
608 | |
|
609 | |
|
610 | |
|
611 | |
|
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
|
617 | |
|
618 | |
|
619 | |
|
620 | |
|
621 | |
|
622 | |
|
623 | |
|
624 | |
|
625 | |
protected static float parseJavadocVersion( String output ) |
626 | |
throws IllegalArgumentException, PatternSyntaxException |
627 | |
{ |
628 | 59 | if ( StringUtils.isEmpty( output ) ) |
629 | |
{ |
630 | 1 | throw new IllegalArgumentException( "The output could not be null." ); |
631 | |
} |
632 | |
|
633 | 58 | Pattern pattern = Pattern.compile( "(?s).*?([0-9]+\\.[0-9]+)(\\.([0-9]+))?.*" ); |
634 | |
|
635 | 58 | Matcher matcher = pattern.matcher( output ); |
636 | 58 | if ( !matcher.matches() ) |
637 | |
{ |
638 | 1 | throw new PatternSyntaxException( "Unrecognized version of Javadoc: '" + output + "'", pattern.pattern(), |
639 | |
pattern.toString().length() - 1 ); |
640 | |
} |
641 | |
|
642 | 57 | String version = matcher.group( 3 ); |
643 | 57 | if ( version == null ) |
644 | |
{ |
645 | 1 | version = matcher.group( 1 ); |
646 | |
} |
647 | |
else |
648 | |
{ |
649 | 56 | version = matcher.group( 1 ) + version; |
650 | |
} |
651 | |
|
652 | 57 | return Float.parseFloat( version ); |
653 | |
} |
654 | |
|
655 | |
|
656 | |
|
657 | |
|
658 | |
|
659 | |
|
660 | |
|
661 | |
|
662 | |
|
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
|
668 | |
|
669 | |
|
670 | |
|
671 | |
|
672 | |
|
673 | |
|
674 | |
|
675 | |
|
676 | |
|
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
|
683 | |
|
684 | |
protected static String parseJavadocMemory( String memory ) |
685 | |
throws IllegalArgumentException |
686 | |
{ |
687 | 18 | if ( StringUtils.isEmpty( memory ) ) |
688 | |
{ |
689 | 1 | throw new IllegalArgumentException( "The memory could not be null." ); |
690 | |
} |
691 | |
|
692 | 17 | Pattern p = Pattern.compile( "^\\s*(\\d+)\\s*?\\s*$" ); |
693 | 17 | Matcher m = p.matcher( memory ); |
694 | 17 | if ( m.matches() ) |
695 | |
{ |
696 | 2 | return m.group( 1 ) + "m"; |
697 | |
} |
698 | |
|
699 | 15 | p = Pattern.compile( "^\\s*(\\d+)\\s*k(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
700 | 15 | m = p.matcher( memory ); |
701 | 15 | if ( m.matches() ) |
702 | |
{ |
703 | 2 | return m.group( 1 ) + "k"; |
704 | |
} |
705 | |
|
706 | 13 | p = Pattern.compile( "^\\s*(\\d+)\\s*m(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
707 | 13 | m = p.matcher( memory ); |
708 | 13 | if ( m.matches() ) |
709 | |
{ |
710 | 7 | return m.group( 1 ) + "m"; |
711 | |
} |
712 | |
|
713 | 6 | p = Pattern.compile( "^\\s*(\\d+)\\s*g(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
714 | 6 | m = p.matcher( memory ); |
715 | 6 | if ( m.matches() ) |
716 | |
{ |
717 | 2 | return ( Integer.parseInt( m.group( 1 ) ) * 1024 ) + "m"; |
718 | |
} |
719 | |
|
720 | 4 | p = Pattern.compile( "^\\s*(\\d+)\\s*t(b)?\\s*$", Pattern.CASE_INSENSITIVE ); |
721 | 4 | m = p.matcher( memory ); |
722 | 4 | if ( m.matches() ) |
723 | |
{ |
724 | 2 | return ( Integer.parseInt( m.group( 1 ) ) * 1024 * 1024 ) + "m"; |
725 | |
} |
726 | |
|
727 | 2 | throw new IllegalArgumentException( "Could convert not to a memory size: " + memory ); |
728 | |
} |
729 | |
|
730 | |
|
731 | |
|
732 | |
|
733 | |
|
734 | |
|
735 | |
|
736 | |
protected static boolean validateEncoding( String charsetName ) |
737 | |
{ |
738 | 122 | if ( StringUtils.isEmpty( charsetName ) ) |
739 | |
{ |
740 | 1 | return false; |
741 | |
} |
742 | |
|
743 | 121 | OutputStream ost = new ByteArrayOutputStream(); |
744 | 121 | OutputStreamWriter osw = null; |
745 | |
try |
746 | |
{ |
747 | 121 | osw = new OutputStreamWriter( ost, charsetName ); |
748 | |
} |
749 | 5 | catch ( UnsupportedEncodingException exc ) |
750 | |
{ |
751 | 5 | return false; |
752 | |
} |
753 | |
finally |
754 | |
{ |
755 | 121 | IOUtil.close( osw ); |
756 | 116 | } |
757 | |
|
758 | 116 | return true; |
759 | |
} |
760 | |
|
761 | |
|
762 | |
|
763 | |
|
764 | |
|
765 | |
|
766 | |
|
767 | |
|
768 | |
|
769 | |
protected static String hideProxyPassword( String cmdLine, Settings settings ) |
770 | |
{ |
771 | 39 | if ( cmdLine == null ) |
772 | |
{ |
773 | 0 | throw new IllegalArgumentException( "cmdLine could not be null" ); |
774 | |
} |
775 | |
|
776 | 39 | if ( settings == null ) |
777 | |
{ |
778 | 35 | return cmdLine; |
779 | |
} |
780 | |
|
781 | 4 | Proxy activeProxy = settings.getActiveProxy(); |
782 | 4 | if ( activeProxy != null && StringUtils.isNotEmpty( activeProxy.getHost() ) |
783 | |
&& StringUtils.isNotEmpty( activeProxy.getUsername() ) |
784 | |
&& StringUtils.isNotEmpty( activeProxy.getPassword() ) ) |
785 | |
{ |
786 | 3 | String pass = "-J-Dhttp.proxyPassword=\"" + activeProxy.getPassword() + "\""; |
787 | 3 | String hidepass = |
788 | |
"-J-Dhttp.proxyPassword=\"" + StringUtils.repeat( "*", activeProxy.getPassword().length() ) + "\""; |
789 | |
|
790 | 3 | return StringUtils.replace( cmdLine, pass, hidepass ); |
791 | |
} |
792 | |
|
793 | 1 | return cmdLine; |
794 | |
} |
795 | |
|
796 | |
|
797 | |
|
798 | |
|
799 | |
|
800 | |
|
801 | |
|
802 | |
|
803 | |
|
804 | |
|
805 | |
|
806 | |
|
807 | |
|
808 | |
|
809 | |
|
810 | |
protected static List<String> getTagletClassNames( File jarFile ) |
811 | |
throws IOException, ClassNotFoundException, NoClassDefFoundError |
812 | |
{ |
813 | 2 | List<String> classes = getClassNamesFromJar( jarFile ); |
814 | |
ClassLoader cl; |
815 | |
|
816 | |
|
817 | 2 | File tools = new File( System.getProperty( "java.home" ), "../lib/tools.jar" ); |
818 | 2 | if ( tools.exists() && tools.isFile() ) |
819 | |
{ |
820 | 0 | cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL(), tools.toURI().toURL() }, null ); |
821 | |
} |
822 | |
else |
823 | |
{ |
824 | 2 | cl = new URLClassLoader( new URL[] { jarFile.toURI().toURL() }, null ); |
825 | |
} |
826 | |
|
827 | 2 | List<String> tagletClasses = new ArrayList<String>(); |
828 | |
|
829 | 2 | Class<?> tagletClass = cl.loadClass( "com.sun.tools.doclets.Taglet" ); |
830 | 2 | for ( String s : classes ) |
831 | |
{ |
832 | 24 | Class<?> c = cl.loadClass( s ); |
833 | |
|
834 | 24 | if ( tagletClass.isAssignableFrom( c ) && !Modifier.isAbstract( c.getModifiers() ) ) |
835 | |
{ |
836 | 20 | tagletClasses.add( c.getName() ); |
837 | |
} |
838 | 24 | } |
839 | |
|
840 | 2 | return tagletClasses; |
841 | |
} |
842 | |
|
843 | |
|
844 | |
|
845 | |
|
846 | |
|
847 | |
|
848 | |
|
849 | |
|
850 | |
|
851 | |
protected static void copyResource( URL url, File file ) |
852 | |
throws IOException |
853 | |
{ |
854 | 4 | if ( file == null ) |
855 | |
{ |
856 | 0 | throw new IOException( "The file " + file + " can't be null." ); |
857 | |
} |
858 | 4 | if ( url == null ) |
859 | |
{ |
860 | 0 | throw new IOException( "The url " + url + " could not be null." ); |
861 | |
} |
862 | |
|
863 | 4 | InputStream is = url.openStream(); |
864 | 4 | if ( is == null ) |
865 | |
{ |
866 | 0 | throw new IOException( "The resource " + url + " doesn't exists." ); |
867 | |
} |
868 | |
|
869 | 4 | if ( !file.getParentFile().exists() ) |
870 | |
{ |
871 | 0 | file.getParentFile().mkdirs(); |
872 | |
} |
873 | |
|
874 | 4 | FileOutputStream os = null; |
875 | |
try |
876 | |
{ |
877 | 4 | os = new FileOutputStream( file ); |
878 | |
|
879 | 4 | IOUtil.copy( is, os ); |
880 | |
} |
881 | |
finally |
882 | |
{ |
883 | 4 | IOUtil.close( is ); |
884 | |
|
885 | 4 | IOUtil.close( os ); |
886 | 4 | } |
887 | 4 | } |
888 | |
|
889 | |
|
890 | |
|
891 | |
|
892 | |
|
893 | |
|
894 | |
|
895 | |
|
896 | |
|
897 | |
|
898 | |
|
899 | |
|
900 | |
|
901 | |
|
902 | |
|
903 | |
|
904 | |
|
905 | |
protected static void invokeMaven( Log log, File localRepositoryDir, File projectFile, List<String> goals, |
906 | |
Properties properties, File invokerLog ) |
907 | |
throws MavenInvocationException |
908 | |
{ |
909 | 6 | if ( projectFile == null ) |
910 | |
{ |
911 | 0 | throw new IllegalArgumentException( "projectFile should be not null." ); |
912 | |
} |
913 | 6 | if ( !projectFile.isFile() ) |
914 | |
{ |
915 | 0 | throw new IllegalArgumentException( projectFile.getAbsolutePath() + " is not a file." ); |
916 | |
} |
917 | 6 | if ( goals == null || goals.size() == 0 ) |
918 | |
{ |
919 | 0 | throw new IllegalArgumentException( "goals should be not empty." ); |
920 | |
} |
921 | 6 | if ( localRepositoryDir == null || !localRepositoryDir.isDirectory() ) |
922 | |
{ |
923 | 0 | throw new IllegalArgumentException( "localRepositoryDir '" + localRepositoryDir |
924 | |
+ "' should be a directory." ); |
925 | |
} |
926 | |
|
927 | 6 | String mavenHome = getMavenHome( log ); |
928 | 6 | if ( StringUtils.isEmpty( mavenHome ) ) |
929 | |
{ |
930 | 0 | String msg = |
931 | |
"Could NOT invoke Maven because no Maven Home is defined. You need to have set the M2_HOME " |
932 | |
+ "system env variable or a maven.home Java system properties."; |
933 | 0 | if ( log != null ) |
934 | |
{ |
935 | 0 | log.error( msg ); |
936 | |
} |
937 | |
else |
938 | |
{ |
939 | 0 | System.err.println( msg ); |
940 | |
} |
941 | 0 | return; |
942 | |
} |
943 | |
|
944 | 6 | Invoker invoker = new DefaultInvoker(); |
945 | 6 | invoker.setMavenHome( new File( mavenHome ) ); |
946 | 6 | invoker.setLocalRepositoryDirectory( localRepositoryDir ); |
947 | |
|
948 | 6 | InvocationRequest request = new DefaultInvocationRequest(); |
949 | 6 | request.setBaseDirectory( projectFile.getParentFile() ); |
950 | 6 | request.setPomFile( projectFile ); |
951 | 6 | if ( log != null ) |
952 | |
{ |
953 | 6 | request.setDebug( log.isDebugEnabled() ); |
954 | |
} |
955 | |
else |
956 | |
{ |
957 | 0 | request.setDebug( true ); |
958 | |
} |
959 | 6 | request.setGoals( goals ); |
960 | 6 | if ( properties != null ) |
961 | |
{ |
962 | 3 | request.setProperties( properties ); |
963 | |
} |
964 | 6 | File javaHome = getJavaHome( log ); |
965 | 6 | if ( javaHome != null ) |
966 | |
{ |
967 | 6 | request.setJavaHome( javaHome ); |
968 | |
} |
969 | |
|
970 | 6 | if ( log != null && log.isDebugEnabled() ) |
971 | |
{ |
972 | 0 | log.debug( "Invoking Maven for the goals: " + goals + " with " |
973 | |
+ ( properties == null ? "no properties" : "properties=" + properties ) ); |
974 | |
} |
975 | 6 | InvocationResult result = invoke( log, invoker, request, invokerLog, goals, properties, null ); |
976 | |
|
977 | 6 | if ( result.getExitCode() != 0 ) |
978 | |
{ |
979 | 0 | String invokerLogContent = readFile( invokerLog, "UTF-8" ); |
980 | |
|
981 | |
|
982 | 0 | if ( invokerLogContent != null && ( invokerLogContent.indexOf( "Scanning for projects..." ) == -1 |
983 | |
|| invokerLogContent.indexOf( OutOfMemoryError.class.getName() ) != -1 ) ) |
984 | |
{ |
985 | 0 | if ( log != null ) |
986 | |
{ |
987 | 0 | log.error( "Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS..." ); |
988 | |
|
989 | 0 | if ( log.isDebugEnabled() ) |
990 | |
{ |
991 | 0 | log.debug( "Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS..." ); |
992 | |
} |
993 | |
} |
994 | 0 | result = invoke( log, invoker, request, invokerLog, goals, properties, "" ); |
995 | |
} |
996 | |
} |
997 | |
|
998 | 6 | if ( result.getExitCode() != 0 ) |
999 | |
{ |
1000 | 0 | String invokerLogContent = readFile( invokerLog, "UTF-8" ); |
1001 | |
|
1002 | |
|
1003 | 0 | if ( invokerLogContent != null && ( invokerLogContent.indexOf( "Scanning for projects..." ) == -1 |
1004 | |
|| invokerLogContent.indexOf( OutOfMemoryError.class.getName() ) != -1 ) ) |
1005 | |
{ |
1006 | 0 | throw new MavenInvocationException( ERROR_INIT_VM ); |
1007 | |
} |
1008 | |
|
1009 | 0 | throw new MavenInvocationException( "Error when invoking Maven, consult the invoker log file: " |
1010 | |
+ invokerLog.getAbsolutePath() ); |
1011 | |
} |
1012 | 6 | } |
1013 | |
|
1014 | |
|
1015 | |
|
1016 | |
|
1017 | |
|
1018 | |
|
1019 | |
|
1020 | |
|
1021 | |
|
1022 | |
|
1023 | |
protected static String readFile( final File javaFile, final String encoding ) |
1024 | |
{ |
1025 | |
try |
1026 | |
{ |
1027 | 0 | return FileUtils.fileRead( javaFile, encoding ); |
1028 | |
} |
1029 | 0 | catch ( IOException e ) |
1030 | |
{ |
1031 | 0 | return null; |
1032 | |
} |
1033 | |
} |
1034 | |
|
1035 | |
|
1036 | |
|
1037 | |
|
1038 | |
|
1039 | |
|
1040 | |
|
1041 | |
|
1042 | |
|
1043 | |
|
1044 | |
|
1045 | |
|
1046 | |
|
1047 | |
|
1048 | |
|
1049 | |
|
1050 | |
protected static String[] splitPath( final String path ) |
1051 | |
{ |
1052 | 9 | if ( path == null ) |
1053 | |
{ |
1054 | 0 | return null; |
1055 | |
} |
1056 | |
|
1057 | 9 | List<String> subpaths = new ArrayList<String>(); |
1058 | 9 | PathTokenizer pathTokenizer = new PathTokenizer( path ); |
1059 | 42 | while ( pathTokenizer.hasMoreTokens() ) |
1060 | |
{ |
1061 | 33 | subpaths.add( pathTokenizer.nextToken() ); |
1062 | |
} |
1063 | |
|
1064 | 9 | return subpaths.toArray( new String[subpaths.size()] ); |
1065 | |
} |
1066 | |
|
1067 | |
|
1068 | |
|
1069 | |
|
1070 | |
|
1071 | |
|
1072 | |
|
1073 | |
|
1074 | |
|
1075 | |
|
1076 | |
|
1077 | |
|
1078 | |
|
1079 | |
|
1080 | |
|
1081 | |
|
1082 | |
|
1083 | |
protected static String unifyPathSeparator( final String path ) |
1084 | |
{ |
1085 | 49 | if ( path == null ) |
1086 | |
{ |
1087 | 41 | return null; |
1088 | |
} |
1089 | |
|
1090 | 8 | return StringUtils.join( splitPath( path ), File.pathSeparator ); |
1091 | |
} |
1092 | |
|
1093 | |
|
1094 | |
|
1095 | |
|
1096 | |
|
1097 | |
|
1098 | |
|
1099 | |
|
1100 | |
|
1101 | |
|
1102 | |
private static List<String> getClassNamesFromJar( File jarFile ) |
1103 | |
throws IOException |
1104 | |
{ |
1105 | 2 | if ( jarFile == null || !jarFile.exists() || !jarFile.isFile() ) |
1106 | |
{ |
1107 | 0 | throw new IOException( "The jar '" + jarFile + "' doesn't exist or is not a file." ); |
1108 | |
} |
1109 | |
|
1110 | 2 | List<String> classes = new ArrayList<String>(); |
1111 | 2 | JarInputStream jarStream = null; |
1112 | |
|
1113 | |
try |
1114 | |
{ |
1115 | 2 | jarStream = new JarInputStream( new FileInputStream( jarFile ) ); |
1116 | 2 | JarEntry jarEntry = jarStream.getNextJarEntry(); |
1117 | 48 | while ( jarEntry != null ) |
1118 | |
{ |
1119 | 46 | if ( jarEntry.getName().toLowerCase( Locale.ENGLISH ).endsWith( ".class" ) ) |
1120 | |
{ |
1121 | 24 | String name = jarEntry.getName().substring( 0, jarEntry.getName().indexOf( "." ) ); |
1122 | |
|
1123 | 24 | classes.add( name.replaceAll( "/", "\\." ) ); |
1124 | |
} |
1125 | |
|
1126 | 46 | jarStream.closeEntry(); |
1127 | 46 | jarEntry = jarStream.getNextJarEntry(); |
1128 | |
} |
1129 | |
} |
1130 | |
finally |
1131 | |
{ |
1132 | 2 | IOUtil.close( jarStream ); |
1133 | 2 | } |
1134 | |
|
1135 | 2 | return classes; |
1136 | |
} |
1137 | |
|
1138 | |
|
1139 | |
|
1140 | |
|
1141 | |
|
1142 | |
|
1143 | |
|
1144 | |
|
1145 | |
|
1146 | |
|
1147 | |
|
1148 | |
|
1149 | |
|
1150 | |
private static InvocationResult invoke( Log log, Invoker invoker, InvocationRequest request, File invokerLog, |
1151 | |
List<String> goals, Properties properties, String mavenOpts ) |
1152 | |
throws MavenInvocationException |
1153 | |
{ |
1154 | |
PrintStream ps; |
1155 | 6 | OutputStream os = null; |
1156 | 6 | if ( invokerLog != null ) |
1157 | |
{ |
1158 | 6 | if ( log != null && log.isDebugEnabled() ) |
1159 | |
{ |
1160 | 0 | log.debug( "Using " + invokerLog.getAbsolutePath() + " to log the invoker" ); |
1161 | |
} |
1162 | |
|
1163 | |
try |
1164 | |
{ |
1165 | 6 | if ( !invokerLog.exists() ) |
1166 | |
{ |
1167 | 6 | invokerLog.getParentFile().mkdirs(); |
1168 | |
} |
1169 | 6 | os = new FileOutputStream( invokerLog ); |
1170 | 6 | ps = new PrintStream( os, true, "UTF-8" ); |
1171 | |
} |
1172 | 0 | catch ( FileNotFoundException e ) |
1173 | |
{ |
1174 | 0 | if ( log != null && log.isErrorEnabled() ) |
1175 | |
{ |
1176 | 0 | log.error( "FileNotFoundException: " + e.getMessage() + ". Using System.out to log the invoker." ); |
1177 | |
} |
1178 | 0 | ps = System.out; |
1179 | |
} |
1180 | 0 | catch ( UnsupportedEncodingException e ) |
1181 | |
{ |
1182 | 0 | if ( log != null && log.isErrorEnabled() ) |
1183 | |
{ |
1184 | 0 | log.error( "UnsupportedEncodingException: " + e.getMessage() |
1185 | |
+ ". Using System.out to log the invoker." ); |
1186 | |
} |
1187 | 0 | ps = System.out; |
1188 | 6 | } |
1189 | |
} |
1190 | |
else |
1191 | |
{ |
1192 | 0 | if ( log != null && log.isDebugEnabled() ) |
1193 | |
{ |
1194 | 0 | log.debug( "Using System.out to log the invoker." ); |
1195 | |
} |
1196 | |
|
1197 | 0 | ps = System.out; |
1198 | |
} |
1199 | |
|
1200 | 6 | if ( mavenOpts != null ) |
1201 | |
{ |
1202 | 0 | request.setMavenOpts( mavenOpts ); |
1203 | |
} |
1204 | |
|
1205 | 6 | InvocationOutputHandler outputHandler = new PrintStreamHandler( ps, false ); |
1206 | 6 | request.setOutputHandler( outputHandler ); |
1207 | |
|
1208 | 6 | outputHandler.consumeLine( "Invoking Maven for the goals: " + goals + " with " |
1209 | |
+ ( properties == null ? "no properties" : "properties=" + properties ) ); |
1210 | 6 | outputHandler.consumeLine( "" ); |
1211 | 6 | outputHandler.consumeLine( "M2_HOME=" + getMavenHome( log ) ); |
1212 | 6 | outputHandler.consumeLine( "MAVEN_OPTS=" + getMavenOpts( log ) ); |
1213 | 6 | outputHandler.consumeLine( "JAVA_HOME=" + getJavaHome( log ) ); |
1214 | 6 | outputHandler.consumeLine( "JAVA_OPTS=" + getJavaOpts( log ) ); |
1215 | 6 | outputHandler.consumeLine( "" ); |
1216 | |
|
1217 | |
try |
1218 | |
{ |
1219 | 6 | return invoker.execute( request ); |
1220 | |
} |
1221 | |
finally |
1222 | |
{ |
1223 | 6 | IOUtil.close( os ); |
1224 | 6 | ps = null; |
1225 | |
} |
1226 | |
} |
1227 | |
|
1228 | |
|
1229 | |
|
1230 | |
|
1231 | |
|
1232 | |
|
1233 | |
|
1234 | |
private static String getMavenHome( Log log ) |
1235 | |
{ |
1236 | 12 | String mavenHome = System.getProperty( "maven.home" ); |
1237 | 12 | if ( mavenHome == null ) |
1238 | |
{ |
1239 | |
try |
1240 | |
{ |
1241 | 0 | mavenHome = CommandLineUtils.getSystemEnvVars().getProperty( "M2_HOME" ); |
1242 | |
} |
1243 | 0 | catch ( IOException e ) |
1244 | |
{ |
1245 | 0 | if ( log != null && log.isDebugEnabled() ) |
1246 | |
{ |
1247 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1248 | |
} |
1249 | 0 | } |
1250 | |
} |
1251 | |
|
1252 | 12 | File m2Home = new File( mavenHome ); |
1253 | 12 | if ( !m2Home.exists() ) |
1254 | |
{ |
1255 | 0 | if ( log != null && log.isErrorEnabled() ) |
1256 | |
{ |
1257 | 0 | log |
1258 | |
.error( "Cannot find Maven application directory. Either specify \'maven.home\' system property, or " |
1259 | |
+ "M2_HOME environment variable." ); |
1260 | |
} |
1261 | |
} |
1262 | |
|
1263 | 12 | return mavenHome; |
1264 | |
} |
1265 | |
|
1266 | |
|
1267 | |
|
1268 | |
|
1269 | |
|
1270 | |
|
1271 | |
private static String getMavenOpts( Log log ) |
1272 | |
{ |
1273 | 6 | String mavenOpts = null; |
1274 | |
try |
1275 | |
{ |
1276 | 6 | mavenOpts = CommandLineUtils.getSystemEnvVars().getProperty( "MAVEN_OPTS" ); |
1277 | |
} |
1278 | 0 | catch ( IOException e ) |
1279 | |
{ |
1280 | 0 | if ( log != null && log.isDebugEnabled() ) |
1281 | |
{ |
1282 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1283 | |
} |
1284 | 6 | } |
1285 | |
|
1286 | 6 | return mavenOpts; |
1287 | |
} |
1288 | |
|
1289 | |
|
1290 | |
|
1291 | |
|
1292 | |
|
1293 | |
|
1294 | |
|
1295 | |
|
1296 | |
private static File getJavaHome( Log log ) |
1297 | |
{ |
1298 | |
File javaHome; |
1299 | 12 | if ( SystemUtils.IS_OS_MAC_OSX ) |
1300 | |
{ |
1301 | 12 | javaHome = SystemUtils.getJavaHome(); |
1302 | |
} |
1303 | |
else |
1304 | |
{ |
1305 | 0 | javaHome = new File( SystemUtils.getJavaHome(), ".." ); |
1306 | |
} |
1307 | |
|
1308 | 12 | if ( javaHome == null || !javaHome.exists() ) |
1309 | |
{ |
1310 | |
try |
1311 | |
{ |
1312 | 0 | javaHome = new File( CommandLineUtils.getSystemEnvVars().getProperty( "JAVA_HOME" ) ); |
1313 | |
} |
1314 | 0 | catch ( IOException e ) |
1315 | |
{ |
1316 | 0 | if ( log != null && log.isDebugEnabled() ) |
1317 | |
{ |
1318 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1319 | |
} |
1320 | 0 | } |
1321 | |
} |
1322 | |
|
1323 | 12 | if ( javaHome == null || !javaHome.exists() ) |
1324 | |
{ |
1325 | 0 | if ( log != null && log.isErrorEnabled() ) |
1326 | |
{ |
1327 | 0 | log.error( "Cannot find Java application directory. Either specify \'java.home\' system property, or " |
1328 | |
+ "JAVA_HOME environment variable." ); |
1329 | |
} |
1330 | |
} |
1331 | |
|
1332 | 12 | return javaHome; |
1333 | |
} |
1334 | |
|
1335 | |
|
1336 | |
|
1337 | |
|
1338 | |
|
1339 | |
|
1340 | |
private static String getJavaOpts( Log log ) |
1341 | |
{ |
1342 | 6 | String javaOpts = null; |
1343 | |
try |
1344 | |
{ |
1345 | 6 | javaOpts = CommandLineUtils.getSystemEnvVars().getProperty( "JAVA_OPTS" ); |
1346 | |
} |
1347 | 0 | catch ( IOException e ) |
1348 | |
{ |
1349 | 0 | if ( log != null && log.isDebugEnabled() ) |
1350 | |
{ |
1351 | 0 | log.debug( "IOException: " + e.getMessage() ); |
1352 | |
} |
1353 | 6 | } |
1354 | |
|
1355 | 6 | return javaOpts; |
1356 | |
} |
1357 | |
|
1358 | |
|
1359 | |
|
1360 | |
|
1361 | |
|
1362 | |
|
1363 | |
|
1364 | |
|
1365 | |
|
1366 | |
|
1367 | |
|
1368 | 0 | private static class PathTokenizer |
1369 | |
{ |
1370 | |
|
1371 | |
|
1372 | |
|
1373 | |
private StringTokenizer tokenizer; |
1374 | |
|
1375 | |
|
1376 | |
|
1377 | |
|
1378 | |
|
1379 | 9 | private String lookahead = null; |
1380 | |
|
1381 | |
|
1382 | |
|
1383 | |
|
1384 | |
|
1385 | |
|
1386 | 9 | private boolean onNetWare = Os.isFamily( "netware" ); |
1387 | |
|
1388 | |
|
1389 | |
|
1390 | |
|
1391 | |
|
1392 | |
private boolean dosStyleFilesystem; |
1393 | |
|
1394 | |
|
1395 | |
|
1396 | |
|
1397 | |
|
1398 | |
|
1399 | |
public PathTokenizer( String path ) |
1400 | 9 | { |
1401 | 9 | if ( onNetWare ) |
1402 | |
{ |
1403 | |
|
1404 | |
|
1405 | 0 | tokenizer = new StringTokenizer( path, ":;", true ); |
1406 | |
} |
1407 | |
else |
1408 | |
{ |
1409 | |
|
1410 | |
|
1411 | 9 | tokenizer = new StringTokenizer( path, ":;", false ); |
1412 | |
} |
1413 | 9 | dosStyleFilesystem = File.pathSeparatorChar == ';'; |
1414 | 9 | } |
1415 | |
|
1416 | |
|
1417 | |
|
1418 | |
|
1419 | |
|
1420 | |
|
1421 | |
|
1422 | |
|
1423 | |
|
1424 | |
public boolean hasMoreTokens() |
1425 | |
{ |
1426 | 42 | if ( lookahead != null ) |
1427 | |
{ |
1428 | 0 | return true; |
1429 | |
} |
1430 | |
|
1431 | 42 | return tokenizer.hasMoreTokens(); |
1432 | |
} |
1433 | |
|
1434 | |
|
1435 | |
|
1436 | |
|
1437 | |
|
1438 | |
|
1439 | |
|
1440 | |
|
1441 | |
|
1442 | |
public String nextToken() |
1443 | |
throws NoSuchElementException |
1444 | |
{ |
1445 | 33 | String token = null; |
1446 | 33 | if ( lookahead != null ) |
1447 | |
{ |
1448 | 0 | token = lookahead; |
1449 | 0 | lookahead = null; |
1450 | |
} |
1451 | |
else |
1452 | |
{ |
1453 | 33 | token = tokenizer.nextToken().trim(); |
1454 | |
} |
1455 | |
|
1456 | 33 | if ( !onNetWare ) |
1457 | |
{ |
1458 | 33 | if ( token.length() == 1 && Character.isLetter( token.charAt( 0 ) ) && dosStyleFilesystem |
1459 | |
&& tokenizer.hasMoreTokens() ) |
1460 | |
{ |
1461 | |
|
1462 | |
|
1463 | 0 | String nextToken = tokenizer.nextToken().trim(); |
1464 | 0 | if ( nextToken.startsWith( "\\" ) || nextToken.startsWith( "/" ) ) |
1465 | |
{ |
1466 | |
|
1467 | |
|
1468 | |
|
1469 | 0 | token += ":" + nextToken; |
1470 | |
} |
1471 | |
else |
1472 | |
{ |
1473 | |
|
1474 | 0 | lookahead = nextToken; |
1475 | |
} |
1476 | 0 | } |
1477 | |
} |
1478 | |
else |
1479 | |
{ |
1480 | |
|
1481 | |
|
1482 | 0 | if ( token.equals( File.pathSeparator ) || token.equals( ":" ) ) |
1483 | |
{ |
1484 | |
|
1485 | 0 | token = tokenizer.nextToken().trim(); |
1486 | |
} |
1487 | |
|
1488 | 0 | if ( tokenizer.hasMoreTokens() ) |
1489 | |
{ |
1490 | |
|
1491 | 0 | String nextToken = tokenizer.nextToken().trim(); |
1492 | |
|
1493 | |
|
1494 | 0 | if ( !nextToken.equals( File.pathSeparator ) ) |
1495 | |
{ |
1496 | 0 | if ( nextToken.equals( ":" ) ) |
1497 | |
{ |
1498 | 0 | if ( !token.startsWith( "/" ) && !token.startsWith( "\\" ) && !token.startsWith( "." ) |
1499 | |
&& !token.startsWith( ".." ) ) |
1500 | |
{ |
1501 | |
|
1502 | 0 | String oneMore = tokenizer.nextToken().trim(); |
1503 | 0 | if ( !oneMore.equals( File.pathSeparator ) ) |
1504 | |
{ |
1505 | 0 | token += ":" + oneMore; |
1506 | |
} |
1507 | |
else |
1508 | |
{ |
1509 | 0 | token += ":"; |
1510 | 0 | lookahead = oneMore; |
1511 | |
} |
1512 | 0 | } |
1513 | |
|
1514 | |
|
1515 | |
} |
1516 | |
else |
1517 | |
{ |
1518 | |
|
1519 | 0 | lookahead = nextToken; |
1520 | |
} |
1521 | |
} |
1522 | |
} |
1523 | |
} |
1524 | 33 | return token; |
1525 | |
} |
1526 | |
} |
1527 | |
|
1528 | |
static List<String> toList( String src ) |
1529 | |
{ |
1530 | 43 | return toList( src, null, null ); |
1531 | |
} |
1532 | |
|
1533 | |
static List<String> toList( String src, String elementPrefix, String elementSuffix ) |
1534 | |
{ |
1535 | 43 | if ( StringUtils.isEmpty( src ) ) |
1536 | |
{ |
1537 | 41 | return null; |
1538 | |
} |
1539 | |
|
1540 | 2 | List<String> result = new ArrayList<String>(); |
1541 | |
|
1542 | 2 | StringTokenizer st = new StringTokenizer( src, "[,:;]" ); |
1543 | 2 | StringBuilder sb = new StringBuilder( 256 ); |
1544 | 6 | while ( st.hasMoreTokens() ) |
1545 | |
{ |
1546 | 4 | sb.setLength( 0 ); |
1547 | 4 | if ( StringUtils.isNotEmpty( elementPrefix ) ) |
1548 | |
{ |
1549 | 0 | sb.append( elementPrefix ); |
1550 | |
} |
1551 | |
|
1552 | 4 | sb.append( st.nextToken() ); |
1553 | |
|
1554 | 4 | if ( StringUtils.isNotEmpty( elementSuffix ) ) |
1555 | |
{ |
1556 | 0 | sb.append( elementSuffix ); |
1557 | |
} |
1558 | |
|
1559 | 4 | result.add( sb.toString() ); |
1560 | |
} |
1561 | |
|
1562 | 2 | return result; |
1563 | |
} |
1564 | |
|
1565 | |
static <T> List<T> toList( T[] multiple ) |
1566 | |
{ |
1567 | 258 | return toList( null, multiple ); |
1568 | |
} |
1569 | |
|
1570 | |
static <T> List<T> toList( T single, T[] multiple ) |
1571 | |
{ |
1572 | 344 | if ( single == null && ( multiple == null || multiple.length < 1 ) ) |
1573 | |
{ |
1574 | 336 | return null; |
1575 | |
} |
1576 | |
|
1577 | 8 | List<T> result = new ArrayList<T>(); |
1578 | 8 | if ( single != null ) |
1579 | |
{ |
1580 | 2 | result.add( single ); |
1581 | |
} |
1582 | |
|
1583 | 8 | if ( multiple != null && multiple.length > 0 ) |
1584 | |
{ |
1585 | 6 | result.addAll( Arrays.asList( multiple ) ); |
1586 | |
} |
1587 | |
|
1588 | 8 | return result; |
1589 | |
} |
1590 | |
|
1591 | |
|
1592 | |
public static String toRelative( File basedir, String absolutePath ) |
1593 | |
{ |
1594 | |
String relative; |
1595 | |
|
1596 | 16 | absolutePath = absolutePath.replace( '\\', '/' ); |
1597 | 16 | String basedirPath = basedir.getAbsolutePath().replace( '\\', '/' ); |
1598 | |
|
1599 | 16 | if ( absolutePath.startsWith( basedirPath ) ) |
1600 | |
{ |
1601 | 16 | relative = absolutePath.substring( basedirPath.length() ); |
1602 | 16 | if ( relative.startsWith( "/" ) ) |
1603 | |
{ |
1604 | 16 | relative = relative.substring( 1 ); |
1605 | |
} |
1606 | 16 | if ( relative.length() <= 0 ) |
1607 | |
{ |
1608 | 0 | relative = "."; |
1609 | |
} |
1610 | |
} |
1611 | |
else |
1612 | |
{ |
1613 | 0 | relative = absolutePath; |
1614 | |
} |
1615 | |
|
1616 | 16 | return relative; |
1617 | |
} |
1618 | |
|
1619 | |
|
1620 | |
|
1621 | |
|
1622 | |
public static boolean isNotEmpty( final Collection<?> collection ) |
1623 | |
{ |
1624 | 76 | return collection != null && !collection.isEmpty(); |
1625 | |
} |
1626 | |
|
1627 | |
|
1628 | |
|
1629 | |
|
1630 | |
public static boolean isEmpty( final Collection<?> collection ) |
1631 | |
{ |
1632 | 146 | return collection == null || collection.isEmpty(); |
1633 | |
} |
1634 | |
|
1635 | |
|
1636 | |
|
1637 | |
|
1638 | |
|
1639 | |
|
1640 | |
|
1641 | |
|
1642 | |
|
1643 | |
|
1644 | |
|
1645 | |
|
1646 | |
|
1647 | |
|
1648 | |
|
1649 | |
|
1650 | |
|
1651 | |
|
1652 | |
protected static boolean isValidPackageList( URL url, Settings settings, boolean validateContent ) |
1653 | |
throws IOException |
1654 | |
{ |
1655 | 14 | if ( url == null ) |
1656 | |
{ |
1657 | 1 | throw new IllegalArgumentException( "The url is null" ); |
1658 | |
} |
1659 | |
|
1660 | 13 | BufferedReader reader = null; |
1661 | 13 | GetMethod httpMethod = null; |
1662 | |
|
1663 | |
try |
1664 | |
{ |
1665 | 13 | if ( "file".equals( url.getProtocol() ) ) |
1666 | |
{ |
1667 | |
|
1668 | 4 | reader = new BufferedReader( new InputStreamReader( url.openStream() ) ); |
1669 | |
} |
1670 | |
else |
1671 | |
{ |
1672 | |
|
1673 | 9 | HttpClient httpClient = createHttpClient( settings, url ); |
1674 | |
|
1675 | 9 | httpMethod = new GetMethod( url.toString() ); |
1676 | |
int status; |
1677 | |
try |
1678 | |
{ |
1679 | 9 | status = httpClient.executeMethod( httpMethod ); |
1680 | |
} |
1681 | 1 | catch ( SocketTimeoutException e ) |
1682 | |
{ |
1683 | |
|
1684 | 1 | status = httpClient.executeMethod( httpMethod ); |
1685 | 8 | } |
1686 | |
|
1687 | 8 | if ( status != HttpStatus.SC_OK ) |
1688 | |
{ |
1689 | 4 | throw new FileNotFoundException( |
1690 | |
"Unexpected HTTP status code " + status + " getting resource " + url.toExternalForm() + "." ); |
1691 | |
|
1692 | |
} |
1693 | |
|
1694 | |
|
1695 | 4 | reader = new BufferedReader( new InputStreamReader( httpMethod.getResponseBodyAsStream() ) ); |
1696 | |
} |
1697 | |
|
1698 | 8 | if ( validateContent ) |
1699 | |
{ |
1700 | |
String line; |
1701 | 21 | while ( ( line = reader.readLine() ) != null ) |
1702 | |
{ |
1703 | 17 | if ( !isValidPackageName( line ) ) |
1704 | |
{ |
1705 | 1 | return false; |
1706 | |
} |
1707 | |
} |
1708 | |
} |
1709 | |
|
1710 | 7 | return true; |
1711 | |
} |
1712 | |
finally |
1713 | |
{ |
1714 | 13 | IOUtil.close( reader ); |
1715 | |
|
1716 | 13 | if ( httpMethod != null ) |
1717 | |
{ |
1718 | 9 | httpMethod.releaseConnection(); |
1719 | |
} |
1720 | |
} |
1721 | |
} |
1722 | |
|
1723 | |
private static boolean isValidPackageName( String str ) |
1724 | |
{ |
1725 | 17 | if ( StringUtils.isEmpty( str ) ) |
1726 | |
{ |
1727 | 0 | return false; |
1728 | |
} |
1729 | |
|
1730 | |
int idx; |
1731 | 101 | while ( ( idx = str.indexOf( '.' ) ) != -1 ) |
1732 | |
{ |
1733 | 85 | if ( !isValidClassName( str.substring( 0, idx ) ) ) |
1734 | |
{ |
1735 | 1 | return false; |
1736 | |
} |
1737 | |
|
1738 | 84 | str = str.substring( idx + 1 ); |
1739 | |
} |
1740 | |
|
1741 | 16 | return isValidClassName( str ); |
1742 | |
} |
1743 | |
|
1744 | |
private static boolean isValidClassName( String str ) |
1745 | |
{ |
1746 | 101 | if ( StringUtils.isEmpty( str ) || !Character.isJavaIdentifierStart( str.charAt( 0 ) ) ) |
1747 | |
{ |
1748 | 1 | return false; |
1749 | |
} |
1750 | |
|
1751 | 544 | for ( int i = str.length() - 1; i > 0; i-- ) |
1752 | |
{ |
1753 | 444 | if ( !Character.isJavaIdentifierPart( str.charAt( i ) ) ) |
1754 | |
{ |
1755 | 0 | return false; |
1756 | |
} |
1757 | |
} |
1758 | |
|
1759 | 100 | return true; |
1760 | |
} |
1761 | |
|
1762 | |
|
1763 | |
|
1764 | |
|
1765 | |
|
1766 | |
|
1767 | |
|
1768 | |
|
1769 | |
|
1770 | |
|
1771 | |
|
1772 | |
|
1773 | |
private static HttpClient createHttpClient( Settings settings, URL url ) |
1774 | |
{ |
1775 | 9 | HttpClient httpClient = new HttpClient( new MultiThreadedHttpConnectionManager() ); |
1776 | 9 | httpClient.getHttpConnectionManager().getParams().setConnectionTimeout( DEFAULT_TIMEOUT ); |
1777 | 9 | httpClient.getHttpConnectionManager().getParams().setSoTimeout( DEFAULT_TIMEOUT ); |
1778 | 9 | httpClient.getParams().setBooleanParameter( HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true ); |
1779 | |
|
1780 | |
|
1781 | 9 | httpClient.getParams().setParameter( HttpMethodParams.USER_AGENT, |
1782 | |
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" ); |
1783 | |
|
1784 | 9 | if ( settings != null && settings.getActiveProxy() != null ) |
1785 | |
{ |
1786 | 5 | Proxy activeProxy = settings.getActiveProxy(); |
1787 | |
|
1788 | 5 | ProxyInfo proxyInfo = new ProxyInfo(); |
1789 | 5 | proxyInfo.setNonProxyHosts( activeProxy.getNonProxyHosts() ); |
1790 | |
|
1791 | 5 | if ( StringUtils.isNotEmpty( activeProxy.getHost() ) |
1792 | |
&& ( url == null || !ProxyUtils.validateNonProxyHosts( proxyInfo, url.getHost() ) ) ) |
1793 | |
{ |
1794 | 4 | httpClient.getHostConfiguration().setProxy( activeProxy.getHost(), activeProxy.getPort() ); |
1795 | |
|
1796 | 4 | if ( StringUtils.isNotEmpty( activeProxy.getUsername() ) && activeProxy.getPassword() != null ) |
1797 | |
{ |
1798 | 3 | Credentials credentials = |
1799 | |
new UsernamePasswordCredentials( activeProxy.getUsername(), activeProxy.getPassword() ); |
1800 | |
|
1801 | 3 | httpClient.getState().setProxyCredentials( AuthScope.ANY, credentials ); |
1802 | |
} |
1803 | |
} |
1804 | |
} |
1805 | |
|
1806 | 9 | return httpClient; |
1807 | |
} |
1808 | |
|
1809 | |
static boolean equalsIgnoreCase( String value, String... strings ) |
1810 | |
{ |
1811 | 6 | for ( String s : strings ) |
1812 | |
{ |
1813 | 6 | if ( s.equalsIgnoreCase( value ) ) |
1814 | |
{ |
1815 | 3 | return true; |
1816 | |
} |
1817 | |
} |
1818 | 0 | return false; |
1819 | |
} |
1820 | |
|
1821 | |
static boolean equals( String value, String... strings ) |
1822 | |
{ |
1823 | 2 | for ( String s : strings ) |
1824 | |
{ |
1825 | 2 | if ( s.equals( value ) ) |
1826 | |
{ |
1827 | 2 | return true; |
1828 | |
} |
1829 | |
} |
1830 | 0 | return false; |
1831 | |
} |
1832 | |
} |