📄 genericdeploymenttool.java
字号:
* contents the file being added. * @param logicalFilename A String representing the name, including * all relevant path information, that should be stored for the entry * being added. * @throws BuildException if there is a problem. */ protected void addFileToJar(JarOutputStream jStream, File inputFile, String logicalFilename) throws BuildException { FileInputStream iStream = null; try { if (!addedfiles.contains(logicalFilename)) { iStream = new FileInputStream(inputFile); // Create the zip entry and add it to the jar file ZipEntry zipEntry = new ZipEntry(logicalFilename.replace('\\', '/')); jStream.putNextEntry(zipEntry); // Create the file input stream, and buffer everything over // to the jar output stream byte[] byteBuffer = new byte[2 * DEFAULT_BUFFER_SIZE]; int count = 0; do { jStream.write(byteBuffer, 0, count); count = iStream.read(byteBuffer, 0, byteBuffer.length); } while (count != -1); //add it to list of files in jar addedfiles.add(logicalFilename); } } catch (IOException ioe) { log("WARNING: IOException while adding entry " + logicalFilename + " to jarfile from " + inputFile.getPath() + " " + ioe.getClass().getName() + "-" + ioe.getMessage(), Project.MSG_WARN); } finally { // Close up the file input stream for the class file if (iStream != null) { try { iStream.close(); } catch (IOException closeException) { // ignore } } } } /** * Get a descriptionHandler. * @param srcDir the source directory. * @return a handler. */ protected DescriptorHandler getDescriptorHandler(File srcDir) { DescriptorHandler h = new DescriptorHandler(getTask(), srcDir); registerKnownDTDs(h); // register any DTDs supplied by the user for (Iterator i = getConfig().dtdLocations.iterator(); i.hasNext();) { EjbJar.DTDLocation dtdLocation = (EjbJar.DTDLocation) i.next(); h.registerDTD(dtdLocation.getPublicId(), dtdLocation.getLocation()); } return h; } /** * Register the locations of all known DTDs. * * vendor-specific subclasses should override this method to define * the vendor-specific locations of the EJB DTDs * @param handler no used in this class. */ protected void registerKnownDTDs(DescriptorHandler handler) { // none to register for generic } /** {@inheritDoc}. */ public void processDescriptor(String descriptorFileName, SAXParser saxParser) { checkConfiguration(descriptorFileName, saxParser); try { handler = getDescriptorHandler(config.srcDir); // Retrive the files to be added to JAR from EJB descriptor Hashtable ejbFiles = parseEjbFiles(descriptorFileName, saxParser); // Add any support classes specified in the build file addSupportClasses(ejbFiles); // Determine the JAR filename (without filename extension) String baseName = getJarBaseName(descriptorFileName); String ddPrefix = getVendorDDPrefix(baseName, descriptorFileName); File manifestFile = getManifestFile(ddPrefix); if (manifestFile != null) { ejbFiles.put(MANIFEST, manifestFile); } // First the regular deployment descriptor ejbFiles.put(META_DIR + EJB_DD, new File(config.descriptorDir, descriptorFileName)); // now the vendor specific files, if any addVendorFiles(ejbFiles, ddPrefix); // add any dependent files checkAndAddDependants(ejbFiles); // Lastly create File object for the Jar files. If we are using // a flat destination dir, then we need to redefine baseName! if (config.flatDestDir && baseName.length() != 0) { int startName = baseName.lastIndexOf(File.separator); if (startName == -1) { startName = 0; } int endName = baseName.length(); baseName = baseName.substring(startName, endName); } File jarFile = getVendorOutputJarFile(baseName); // Check to see if we need a build and start doing the work! if (needToRebuild(ejbFiles, jarFile)) { // Log that we are going to build... log("building " + jarFile.getName() + " with " + String.valueOf(ejbFiles.size()) + " files", Project.MSG_INFO); // Use helper method to write the jarfile String publicId = getPublicId(); writeJar(baseName, jarFile, ejbFiles, publicId); } else { // Log that the file is up to date... log(jarFile.toString() + " is up to date.", Project.MSG_VERBOSE); } } catch (SAXException se) { String msg = "SAXException while parsing '" + descriptorFileName + "'. This probably indicates badly-formed XML." + " Details: " + se.getMessage(); throw new BuildException(msg, se); } catch (IOException ioe) { String msg = "IOException while parsing'" + descriptorFileName + "'. This probably indicates that the descriptor" + " doesn't exist. Details: " + ioe.getMessage(); throw new BuildException(msg, ioe); } } /** * This method is called as the first step in the processDescriptor method * to allow vendor-specific subclasses to validate the task configuration * prior to processing the descriptor. If the configuration is invalid, * a BuildException should be thrown. * * @param descriptorFileName String representing the file name of an EJB * descriptor to be processed * @param saxParser SAXParser which may be used to parse the XML * descriptor * @throws BuildException if there is a problem. */ protected void checkConfiguration(String descriptorFileName, SAXParser saxParser) throws BuildException { /* * For the GenericDeploymentTool, do nothing. Vendor specific * subclasses should throw a BuildException if the configuration is * invalid for their server. */ } /** * This method returns a list of EJB files found when the specified EJB * descriptor is parsed and processed. * * @param descriptorFileName String representing the file name of an EJB * descriptor to be processed * @param saxParser SAXParser which may be used to parse the XML * descriptor * @return Hashtable of EJB class (and other) files to be * added to the completed JAR file * @throws SAXException Any SAX exception, possibly wrapping another * exception * @throws IOException An IOException from the parser, possibly from a * the byte stream or character stream */ protected Hashtable parseEjbFiles(String descriptorFileName, SAXParser saxParser) throws IOException, SAXException { FileInputStream descriptorStream = null; Hashtable ejbFiles = null; try { /* Parse the ejb deployment descriptor. While it may not * look like much, we use a SAXParser and an inner class to * get hold of all the classfile names for the descriptor. */ descriptorStream = new FileInputStream(new File(config.descriptorDir, descriptorFileName)); saxParser.parse(new InputSource(descriptorStream), handler); ejbFiles = handler.getFiles(); } finally { if (descriptorStream != null) { try { descriptorStream.close(); } catch (IOException closeException) { // ignore } } } return ejbFiles; } /** * Adds any classes the user specifies using <i>support</i> nested elements * to the <code>ejbFiles</code> Hashtable. * * @param ejbFiles Hashtable of EJB classes (and other) files that will be * added to the completed JAR file */ protected void addSupportClasses(Hashtable ejbFiles) { // add in support classes if any Project project = task.getProject(); for (Iterator i = config.supportFileSets.iterator(); i.hasNext();) { FileSet supportFileSet = (FileSet) i.next(); File supportBaseDir = supportFileSet.getDir(project); DirectoryScanner supportScanner = supportFileSet.getDirectoryScanner(project); supportScanner.scan(); String[] supportFiles = supportScanner.getIncludedFiles(); for (int j = 0; j < supportFiles.length; ++j) { ejbFiles.put(supportFiles[j], new File(supportBaseDir, supportFiles[j])); } } } /** * Using the EJB descriptor file name passed from the <code>ejbjar</code> * task, this method returns the "basename" which will be used to name the * completed JAR file. * * @param descriptorFileName String representing the file name of an EJB * descriptor to be processed * @return The "basename" which will be used to name the * completed JAR file */ protected String getJarBaseName(String descriptorFileName) { String baseName = ""; // Work out what the base name is if (config.namingScheme.getValue().equals(EjbJar.NamingScheme.BASEJARNAME)) { String canonicalDescriptor = descriptorFileName.replace('\\', '/'); int index = canonicalDescriptor.lastIndexOf('/'); if (index != -1) { baseName = descriptorFileName.substring(0, index + 1); } baseName += config.baseJarName; } else if (config.namingScheme.getValue().equals(EjbJar.NamingScheme.DESCRIPTOR)) { int lastSeparatorIndex = descriptorFileName.lastIndexOf(File.separator); int endBaseName = -1; if (lastSeparatorIndex != -1) { endBaseName = descriptorFileName.indexOf(config.baseNameTerminator, lastSeparatorIndex); } else { endBaseName = descriptorFileName.indexOf(config.baseNameTerminator); } if (endBaseName != -1) { baseName = descriptorFileName.substring(0, endBaseName); } else { throw new BuildException("Unable to determine jar name " + "from descriptor \"" + descriptorFileName + "\""); } } else if (config.namingScheme.getValue().equals(EjbJar.NamingScheme.DIRECTORY)) { File descriptorFile = new File(config.descriptorDir, descriptorFileName); String path = descriptorFile.getAbsolutePath(); int lastSeparatorIndex = path.lastIndexOf(File.separator); if (lastSeparatorIndex == -1) { throw new BuildException("Unable to determine directory name holding descriptor"); } String dirName = path.substring(0, lastSeparatorIndex); int dirSeparatorIndex = dirName.lastIndexOf(File.separator); if (dirSeparatorIndex != -1) { dirName = dirName.substring(dirSeparatorIndex + 1); } baseName = dirName; } else if (config.namingScheme.getValue().equals(EjbJar.NamingScheme.EJB_NAME)) { baseName = handler.getEjbName(); } return baseName; } /** * Get the prefix for vendor deployment descriptors. * * This will contain the path and the start of the descriptor name, * depending on the naming scheme
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -