KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > tools > OPP > compiler > ExternalJavaCompiler


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id: ExternalJavaCompiler.java,v 1.2.2.1 2004/01/11 20:42:34 per_nyfelt Exp $
8
package org.ozoneDB.tools.OPP.compiler;
9
10 import java.util.Collection JavaDoc;
11 import java.util.Arrays JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.InputStreamReader JavaDoc;
14 import java.io.BufferedReader JavaDoc;
15 import java.io.InputStream JavaDoc;
16
17 /**
18  * Compiles resolver by starting an externalcompiler process.
19  */

20 public class ExternalJavaCompiler extends AbstractJavaCompiler {
21
22     private String JavaDoc compilerCommand;
23
24     /**
25      * @param compilerCommand The command to run the compiler..
26      */

27     public ExternalJavaCompiler(String JavaDoc compilerCommand) {
28         this.compilerCommand = compilerCommand;
29     }
30
31     /**
32      * Compiles the classes passed as strings in the classes parameter.
33      * @param classes The classes to compile
34      * @throws CompilerException
35      */

36     public void compile(Collection JavaDoc classes) throws CompilerException {
37         String JavaDoc args[] = getArguments(compilerCommand, classes);
38         Process JavaDoc process = null;
39         try {
40             process = Runtime.getRuntime().exec(args);
41             process.waitFor();
42         } catch (IOException JavaDoc e) {
43             throw new CompilerException("Failed to start compiler: " + e.getMessage(), args);
44         } catch (InterruptedException JavaDoc e) {
45             throw new CompilerException("Failed to start compiler: " + e.getMessage(), args);
46         }
47         if (process.exitValue() != 0) {
48             String JavaDoc output = streamToString(process.getInputStream());
49             String JavaDoc error = streamToString(process.getErrorStream());
50
51             // Either plug in a messageWriter or use some other logging (like log4j here)
52
System.out.println("Error running compiler: " + error);
53             System.out.println("compiling source " + getSourcePath() + " to " + getOutputPath());
54             System.out.println("output: " + output);
55             System.out.println("args: " + Arrays.asList(args));
56             throw new CompilerException("Failed to compile generated resolver", args);
57         }
58     }
59
60     private String JavaDoc streamToString(InputStream JavaDoc stream) {
61         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(stream));
62             String JavaDoc result = null;
63
64             try {
65                 String JavaDoc temp = reader.readLine();
66                 while (temp != null ) {
67                     result += " " + temp;
68                     temp = reader.readLine();
69                 }
70             } catch (IOException JavaDoc e) {
71                 e.printStackTrace();
72             }
73         return result;
74     }
75 }
76
Popular Tags