22
33import java .io .File ;
44import java .io .IOException ;
5+ import java .util .concurrent .Callable ;
6+ import java .util .concurrent .ExecutorService ;
7+ import java .util .concurrent .Executors ;
8+ import java .util .concurrent .Future ;
9+ import java .util .concurrent .TimeUnit ;
10+
11+ import com .sandwich .util .ExceptionUtils ;
512
613class FileCompilerAction implements FileAction {
714
815 private final String destinationPath ;
916 private final String [] classPaths ;
10- private CompilationListener compilationListner ;
17+ private CompilationListener compilationListener ;
18+ private final long timeout ;
1119 static final CompilationListener LOGGING_HANDLER = new CompilationFailureLogger ();
1220
1321 public FileCompilerAction (File destinationPath , CompilationListener errorHandler , String ...classPaths ){
22+ this (destinationPath , errorHandler , 1000 , classPaths );
23+ }
24+
25+ public FileCompilerAction (File destinationPath ,
26+ CompilationListener errorHandler , long timeout , String [] classPaths ) {
1427 if (destinationPath == null ){
1528 throw new IllegalArgumentException ("the destination path is required" );
1629 }
1730 this .destinationPath = destinationPath .getAbsolutePath ();
1831 this .classPaths = classPaths ;
19- this .compilationListner = errorHandler == null ? LOGGING_HANDLER : errorHandler ;
32+ this .compilationListener = errorHandler == null ? LOGGING_HANDLER : errorHandler ;
33+ this .timeout = timeout ;
2034 }
2135
2236 public void sourceToDestination (File src , File bin ) throws IOException {
@@ -25,19 +39,49 @@ public void sourceToDestination(File src, File bin) throws IOException {
2539 String [] command = constructJavaCompilationCommand (src );
2640 Process p = Runtime .getRuntime ().exec (command );
2741 try {
28- if (p .waitFor () != 0 ) {
29- compilationListner .compilationFailed (src , command , p .exitValue (), StreamUtils .convertStreamToString (p .getErrorStream ()), null );
30- }else {
31- compilationListner .compilationSucceeded (src , command , StreamUtils .convertStreamToString (p .getInputStream ()), null );
32- }
42+ executeWithTimeout (src , command , p , timeout );
43+ } catch (IllegalThreadStateException x ) {
44+ compilationListener .compilationFailed (src , command ,
45+ 255 , "Compilation took longer than " +timeout +" ms.\n " +
46+ "It is likely that the compiler has locked up compiling this file.\n " +
47+ "Please revert your last change and try something different." , x );
3348 } catch (Exception x ) {
3449 x .printStackTrace ();
35- compilationListner .compilationFailed (src , command ,
50+ compilationListener .compilationFailed (src , command ,
3651 p .exitValue (), StreamUtils .convertStreamToString (p .getErrorStream ()), x );
3752 }
3853 }
3954 }
4055
56+ private void executeWithTimeout (final File src , final String [] command ,
57+ final Process p , final long timeout ) throws InterruptedException {
58+ ExecutorService executor = Executors .newCachedThreadPool ();
59+ Callable <Object > task = new Callable <Object >() {
60+ public Object call () {
61+ try {
62+ if (p .waitFor () != 0 ) {
63+ compilationListener .compilationFailed (src , command , p
64+ .exitValue (), StreamUtils .convertStreamToString (p .getErrorStream ()), null );
65+ } else {
66+ compilationListener .compilationSucceeded (src , command ,
67+ StreamUtils .convertStreamToString (p .getInputStream ()), null );
68+ }
69+ } catch (InterruptedException e ) {
70+ compilationListener .compilationFailed (src , command , p .exitValue (),
71+ ExceptionUtils .convertToPopulatedStackTraceString (e ), null );
72+ }
73+ return null ;
74+ }
75+ };
76+ Future <Object > future = executor .submit (task );
77+ try {
78+ future .get (timeout , TimeUnit .MILLISECONDS );
79+ } catch (Exception e ) {
80+ compilationListener .compilationFailed (src , command , p .exitValue (),
81+ ExceptionUtils .convertToPopulatedStackTraceString (e ), null );
82+ }
83+ }
84+
4185 private String [] constructJavaCompilationCommand (File src ) {
4286 return copy (new String []{"javac" , "-d" , destinationPath }, getClasspath (), new String []{src .getAbsolutePath ()});
4387 }
0 commit comments