KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > JavaCompiler


1 package com.quadcap.http.servlets.jsp;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.ByteArrayOutputStream JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.OutputStream JavaDoc;
48
49 import java.util.Properties JavaDoc;
50
51 import javax.servlet.ServletContext JavaDoc;
52
53 import com.quadcap.http.server22.WebApplication;
54
55 import com.quadcap.io.IO;
56
57 import com.quadcap.util.Debug;
58
59 /**
60  * Compile java to class files using the configured compiler options.
61  *
62  * @author Stan Bailes
63  */

64 public class JavaCompiler {
65     File JavaDoc root;
66     String JavaDoc compileCmd;
67     String JavaDoc contextClassPath = "";
68     Properties JavaDoc defaultProps;
69
70     public JavaCompiler() {}
71     
72     public void init(ServletContext JavaDoc context, File JavaDoc root,
73                      String JavaDoc compileCmd, Properties JavaDoc defaultProps) {
74     this.root = root;
75     this.compileCmd = compileCmd;
76     this.defaultProps = defaultProps;
77         if (context instanceof WebApplication) {
78             WebApplication app = (WebApplication)context;
79             contextClassPath = app.getContextClassPath();
80         }
81     }
82
83     private Thread JavaDoc copyOutput(final OutputStream JavaDoc os,
84                               final InputStream JavaDoc is) {
85         return new Thread JavaDoc() {
86             public void run() {
87                 int c;
88                 try {
89                     IO.copyStream(is, os);
90                 } catch (IOException JavaDoc e) {
91                 }
92             }
93         };
94     }
95
96     String JavaDoc getCompileCommand(Properties JavaDoc props) {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         StringBuffer JavaDoc nb = new StringBuffer JavaDoc();
99         int state = 0;
100         for (int i = 0; i < compileCmd.length(); i++) {
101             char c = compileCmd.charAt(i);
102             switch (state) {
103             case 0:
104                 if (c == '%') {
105                     state = 1;
106                     nb.setLength(0);
107                 } else {
108                     sb.append(c);
109                 }
110                 break;
111             case 1:
112                 if (c == '%') {
113                     sb.append(c);
114                     state = 0;
115                 } else {
116                     nb.append(c);
117                     state = 2;
118                 }
119                 break;
120             case 2:
121                 if (c == '%') {
122                     sb.append(props.getProperty(nb.toString()));
123                     state = 0;
124                 } else {
125                     nb.append(c);
126                 }
127             }
128         }
129         return sb.toString();
130     }
131
132     public boolean doCompile(String JavaDoc cmd, OutputStream JavaDoc out)
133         throws IOException JavaDoc
134     {
135         Process JavaDoc p = Runtime.getRuntime().exec(cmd);
136         Thread JavaDoc errthread = copyOutput(out, p.getErrorStream());
137         Thread JavaDoc outthread = copyOutput(out, p.getInputStream());
138         errthread.start();
139         outthread.start();
140         int res = -1;
141         try { res = p.waitFor(); } catch (Throwable JavaDoc t) {}
142         try { errthread.join(); } catch (Throwable JavaDoc t) {}
143         try { outthread.join(); } catch (Throwable JavaDoc t) {}
144         return res == 0;
145     }
146
147     public void compile(File JavaDoc javaFile, File JavaDoc classFile)
148         throws IOException JavaDoc, ClassNotFoundException JavaDoc, JspException
149     {
150         Properties JavaDoc cprops = new Properties JavaDoc(defaultProps);
151         cprops.put("source", javaFile.getPath());
152         cprops.put("repository", root.getPath());
153         cprops.put("context.classpath", contextClassPath);
154         String JavaDoc cmd = getCompileCommand(cprops);
155         if (Trace.level() > 2) {
156             Debug.println("COMPILE: " + cmd);
157         }
158         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
159         boolean ok = doCompile(cmd, bos);
160         if (Trace.level() > 3) {
161             Debug.println("COMPILE OUTPUT: " + bos.toString());
162         }
163         if (!ok) {
164             throw new JspException("compile failed: " + bos.toString());
165         }
166         if (!classFile.exists()) {
167             throw new ClassNotFoundException JavaDoc("No class file generated by compile: " +
168                                              bos.toString());
169         }
170     }
171
172     //#ifdef DEBUG
173
public static void main(String JavaDoc args[]) {
174         String JavaDoc cmd = "./jikes -classpath %java.class.path%;%repository%;%java.home%/lib/rt.jar -d %repository% +D +E -nowarn %source%";
175         Properties JavaDoc props = System.getProperties();
176         try {
177             JavaCompiler jc = new JavaCompiler();
178             jc.init(null, new File JavaDoc("./repository"), cmd, props);
179             File JavaDoc j = new File JavaDoc("./repository/__jsp/login.java");
180             File JavaDoc c = new File JavaDoc("./repository/__jsp/login.class");
181             jc.compile(j, c);
182         } catch (Throwable JavaDoc tt) {
183             tt.printStackTrace(System.err);
184         }
185     }
186     //#endif
187
}
188
Popular Tags