1 5 package ve.luz.ica.jackass.tools; 6 7 import java.io.*; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.Set ; 11 import java.util.zip.ZipEntry ; 12 import java.util.zip.ZipFile ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 17 import ve.luz.ica.jackass.deploy.util.Application; 18 import ve.luz.ica.jackass.deploy.util.Component; 19 import ve.luz.ica.jackass.deploy.descriptor.ica.IcaJackassApplication; 20 import ve.luz.ica.jackass.deploy.descriptor.standard.JackassApplication; 21 import ve.luz.ica.util.ZipUtil; 22 23 28 public final class Preprocessor 29 { 30 private static final Log LOG = LogFactory.getLog(Preprocessor.class); 31 32 private static final String JAR_FILE_PREFIX = "jackass-"; 33 private static final String JAR_EXTENSION = ".jar"; 34 private static final String PREPROCESSOR_DIRECTORY = "jackass-preprocessor"; 35 private static final String TEMP_DIR_PROPERTY = "java.io.tmpdir"; 36 private static final char PERIOD = '.'; 37 private static final char SLASH = '/'; 38 private static final String PERIOD_STRING = "."; 39 private static final String SLASH_STRING = "/"; 40 41 private static final String PUBLIC_VOID_DELEGATE = "public void _delegate"; 42 private static final String PUBLIC_KEYWORD = "public "; 43 private static final String PROXY_SUFFIX = "Proxy"; 44 private static final String POATIE_SUFFIX = "POATie"; 45 private static final String OPERATIONS_SUFFIX = "Operations"; 46 private static final String PROXY_FILE_SUFFIX = "Proxy.java"; 48 private static final String POATIE_FILE_SUFFIX = "POATie.java"; 49 51 private static final String BIN_DIR = "bin"; 52 private static final String IDL_DIR = "idl"; 53 54 private static final String IDL_COMPILER_COMMAND1 = "idlj -fall -td "; 55 private static final String IDL_COMPILER_COMMAND2 = "idlj -fserverTIE -td "; 56 private static final String JAR_COMMAND = "jar -cf "; 57 private static final String ZIP_UPDATE_COMMAND = "jar -uMf "; 58 59 private static String [] javacCommand = { 60 File.separator.equals(SLASH_STRING) ? "sh" : "cmd", 61 File.separator.equals(SLASH_STRING) ? "-c" : "/C", 62 "javac -classpath " + System.getProperty("java.class.path", PERIOD_STRING) + " -d ", 63 }; 64 65 private String preprocessorDir = null; 66 private String zipFileName = null; 67 private Runtime runtime = null; 68 69 73 public static void main(String [] args) 74 { 75 try 76 { 77 if (args.length < 1) 78 { 79 System.out.println("Usage: preprocessor <deployment file name>"); 80 return; 81 } 82 83 if (LOG.isDebugEnabled()) LOG.info("Preprocessing " + args[0]); 84 85 String tempDir = System.getProperty(TEMP_DIR_PROPERTY); 86 if (LOG.isDebugEnabled()) LOG.debug("Temp.dir "+tempDir); 87 File tempDirFile = new File (tempDir, PREPROCESSOR_DIRECTORY); 88 tempDirFile.mkdir(); 89 tempDirFile.deleteOnExit(); 90 Preprocessor pr = new Preprocessor(tempDirFile.getPath(), args[0]); 91 pr.process(); 92 deleteFile(tempDirFile); 93 94 if (LOG.isDebugEnabled()) LOG.info("Preprocessing complete"); 95 } 96 catch (Exception e) 97 { 98 if (LOG.isDebugEnabled()) 99 { 100 LOG.error("Error ", e); 101 } 102 else 103 { 104 LOG.error("Error processing the file", e); 105 } 106 } 107 108 } 109 110 114 private static void deleteFile(File file) 115 { 116 if (file.isDirectory()) 117 { 118 File [] fileList = file.listFiles(); 119 for (int i = 0; i<fileList.length; ++i) 120 { 121 deleteFile(fileList[i]); 122 } 123 } 124 file.delete(); 125 } 126 127 132 private Preprocessor(String workingDir, String zipFile) 133 { 134 this.preprocessorDir = workingDir; 135 this.zipFileName = zipFile; 136 runtime = Runtime.getRuntime(); 137 } 138 139 142 private void process() 143 { 144 try 145 { 146 ZipFile zip = new ZipFile (zipFileName); 147 148 ZipEntry entry = zip.getEntry(Application.STANDARD_DESCRIPTOR_NAME); 150 if (entry == null) 151 { 152 System.out.println("No standard deployment descriptor present: deployment.xml"); 153 return; 154 } 155 InputStream sis = new BufferedInputStream(zip.getInputStream(entry)); 156 entry = zip.getEntry(Application.ICA_DESCRIPTOR_NAME); 157 if (entry == null) 158 { 159 System.out.println("No ica deployment descriptor present: ica.xml"); 160 return; 161 } 162 InputStream iis = new BufferedInputStream(zip.getInputStream(entry)); 163 164 Reader standardReader = new InputStreamReader(sis); 165 Reader icaReader = new InputStreamReader(iis); 166 JackassApplication jackApp = JackassApplication.unmarshal(standardReader); 167 IcaJackassApplication icaApp = IcaJackassApplication.unmarshal(icaReader); 168 Application app = new Application(jackApp, icaApp); 169 170 this.compileIdlFiles(zip); 172 Set javaPathSet = this.generateProxies(app); 173 File binDirFile = new File (preprocessorDir, BIN_DIR); 174 this.compileJavaFiles(javaPathSet, binDirFile); 175 String jarFileName = this.generateJarFile(app, binDirFile); 176 this.addJarToZip(jarFileName); 177 } 178 catch (Exception e) 179 { 180 System.out.println(e.getMessage()); 181 if (LOG.isDebugEnabled()) LOG.debug(e); 182 } 183 } 184 185 191 void compileIdlFiles(ZipFile zip) throws Exception 192 { 193 ZipEntry entry = zip.getEntry(IDL_DIR); 195 if (entry == null) 196 { 197 throw new Exception ("No idl directory present"); 198 } 199 200 File idlDirFile = new File (preprocessorDir, IDL_DIR); 201 File preprocessorFile = new File (preprocessorDir); 202 203 ZipUtil.extractFiles(zip, preprocessorFile, IDL_DIR); 205 File [] idlFiles = idlDirFile.listFiles(); 206 207 if (idlFiles.length == 0) 209 { 210 throw new Exception ("No IDL files present"); 211 } 212 213 for (int i = 0; i<idlFiles.length; ++i) 215 { 216 String idlFile = idlFiles[i].getPath(); 217 218 if (LOG.isDebugEnabled()) LOG.debug(idlFile); 219 220 String line = null; 222 Process p = runtime.exec(IDL_COMPILER_COMMAND1 + preprocessorDir + " " + idlFile); 223 BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream())); 224 while ((line = input.readLine()) != null) 225 { 226 System.out.println(line); 227 } 228 229 input.close(); 230 231 p = runtime.exec(IDL_COMPILER_COMMAND2 + preprocessorDir + " " + idlFile); 233 input = new BufferedReader(new InputStreamReader(p.getErrorStream())); 234 while ((line = input.readLine()) != null) 235 { 236 System.out.println(line); 237 } 238 239 input.close(); 240 } 241 } 242 243 251 private Set generateProxies(Application app) throws Exception 252 { 253 Set javaPathSet = new HashSet (); 254 255 Iterator components = app.getComponents(); 257 while (components.hasNext()) 258 { 259 Component comp = (Component) components.next(); 260 String interfaceName = comp.getInterface(); 261 String interfacePath = preprocessorDir+File.separator+interfaceName.replace(PERIOD, SLASH); 262 String interfaceFileName = interfacePath.substring(interfacePath.lastIndexOf(SLASH)+1); 263 interfacePath = interfacePath.substring(0, interfacePath.lastIndexOf(SLASH)); 264 265 javaPathSet.add(interfacePath); 267 268 File tieFile = new File (interfacePath, interfaceFileName + POATIE_FILE_SUFFIX); 270 if (!tieFile.exists()) 271 { 272 throw new Exception ("File " + interfaceFileName + POATIE_FILE_SUFFIX + " not found"); 273 } 274 FileReader fr = new FileReader(tieFile); 275 LineNumberReader lr = new LineNumberReader(fr); 276 277 File proxyFile = new File (interfacePath, interfaceFileName + PROXY_FILE_SUFFIX); 279 FileWriter fw = new FileWriter(proxyFile); 280 PrintWriter pw = new PrintWriter(fw); 281 if (LOG.isDebugEnabled()) 282 { 283 LOG.debug("Generating proxy file " + proxyFile.getPath()); 284 LOG.debug("Interface name " + interfaceName); 285 } 286 this.generateProxy(lr, pw, interfaceName, interfaceFileName); 287 } 288 return javaPathSet; 289 } 290 291 301 private void generateProxy(LineNumberReader input, PrintWriter output, String qualifiedInterfaceName, 302 String interfaceName) throws IOException 303 { 304 String interfaceOperations = qualifiedInterfaceName + OPERATIONS_SUFFIX; 305 String tieClass = interfaceName + POATIE_SUFFIX; 306 String proxyClass = interfaceName + PROXY_SUFFIX; 307 if (LOG.isDebugEnabled()) 308 { 309 LOG.debug(interfaceOperations); 310 LOG.debug(tieClass); 311 } 312 boolean constructorGenerated = false; 313 boolean setMethodGenerated = false; 314 String line = input.readLine(); 315 while (line != null) 316 { 317 String processedLine = null; 318 int index = 0; 319 320 if (!constructorGenerated) 323 { 324 index = line.indexOf(PUBLIC_KEYWORD + tieClass); 325 if (index != -1) 326 { 327 output.println(" public " + proxyClass + "(org.omg.CORBA.Object delegate) {"); 328 output.println(" this._impl = " + qualifiedInterfaceName + "Helper.narrow(delegate);"); 329 output.println(" }"); 330 constructorGenerated = true; 331 } 332 } 333 334 if (!setMethodGenerated) 337 { 338 index = line.indexOf(PUBLIC_VOID_DELEGATE); 339 if (index != -1) 340 { 341 output.println(" public void _delegate(org.omg.CORBA.Object delegate) {"); 342 output.println(" this._impl = " + qualifiedInterfaceName + "Helper.narrow(delegate);"); 343 output.println(" }"); 344 setMethodGenerated = true; 345 } 346 } 347 348 index = line.indexOf(interfaceOperations); 350 if (index == -1) 351 { 352 processedLine = line; 353 } 354 else 355 { 356 processedLine = line.substring(0, index) + qualifiedInterfaceName + 357 line.substring(index+interfaceOperations.length()); 358 } 359 360 index = processedLine.indexOf(tieClass); 362 if (index != -1) 363 { 364 processedLine = processedLine.substring(0, index) + 365 proxyClass + processedLine.substring(index+tieClass.length()); 366 } 367 output.println(processedLine); 368 line = input.readLine(); 369 } 370 input.close(); 371 output.close(); 372 } 373 374 383 void compileJavaFiles(Set javaPathSet, File binDirFile) throws IOException 384 { 385 binDirFile.mkdir(); 386 Iterator sourcePaths = javaPathSet.iterator(); 387 while (sourcePaths.hasNext()) 388 { 389 String sourcePath = (String ) sourcePaths.next(); 391 javacCommand[2] = javacCommand[2] + binDirFile.getPath() + " " +sourcePath + "/*.java"; 392 if (LOG.isDebugEnabled()) LOG.debug("Compiling " + javacCommand[2]); 393 String line = null; 394 Process p = runtime.exec(javacCommand); 395 BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream())); 396 while ((line = input.readLine()) != null) 397 { 398 System.out.println(line); 399 } 400 input.close(); 401 } 402 } 403 404 411 private String generateJarFile(Application app, File binDirFile) throws IOException 412 { 413 String jarFileName = JAR_FILE_PREFIX + app.getName() + JAR_EXTENSION; 415 File jarFile = new File (preprocessorDir, jarFileName); 416 String jarFilePath = jarFile.getPath(); 417 String jarCommand = JAR_COMMAND + jarFilePath + " -C " + binDirFile.getPath(); 418 String [] jarList = binDirFile.list(); 419 for (int i = 0; i<jarList.length; ++i) 420 { 421 jarCommand = jarCommand + " " + jarList[i]; 422 } 423 if (LOG.isDebugEnabled()) 424 { 425 LOG.debug("Command: " + jarCommand); 426 } 427 String line = null; 428 Process p = runtime.exec(jarCommand); 429 BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream())); 430 while ((line = input.readLine()) != null) 431 { 432 System.out.println(line); 433 } 434 input.close(); 435 return jarFileName; 436 } 437 438 443 private void addJarToZip(String jarFileName) throws IOException 444 { 445 String line = null; 447 String zipCommand = ZIP_UPDATE_COMMAND + zipFileName + " -C " + preprocessorDir + " " + jarFileName; 448 Process p = runtime.exec(zipCommand); 449 BufferedReader input = new BufferedReader(new InputStreamReader(p.getErrorStream())); 450 while ((line = input.readLine()) != null) 451 { 452 System.out.println(line); 453 } 454 input.close(); 455 } 456 } 457 | Popular Tags |