1 23 24 34 35 package com.sun.ejb.codegen; 36 37 import java.util.*; 38 import java.io.*; 39 import java.util.logging.*; 40 import com.sun.logging.LogDomains; 41 import com.sun.enterprise.instance.UniqueIdGenerator; 42 import com.sun.enterprise.server.Constants; 43 import com.sun.enterprise.util.OS; 44 import com.sun.enterprise.util.i18n.StringManager; 45 import com.sun.enterprise.util.io.FileUtils; 46 import com.sun.enterprise.util.StringUtils; 47 48 abstract class Compiler 49 { 50 protected static final String JAVA_EXT_DIRS_SYS_PROP = "java.ext.dirs"; 51 52 Compiler(List theOptions, List theFiles) throws JavaCompilerException 53 { 54 if(theOptions == null || theOptions.size() <= 0) 55 throw new JavaCompilerException("java_compiler.badargs", 56 "JavaCompiler given null or empty {0} list", 57 new Object [] { "options" } ); 58 if(theFiles == null || theFiles.size() <= 0) 59 throw new JavaCompilerException("java_compiler.badargs", 60 "JavaCompiler given null or empty {0} list", 61 new Object [] { "file" } ); 62 63 64 options = theOptions; 65 files = theFiles; 66 init(); 67 } 68 69 71 final void compile() throws JavaCompilerException 72 { 73 try 74 { 75 internal_compile(); 76 } 77 catch(JavaCompilerException jce) 78 { 79 throw jce; 80 } 81 catch(Throwable t) 82 { 83 throw new JavaCompilerException(t); 85 } 86 finally 87 { 88 if (fileOfFilenames != null) 89 { 90 if(!fileOfFilenames.delete()) fileOfFilenames.deleteOnExit(); 92 } 93 } 94 } 95 96 98 abstract protected void internal_compile() throws JavaCompilerException, ProcessExecutorException; 99 abstract protected void internal_init(); 100 101 103 final void init() 104 { 105 installRoot = System.getProperty(Constants.INSTALL_ROOT); 106 initJDKDir(); 107 logger.log(Level.FINE, "[Compiler] JDK Directory: " + ((jdkDir == null) ? "null" : jdkDir.getPath())); 108 109 String enableJavacFileStr = System.getProperty(Constants.ENABLE_JAVAC_FILE); 110 111 if(enableJavacFileStr != null) 112 useFileContainingFilenames = Boolean.valueOf(enableJavacFileStr).booleanValue(); 113 else 114 useFileContainingFilenames = OS.isWindows(); 115 116 internal_init(); 117 } 118 119 121 final void initJDKDir() { 122 125 String jh = System.getProperty("JAVA_HOME"); 126 127 if(StringUtils.ok(jh)) 128 { 129 jdkDir = new File(jh); 131 if(FileUtils.safeIsDirectory(jdkDir)) 132 { 133 jdkDir = FileUtils.safeGetCanonicalFile(jdkDir); 134 return; 135 } 136 } 137 138 jdkDir = null; 139 140 if(installRoot != null) { 142 jdkDir = new File(installRoot + "/jdk"); 143 144 if(FileUtils.safeIsDirectory(jdkDir)) { 145 jdkDir = FileUtils.safeGetCanonicalFile(jdkDir); 146 return; 147 } 148 } 149 150 jdkDir = null; 152 153 String jreHome = System.getProperty("java.home"); 154 155 if(StringUtils.ok(jreHome)) { 156 157 if (OS.isDarwin()) { 160 jdkDir = new File(jreHome); 161 } else { 162 jdkDir = (new File(jreHome)).getParentFile(); } 164 165 if(FileUtils.safeIsDirectory(jdkDir)) { 166 jdkDir = FileUtils.safeGetCanonicalFile(jdkDir); 167 return; 168 } 169 } 170 171 jdkDir = null; 173 } 174 175 177 protected void runProcess(String [] cmds, long timeout) throws ProcessExecutorException 178 { 179 ProcessExecutor exec = new ProcessExecutor(cmds, timeout); 180 exec.execute(); 181 } 185 186 188 protected void logCompilerName(String compilerName) 189 { 190 logger.log(Level.FINE, "[EJBC] Successfully compiled with " + compilerName); 191 } 192 193 195 protected static String getSystemPropertyIgnoreCase(final String key) 196 { 197 Properties p = System.getProperties(); 198 Set set = p.entrySet(); 199 200 for(Iterator it = set.iterator(); it.hasNext(); ) 201 { 202 Map.Entry me = (Map.Entry)it.next(); 203 String propKey = (String )me.getKey(); 204 205 if(key.compareToIgnoreCase(propKey) == 0) 206 return (String )me.getValue(); 207 } 208 209 return null; 210 } 211 212 214 protected void addJavaFiles(List list) 215 { 216 if(!useFileContainingFilenames) 218 { 219 list.addAll(files); 220 return; 221 } 222 223 writeFileOfFilenames(); 225 226 if(fileOfFilenames == null) 227 { 228 list.addAll(files); 231 return; 232 } 233 234 list.add("@" + FileUtils.safeGetCanonicalPath(fileOfFilenames)); 235 } 236 237 239 245 protected void writeFileOfFilenames() 246 { 247 fileOfFilenames = null; 248 BufferedWriter writer = null; 249 250 try 251 { 252 fileOfFilenames = File.createTempFile( 253 hostUniqueStr + 254 Long.toString(UniqueIdGenerator.getInstance().getNextUniqueId(), 16) + 255 "_", ".s1a"); 256 257 writer = new BufferedWriter(new FileWriter(fileOfFilenames)); 258 259 for(Iterator it = files.iterator(); it.hasNext(); ) 260 { 261 267 String fileSpec = (String ) it.next(); 268 if (fileSpec.indexOf(' ') != -1) { 269 fileSpec = prepareFileSpec(fileSpec); 270 } 271 writer.write(fileSpec); 272 writer.newLine(); 273 } 274 } 275 catch(Exception e) 276 { 277 fileOfFilenames = null; 278 } 279 280 finally 281 { 282 try 283 { 284 if (writer != null) 285 { 286 writer.close(); 287 } 288 } 289 catch(Exception ex) 290 { 291 } 292 } 293 } 294 295 297 protected static int getTimeout(String what, int defValue, int min, int max) 298 { 299 int to = defValue; 300 String sysValue = System.getProperty(what); 301 302 if(sysValue != null) 303 { 304 try 305 { 306 to = Integer.parseInt(sysValue); 307 } 308 catch(Exception e) 309 { 310 to = defValue; 311 } 312 } 313 314 if(to < min || to > max) 315 to = defValue; 316 317 return to; 318 } 319 320 321 330 protected static String ensureDoubleBackslashes(String original) { 331 StringBuffer answer = new StringBuffer (); 332 int match = -1; int placeAfterPreviousSlash = 0; while ((match = original.indexOf("\\", placeAfterPreviousSlash)) != -1) { 335 341 boolean slashIsAtEnd = (match + 1) >= original.length(); 342 boolean slashIsDoubled = (! slashIsAtEnd) && (original.charAt(match + 1) == '\\'); 343 if (slashIsAtEnd || ! slashIsDoubled) { 344 349 answer.append(original.substring(placeAfterPreviousSlash, match + 1)).append("\\"); 350 } 351 if (slashIsDoubled) { 352 placeAfterPreviousSlash = match + 2; 353 } else { 354 placeAfterPreviousSlash = match + 1; 355 } 356 } 357 answer.append(original.substring(placeAfterPreviousSlash)); 358 return answer.toString(); 359 } 360 361 372 protected static String prepareFileSpec (String fileSpec) { 373 String result = "\"" + ensureDoubleBackslashes(fileSpec) + "\""; 374 return result; 375 } 376 378 protected File jdkDir = null; 379 protected String installRoot = null; 380 protected List options; 381 protected List files; 382 protected File fileOfFilenames = null; 383 protected boolean useFileContainingFilenames = false; 384 protected static final Logger logger = LogDomains.getLogger(LogDomains.DPL_LOGGER); 385 protected static final StringManager localStrings = StringManager.getManager(JavaCompiler.class); 386 protected static final String hostUniqueStr = Integer.toString((new Object ()).hashCode(), 16) + "_"; 387 } 388 389 | Popular Tags |