Prior to JDK1.3, an "Optional Package" was known as an Extension. + * The specification for this mechanism is available in the JDK1.3 + * documentation in the directory + * $JDK_HOME/docs/guide/extensions/versioning.html. Alternatively it is + * available online at + * http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html.
+ * + * @author Peter Donald + * @ant.task name="extension-display" + */ +public class ExtensionDisplayTask + extends AbstractTask +{ + private final static Resources REZ = + ResourceManager.getPackageResources( ExtensionDisplayTask.class ); + + /** + * The library to display information about. + */ + private File m_file; + + /** + * Filesets specifying all the librarys + * to display information about. + */ + private final Vector m_filesets = new Vector(); + + /** + * The jar library to display information for. + * + * @param file The jar library to display information for. + */ + public void setFile( final File file ) + { + m_file = file; + } + + /** + * Adds a set of files about which library data will be displayed. + * + * @param fileSet a set of files about which library data will be displayed. + */ + public void addFileset( final FileSet fileSet ) + { + m_filesets.addElement( fileSet ); + } + + public void execute() + throws TaskException + { + validate(); + + final LibraryDisplay displayer = new LibraryDisplay(); + // Check if list of files to check has been specified + if( !m_filesets.isEmpty() ) + { + final Iterator iterator = m_filesets.iterator(); + while( iterator.hasNext() ) + { + final FileSet fileSet = (FileSet)iterator.next(); + final DirectoryScanner scanner = ScannerUtil.getDirectoryScanner( fileSet ); + final File basedir = scanner.getBasedir(); + final String[] files = scanner.getIncludedFiles(); + for( int i = 0; i < files.length; i++ ) + { + final File file = new File( basedir, files[ i ] ); + displayer.displayLibrary( file ); + } + } + } + else + { + displayer.displayLibrary( m_file ); + } + } + + /** + * Validate the tasks parameters. + * + * @throws TaskException if invalid parameters found + */ + private void validate() + throws TaskException + { + if( null == m_file && m_filesets.isEmpty() ) + { + final String message = REZ.getString( "extension.missing-file.error" ); + throw new TaskException( message ); + } + if( null != m_file && !m_file.exists() ) + { + final String message = REZ.getString( "extension.file-noexist.error", m_file ); + throw new TaskException( message ); + } + if( null != m_file && !m_file.isFile() ) + { + final String message = REZ.getString( "extension.bad-file.error", m_file ); + throw new TaskException( message ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/extensions/LibraryDisplay.java b/proposal/myrmidon/src/java/org/apache/antlib/extensions/LibraryDisplay.java new file mode 100644 index 000000000..1510dd4e5 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/extensions/LibraryDisplay.java @@ -0,0 +1,164 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.antlib.extensions; + +import java.io.File; +import java.io.IOException; +import java.util.jar.Manifest; +import java.util.jar.JarFile; +import java.text.ParseException; +import java.lang.StringBuffer; +import org.apache.myrmidon.api.TaskException; +import org.apache.avalon.excalibur.extension.Extension; +import org.apache.avalon.excalibur.extension.Specification; + +/** + * Utility class to output the information in a jar relating + * to "Optional Packages" (formely known as "Extensions") + * and Package Specifications. + * + * @author Peter Donald + * @version $Revision$ $Date$ + */ +class LibraryDisplay +{ + /** + * Display the extensions and specifications contained + * within specified file. + * + * @param file the file + * @throws TaskException if fail to read file + */ + public void displayLibrary( final File file ) + throws TaskException + { + final Manifest manifest = getManifest( file ); + final Extension[] available = Extension.getAvailable( manifest ); + final Extension[] required = Extension.getRequired( manifest ); + final Specification[] specifications = getSpecifications( manifest ); + + if( 0 == available.length && + 0 == required.length && + 0 == specifications.length ) + { + return; + } + + final String message = "File: " + file; + final int size = message.length(); + printLine( size ); + System.out.println( message ); + printLine( size ); + if( 0 != available.length ) + { + System.out.println( "Extensions Supported By Library:" ); + for( int i = 0; i < available.length; i++ ) + { + final Extension extension = available[ i ]; + System.out.println( extension.toString() ); + } + } + + if( 0 != required.length ) + { + System.out.println( "Extensions Required By Library:" ); + for( int i = 0; i < required.length; i++ ) + { + final Extension extension = required[ i ]; + System.out.println( extension.toString() ); + } + } + + if( 0 != specifications.length ) + { + System.out.println( "Specifications Supported By Library:" ); + for( int i = 0; i < specifications.length; i++ ) + { + final Specification specification = specifications[ i ]; + displaySpecification( specification ); + } + } + } + + /** + * Print out a line of '-'s equal to specified size. + * + * @param size the number of dashes to printout + */ + private void printLine( final int size ) + { + for( int i = 0; i < size; i++ ) + { + System.out.print( "-" ); + } + System.out.println(); + } + + /** + * Get specifications from manifest. + * + * @param manifest the manifest + * @return the specifications or null if none + * @throws org.apache.myrmidon.api.TaskException if malformed specification sections + */ + private Specification[] getSpecifications( final Manifest manifest ) + throws TaskException + { + try + { + return Specification.getSpecifications( manifest ); + } + catch( final ParseException pe ) + { + throw new TaskException( pe.getMessage(), pe ); + } + } + + /** + * Print out specification details. + * + * @param specification the specification + */ + private void displaySpecification( final Specification specification ) + { + final String[] sections = specification.getSections(); + if( null != sections ) + { + final StringBuffer sb = new StringBuffer( "Sections: " ); + for( int i = 0; i < sections.length; i++ ) + { + sb.append( " " ); + sb.append( sections[ i ] ); + } + System.out.println( sb ); + } + System.out.println( specification.toString() ); + } + + /** + * retrieve manifest for specified file. + * + * @param file the file + * @return the manifest + * @throws org.apache.myrmidon.api.TaskException if errror occurs (file not exist, + * file not a jar, manifest not exist in file) + */ + private Manifest getManifest( final File file ) + throws TaskException + { + try + { + final JarFile jarFile = new JarFile( file ); + return jarFile.getManifest(); + } + catch( final IOException ioe ) + { + throw new TaskException( ioe.getMessage(), ioe ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/extensions/Resources.properties b/proposal/myrmidon/src/java/org/apache/antlib/extensions/Resources.properties new file mode 100644 index 000000000..923da1a2d --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/extensions/Resources.properties @@ -0,0 +1,3 @@ +extension.missing-file.error=File attribute not specified. +extension.file-noexist.error=File "{0}" does not exist. +extension.bad-file.error="{0}" is not a file. \ No newline at end of file diff --git a/proposal/myrmidon/src/samples/sample.ant b/proposal/myrmidon/src/samples/sample.ant index 110a763e3..95804f483 100644 --- a/proposal/myrmidon/src/samples/sample.ant +++ b/proposal/myrmidon/src/samples/sample.ant @@ -323,4 +323,14 @@ Legal: +