| 1 18 19 package org.apache.tools.ant.taskdefs.optional.ejb; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.Hashtable ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Properties ; 35 import java.util.StringTokenizer ; 36 import javax.xml.parsers.SAXParser ; 37 import javax.xml.parsers.SAXParserFactory ; 38 import org.xml.sax.AttributeList ; 39 import org.xml.sax.HandlerBase ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 43 67 public class IPlanetEjbc { 68 69 private static final int MIN_NUM_ARGS = 2; 70 private static final int MAX_NUM_ARGS = 8; 71 private static final int NUM_CLASSES_WITH_IIOP = 15; 72 private static final int NUM_CLASSES_WITHOUT_IIOP = 9; 73 74 75 private static final String ENTITY_BEAN = "entity"; 76 private static final String STATELESS_SESSION = "stateless"; 77 private static final String STATEFUL_SESSION = "stateful"; 78 79 80 private File stdDescriptor; 81 private File iasDescriptor; 82 83 87 private File destDirectory; 88 89 90 private String classpath; 91 private String [] classpathElements; 92 93 94 private boolean retainSource = false; 95 private boolean debugOutput = false; 96 97 98 private File iasHomeDir; 99 100 101 private SAXParser parser; 102 private EjbcHandler handler = new EjbcHandler(); 103 104 111 private Hashtable ejbFiles = new Hashtable (); 112 113 114 private String displayName; 115 116 132 public IPlanetEjbc(File stdDescriptor, 133 File iasDescriptor, 134 File destDirectory, 135 String classpath, 136 SAXParser parser) { 137 this.stdDescriptor = stdDescriptor; 138 this.iasDescriptor = iasDescriptor; 139 this.destDirectory = destDirectory; 140 this.classpath = classpath; 141 this.parser = parser; 142 143 147 List elements = new ArrayList (); 148 if (classpath != null) { 149 StringTokenizer st = new StringTokenizer (classpath, 150 File.pathSeparator); 151 while (st.hasMoreTokens()) { 152 elements.add(st.nextToken()); 153 } 154 classpathElements 155 = (String []) elements.toArray(new String [elements.size()]); 156 } 157 } 158 159 167 public void setRetainSource(boolean retainSource) { 168 this.retainSource = retainSource; 169 } 170 171 177 public void setDebugOutput(boolean debugOutput) { 178 this.debugOutput = debugOutput; 179 } 180 181 190 public void registerDTD(String publicID, String location) { 191 handler.registerDTD(publicID, location); 192 } 193 194 201 public void setIasHomeDir(File iasHomeDir) { 202 this.iasHomeDir = iasHomeDir; 203 } 204 205 214 public Hashtable getEjbFiles() { 215 return ejbFiles; 216 } 217 218 223 public String getDisplayName() { 224 return displayName; 225 } 226 227 232 public String [] getCmpDescriptors() { 233 List returnList = new ArrayList (); 234 235 EjbInfo[] ejbs = handler.getEjbs(); 236 237 for (int i = 0; i < ejbs.length; i++) { 238 List descriptors = (List ) ejbs[i].getCmpDescriptors(); 239 returnList.addAll(descriptors); 240 } 241 242 return (String []) returnList.toArray(new String [returnList.size()]); 243 } 244 245 252 public static void main(String [] args) { 253 File stdDescriptor; 254 File iasDescriptor; 255 File destDirectory = null; 256 String classpath = null; 257 SAXParser parser = null; 258 boolean debug = false; 259 boolean retainSource = false; 260 IPlanetEjbc ejbc; 261 262 if ((args.length < MIN_NUM_ARGS) || (args.length > MAX_NUM_ARGS)) { 263 usage(); 264 return; 265 } 266 267 stdDescriptor = new File (args[args.length - 2]); 268 iasDescriptor = new File (args[args.length - 1]); 269 270 for (int i = 0; i < args.length - 2; i++) { 271 if (args[i].equals("-classpath")) { 272 classpath = args[++i]; 273 } else if (args[i].equals("-d")) { 274 destDirectory = new File (args[++i]); 275 } else if (args[i].equals("-debug")) { 276 debug = true; 277 } else if (args[i].equals("-keepsource")) { 278 retainSource = true; 279 } else { 280 usage(); 281 return; 282 } 283 } 284 285 286 if (classpath == null) { 287 Properties props = System.getProperties(); 288 classpath = props.getProperty("java.class.path"); 289 } 290 291 295 if (destDirectory == null) { 296 Properties props = System.getProperties(); 297 destDirectory = new File (props.getProperty("user.dir")); 298 } 299 300 301 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 302 parserFactory.setValidating(true); 303 try { 304 parser = parserFactory.newSAXParser(); 305 } catch (Exception e) { 306 System.out.println("An exception was generated while trying to "); 308 System.out.println("create a new SAXParser."); 309 e.printStackTrace(); 310 return; 311 } 312 313 314 ejbc = new IPlanetEjbc(stdDescriptor, iasDescriptor, destDirectory, 315 classpath, parser); 316 ejbc.setDebugOutput(debug); 317 ejbc.setRetainSource(retainSource); 318 319 320 try { 321 ejbc.execute(); 322 } catch (IOException e) { 323 System.out.println("An IOException has occurred while reading the " 324 + "XML descriptors (" + e.getMessage() + ")."); 325 return; 326 } catch (SAXException e) { 327 System.out.println("A SAXException has occurred while reading the " 328 + "XML descriptors (" + e.getMessage() + ")."); 329 return; 330 } catch (IPlanetEjbc.EjbcException e) { 331 System.out.println("An error has occurred while executing the ejbc " 332 + "utility (" + e.getMessage() + ")."); 333 return; 334 } 335 } 336 337 340 private static void usage() { 341 System.out.println("java org.apache.tools.ant.taskdefs.optional.ejb.IPlanetEjbc \\"); 342 System.out.println(" [OPTIONS] [EJB 1.1 descriptor] [iAS EJB descriptor]"); 343 System.out.println(""); 344 System.out.println("Where OPTIONS are:"); 345 System.out.println(" -debug -- for additional debugging output"); 346 System.out.println(" -keepsource -- to retain Java source files generated"); 347 System.out.println(" -classpath [classpath] -- classpath used for compilation"); 348 System.out.println(" -d [destination directory] -- directory for compiled classes"); 349 System.out.println(""); 350 System.out.println("If a classpath is not specified, the system classpath"); 351 System.out.println("will be used. If a destination directory is not specified,"); 352 System.out.println("the current working directory will be used (classes will"); 353 System.out.println("still be placed in subfolders which correspond to their"); 354 System.out.println("package name)."); 355 System.out.println(""); 356 System.out.println("The EJB home interface, remote interface, and implementation"); 357 System.out.println("class must be found in the destination directory. In"); 358 System.out.println("addition, the destination will look for the stubs and skeletons"); 359 System.out.println("in the destination directory to ensure they are up to date."); 360 } 361 362 374 public void execute() throws EjbcException, IOException , SAXException { 375 376 checkConfiguration(); 378 EjbInfo[] ejbs = getEjbs(); 380 for (int i = 0; i < ejbs.length; i++) { 381 log("EJBInfo..."); 382 log(ejbs[i].toString()); 383 } 384 385 for (int i = 0; i < ejbs.length; i++) { 386 EjbInfo ejb = ejbs[i]; 387 388 ejb.checkConfiguration(destDirectory); 390 if (ejb.mustBeRecompiled(destDirectory)) { 391 log(ejb.getName() + " must be recompiled using ejbc."); 392 393 String [] arguments = buildArgumentList(ejb); 394 callEjbc(arguments); 395 396 } else { 397 log(ejb.getName() + " is up to date."); 398 } 399 } 400 } 401 402 407 private void callEjbc(String [] arguments) { 408 409 410 StringBuffer args = new StringBuffer (); 411 for (int i = 0; i < arguments.length; i++) { 412 args.append(arguments[i]).append(" "); 413 } 414 415 416 String command; 417 if (iasHomeDir == null) { 418 command = ""; 419 } else { 420 command = iasHomeDir.toString() + File.separator + "bin" 421 + File.separator; 422 } 423 command += "ejbc "; 424 425 log(command + args); 426 427 432 try { 433 Process p = Runtime.getRuntime().exec(command + args); 434 RedirectOutput output = new RedirectOutput(p.getInputStream()); 435 RedirectOutput error = new RedirectOutput(p.getErrorStream()); 436 output.start(); 437 error.start(); 438 p.waitFor(); 439 p.destroy(); 440 } catch (IOException e) { 441 log("An IOException has occurred while trying to execute ejbc."); 442 e.printStackTrace(); 443 } catch (InterruptedException e) { 444 } 446 } 447 448 453 protected void checkConfiguration() throws EjbcException { 454 455 String msg = ""; 456 457 if (stdDescriptor == null) { 458 msg += "A standard XML descriptor file must be specified. "; 459 } 460 if (iasDescriptor == null) { 461 msg += "An iAS-specific XML descriptor file must be specified. "; 462 } 463 if (classpath == null) { 464 msg += "A classpath must be specified. "; 465 } 466 if (parser == null) { 467 msg += "An XML parser must be specified. "; 468 } 469 470 if (destDirectory == null) { 471 msg += "A destination directory must be specified. "; 472 } else if (!destDirectory.exists()) { 473 msg += "The destination directory specified does not exist. "; 474 } else if (!destDirectory.isDirectory()) { 475 msg += "The destination specified is not a directory. "; 476 } 477 478 if (msg.length() > 0) { 479 throw new EjbcException(msg); 480 } 481 } 482 483 494 private EjbInfo[] getEjbs() throws IOException , SAXException { 495 EjbInfo[] ejbs = null; 496 497 501 502 parser.parse(stdDescriptor, handler); 503 parser.parse(iasDescriptor, handler); 504 ejbs = handler.getEjbs(); 505 506 return ejbs; 507 } 508 509 517 private String [] buildArgumentList(EjbInfo ejb) { 518 519 List arguments = new ArrayList (); 520 521 522 523 if (debugOutput) { 524 arguments.add("-debug"); 525 } 526 527 528 if (ejb.getBeantype().equals(STATELESS_SESSION)) { 529 arguments.add("-sl"); 530 } else if (ejb.getBeantype().equals(STATEFUL_SESSION)) { 531 arguments.add("-sf"); 532 } 533 534 if (ejb.getIiop()) { 535 arguments.add("-iiop"); 536 } 537 538 if (ejb.getCmp()) { 539 arguments.add("-cmp"); 540 } 541 542 if (retainSource) { 543 arguments.add("-gs"); 544 } 545 546 if (ejb.getHasession()) { 547 arguments.add("-fo"); 548 } 549 550 551 552 arguments.add("-classpath"); 553 arguments.add(classpath); 554 555 arguments.add("-d"); 556 arguments.add(destDirectory.toString()); 557 558 arguments.add(ejb.getHome().getQualifiedClassName()); 559 arguments.add(ejb.getRemote().getQualifiedClassName()); 560 arguments.add(ejb.getImplementation().getQualifiedClassName()); 561 562 563 return (String []) arguments.toArray(new String [arguments.size()]); 564 } 565 566 572 private void log(String msg) { 573 if (debugOutput) { 574 System.out.println(msg); 575 } 576 } 577 578 579 580 581 582 587 public class EjbcException extends Exception { 588 589 594 public EjbcException(String msg) { 595 super(msg); 596 } 597 } 599 600 609 private class EjbcHandler extends HandlerBase { 610 611 private static final String PUBLICID_EJB11 = 612 "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"; 613 614 private static final String PUBLICID_IPLANET_EJB_60 = 615 "-//Sun Microsystems, Inc.//DTD iAS Enterprise JavaBeans 1.0//EN"; 616 617 private static final String DEFAULT_IAS60_EJB11_DTD_LOCATION = 618 "ejb-jar_1_1.dtd"; 619 620 private static final String DEFAULT_IAS60_DTD_LOCATION = 621 "IASEjb_jar_1_0.dtd"; 622 623 628 private Map resourceDtds = new HashMap (); 629 private Map fileDtds = new HashMap (); 630 631 private Map ejbs = new HashMap (); private EjbInfo currentEjb; private boolean iasDescriptor = false; 635 private String currentLoc = ""; private String currentText; private String ejbType; 639 644 public EjbcHandler() { 645 registerDTD(PUBLICID_EJB11, DEFAULT_IAS60_EJB11_DTD_LOCATION); 646 registerDTD(PUBLICID_IPLANET_EJB_60, DEFAULT_IAS60_DTD_LOCATION); 647 } 648 649 656 public EjbInfo[] getEjbs() { 657 return (EjbInfo[]) ejbs.values().toArray(new EjbInfo[ejbs.size()]); 658 } 659 660 666 public String getDisplayName() { 667 return displayName; 668 } 669 670 682 public void registerDTD(String publicID, String location) { 683 log("Registering: " + location); 684 if ((publicID == null) || (location == null)) { 685 return; 686 } 687 688 if (ClassLoader.getSystemResource(location) != null) { 689 log("Found resource: " + location); 690 resourceDtds.put(publicID, location); 691 } else { 692 File dtdFile = new File (location); 693 if (dtdFile.exists() && dtdFile.isFile()) { 694 log("Found file: " + location); 695 fileDtds.put(publicID, location); 696 } 697 } 698 } 699 700 710 public InputSource resolveEntity(String publicId, String systemId) 711 throws SAXException { 712 InputStream inputStream = null; 713 714 715 try { 716 717 718 719 String location = (String ) resourceDtds.get(publicId); 720 if (location != null) { 721 inputStream 722 = ClassLoader.getSystemResource(location).openStream(); 723 } else { 724 location = (String ) fileDtds.get(publicId); 725 if (location != null) { 726 inputStream = new FileInputStream (location); 727 } 728 } 729 } catch (IOException e) { 730 return super.resolveEntity(publicId, systemId); 731 } 732 733 if (inputStream == null) { 734 return super.resolveEntity(publicId, systemId); 735 } else { 736 return new InputSource (inputStream); 737 } 738 } 739 740 748 public void startElement(String name, AttributeList atts) 749 throws SAXException { 750 751 755 currentLoc += "\\" + name; 756 757 758 currentText = ""; 759 760 if (currentLoc.equals("\\ejb-jar")) { 761 iasDescriptor = false; 762 } else if (currentLoc.equals("\\ias-ejb-jar")) { 763 iasDescriptor = true; 764 } 765 766 if ((name.equals("session")) || (name.equals("entity"))) { 767 ejbType = name; 768 } 769 } 770 771 780 public void characters(char[] ch, int start, int len) 781 throws SAXException { 782 783 &nbs
|