Subversion Repository Public Repository

litesoft

Diff Revisions 958 vs 959 for /trunk/Java/ScarPlus/src/com/esotericsoftware/scar/Utils.java

Diff revisions: vs.
  @@ -9,22 +9,17 @@
9 9 import java.util.regex.*;
10 10
11 11 @SuppressWarnings("UnusedDeclaration")
12 - public class Utils extends FileUtil
13 - {
12 + public class Utils extends FileUtil {
14 13 /**
15 14 * The Scar installation directory. The value comes from the SCAR_HOME environment variable, if it exists. Alternatively, the
16 15 * "scar.home" System property can be defined.
17 16 */
18 17 static public final String SCAR_HOME;
19 18
20 - static
21 - {
22 - if ( System.getProperty( "scar.home" ) != null )
23 - {
19 + static {
20 + if ( System.getProperty( "scar.home" ) != null ) {
24 21 SCAR_HOME = System.getProperty( "scar.home" );
25 - }
26 - else
27 - {
22 + } else {
28 23 SCAR_HOME = System.getenv( "SCAR_HOME" );
29 24 }
30 25 }
  @@ -38,10 +33,8 @@
38 33 * Returns the full path for the specified file name in the current working directory, the {@link #SCAR_HOME}, and the bin
39 34 * directory of {@link #JAVA_HOME}.
40 35 */
41 - static public String resolvePath( String fileName )
42 - {
43 - if ( fileName == null )
44 - {
36 + static public String resolvePath( String fileName ) {
37 + if ( fileName == null ) {
45 38 return null;
46 39 }
47 40
  @@ -50,43 +43,34 @@
50 43 return foundFile;
51 44 }
52 45
53 - private static String lowLevelResolve( String fileName )
54 - {
46 + private static String lowLevelResolve( String fileName ) {
55 47 String foundFile;
56 - if ( fileExists( foundFile = canonical( fileName ) ) )
57 - {
48 + if ( fileExists( foundFile = canonical( fileName ) ) ) {
58 49 return foundFile;
59 50 }
60 - if ( fileExists( foundFile = new File( SCAR_HOME, fileName ).getPath() ) )
61 - {
51 + if ( fileExists( foundFile = new File( SCAR_HOME, fileName ).getPath() ) ) {
62 52 return foundFile;
63 53 }
64 - if ( fileExists( foundFile = new File( JAVA_HOME, "bin/" + fileName ).getPath() ) )
65 - {
54 + if ( fileExists( foundFile = new File( JAVA_HOME, "bin/" + fileName ).getPath() ) ) {
66 55 return foundFile;
67 56 }
68 57 return fileName;
69 58 }
70 59
71 - static public File findCommonDirPathFromCanonicalDirPaths( File pPath1, File pPath2 )
72 - {
60 + static public File findCommonDirPathFromCanonicalDirPaths( File pPath1, File pPath2 ) {
73 61 String zPath1 = pPath1.getPath();
74 62 String zPath2 = pPath2.getPath();
75 - if (zPath2.startsWith(zPath1)) // Check the Happy Cases
63 + if ( zPath2.startsWith( zPath1 ) ) // Check the Happy Cases
76 64 {
77 65 return pPath1;
78 66 }
79 - if (zPath1.startsWith(zPath2))
80 - {
67 + if ( zPath1.startsWith( zPath2 ) ) {
81 68 return pPath2;
82 69 }
83 70 String zSharedPath = findSharedPath( zPath1, zPath2 );
84 - if ( zSharedPath.length() != 0 )
85 - {
86 - for ( File zShared = new File( zSharedPath ); zShared != null; zShared = zShared.getParentFile() )
87 - {
88 - if ( zShared.isDirectory() )
89 - {
71 + if ( zSharedPath.length() != 0 ) {
72 + for ( File zShared = new File( zSharedPath ); zShared != null; zShared = zShared.getParentFile() ) {
73 + if ( zShared.isDirectory() ) {
90 74 return zShared;
91 75 }
92 76 }
  @@ -94,16 +78,13 @@
94 78 return null;
95 79 }
96 80
97 - static private String findSharedPath( String pPath1, String pPath2 )
98 - {
99 - int zLength = Math.min(pPath1.length(), pPath2.length());
100 - if ( (zLength == 0) || (pPath1.charAt(0) != pPath2.charAt(0)) )
101 - {
81 + static private String findSharedPath( String pPath1, String pPath2 ) {
82 + int zLength = Math.min( pPath1.length(), pPath2.length() );
83 + if ( (zLength == 0) || (pPath1.charAt( 0 ) != pPath2.charAt( 0 )) ) {
102 84 return "";
103 85 }
104 - for (int i = 1; i < zLength; i++) {
105 - if ( pPath1.charAt( i ) != pPath2.charAt( i ) )
106 - {
86 + for ( int i = 1; i < zLength; i++ ) {
87 + if ( pPath1.charAt( i ) != pPath2.charAt( i ) ) {
107 88 return pPath1.substring( 0, i ); // i == Exclusive
108 89 }
109 90 }
  @@ -113,20 +94,16 @@
113 94 /**
114 95 * Returns the canonical path for the specified path. Eg, if "." is passed, this will resolve the actual path and return it.
115 96 */
116 - static public String canonical( String path )
117 - {
97 + static public String canonical( String path ) {
118 98 path = assertNotEmpty( "path", path );
119 99
120 100 File file = new File( path );
121 - try
122 - {
101 + try {
123 102 return file.getCanonicalPath();
124 103 }
125 - catch ( IOException ex )
126 - {
104 + catch ( IOException ex ) {
127 105 file = file.getAbsoluteFile();
128 - if ( file.getName().equals( "." ) )
129 - {
106 + if ( file.getName().equals( "." ) ) {
130 107 file = file.getParentFile();
131 108 }
132 109 return file.getPath();
  @@ -136,16 +113,13 @@
136 113 /**
137 114 * Returns the canonical path for the specified path. Eg, if "." is passed, this will resolve the actual path and return it.
138 115 */
139 - static public File canonical( File path )
140 - {
116 + static public File canonical( File path ) {
141 117 assertNotNull( "path", path );
142 118
143 - try
144 - {
119 + try {
145 120 return path.getCanonicalFile();
146 121 }
147 - catch ( IOException ex )
148 - {
122 + catch ( IOException ex ) {
149 123 return path.getAbsoluteFile();
150 124 }
151 125 }
  @@ -153,32 +127,28 @@
153 127 /**
154 128 * Returns true if the file exists.
155 129 */
156 - static public boolean fileExists( String path )
157 - {
130 + static public boolean fileExists( String path ) {
158 131 return new File( assertNotEmpty( "path", path ) ).exists();
159 132 }
160 133
161 134 /**
162 135 * Returns only the filename portion of the specified path.
163 136 */
164 - static public String fileName( String path )
165 - {
137 + static public String fileName( String path ) {
166 138 return new File( canonical( path ) ).getName();
167 139 }
168 140
169 141 /**
170 142 * Returns the parent directory of the specified path.
171 143 */
172 - static public String parent( String path )
173 - {
144 + static public String parent( String path ) {
174 145 return new File( canonical( path ) ).getParent();
175 146 }
176 147
177 148 /**
178 149 * Returns only the extension portion of the specified path, or an empty string if there is no extension.
179 150 */
180 - static public String fileExtension( String file )
181 - {
151 + static public String fileExtension( String file ) {
182 152 file = assertNotEmpty( "file", file );
183 153 int commaIndex = file.indexOf( '.' );
184 154 return (commaIndex == -1) ? "" : file.substring( commaIndex + 1 );
  @@ -187,21 +157,16 @@
187 157 /**
188 158 * Returns only the filename portion of the specified path, without the extension, if any.
189 159 */
190 - static public String fileWithoutExtension( String file )
191 - {
160 + static public String fileWithoutExtension( String file ) {
192 161 file = assertNotEmpty( "file", file );
193 162 int commaIndex = file.indexOf( '.' );
194 - if ( commaIndex == -1 )
195 - {
163 + if ( commaIndex == -1 ) {
196 164 commaIndex = file.length();
197 165 }
198 166 int slashIndex = file.replace( '\\', '/' ).lastIndexOf( '/' );
199 - if ( slashIndex == -1 )
200 - {
167 + if ( slashIndex == -1 ) {
201 168 slashIndex = 0;
202 - }
203 - else
204 - {
169 + } else {
205 170 slashIndex++;
206 171 }
207 172 return file.substring( slashIndex, commaIndex );
  @@ -212,8 +177,7 @@
212 177 *
213 178 * @param end The end index of the substring. If negative, the index used will be "text.length() + end".
214 179 */
215 - static public String substring( String text, int start, int end )
216 - {
180 + static public String substring( String text, int start, int end ) {
217 181 assertNotNull( "text", text );
218 182 assertNotNegative( "start", start );
219 183 return text.substring( start, (end >= 0) ? end : text.length() + end );
  @@ -222,23 +186,16 @@
222 186 /**
223 187 * Splits the specified command at spaces that are not surrounded by quotes and passes the result to {@link #shell(String...)}.
224 188 */
225 - static public void shell( String command )
226 - {
189 + static public void shell( String command ) {
227 190 List<String> matchList = new ArrayList<String>();
228 191 Pattern regex = Pattern.compile( "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'" );
229 192 Matcher regexMatcher = regex.matcher( command );
230 - while ( regexMatcher.find() )
231 - {
232 - if ( regexMatcher.group( 1 ) != null )
233 - {
193 + while ( regexMatcher.find() ) {
194 + if ( regexMatcher.group( 1 ) != null ) {
234 195 matchList.add( regexMatcher.group( 1 ) );
235 - }
236 - else if ( regexMatcher.group( 2 ) != null )
237 - {
196 + } else if ( regexMatcher.group( 2 ) != null ) {
238 197 matchList.add( regexMatcher.group( 2 ) );
239 - }
240 - else
241 - {
198 + } else {
242 199 matchList.add( regexMatcher.group() );
243 200 }
244 201 }
  @@ -249,34 +206,26 @@
249 206 * Executes the specified shell command. {@link #resolvePath(String)} is used to locate the file to execute. If not found, on
250 207 * Windows the same filename with an "exe" extension is also tried.
251 208 */
252 - static public void shell( String... command )
253 - {
209 + static public void shell( String... command ) {
254 210 assertNotEmpty( "command", command );
255 211
256 212 String originalCommand = (command[0] = assertNotEmpty( "shell Command", command[0] ));
257 213 command[0] = resolvePath( command[0] );
258 - if ( !fileExists( command[0] ) && isWindows )
259 - {
214 + if ( !fileExists( command[0] ) && isWindows ) {
260 215 command[0] = resolvePath( command[0] + ".exe" );
261 - if ( !fileExists( command[0] ) )
262 - {
216 + if ( !fileExists( command[0] ) ) {
263 217 command[0] = originalCommand;
264 218 }
265 219 }
266 220
267 - if ( LOGGER.trace.isEnabled() )
268 - {
221 + if ( LOGGER.trace.isEnabled() ) {
269 222 StringBuilder buffer = new StringBuilder( 256 );
270 - for ( String text : command )
271 - {
272 - if ( text.contains( " " ) )
273 - {
223 + for ( String text : command ) {
224 + if ( text.contains( " " ) ) {
274 225 buffer.append( '"' );
275 226 buffer.append( text );
276 227 buffer.append( '"' );
277 - }
278 - else
279 - {
228 + } else {
280 229 buffer.append( text );
281 230 }
282 231 buffer.append( ' ' );
  @@ -284,8 +233,7 @@
284 233 LOGGER.trace.log( "Executing command: ", buffer );
285 234 }
286 235
287 - try
288 - {
236 + try {
289 237 Process process = new ProcessBuilder( command ).start();
290 238 Thread zOut = new CopyOutputThread( process.getInputStream(), System.out );
291 239 Thread zErr = new CopyOutputThread( process.getErrorStream(), System.err );
  @@ -296,23 +244,19 @@
296 244 // process.waitFor();
297 245 // } catch (InterruptedException ignored) {
298 246 // }
299 - if ( process.exitValue() != 0 )
300 - {
247 + if ( process.exitValue() != 0 ) {
301 248 StringBuilder buffer = new StringBuilder( 256 );
302 - for ( String text : command )
303 - {
249 + for ( String text : command ) {
304 250 buffer.append( text );
305 251 buffer.append( ' ' );
306 252 }
307 253 throw new RuntimeException( "Error (" + process.exitValue() + ") executing command: " + buffer );
308 254 }
309 255 }
310 - catch ( IOException e )
311 - {
256 + catch ( IOException e ) {
312 257 throw new WrappedIOException( e );
313 258 }
314 - catch ( InterruptedException e )
315 - {
259 + catch ( InterruptedException e ) {
316 260 throw new RuntimeException( e );
317 261 }
318 262 }
  @@ -322,17 +266,14 @@
322 266 *
323 267 * @return The path to the keystore file.
324 268 */
325 - static public String keystore( String keystoreFile, String alias, String password, String company, String title )
326 - {
327 - if ( fileExists( keystoreFile = assertNotEmpty( "keystoreFile", keystoreFile ) ) )
328 - {
269 + static public String keystore( String keystoreFile, String alias, String password, String company, String title ) {
270 + if ( fileExists( keystoreFile = assertNotEmpty( "keystoreFile", keystoreFile ) ) ) {
329 271 return keystoreFile;
330 272 }
331 273 alias = assertNotEmpty( "alias", alias );
332 274 company = assertNotEmpty( "company", company );
333 275 title = assertNotEmpty( "title", title );
334 - if ( (password = assertNotEmpty( "password", password )).length() < 6 )
335 - {
276 + if ( (password = assertNotEmpty( "password", password )).length() < 6 ) {
336 277 throw new IllegalArgumentException( "password must be 6 or more characters." );
337 278 }
338 279 LOGGER.debug.log( "Creating keystore (", alias, ":", password, ", ", company, ", ", title, "): ", keystoreFile );
  @@ -341,8 +282,7 @@
341 282 //noinspection ResultOfMethodCallIgnored
342 283 file.delete();
343 284 Process process;
344 - try
345 - {
285 + try {
346 286 process = Runtime.getRuntime().exec( new String[]{resolvePath( "keytool" ), "-genkey", "-keystore", keystoreFile, "-alias", alias} );
347 287 OutputStreamWriter writer = new OutputStreamWriter( process.getOutputStream() );
348 288 writer.write( password + "\n" ); // Enter keystore password:
  @@ -360,66 +300,52 @@
360 300 process.getInputStream().close();
361 301 process.getErrorStream().close();
362 302 }
363 - catch ( IOException e )
364 - {
303 + catch ( IOException e ) {
365 304 throw new WrappedIOException( e );
366 305 }
367 - try
368 - {
306 + try {
369 307 process.waitFor();
370 308 }
371 - catch ( InterruptedException ignored )
372 - {
309 + catch ( InterruptedException ignored ) {
373 310 }
374 - if ( !file.exists() )
375 - {
311 + if ( !file.exists() ) {
376 312 throw new RuntimeException( "Error creating keystore." );
377 313 }
378 314 return keystoreFile;
379 315 }
380 316
381 317 public static Class compileDynamicCodeToClass( int pOverheadStartLines, final String pCode, final URL... pClasspathURLs )
382 - throws ClassNotFoundException
383 - {
318 + throws ClassNotFoundException {
384 319 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
385 - if ( compiler == null )
386 - {
320 + if ( compiler == null ) {
387 321 throw new RuntimeException( "No compiler available. Ensure you are running from a 1.6+ JDK, and not a JRE." );
388 322 }
389 323
390 324 final ByteArrayOutputStream output = new ByteArrayOutputStream( 32 * 1024 );
391 - final SimpleJavaFileObject javaObject = new SimpleJavaFileObject( URI.create( "Generated.java" ), JavaFileObject.Kind.SOURCE )
392 - {
325 + final SimpleJavaFileObject javaObject = new SimpleJavaFileObject( URI.create( "Generated.java" ), JavaFileObject.Kind.SOURCE ) {
393 326 @Override
394 - public OutputStream openOutputStream()
395 - {
327 + public OutputStream openOutputStream() {
396 328 return output;
397 329 }
398 330
399 331 @Override
400 - public CharSequence getCharContent( boolean ignoreEncodingErrors )
401 - {
332 + public CharSequence getCharContent( boolean ignoreEncodingErrors ) {
402 333 return pCode;
403 334 }
404 335 };
405 336 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
406 337 //noinspection unchecked
407 - compiler.getTask( null, new ForwardingJavaFileManager( compiler.getStandardFileManager( null, null, null ) )
408 - {
338 + compiler.getTask( null, new ForwardingJavaFileManager( compiler.getStandardFileManager( null, null, null ) ) {
409 339 @Override
410 - public JavaFileObject getJavaFileForOutput( Location location, String className, JavaFileObject.Kind kind, FileObject sibling )
411 - {
340 + public JavaFileObject getJavaFileForOutput( Location location, String className, JavaFileObject.Kind kind, FileObject sibling ) {
412 341 return javaObject;
413 342 }
414 343 }, diagnostics, null, null, Arrays.asList( javaObject ) ).call();
415 344
416 - if ( !diagnostics.getDiagnostics().isEmpty() )
417 - {
345 + if ( !diagnostics.getDiagnostics().isEmpty() ) {
418 346 StringBuilder buffer = new StringBuilder( 1024 );
419 - for ( Diagnostic diagnostic : diagnostics.getDiagnostics() )
420 - {
421 - if ( buffer.length() > 0 )
422 - {
347 + for ( Diagnostic diagnostic : diagnostics.getDiagnostics() ) {
348 + if ( buffer.length() > 0 ) {
423 349 buffer.append( "\n" );
424 350 }
425 351 buffer.append( "Line " );
  @@ -431,27 +357,21 @@
431 357 }
432 358
433 359 // Load class.
434 - return new URLClassLoader( pClasspathURLs, Scar.class.getClassLoader() )
435 - {
360 + return new URLClassLoader( pClasspathURLs, Scar.class.getClassLoader() ) {
436 361 @Override
437 362 protected synchronized Class<?> loadClass( String name, boolean resolve )
438 - throws ClassNotFoundException
439 - {
363 + throws ClassNotFoundException {
440 364 // Look in this classloader before the parent.
441 365 Class c = findLoadedClass( name );
442 - if ( c == null )
443 - {
444 - try
445 - {
366 + if ( c == null ) {
367 + try {
446 368 c = findClass( name );
447 369 }
448 - catch ( ClassNotFoundException e )
449 - {
370 + catch ( ClassNotFoundException e ) {
450 371 return super.loadClass( name, resolve );
451 372 }
452 373 }
453 - if ( resolve )
454 - {
374 + if ( resolve ) {
455 375 resolveClass( c );
456 376 }
457 377 return c;
  @@ -459,10 +379,8 @@
459 379
460 380 @Override
461 381 protected Class<?> findClass( String name )
462 - throws ClassNotFoundException
463 - {
464 - if ( name.equals( "Generated" ) )
465 - {
382 + throws ClassNotFoundException {
383 + if ( name.equals( "Generated" ) ) {
466 384 byte[] bytes = output.toByteArray();
467 385 return defineClass( name, bytes, 0, bytes.length );
468 386 }