📄 iplanetejbc.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.ejb;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.StringTokenizer;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.AttributeList;import org.xml.sax.HandlerBase;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * Compiles EJB stubs and skeletons for the iPlanet Application * Server (iAS). The class will read a standard EJB descriptor (as well as an * EJB descriptor specific to iPlanet Application Server) to identify one or * more EJBs to process. It will search for EJB "source" classes (the remote; * interface, home interface, and EJB implementation class) and the EJB stubs * and skeletons in the specified destination directory. Only if the stubs and * skeletons cannot be found or if they're out of date will the iPlanet * Application Server ejbc utility be run. * <p> * Because this class (and it's assorted inner classes) may be bundled into the * iPlanet Application Server distribution at some point (and removed from the * Ant distribution), the class has been written to be independent of all * Ant-specific classes. It is also for this reason (and to avoid cluttering * the Apache Ant source files) that this utility has been packaged into a * single source file. * <p> * For more information on Ant Tasks for iPlanet Application Server, see the * <code>IPlanetDeploymentTool</code> and <code>IPlanetEjbcTask</code> classes. * * @see IPlanetDeploymentTool * @see IPlanetEjbcTask * @ant.task ignore="true" */public class IPlanetEjbc { private static final int MIN_NUM_ARGS = 2; private static final int MAX_NUM_ARGS = 8; private static final int NUM_CLASSES_WITH_IIOP = 15; private static final int NUM_CLASSES_WITHOUT_IIOP = 9; /* Constants used for the "beantype" attribute */ private static final String ENTITY_BEAN = "entity"; private static final String STATELESS_SESSION = "stateless"; private static final String STATEFUL_SESSION = "stateful"; /* Filenames of the standard EJB descriptor and the iAS-specific descriptor */ private File stdDescriptor; private File iasDescriptor; /* * Directory where "source" EJB files are stored and where stubs and * skeletons will also be written. */ private File destDirectory; /* Classpath used when the iAS ejbc is called */ private String classpath; private String[] classpathElements; /* Options passed to the iAS ejbc */ private boolean retainSource = false; private boolean debugOutput = false; /* iAS installation directory (used if ejbc isn't on user's PATH) */ private File iasHomeDir; /* Parser and handler used to process both EJB descriptor files */ private SAXParser parser; private EjbcHandler handler = new EjbcHandler(); /* * This Hashtable maintains a list of EJB class files processed by the ejbc * utility (both "source" class files as well as stubs and skeletons). The * key for the Hashtable is a String representing the path to the class file * (relative to the destination directory). The value for the Hashtable is * a File object which reference the actual class file. */ private Hashtable ejbFiles = new Hashtable(); /* Value of the display-name element read from the standard EJB descriptor */ private String displayName; /** * Constructs an instance which may be used to process EJB descriptors and * generate EJB stubs and skeletons, if needed. * * @param stdDescriptor File referencing a standard EJB descriptor. * @param iasDescriptor File referencing an iAS-specific EJB descriptor. * @param destDirectory File referencing the base directory where both * EJB "source" files are found and where stubs and * skeletons will be written. * @param classpath String representation of the classpath to be used * by the iAS ejbc utility. * @param parser SAXParser to be used to process both of the EJB * descriptors. * @todo classpathElements is not needed here, its never used * (at least IDEA tells me so! :) */ public IPlanetEjbc(File stdDescriptor, File iasDescriptor, File destDirectory, String classpath, SAXParser parser) { this.stdDescriptor = stdDescriptor; this.iasDescriptor = iasDescriptor; this.destDirectory = destDirectory; this.classpath = classpath; this.parser = parser; /* * Parse the classpath into it's individual elements and store the * results in the "classpathElements" instance variable. */ List elements = new ArrayList(); if (classpath != null) { StringTokenizer st = new StringTokenizer(classpath, File.pathSeparator); while (st.hasMoreTokens()) { elements.add(st.nextToken()); } classpathElements = (String[]) elements.toArray(new String[elements.size()]); } } /** * If true, the Java source files which are generated by the * ejbc process are retained. * * @param retainSource A boolean indicating if the Java source files for * the stubs and skeletons should be retained. * @todo This is not documented in the HTML. On purpose? */ public void setRetainSource(boolean retainSource) { this.retainSource = retainSource; } /** * If true, enables debugging output when ejbc is executed. * * @param debugOutput A boolean indicating if debugging output should be * generated */ public void setDebugOutput(boolean debugOutput) { this.debugOutput = debugOutput; } /** * Registers the location of a local DTD file or resource. By registering * a local DTD, EJB descriptors can be parsed even when the remote servers * which contain the "public" DTDs cannot be accessed. * * @param publicID The public DTD identifier found in an XML document. * @param location The file or resource name for the appropriate DTD stored * on the local machine. */ public void registerDTD(String publicID, String location) { handler.registerDTD(publicID, location); } /** * May be used to specify the "home" directory for this iAS installation. * The directory specified should typically be * <code>[install-location]/iplanet/ias6/ias</code>. * * @param iasHomeDir The home directory for the user's iAS installation. */ public void setIasHomeDir(File iasHomeDir) { this.iasHomeDir = iasHomeDir; } /** * Returns a Hashtable which contains a list of EJB class files processed by * the ejbc utility (both "source" class files as well as stubs and * skeletons). The key for the Hashtable is a String representing the path * to the class file (relative to the destination directory). The value for * the Hashtable is a File object which reference the actual class file. * * @return The list of EJB files processed by the ejbc utility. */ public Hashtable getEjbFiles() { return ejbFiles; } /** * Returns the display-name element read from the standard EJB descriptor. * * @return The EJB-JAR display name. */ public String getDisplayName() { return displayName; } /** * Returns the list of CMP descriptors referenced in the EJB descriptors. * * @return An array of CMP descriptors. */ public String[] getCmpDescriptors() { List returnList = new ArrayList(); EjbInfo[] ejbs = handler.getEjbs(); for (int i = 0; i < ejbs.length; i++) { List descriptors = (List) ejbs[i].getCmpDescriptors(); returnList.addAll(descriptors); } return (String[]) returnList.toArray(new String[returnList.size()]); } /** * Main application method for the iPlanet Application Server ejbc utility. * If the application is run with no commandline arguments, a usage * statement is printed for the user. * * @param args The commandline arguments passed to the application. */ public static void main(String[] args) { File stdDescriptor; File iasDescriptor; File destDirectory = null; String classpath = null; SAXParser parser = null; boolean debug = false; boolean retainSource = false; IPlanetEjbc ejbc; if ((args.length < MIN_NUM_ARGS) || (args.length > MAX_NUM_ARGS)) { usage(); return; } stdDescriptor = new File(args[args.length - 2]); iasDescriptor = new File(args[args.length - 1]); for (int i = 0; i < args.length - 2; i++) { if (args[i].equals("-classpath")) { classpath = args[++i]; } else if (args[i].equals("-d")) { destDirectory = new File(args[++i]); } else if (args[i].equals("-debug")) { debug = true; } else if (args[i].equals("-keepsource")) { retainSource = true; } else { usage(); return; } } /* If the -classpath flag isn't specified, use the system classpath */ if (classpath == null) { Properties props = System.getProperties(); classpath = props.getProperty("java.class.path"); } /* * If the -d flag isn't specified, use the working directory as the * destination directory */ if (destDirectory == null) { Properties props = System.getProperties(); destDirectory = new File(props.getProperty("user.dir")); } /* Construct a SAXParser used to process the descriptors */ SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(true); try { parser = parserFactory.newSAXParser(); } catch (Exception e) { // SAXException or ParserConfigurationException may be thrown System.out.println("An exception was generated while trying to "); System.out.println("create a new SAXParser."); e.printStackTrace(); return; } /* Build and populate an instance of the ejbc utility */ ejbc = new IPlanetEjbc(stdDescriptor, iasDescriptor, destDirectory, classpath, parser); ejbc.setDebugOutput(debug); ejbc.setRetainSource(retainSource); /* Execute the ejbc utility -- stubs/skeletons are rebuilt, if needed */ try { ejbc.execute(); } catch (IOException e) { System.out.println("An IOException has occurred while reading the " + "XML descriptors (" + e.getMessage() + ")."); return; } catch (SAXException e) { System.out.println("A SAXException has occurred while reading the " + "XML descriptors (" + e.getMessage() + ")."); return; } catch (IPlanetEjbc.EjbcException e) { System.out.println("An error has occurred while executing the ejbc " + "utility (" + e.getMessage() + ")."); return; } } /** * Print a usage statement. */ private static void usage() { System.out.println("java org.apache.tools.ant.taskdefs.optional.ejb.IPlanetEjbc \\"); System.out.println(" [OPTIONS] [EJB 1.1 descriptor] [iAS EJB descriptor]"); System.out.println(""); System.out.println("Where OPTIONS are:"); System.out.println(" -debug -- for additional debugging output"); System.out.println(" -keepsource -- to retain Java source files generated"); System.out.println(" -classpath [classpath] -- classpath used for compilation"); System.out.println(" -d [destination directory] -- directory for compiled classes"); System.out.println(""); System.out.println("If a classpath is not specified, the system classpath"); System.out.println("will be used. If a destination directory is not specified,"); System.out.println("the current working directory will be used (classes will"); System.out.println("still be placed in subfolders which correspond to their"); System.out.println("package name)."); System.out.println(""); System.out.println("The EJB home interface, remote interface, and implementation"); System.out.println("class must be found in the destination directory. In"); System.out.println("addition, the destination will look for the stubs and skeletons"); System.out.println("in the destination directory to ensure they are up to date."); } /** * Compiles the stub and skeletons for the specified EJBs, if they need to * be updated. * * @throws EjbcException If the ejbc utility cannot be correctly configured * or if one or more of the EJB "source" classes * cannot be found in the destination directory * @throws IOException If the parser encounters a problem reading the XML * file * @throws SAXException If the parser encounters a problem processing the * XML descriptor (it may wrap another exception) */ public void execute() throws EjbcException, IOException, SAXException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -