FileDocCategorySizeDatePackage
JarLibManifestTask.javaAPI DocApache Ant 1.7010897Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs.optional.extension

JarLibManifestTask

public final class JarLibManifestTask extends org.apache.tools.ant.Task
Generates a manifest that declares all the dependencies. The dependencies are determined by looking in the specified path and searching for Extension / "Optional Package" specifications in the manifests of the jars.

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.

ant.task
name="jarlib-manifest"

Fields Summary
private static final String
MANIFEST_VERSION
Version of manifest spec that task generates.
private static final String
CREATED_BY
"Created-By" string used when creating manifest.
private File
destFile
The library to display information about.
private Extension
extension
The extension supported by this library (if any).
private final ArrayList
dependencies
ExtensionAdapter objects representing dependencies required by library.
private final ArrayList
optionals
ExtensionAdapter objects representing optional dependencies required by library.
private final ArrayList
extraAttributes
Extra attributes the user specifies for main section in manifest.
Constructors Summary
Methods Summary
public voidaddConfiguredAttribute(ExtraAttribute attribute)
Adds an attribute that is to be put in main section of manifest.

param
attribute an attribute that is to be put in main section of manifest.

        extraAttributes.add(attribute);
    
public voidaddConfiguredDepends(ExtensionSet extensionSet)
Adds a set of extensions that this library requires.

param
extensionSet a set of extensions that this library requires.

        dependencies.add(extensionSet);
    
public voidaddConfiguredExtension(ExtensionAdapter extensionAdapter)
Adds an extension that this library implements.

param
extensionAdapter an extension that this library implements.
throws
BuildException if there is multiple extensions detected in the library.

        if (null != extension) {
            final String message =
                "Can not have multiple extensions defined in one library.";
            throw new BuildException(message);
        }
        extension = extensionAdapter.toExtension();
    
public voidaddConfiguredOptions(ExtensionSet extensionSet)
Adds a set of extensions that this library optionally requires.

param
extensionSet a set of extensions that this library optionally requires.

        optionals.add(extensionSet);
    
private voidappendExtensionList(java.util.jar.Attributes attributes, java.util.jar.Attributes$Name extensionKey, java.lang.String listPrefix, int size)
Append an attribute such as "Extension-List: lib0 lib1 lib2" using specified prefix and counting up to specified size. Also use specified extensionKey so that can generate list of optional dependencies aswell.

param
size the number of librarys to list
param
listPrefix the prefix for all librarys
param
attributes the attributes to add key-value to
param
extensionKey the key to use

        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size; i++) {
            sb.append(listPrefix);
            sb.append(i);
            sb.append(' ");
        }

        //add in something like
        //"Extension-List: javahelp java3d"
        attributes.put(extensionKey, sb.toString());
    
private voidappendExtraAttributes(java.util.jar.Attributes attributes)
Add any extra attributes to the manifest.

param
attributes the manifest section to write attributes to

        final Iterator iterator = extraAttributes.iterator();
        while (iterator.hasNext()) {
            final ExtraAttribute attribute =
                (ExtraAttribute) iterator.next();
            attributes.putValue(attribute.getName(),
                                 attribute.getValue());
        }
    
private voidappendLibraryList(java.util.jar.Attributes attributes, java.lang.String listPrefix, java.util.ArrayList extensions)
Append specified extensions to specified attributes. Use the extensionKey to list the extensions, usually "Extension-List:" for required dependencies and "Optional-Extension-List:" for optional dependencies. NOTE: "Optional" dependencies are not part of the specification.

param
attributes the attributes to add extensions to
param
extensions the list of extensions
throws
BuildException if an error occurs

        final int size = extensions.size();
        for (int i = 0; i < size; i++) {
            final Extension ext = (Extension) extensions.get(i);
            final String prefix = listPrefix + i + "-";
            Extension.addExtension(ext, prefix, attributes);
        }
    
public voidexecute()
Execute the task.

throws
BuildException if the task fails.

        validate();

        final Manifest manifest = new Manifest();
        final Attributes attributes = manifest.getMainAttributes();

        attributes.put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION);
        final String createdBy = "Apache Ant " + getProject().getProperty(MagicNames.ANT_VERSION);
        attributes.putValue(CREATED_BY, createdBy);

        appendExtraAttributes(attributes);

        if (null != extension) {
            Extension.addExtension(extension, attributes);
        }

        //Add all the dependency data to manifest for dependencies
        final ArrayList depends = toExtensions(dependencies);
        appendExtensionList(attributes,
                             Extension.EXTENSION_LIST,
                             "lib",
                             depends.size());
        appendLibraryList(attributes, "lib", depends);

        //Add all the dependency data to manifest for "optional"
        //dependencies
        final ArrayList option = toExtensions(optionals);
        appendExtensionList(attributes,
                             Extension.OPTIONAL_EXTENSION_LIST,
                             "opt",
                             option.size());
        appendLibraryList(attributes, "opt", option);

        try {
            final String message = "Generating manifest " + destFile.getAbsoluteFile();
            log(message, Project.MSG_INFO);
            writeManifest(manifest);
        } catch (final IOException ioe) {
            throw new BuildException(ioe.getMessage(), ioe);
        }
    
public voidsetDestfile(java.io.File destFile)
The location where generated manifest is placed.

param
destFile The location where generated manifest is placed.


                         
         
        this.destFile = destFile;
    
private java.util.ArrayListtoExtensions(java.util.ArrayList extensionSets)
Convert a list of ExtensionSet objects to extensions.

param
extensionSets the list of ExtensionSets to add to list
throws
BuildException if an error occurs

        final ArrayList results = new ArrayList();

        final int size = extensionSets.size();
        for (int i = 0; i < size; i++) {
            final ExtensionSet set = (ExtensionSet) extensionSets.get(i);
            final Extension[] extensions = set.toExtensions(getProject());
            for (int j = 0; j < extensions.length; j++) {
                results.add(extensions[ j ]);
            }
        }

        return results;
    
private voidvalidate()
Validate the tasks parameters.

throws
BuildException if invalid parameters found

        if (null == destFile) {
            final String message = "Destfile attribute not specified.";
            throw new BuildException(message);
        }
        if (destFile.exists() && !destFile.isFile()) {
            final String message = destFile + " is not a file.";
            throw new BuildException(message);
        }
    
private voidwriteManifest(java.util.jar.Manifest manifest)
Write out manifest to destfile.

param
manifest the manifest
throws
IOException if error writing file

        FileOutputStream output = null;
        try {
            output = new FileOutputStream(destFile);
            manifest.write(output);
            output.flush();
        } finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }