📄 iplanetejbc.java
字号:
checkConfiguration(); // Throws EjbcException if unsuccessful EjbInfo[] ejbs = getEjbs(); // Returns list of EJBs for processing for (int i = 0; i < ejbs.length; i++) { log("EJBInfo..."); log(ejbs[i].toString()); } for (int i = 0; i < ejbs.length; i++) { EjbInfo ejb = ejbs[i]; ejb.checkConfiguration(destDirectory); // Throws EjbcException if (ejb.mustBeRecompiled(destDirectory)) { log(ejb.getName() + " must be recompiled using ejbc."); String[] arguments = buildArgumentList(ejb); callEjbc(arguments); } else { log(ejb.getName() + " is up to date."); } } } /** * Executes the iPlanet Application Server ejbc command-line utility. * * @param arguments Command line arguments to be passed to the ejbc utility. */ private void callEjbc(String[] arguments) { /* Concatenate all of the command line arguments into a single String */ StringBuffer args = new StringBuffer(); for (int i = 0; i < arguments.length; i++) { args.append(arguments[i]).append(" "); } /* If an iAS home directory is specified, prepend it to the commmand */ String command; if (iasHomeDir == null) { command = ""; } else { command = iasHomeDir.toString() + File.separator + "bin" + File.separator; } command += "ejbc "; log(command + args); /* * Use the Runtime object to execute an external command. Use the * RedirectOutput inner class to direct the standard and error output * from the command to the JRE's standard output */ try { Process p = Runtime.getRuntime().exec(command + args); RedirectOutput output = new RedirectOutput(p.getInputStream()); RedirectOutput error = new RedirectOutput(p.getErrorStream()); output.start(); error.start(); p.waitFor(); p.destroy(); } catch (IOException e) { log("An IOException has occurred while trying to execute ejbc."); e.printStackTrace(); } catch (InterruptedException e) { // Do nothing } } /** * Verifies that the user selections are valid. * * @throws EjbcException If the user selections are invalid. */ protected void checkConfiguration() throws EjbcException { String msg = ""; if (stdDescriptor == null) { msg += "A standard XML descriptor file must be specified. "; } if (iasDescriptor == null) { msg += "An iAS-specific XML descriptor file must be specified. "; } if (classpath == null) { msg += "A classpath must be specified. "; } if (parser == null) { msg += "An XML parser must be specified. "; } if (destDirectory == null) { msg += "A destination directory must be specified. "; } else if (!destDirectory.exists()) { msg += "The destination directory specified does not exist. "; } else if (!destDirectory.isDirectory()) { msg += "The destination specified is not a directory. "; } if (msg.length() > 0) { throw new EjbcException(msg); } } /** * Parses the EJB descriptors and returns a list of EJBs which may need to * be compiled. * * @return An array of objects which describe the EJBs to be * processed. * @throws IOException If the parser encounters a problem reading the XML * files * @throws SAXException If the parser encounters a problem processing the * XML descriptor (it may wrap another exception) */ private EjbInfo[] getEjbs() throws IOException, SAXException { EjbInfo[] ejbs = null; /* * The EJB information is gathered from the standard XML EJB descriptor * and the iAS-specific XML EJB descriptor using a SAX parser. */ parser.parse(stdDescriptor, handler); parser.parse(iasDescriptor, handler); ejbs = handler.getEjbs(); return ejbs; } /** * Based on this object's instance variables as well as the EJB to be * processed, the correct flags and parameters are set for the ejbc * command-line utility. * @param ejb The EJB for which stubs and skeletons will be compiled. * @return An array of Strings which are the command-line parameters for * for the ejbc utility. */ private String[] buildArgumentList(EjbInfo ejb) { List arguments = new ArrayList(); /* OPTIONAL COMMAND LINE PARAMETERS */ if (debugOutput) { arguments.add("-debug"); } /* No beantype flag is needed for an entity bean */ if (ejb.getBeantype().equals(STATELESS_SESSION)) { arguments.add("-sl"); } else if (ejb.getBeantype().equals(STATEFUL_SESSION)) { arguments.add("-sf"); } if (ejb.getIiop()) { arguments.add("-iiop"); } if (ejb.getCmp()) { arguments.add("-cmp"); } if (retainSource) { arguments.add("-gs"); } if (ejb.getHasession()) { arguments.add("-fo"); } /* REQUIRED COMMAND LINE PARAMETERS */ arguments.add("-classpath"); arguments.add(classpath); arguments.add("-d"); arguments.add(destDirectory.toString()); arguments.add(ejb.getHome().getQualifiedClassName()); arguments.add(ejb.getRemote().getQualifiedClassName()); arguments.add(ejb.getImplementation().getQualifiedClassName()); /* Convert the List into an Array and return it */ return (String[]) arguments.toArray(new String[arguments.size()]); } /** * Convenience method used to print messages to the user if debugging * messages are enabled. * * @param msg The String to print to standard output. */ private void log(String msg) { if (debugOutput) { System.out.println(msg); } } /* Inner classes follow */ /** * This inner class is used to signal any problems during the execution of * the ejbc compiler. * */ public class EjbcException extends Exception { /** * Constructs an exception with the given descriptive message. * * @param msg Description of the exception which has occurred. */ public EjbcException(String msg) { super(msg); } } // End of EjbcException inner class /** * This inner class is an XML document handler that can be used to parse EJB * descriptors (both the standard EJB descriptor as well as the iAS-specific * descriptor that stores additional values for iAS). Once the descriptors * have been processed, the list of EJBs found can be obtained by calling * the <code>getEjbs()</code> method. * * @see IPlanetEjbc.EjbInfo */ private class EjbcHandler extends HandlerBase { /** EJB 1.1 ID */ private static final String PUBLICID_EJB11 = "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN"; /** IPlanet ID */ private static final String PUBLICID_IPLANET_EJB_60 = "-//Sun Microsystems, Inc.//DTD iAS Enterprise JavaBeans 1.0//EN"; /** EJB 1.1 location */ private static final String DEFAULT_IAS60_EJB11_DTD_LOCATION = "ejb-jar_1_1.dtd"; /** IAS60 location */ private static final String DEFAULT_IAS60_DTD_LOCATION = "IASEjb_jar_1_0.dtd"; /* * Two Maps are used to track local DTDs that will be used in case the * remote copies of these DTDs cannot be accessed. The key for the Map * is the DTDs public ID and the value is the local location for the DTD */ private Map resourceDtds = new HashMap(); private Map fileDtds = new HashMap(); private Map ejbs = new HashMap(); // List of EJBs found in XML private EjbInfo currentEjb; // One item within the Map private boolean iasDescriptor = false; // Is doc iAS or EJB descriptor private String currentLoc = ""; // Tracks current element private String currentText; // Tracks current text data private String ejbType; // "session" or "entity" /** * Constructs a new instance of the handler and registers local copies * of the standard EJB 1.1 descriptor DTD as well as iAS's EJB * descriptor DTD. */ public EjbcHandler() { registerDTD(PUBLICID_EJB11, DEFAULT_IAS60_EJB11_DTD_LOCATION); registerDTD(PUBLICID_IPLANET_EJB_60, DEFAULT_IAS60_DTD_LOCATION); } /** * Returns the list of EJB objects found during the processing of the * standard EJB 1.1 descriptor and iAS-specific EJB descriptor. * * @return An array of EJBs which were found during the descriptor * parsing. */ public EjbInfo[] getEjbs() { return (EjbInfo[]) ejbs.values().toArray(new EjbInfo[ejbs.size()]); } /** * Returns the value of the display-name element found in the standard * EJB 1.1 descriptor. * * @return String display-name value. */ public String getDisplayName() { return displayName; } /** * Registers a local DTD that will be used when parsing an EJB * descriptor. When the DTD's public identifier is found in an XML * document, the parser will reference the local DTD rather than the * remote DTD. This enables XML documents to be processed even when the * public DTD isn't available. * * @param publicID The DTD's public identifier. * @param location The location of the local DTD copy -- the location * may either be a resource found on the classpath or a * local file. */ public void registerDTD(String publicID, String location) { log("Registering: " + location); if ((publicID == null) || (location == null)) { return; } if (ClassLoader.getSystemResource(location) != null) { log("Found resource: " + location); resourceDtds.put(publicID, location); } else { File dtdFile = new File(location); if (dtdFile.exists() && dtdFile.isFile()) { log("Found file: " + location); fileDtds.put(publicID, location); } } } /** * Resolves an external entity found during XML processing. If a public * ID is found that has been registered with the handler, an <code> * InputSource</code> will be returned which refers to the local copy. * If the public ID hasn't been registered or if an error occurs, the * superclass implementation is used. * * @param publicId The DTD's public identifier. * @param systemId The location of the DTD, as found in the XML document. */ public InputSource resolveEntity(String publicId, String systemId) throws SAXException { InputStream inputStream = null; try { /* Search the resource Map and (if not found) file Map */ String location = (String) resourceDtds.get(publicId); if (location != null) { inputStream = ClassLoader.getSystemResource(location).openStream(); } else { location = (String) fileDtds.get(publicId); if (location != null) { inputStream = new FileInputStream(location); } } } catch (IOException e) { return super.resolveEntity(publicId, systemId); } if (inputStream == null) { return super.resolveEntity(publicId, systemId); } else { return new InputSource(inputStream); } } /** * Receive notification that the start of an XML element has been found. * * @param name String name of the element found. * @param atts AttributeList of the attributes included with the element * (if any). * @throws SAXException If the parser cannot process the document. */ public void startElement(String name, AttributeList atts)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -