1 23 24 34 35 package com.sun.ejb.codegen; 36 37 import java.io.*; 38 import java.util.*; 39 import java.util.logging.*; 40 import com.sun.enterprise.util.OS; 41 import com.sun.enterprise.util.StringUtils; 42 import com.sun.enterprise.util.i18n.StringManager; 43 import com.sun.enterprise.util.io.FileUtils; 44 import com.sun.enterprise.server.Constants; 45 import com.sun.tools.javac.Main; 46 47 class JavaCompiler extends Compiler 48 { 49 private static final String JAVAC_EXT_DIRS_OPTION = "-extdirs"; 50 private static final String JAVAC_OUT_OF_PROCESS = 51 "com.sun.aas.deployment.javacoutofprocess"; 52 53 54 JavaCompiler(List theOptions, List theFiles) throws JavaCompilerException 55 { 56 super(theOptions, theFiles); 57 } 58 59 61 protected void internal_compile() throws JavaCompilerException, ProcessExecutorException 62 { 63 70 if(userCompile()) { 71 return; 72 } 73 74 if(fastjavacCompile()) { 75 return; 76 } 77 78 javacCompile(); 79 } 80 81 83 private boolean userCompile() throws ProcessExecutorException 84 { 85 if(userExe == null) 86 return false; 87 88 ArrayList cmd = new ArrayList(); 89 cmd.add(userExe.getPath()); 90 cmd.add(JAVAC_EXT_DIRS_OPTION); 91 cmd.add(System.getProperty(JAVA_EXT_DIRS_SYS_PROP)); 92 cmd.addAll(userOptions); 93 cmd.addAll(options); 94 cmd.addAll(files); 95 96 String [] cmds = new String [cmd.size()]; 97 cmds = (String [])cmd.toArray(cmds); 98 runProcess(cmds, getUserSpecifiedCompilerTimeout() * files.size()); 99 logCompilerName(userExe.getName()); 100 return true; 101 } 102 103 105 private boolean fastjavacCompile() throws ProcessExecutorException 106 { 107 if(fastExe == null || jdkDir == null) 108 return false; 109 110 ArrayList cmd = new ArrayList(); 111 cmd.add(fastExe.getPath()); 112 cmd.add("-jdk"); 113 cmd.add(jdkDir.getPath()); 114 cmd.addAll(options); 115 addJavaFiles(cmd); 116 String [] cmds = new String [cmd.size()]; 117 cmds = (String [])cmd.toArray(cmds); 118 runProcess(cmds, getFastjavacTimeout() * files.size()); 119 logCompilerName("fastjavac"); 120 return true; 121 } 122 123 125 private boolean javacCompile() throws JavaCompilerException, 126 ProcessExecutorException 127 { 128 if(javacExe == null) 129 return false; 130 131 boolean outOfProcess = 132 Boolean.getBoolean(JAVAC_OUT_OF_PROCESS); 133 134 ArrayList cmd = new ArrayList(); 135 if (outOfProcess) { 136 cmd.add(javacExe.getPath()); 137 } 138 cmd.add(JAVAC_EXT_DIRS_OPTION); 139 cmd.add(System.getProperty(JAVA_EXT_DIRS_SYS_PROP)); 140 cmd.addAll(options); 141 addJavaFiles(cmd); 142 String [] cmds = new String [cmd.size()]; 143 cmds = (String [])cmd.toArray(cmds); 144 145 if (outOfProcess) { 146 runProcess(cmds, getJavacTimeout() * files.size()); 147 } else { 148 try { 149 ByteArrayOutputStream bos = 150 new ByteArrayOutputStream(); 151 PrintWriter pw = new PrintWriter(bos); 152 Main compiler = new Main(); 153 int ret = compiler.compile(cmds, pw); 154 if (ret != 0) { 155 byte[] errorBytes = bos.toByteArray(); 156 String errorString = new String (errorBytes); 157 throw new JavaCompilerException( 158 "java_compiler.error", "Native compiler returned an error: {0}\nError messages are: {1}", new Object [] { new Integer (ret), errorString } ); 159 } 160 } 161 catch(JavaCompilerException jce) { 162 throw jce; 163 } 164 catch(Throwable t) 165 { 166 throw new JavaCompilerException( 167 "java_compiler.unknown_exception", 168 "JavaC compiler threw an Exception", t); 169 } 170 } 171 logCompilerName("javac"); 172 return true; 173 } 174 175 177 protected void internal_init() 178 { 179 fastExe = null; 180 javacExe = null; 181 userExe = null; 182 userOptions = new ArrayList(); 183 184 initUserCompiler(); 185 initFastjavac(); 186 initJavac(); 187 logger.log(Level.FINE, "fastExe: " + ((fastExe == null) ? "null" : fastExe.getPath()) ); 188 logger.log(Level.FINE, "javacExe: " + ((javacExe == null) ? "null" : javacExe.getPath()) ); 189 logger.log(Level.FINE, "jdkDir: " + ((jdkDir == null) ? "null" : jdkDir.getPath()) ); 190 } 191 192 194 private void initUserCompiler() 195 { 196 String userSpecified = getSystemPropertyIgnoreCase(Constants.USER_SPECIFIED_COMPILER); 197 198 if(!StringUtils.ok(userSpecified)) 199 return; 200 201 userExe = new File(userSpecified); 202 203 if(!userExe.exists()) 204 { 205 String msg = localStrings.getStringWithDefault( 206 "java_compiler.bad_user_compiler", 207 "Can't locate user-specified Java Compiler for deployment. " 208 +"Environmental Variable= {0}, Value = {1}", 209 new Object [] { Constants.USER_SPECIFIED_COMPILER, userSpecified } ); 210 211 logger.warning(msg); 212 userExe = null; 213 return; 214 } 215 216 222 userExe = FileUtils.safeGetCanonicalFile(userExe); 223 String opts = getSystemPropertyIgnoreCase(Constants.USER_SPECIFIED_COMPILER_OPTIONS); 224 225 if(!StringUtils.ok(opts)) 226 return; 227 228 StringTokenizer tok = new StringTokenizer(opts); 229 230 while(tok.hasMoreTokens()) 231 { 232 userOptions.add(tok.nextToken()); 233 } 234 } 235 236 237 239 private void initFastjavac() 240 { 241 if(installRoot == null || jdkDir == null) 242 return; 243 244 String fastName; 245 261 262 if(OS.isWindows()) 263 fastName = "fastjavac.exe"; 264 else if(OS.isSun()) 265 fastName = "fastjavac.sun"; 266 else if(OS.isLinux()) 267 fastName = "fastjavac.linux"; 268 else 269 fastName = null; 270 271 if(fastName == null) 272 return; 273 274 fastExe = new File(installRoot + "/studio4/bin/fastjavac/" + fastName); 277 if(fastExe.exists()) 278 fastExe = FileUtils.safeGetCanonicalFile(fastExe); 279 else 280 fastExe = null; 281 } 282 283 285 private void initJavac() 286 { 287 if(jdkDir == null) 288 return; 289 290 String javacName; 291 292 if(OS.isWindows()) 293 { 294 javacName = "javac.exe"; 295 } 296 else 297 { 298 javacName = "javac"; 299 } 300 301 javacExe = new File(jdkDir, "/bin/" + javacName); 302 303 if(javacExe.exists()) 304 javacExe = FileUtils.safeGetCanonicalFile(javacExe); 305 else 306 javacExe = null; 307 } 308 309 324 private static int getFastjavacTimeout() 325 { 326 if (fastJavacTimeout < 0 ) 327 { 328 fastJavacTimeout = getTimeout(Constants.FASTJAVAC_TIMEOUT_MS, Constants.DEFAULT_FASTJAVAC_TIMEOUT_MS, 1000, 300000); 329 } 330 return fastJavacTimeout; 331 } 332 333 347 private static int getJavacTimeout() 348 { 349 if (javacTimeout < 0 ) 350 { 351 javacTimeout = getTimeout(Constants.JAVAC_TIMEOUT_MS, Constants.DEFAULT_JAVAC_TIMEOUT_MS, 1000, 900000); 352 } 353 return javacTimeout; 354 } 355 356 370 private static int getUserSpecifiedCompilerTimeout() 371 { 372 if (userTimeout < 0 ) 373 { 374 userTimeout = getTimeout(Constants.USER_SPECIFIED_COMPILER_TIMEOUT_MS, Constants.DEFAULT_USER_SPECIFIED_COMPILER_TIMEOUT_MS, 1000, 900000); 375 } 376 return userTimeout; 377 } 378 379 381 private File userExe; 382 private File fastExe; 383 private File javacExe; 384 private List userOptions; 385 386 private static int fastJavacTimeout = -1; 388 private static int javacTimeout = -1; 389 private static int userTimeout = -1; 390 391 430 } 431 432 | Popular Tags |