aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/ant/apache-ant-1.9.6/src/main/org/apache/tools/ant/taskdefs/optional/extension/JarLibManifestTask.java
blob: 5afc57f17e9abba1ea2b75eb600dc32c8a61e821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 *  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.extension;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.jar.Attributes;
import java.util.jar.Manifest;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.MagicNames;
import org.apache.tools.ant.Project;
import 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.
 *
 * <p>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 <a href="http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html">
 * http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html</a>.</p>
 *
 * @ant.task name="jarlib-manifest"
 */
public final class JarLibManifestTask extends Task {
    /**
     * Version of manifest spec that task generates.
     */
    private static final String MANIFEST_VERSION = "1.0";

    /**
     * "Created-By" string used when creating manifest.
     */
    private static final String CREATED_BY = "Created-By";

    /**
     * The library to display information about.
     */
    private File destFile;

    /**
     * The extension supported by this library (if any).
     */
    private Extension extension;

    /**
     * ExtensionAdapter objects representing
     * dependencies required by library.
     */
    private final ArrayList dependencies = new ArrayList();

    /**
     * ExtensionAdapter objects representing optional
     * dependencies required by library.
     */
    private final ArrayList optionals = new ArrayList();

    /**
     * Extra attributes the user specifies for main section
     * in manifest.
     */
    private final ArrayList extraAttributes = new ArrayList();

    /**
     * The location where generated manifest is placed.
     *
     * @param destFile The location where generated manifest is placed.
     */
    public void setDestfile(final File destFile) {
        this.destFile = destFile;
    }

    /**
     * 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.
     */
    public void addConfiguredExtension(final ExtensionAdapter extensionAdapter)
            throws BuildException {
        if (null != extension) {
            throw new BuildException("Can not have multiple extensions defined in one library.");
        }
        extension = extensionAdapter.toExtension();
    }

    /**
     * Adds a set of extensions that this library requires.
     *
     * @param extensionSet a set of extensions that this library requires.
     */
    public void addConfiguredDepends(final ExtensionSet extensionSet) {
        dependencies.add(extensionSet);
    }

    /**
     * Adds a set of extensions that this library optionally requires.
     *
     * @param extensionSet a set of extensions that this library optionally requires.
     */
    public void addConfiguredOptions(final ExtensionSet extensionSet) {
        optionals.add(extensionSet);
    }

    /**
     * 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.
     */
    public void addConfiguredAttribute(final ExtraAttribute attribute) {
        extraAttributes.add(attribute);
    }

    /**
     * Execute the task.
     *
     * @throws BuildException if the task fails.
     */
    public void execute() throws BuildException {
        validate();

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

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

        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 {
            log("Generating manifest " + destFile.getAbsoluteFile(), Project.MSG_INFO);
            writeManifest(manifest);
        } catch (final IOException ioe) {
            throw new BuildException(ioe.getMessage(), ioe);
        }
    }

    /**
     * Validate the tasks parameters.
     *
     * @throws BuildException if invalid parameters found
     */
    private void validate() throws BuildException {
        if (null == destFile) {
            throw new BuildException("Destfile attribute not specified.");
        }
        if (destFile.exists() && !destFile.isFile()) {
            throw new BuildException(destFile + " is not a file.");
        }
    }

    /**
     * Add any extra attributes to the manifest.
     *
     * @param attributes the manifest section to write
     *        attributes to
     */
    private void appendExtraAttributes(final Attributes attributes) {
        final Iterator iterator = extraAttributes.iterator();
        while (iterator.hasNext()) {
            final ExtraAttribute attribute =
                (ExtraAttribute) iterator.next();
            attributes.putValue(attribute.getName(),
                                 attribute.getValue());
        }
    }

    /**
     * Write out manifest to destfile.
     *
     * @param manifest the manifest
     * @throws IOException if error writing file
     */
    private void writeManifest(final Manifest manifest) throws IOException {
        FileOutputStream output = null;
        try {
            output = new FileOutputStream(destFile);
            manifest.write(output);
            output.flush();
        } finally {
            if (null != output) {
                try {
                    output.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }

    /**
     * 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
     */
    private void appendLibraryList(final Attributes attributes, final String listPrefix,
            final ArrayList extensions) throws BuildException {
        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);
        }
    }

    /**
     * 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 as well.
     *
     * @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
     */
    private void appendExtensionList(final Attributes attributes,
            final Attributes.Name extensionKey, final String listPrefix, final int size) {
        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());
    }

    /**
     * Convert a list of ExtensionSet objects to extensions.
     *
     * @param extensionSets the list of ExtensionSets to add to list
     * @throws BuildException if an error occurs
     */
    private ArrayList toExtensions(final ArrayList extensionSets) throws BuildException {
        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;
    }
}