1 package com.quadcap.http.servlets.jsp; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FileOutputStream ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.OutputStream ; 48 49 import java.util.Properties ; 50 51 import javax.servlet.ServletContext ; 52 53 import com.quadcap.http.server22.WebApplication; 54 55 import com.quadcap.io.IO; 56 57 import com.quadcap.util.Debug; 58 59 64 public class JavaCompiler { 65 File root; 66 String compileCmd; 67 String contextClassPath = ""; 68 Properties defaultProps; 69 70 public JavaCompiler() {} 71 72 public void init(ServletContext context, File root, 73 String compileCmd, Properties 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 copyOutput(final OutputStream os, 84 final InputStream is) { 85 return new Thread () { 86 public void run() { 87 int c; 88 try { 89 IO.copyStream(is, os); 90 } catch (IOException e) { 91 } 92 } 93 }; 94 } 95 96 String getCompileCommand(Properties props) { 97 StringBuffer sb = new StringBuffer (); 98 StringBuffer nb = new StringBuffer (); 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 cmd, OutputStream out) 133 throws IOException 134 { 135 Process p = Runtime.getRuntime().exec(cmd); 136 Thread errthread = copyOutput(out, p.getErrorStream()); 137 Thread outthread = copyOutput(out, p.getInputStream()); 138 errthread.start(); 139 outthread.start(); 140 int res = -1; 141 try { res = p.waitFor(); } catch (Throwable t) {} 142 try { errthread.join(); } catch (Throwable t) {} 143 try { outthread.join(); } catch (Throwable t) {} 144 return res == 0; 145 } 146 147 public void compile(File javaFile, File classFile) 148 throws IOException , ClassNotFoundException , JspException 149 { 150 Properties cprops = new Properties (defaultProps); 151 cprops.put("source", javaFile.getPath()); 152 cprops.put("repository", root.getPath()); 153 cprops.put("context.classpath", contextClassPath); 154 String cmd = getCompileCommand(cprops); 155 if (Trace.level() > 2) { 156 Debug.println("COMPILE: " + cmd); 157 } 158 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 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 ("No class file generated by compile: " + 168 bos.toString()); 169 } 170 } 171 172 public static void main(String args[]) { 174 String cmd = "./jikes -classpath %java.class.path%;%repository%;%java.home%/lib/rt.jar -d %repository% +D +E -nowarn %source%"; 175 Properties props = System.getProperties(); 176 try { 177 JavaCompiler jc = new JavaCompiler(); 178 jc.init(null, new File ("./repository"), cmd, props); 179 File j = new File ("./repository/__jsp/login.java"); 180 File c = new File ("./repository/__jsp/login.class"); 181 jc.compile(j, c); 182 } catch (Throwable tt) { 183 tt.printStackTrace(System.err); 184 } 185 } 186 } 188 | Popular Tags |