|
@@ -5,13 +5,10 @@ |
5 |
5 |
|
import java.nio.channels.*; |
6 |
6 |
|
import java.util.*; |
7 |
7 |
|
import java.util.regex.*; |
8 |
|
- |
|
9 |
8 |
|
import javax.tools.*; |
10 |
9 |
|
|
11 |
10 |
|
import com.esotericsoftware.utils.*; |
12 |
11 |
|
|
13 |
|
- |
import static com.esotericsoftware.minlog.Log.*; |
14 |
|
- |
|
15 |
12 |
|
public class Utils extends Util |
16 |
13 |
|
{ |
17 |
14 |
|
/** |
|
@@ -38,16 +35,6 @@ |
38 |
35 |
|
static public final String JAVA_HOME = System.getProperty( "java.home" ); |
39 |
36 |
|
|
40 |
37 |
|
/** |
41 |
|
- |
* True if running on a Mac OS. |
42 |
|
- |
*/ |
43 |
|
- |
static public final boolean isMac = System.getProperty( "os.name" ).toLowerCase().contains( "mac os x" ); |
44 |
|
- |
|
45 |
|
- |
/** |
46 |
|
- |
* True if running on a Windows OS. |
47 |
|
- |
*/ |
48 |
|
- |
static public final boolean isWindows = System.getProperty( "os.name" ).toLowerCase().contains( "windows" ); |
49 |
|
- |
|
50 |
|
- |
/** |
51 |
38 |
|
* Returns the full path for the specified file name in the current working directory, the {@link #SCAR_HOME}, and the bin |
52 |
39 |
|
* directory of {@link #JAVA_HOME}. |
53 |
40 |
|
*/ |
|
@@ -59,10 +46,7 @@ |
59 |
46 |
|
} |
60 |
47 |
|
|
61 |
48 |
|
String foundFile = lowLevelResolve( fileName ); |
62 |
|
- |
if ( TRACE ) |
63 |
|
- |
{ |
64 |
|
- |
trace( "scar", "Path \"" + fileName + "\" resolved to: " + foundFile ); |
65 |
|
- |
} |
|
49 |
+ |
LOGGER.trace.log( "Path \"", fileName, "\" resolved to: ", foundFile ); |
66 |
50 |
|
return foundFile; |
67 |
51 |
|
} |
68 |
52 |
|
|
|
@@ -89,10 +73,7 @@ |
89 |
73 |
|
*/ |
90 |
74 |
|
static public String canonical( String path ) |
91 |
75 |
|
{ |
92 |
|
- |
if ( path == null ) |
93 |
|
- |
{ |
94 |
|
- |
throw new IllegalArgumentException( "path cannot be null." ); |
95 |
|
- |
} |
|
76 |
+ |
path = assertNotEmpty( "path", path ); |
96 |
77 |
|
|
97 |
78 |
|
File file = new File( path ); |
98 |
79 |
|
try |
|
@@ -128,49 +109,13 @@ |
128 |
109 |
|
} |
129 |
110 |
|
|
130 |
111 |
|
/** |
131 |
|
- |
* Deletes a file or directory and all files and subdirecties under it. |
132 |
|
- |
*/ |
133 |
|
- |
static public boolean delete( String fileName ) |
134 |
|
- |
{ |
135 |
|
- |
if ( fileName == null ) |
136 |
|
- |
{ |
137 |
|
- |
throw new IllegalArgumentException( "fileName cannot be null." ); |
138 |
|
- |
} |
139 |
|
- |
|
140 |
|
- |
File file = new File( fileName ); |
141 |
|
- |
return !file.exists() || delete( file ); |
142 |
|
- |
} |
143 |
|
- |
|
144 |
|
- |
static public boolean delete( File file ) |
145 |
|
- |
{ |
146 |
|
- |
if ( file.isDirectory() ) |
147 |
|
- |
{ |
148 |
|
- |
File[] files = file.listFiles(); |
149 |
|
- |
for ( File aFile : files ) |
150 |
|
- |
{ |
151 |
|
- |
delete( aFile ); |
152 |
|
- |
} |
153 |
|
- |
} |
154 |
|
- |
if ( TRACE ) |
155 |
|
- |
{ |
156 |
|
- |
trace( "scar", "Deleting file: " + file ); |
157 |
|
- |
} |
158 |
|
- |
return file.delete(); |
159 |
|
- |
} |
160 |
|
- |
|
161 |
|
- |
/** |
162 |
112 |
|
* Creates the directories in the specified path. |
163 |
113 |
|
*/ |
164 |
114 |
|
static public String mkdir( String path ) |
165 |
115 |
|
{ |
166 |
|
- |
if ( path == null ) |
|
116 |
+ |
if ( new File( path = assertNotEmpty( "path", path ) ).mkdirs() ) |
167 |
117 |
|
{ |
168 |
|
- |
throw new IllegalArgumentException( "path cannot be null." ); |
169 |
|
- |
} |
170 |
|
- |
|
171 |
|
- |
if ( new File( path ).mkdirs() && TRACE ) |
172 |
|
- |
{ |
173 |
|
- |
trace( "scar", "Created directory: " + path ); |
|
118 |
+ |
LOGGER.trace.log( "Created directory: ", path ); |
174 |
119 |
|
} |
175 |
120 |
|
return path; |
176 |
121 |
|
} |
|
@@ -180,12 +125,7 @@ |
180 |
125 |
|
*/ |
181 |
126 |
|
static public boolean fileExists( String path ) |
182 |
127 |
|
{ |
183 |
|
- |
if ( path == null ) |
184 |
|
- |
{ |
185 |
|
- |
throw new IllegalArgumentException( "path cannot be null." ); |
186 |
|
- |
} |
187 |
|
- |
|
188 |
|
- |
return new File( path ).exists(); |
|
128 |
+ |
return new File( assertNotEmpty( "path", path ) ).exists(); |
189 |
129 |
|
} |
190 |
130 |
|
|
191 |
131 |
|
/** |
|
@@ -194,15 +134,9 @@ |
194 |
134 |
|
static public void copyStream( InputStream input, OutputStream output ) |
195 |
135 |
|
throws IOException |
196 |
136 |
|
{ |
197 |
|
- |
if ( input == null ) |
198 |
|
- |
{ |
199 |
|
- |
throw new IllegalArgumentException( "input cannot be null." ); |
200 |
|
- |
} |
201 |
|
- |
if ( output == null ) |
202 |
|
- |
{ |
203 |
|
- |
throw new IllegalArgumentException( "output cannot be null." ); |
204 |
|
- |
} |
205 |
|
- |
|
|
137 |
+ |
assertNotNull( "input", input ); |
|
138 |
+ |
assertNotNull( "output", output ); |
|
139 |
+ |
Closeable zCloseable = output; |
206 |
140 |
|
try |
207 |
141 |
|
{ |
208 |
142 |
|
byte[] buffer = new byte[4096]; |
|
@@ -215,23 +149,13 @@ |
215 |
149 |
|
} |
216 |
150 |
|
output.write( buffer, 0, length ); |
217 |
151 |
|
} |
|
152 |
+ |
zCloseable = null; |
|
153 |
+ |
output.close(); |
218 |
154 |
|
} |
219 |
155 |
|
finally |
220 |
156 |
|
{ |
221 |
|
- |
try |
222 |
|
- |
{ |
223 |
|
- |
output.close(); |
224 |
|
- |
} |
225 |
|
- |
catch ( Exception ignored ) |
226 |
|
- |
{ |
227 |
|
- |
} |
228 |
|
- |
try |
229 |
|
- |
{ |
230 |
|
- |
input.close(); |
231 |
|
- |
} |
232 |
|
- |
catch ( Exception ignored ) |
233 |
|
- |
{ |
234 |
|
- |
} |
|
157 |
+ |
dispose( zCloseable ); |
|
158 |
+ |
dispose( input ); |
235 |
159 |
|
} |
236 |
160 |
|
} |
237 |
161 |
|
|
|
@@ -241,50 +165,25 @@ |
241 |
165 |
|
static public String copyFile( String in, String out ) |
242 |
166 |
|
throws IOException |
243 |
167 |
|
{ |
244 |
|
- |
if ( in == null ) |
245 |
|
- |
{ |
246 |
|
- |
throw new IllegalArgumentException( "in cannot be null." ); |
247 |
|
- |
} |
248 |
|
- |
if ( out == null ) |
249 |
|
- |
{ |
250 |
|
- |
throw new IllegalArgumentException( "out cannot be null." ); |
251 |
|
- |
} |
252 |
|
- |
|
253 |
|
- |
if ( TRACE ) |
254 |
|
- |
{ |
255 |
|
- |
trace( "scar", "Copying file: " + in + " -> " + out ); |
256 |
|
- |
} |
|
168 |
+ |
assertNotNull( "in", in ); |
|
169 |
+ |
assertNotNull( "out", out ); |
|
170 |
+ |
LOGGER.trace.log( "Copying file: ", in, " -> ", out ); |
257 |
171 |
|
|
258 |
172 |
|
FileChannel sourceChannel = null; |
259 |
173 |
|
FileChannel destinationChannel = null; |
|
174 |
+ |
Closeable zCloseable = null; |
260 |
175 |
|
try |
261 |
176 |
|
{ |
262 |
177 |
|
sourceChannel = new FileInputStream( in ).getChannel(); |
263 |
|
- |
destinationChannel = new FileOutputStream( out ).getChannel(); |
|
178 |
+ |
zCloseable = (destinationChannel = new FileOutputStream( out ).getChannel()); |
264 |
179 |
|
sourceChannel.transferTo( 0, sourceChannel.size(), destinationChannel ); |
|
180 |
+ |
zCloseable = null; |
|
181 |
+ |
destinationChannel.close(); |
265 |
182 |
|
} |
266 |
183 |
|
finally |
267 |
184 |
|
{ |
268 |
|
- |
try |
269 |
|
- |
{ |
270 |
|
- |
if ( sourceChannel != null ) |
271 |
|
- |
{ |
272 |
|
- |
sourceChannel.close(); |
273 |
|
- |
} |
274 |
|
- |
} |
275 |
|
- |
catch ( Exception ignored ) |
276 |
|
- |
{ |
277 |
|
- |
} |
278 |
|
- |
try |
279 |
|
- |
{ |
280 |
|
- |
if ( destinationChannel != null ) |
281 |
|
- |
{ |
282 |
|
- |
destinationChannel.close(); |
283 |
|
- |
} |
284 |
|
- |
} |
285 |
|
- |
catch ( Exception ignored ) |
286 |
|
- |
{ |
287 |
|
- |
} |
|
185 |
+ |
dispose( zCloseable ); |
|
186 |
+ |
dispose( sourceChannel ); |
288 |
187 |
|
} |
289 |
188 |
|
return out; |
290 |
189 |
|
} |
|
@@ -295,15 +194,8 @@ |
295 |
194 |
|
static public String moveFile( String in, String out ) |
296 |
195 |
|
throws IOException |
297 |
196 |
|
{ |
298 |
|
- |
if ( in == null ) |
299 |
|
- |
{ |
300 |
|
- |
throw new IllegalArgumentException( "in cannot be null." ); |
301 |
|
- |
} |
302 |
|
- |
if ( out == null ) |
303 |
|
- |
{ |
304 |
|
- |
throw new IllegalArgumentException( "out cannot be null." ); |
305 |
|
- |
} |
306 |
|
- |
|
|
197 |
+ |
assertNotNull( "in", in ); |
|
198 |
+ |
assertNotNull( "out", out ); |
307 |
199 |
|
copyFile( in, out ); |
308 |
200 |
|
delete( in ); |
309 |
201 |
|
return out; |
|
@@ -332,13 +224,7 @@ |
332 |
224 |
|
} |
333 |
225 |
|
finally |
334 |
226 |
|
{ |
335 |
|
- |
try |
336 |
|
- |
{ |
337 |
|
- |
reader.close(); |
338 |
|
- |
} |
339 |
|
- |
catch ( Exception ignored ) |
340 |
|
- |
{ |
341 |
|
- |
} |
|
227 |
+ |
dispose( reader ); |
342 |
228 |
|
} |
343 |
229 |
|
return stringBuffer.toString(); |
344 |
230 |
|
} |
|
@@ -364,17 +250,9 @@ |
364 |
250 |
|
*/ |
365 |
251 |
|
static public String fileExtension( String file ) |
366 |
252 |
|
{ |
367 |
|
- |
if ( file == null ) |
368 |
|
- |
{ |
369 |
|
- |
throw new IllegalArgumentException( "fileName cannot be null." ); |
370 |
|
- |
} |
371 |
|
- |
|
|
253 |
+ |
file = assertNotEmpty( "file", file ); |
372 |
254 |
|
int commaIndex = file.indexOf( '.' ); |
373 |
|
- |
if ( commaIndex == -1 ) |
374 |
|
- |
{ |
375 |
|
- |
return ""; |
376 |
|
- |
} |
377 |
|
- |
return file.substring( commaIndex + 1 ); |
|
255 |
+ |
return (commaIndex == -1) ? "" : file.substring( commaIndex + 1 ); |
378 |
256 |
|
} |
379 |
257 |
|
|
380 |
258 |
|
/** |
|
@@ -382,11 +260,7 @@ |
382 |
260 |
|
*/ |
383 |
261 |
|
static public String fileWithoutExtension( String file ) |
384 |
262 |
|
{ |
385 |
|
- |
if ( file == null ) |
386 |
|
- |
{ |
387 |
|
- |
throw new IllegalArgumentException( "fileName cannot be null." ); |
388 |
|
- |
} |
389 |
|
- |
|
|
263 |
+ |
file = assertNotEmpty( "file", file ); |
390 |
264 |
|
int commaIndex = file.indexOf( '.' ); |
391 |
265 |
|
if ( commaIndex == -1 ) |
392 |
266 |
|
{ |
|
@@ -411,16 +285,9 @@ |
411 |
285 |
|
*/ |
412 |
286 |
|
static public String substring( String text, int start, int end ) |
413 |
287 |
|
{ |
414 |
|
- |
if ( text == null ) |
415 |
|
- |
{ |
416 |
|
- |
throw new IllegalArgumentException( "text cannot be null." ); |
417 |
|
- |
} |
418 |
|
- |
|
419 |
|
- |
if ( end >= 0 ) |
420 |
|
- |
{ |
421 |
|
- |
return text.substring( start, end ); |
422 |
|
- |
} |
423 |
|
- |
return text.substring( start, text.length() + end ); |
|
288 |
+ |
assertNotNull( "text", text ); |
|
289 |
+ |
assertNotNegative( "start", start ); |
|
290 |
+ |
return text.substring( start, (end >= 0) ? end : text.length() + end ); |
424 |
291 |
|
} |
425 |
292 |
|
|
426 |
293 |
|
/** |
|
@@ -457,16 +324,9 @@ |
457 |
324 |
|
static public void shell( String... command ) |
458 |
325 |
|
throws IOException |
459 |
326 |
|
{ |
460 |
|
- |
if ( command == null ) |
461 |
|
- |
{ |
462 |
|
- |
throw new IllegalArgumentException( "command cannot be null." ); |
463 |
|
- |
} |
464 |
|
- |
if ( command.length == 0 ) |
465 |
|
- |
{ |
466 |
|
- |
throw new IllegalArgumentException( "command cannot be empty." ); |
467 |
|
- |
} |
|
327 |
+ |
assertNotEmpty( "command", command ); |
468 |
328 |
|
|
469 |
|
- |
String originalCommand = command[0]; |
|
329 |
+ |
String originalCommand = (command[0] = assertNotEmpty( "shell Command", command[0] )); |
470 |
330 |
|
command[0] = resolvePath( command[0] ); |
471 |
331 |
|
if ( !fileExists( command[0] ) && isWindows ) |
472 |
332 |
|
{ |
|
@@ -477,7 +337,7 @@ |
477 |
337 |
|
} |
478 |
338 |
|
} |
479 |
339 |
|
|
480 |
|
- |
if ( TRACE ) |
|
340 |
+ |
if ( LOGGER.trace.isEnabled() ) |
481 |
341 |
|
{ |
482 |
342 |
|
StringBuilder buffer = new StringBuilder( 256 ); |
483 |
343 |
|
for ( String text : command ) |
|
@@ -485,7 +345,7 @@ |
485 |
345 |
|
buffer.append( text ); |
486 |
346 |
|
buffer.append( ' ' ); |
487 |
347 |
|
} |
488 |
|
- |
trace( "scar", "Executing command: " + buffer ); |
|
348 |
+ |
LOGGER.trace.log( "Executing command: ", buffer ); |
489 |
349 |
|
} |
490 |
350 |
|
|
491 |
351 |
|
Process process = new ProcessBuilder( command ).start(); |
|
@@ -534,39 +394,18 @@ |
534 |
394 |
|
static public String keystore( String keystoreFile, String alias, String password, String company, String title ) |
535 |
395 |
|
throws IOException |
536 |
396 |
|
{ |
537 |
|
- |
if ( keystoreFile == null ) |
538 |
|
- |
{ |
539 |
|
- |
throw new IllegalArgumentException( "keystoreFile cannot be null." ); |
540 |
|
- |
} |
541 |
|
- |
if ( fileExists( keystoreFile ) ) |
|
397 |
+ |
if ( fileExists( keystoreFile = assertNotEmpty( "keystoreFile", keystoreFile ) ) ) |
542 |
398 |
|
{ |
543 |
399 |
|
return keystoreFile; |
544 |
400 |
|
} |
545 |
|
- |
if ( alias == null ) |
546 |
|
- |
{ |
547 |
|
- |
throw new IllegalArgumentException( "alias cannot be null." ); |
548 |
|
- |
} |
549 |
|
- |
if ( password == null ) |
550 |
|
- |
{ |
551 |
|
- |
throw new IllegalArgumentException( "password cannot be null." ); |
552 |
|
- |
} |
553 |
|
- |
if ( password.length() < 6 ) |
|
401 |
+ |
alias = assertNotEmpty( "alias", alias ); |
|
402 |
+ |
company = assertNotEmpty( "company", company ); |
|
403 |
+ |
title = assertNotEmpty( "title", title ); |
|
404 |
+ |
if ( (password = assertNotEmpty( "password", password )).length() < 6 ) |
554 |
405 |
|
{ |
555 |
406 |
|
throw new IllegalArgumentException( "password must be 6 or more characters." ); |
556 |
407 |
|
} |
557 |
|
- |
if ( company == null ) |
558 |
|
- |
{ |
559 |
|
- |
throw new IllegalArgumentException( "company cannot be null." ); |
560 |
|
- |
} |
561 |
|
- |
if ( title == null ) |
562 |
|
- |
{ |
563 |
|
- |
throw new IllegalArgumentException( "title cannot be null." ); |
564 |
|
- |
} |
565 |
|
- |
|
566 |
|
- |
if ( DEBUG ) |
567 |
|
- |
{ |
568 |
|
- |
debug( "scar", "Creating keystore (" + alias + ":" + password + ", " + company + ", " + title + "): " + keystoreFile ); |
569 |
|
- |
} |
|
408 |
+ |
LOGGER.debug.log( "Creating keystore (", alias, ":", password, ", ", company, ", ", title, "): ", keystoreFile ); |
570 |
409 |
|
|
571 |
410 |
|
File file = new File( keystoreFile ); |
572 |
411 |
|
file.delete(); |
|
@@ -627,13 +466,13 @@ |
627 |
466 |
|
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); |
628 |
467 |
|
//noinspection unchecked |
629 |
468 |
|
compiler.getTask( null, new ForwardingJavaFileManager( compiler.getStandardFileManager( null, null, null ) ) |
630 |
|
- |
{ |
631 |
|
- |
@Override |
632 |
|
- |
public JavaFileObject getJavaFileForOutput( Location location, String className, JavaFileObject.Kind kind, FileObject sibling ) |
633 |
|
- |
{ |
634 |
|
- |
return javaObject; |
635 |
|
- |
} |
636 |
|
- |
}, diagnostics, null, null, Arrays.asList( javaObject ) ).call(); |
|
469 |
+ |
{ |
|
470 |
+ |
@Override |
|
471 |
+ |
public JavaFileObject getJavaFileForOutput( Location location, String className, JavaFileObject.Kind kind, FileObject sibling ) |
|
472 |
+ |
{ |
|
473 |
+ |
return javaObject; |
|
474 |
+ |
} |
|
475 |
+ |
}, diagnostics, null, null, Arrays.asList( javaObject ) ).call(); |
637 |
476 |
|
|
638 |
477 |
|
if ( !diagnostics.getDiagnostics().isEmpty() ) |
639 |
478 |
|
{ |