diff options
author | Simon Rettberg | 2015-05-04 19:12:39 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-05-04 19:12:39 +0200 |
commit | 4487a9dad97f8f004303eb74c48278fbb90e7a44 (patch) | |
tree | af2f4a9a37a0060fbf9aab845104433d0d0caf9c /src/main/java/org/openslx/satserver | |
parent | Add PortScan task (diff) | |
download | tmlite-bwlp-4487a9dad97f8f004303eb74c48278fbb90e7a44.tar.gz tmlite-bwlp-4487a9dad97f8f004303eb74c48278fbb90e7a44.tar.xz tmlite-bwlp-4487a9dad97f8f004303eb74c48278fbb90e7a44.zip |
Update CreateAdConfig to match new ldadp config format
Diffstat (limited to 'src/main/java/org/openslx/satserver')
-rw-r--r-- | src/main/java/org/openslx/satserver/util/Exec.java | 110 |
1 files changed, 105 insertions, 5 deletions
diff --git a/src/main/java/org/openslx/satserver/util/Exec.java b/src/main/java/org/openslx/satserver/util/Exec.java index db59b0d..b7de984 100644 --- a/src/main/java/org/openslx/satserver/util/Exec.java +++ b/src/main/java/org/openslx/satserver/util/Exec.java @@ -1,7 +1,9 @@ package org.openslx.satserver.util; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; public class Exec { @@ -9,25 +11,123 @@ public class Exec /** * Run command, return exit status of process, or -1 on error * + * @param timeoutSec maximum time in seconds to wait for process to finish * @param command Command and arguments * @return exit code */ - public static int sync( String... command ) + public static int sync( int timeoutSec, ExecCallback callback, String... command ) { - return syncAt( "/", command ); + return syncAt( timeoutSec, callback, "/", command ); } - public static int syncAt( String cwd, String... command ) + public static int sync( int timeoutSec, String... command ) + { + return sync( timeoutSec, null, command ); + } + + public static int syncAt( int timeoutSec, ExecCallback callback, String cwd, String... command ) { ProcessBuilder pb = new ProcessBuilder( command ); pb.directory( new File( cwd ) ); - Process p; + Process p = null; + Thread[] list = null; try { p = pb.start(); - return p.waitFor(); + if ( callback != null ) { + list = setupCallback( p, callback ); + } + if ( timeoutSec <= 0 ) { + return p.waitFor(); + } else { + for ( int i = 0; i < timeoutSec * 10; ++i ) { + Thread.sleep( 100 ); + try { + return p.exitValue(); + } catch ( IllegalThreadStateException e ) { + // Wait... + } + } + return -1; + } } catch ( IOException | InterruptedException e ) { return -1; + } finally { + try { + if ( p != null ) { + Util.multiClose( p.getOutputStream(), p.getErrorStream() ); + p.destroy(); + } + } catch ( Exception e ) { + // + } + if ( list != null ) { + for ( Thread t : list ) { + try { + t.interrupt(); + } catch ( Exception e ) { + // + } + } + } } } + public static int syncAt( int timeoutSec, String cwd, String... command ) + { + return syncAt( timeoutSec, null, cwd, command ); + } + + private static Thread[] setupCallback( final Process p, final ExecCallback cb ) + { + // Read its stdout + Thread stdout = new Thread( new Runnable() { + @Override + public void run() + { + try { + BufferedReader reader = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); + String line; + while ( ( line = reader.readLine() ) != null ) { + synchronized ( p ) { + cb.processStdOut( line ); + } + } + } catch ( Exception e ) { + } + } + } ); + // Read its stderr + Thread stderr = new Thread( new Runnable() { + @Override + public void run() + { + try { + BufferedReader reader = new BufferedReader( new InputStreamReader( p.getErrorStream() ) ); + String line; + while ( ( line = reader.readLine() ) != null ) { + synchronized ( p ) { + cb.processStdErr( line ); + } + } + } catch ( Exception e ) { + } + } + } ); + + stdout.start(); + stderr.start(); + + Thread[] t = new Thread[] { stdout, stderr }; + return t; + } + + /**/ + + public interface ExecCallback + { + public void processStdOut( String line ); + + public void processStdErr( String line ); + } + } |