~ubuntu-branches/ubuntu/maverick/ant/maverick

« back to all changes in this revision

Viewing changes to src/main/org/apache/tools/ant/taskdefs/Jar.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2008-09-30 14:47:45 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080930144745-0x8uzivd9t15dua3
Tags: 1.7.1-0ubuntu1
* New upstream version (bug fix release).
  - mainly a bugfix release.
  - has extended support for Java6 features.
  - <script> now has support for JavaFX.
  - release notes: http://apache.linux-mirror.org/ant/README.html
* Remove debian/patches/05_ant-bug433444.patch. Obsoleted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import java.io.ByteArrayInputStream;
22
22
import java.io.ByteArrayOutputStream;
23
23
import java.io.File;
 
24
import java.io.FileInputStream;
24
25
import java.io.FileOutputStream;
25
 
import java.io.FileInputStream;
26
26
import java.io.IOException;
27
27
import java.io.InputStream;
28
 
import java.io.UnsupportedEncodingException;
29
28
import java.io.InputStreamReader;
30
29
import java.io.OutputStreamWriter;
31
30
import java.io.PrintWriter;
32
31
import java.io.Reader;
 
32
import java.io.UnsupportedEncodingException;
33
33
import java.util.ArrayList;
34
34
import java.util.Collections;
35
35
import java.util.Comparator;
42
42
import java.util.Vector;
43
43
import java.util.zip.ZipEntry;
44
44
import java.util.zip.ZipFile;
 
45
 
45
46
import org.apache.tools.ant.BuildException;
46
47
import org.apache.tools.ant.Project;
 
48
import org.apache.tools.ant.taskdefs.Manifest.Section;
47
49
import org.apache.tools.ant.types.EnumeratedAttribute;
48
50
import org.apache.tools.ant.types.Path;
49
51
import org.apache.tools.ant.types.ResourceCollection;
50
52
import org.apache.tools.ant.types.ZipFileSet;
51
53
import org.apache.tools.ant.types.spi.Service;
 
54
import org.apache.tools.ant.util.FileUtils;
52
55
import org.apache.tools.zip.JarMarker;
53
56
import org.apache.tools.zip.ZipExtraField;
54
57
import org.apache.tools.zip.ZipOutputStream;
74
77
 
75
78
    /** merged manifests added through addConfiguredManifest */
76
79
    private Manifest configuredManifest;
 
80
 
77
81
    /** shadow of the above if upToDate check alters the value */
78
82
    private Manifest savedConfiguredManifest;
79
83
 
139
143
     */
140
144
    private Path indexJars;
141
145
 
 
146
    // CheckStyle:LineLength OFF - Link is too long.
 
147
    /**
 
148
     * Strict mode for checking rules of the JAR-Specification.
 
149
     * @see http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
 
150
     */
 
151
    private StrictMode strict = new StrictMode("ignore");
 
152
 
 
153
    // CheckStyle:LineLength ON
 
154
 
142
155
    /**
143
156
     * Extra fields needed to make Solaris recognize the archive as a jar file.
144
157
     *
148
161
        JarMarker.getInstance()
149
162
    };
150
163
 
151
 
    // CheckStyle:VisibilityModifier OFF - bc
152
 
    protected String emptyBehavior = "create";
153
 
    // CheckStyle:VisibilityModifier ON
154
 
 
155
164
    /** constructor */
156
165
    public Jar() {
157
166
        super();
186
195
    }
187
196
 
188
197
    /**
 
198
     * Activate the strict mode. When set to <i>true</i> a BuildException
 
199
     * will be thrown if the Jar-Packaging specification was broken.
 
200
     * @param strict New value of the strict mode.
 
201
     * @since Ant 1.7.1
 
202
     */
 
203
    public void setStrict(StrictMode strict) {
 
204
        this.strict = strict;
 
205
    }
 
206
 
 
207
    /**
189
208
     * Set the destination file.
190
209
     * @param jarFile the destination file
191
210
     * @deprecated since 1.5.x.
267
286
                                     + manifestFile
268
287
                                     + " (" + e.getMessage() + ")", e);
269
288
        } finally {
270
 
            if (isr != null) {
271
 
                try {
272
 
                    isr.close();
273
 
                } catch (IOException e) {
274
 
                    // do nothing
275
 
                }
276
 
            }
 
289
            FileUtils.close(isr);
277
290
        }
278
291
        return newManifest;
279
292
    }
397
410
           service = (Service) serviceIterator.next();
398
411
           //stolen from writeManifest
399
412
           super.zipFile(service.getAsStream(), zOut,
400
 
                         "META-INF/service/" + service.getType(),
 
413
                         "META-INF/services/" + service.getType(),
401
414
                         System.currentTimeMillis(), null,
402
415
                         ZipFileSet.DEFAULT_FILE_MODE);
403
416
        }
460
473
        throws IOException {
461
474
        for (Enumeration e = manifest.getWarnings();
462
475
             e.hasMoreElements();) {
463
 
            log("Manifest warning: " + (String) e.nextElement(),
 
476
            log("Manifest warning: " + e.nextElement(),
464
477
                Project.MSG_WARN);
465
478
        }
466
479
 
471
484
        OutputStreamWriter osw = new OutputStreamWriter(baos, Manifest.JAR_ENCODING);
472
485
        PrintWriter writer = new PrintWriter(osw);
473
486
        manifest.write(writer);
474
 
        writer.flush();
 
487
        writer.close();
475
488
 
476
489
        ByteArrayInputStream bais =
477
490
            new ByteArrayInputStream(baos.toByteArray());
553
566
            }
554
567
        }
555
568
 
556
 
        writer.flush();
 
569
        writer.close();
557
570
        ByteArrayInputStream bais =
558
571
            new ByteArrayInputStream(baos.toByteArray());
559
572
        super.zipFile(bais, zOut, INDEX_NAME, System.currentTimeMillis(), null,
575
588
                           long lastModified, File fromArchive, int mode)
576
589
        throws IOException {
577
590
        if (MANIFEST_NAME.equalsIgnoreCase(vPath))  {
578
 
            if (!doubleFilePass || (doubleFilePass && skipWriting)) {
 
591
            if (!doubleFilePass || skipWriting) {
579
592
                filesetManifest(fromArchive, is);
580
593
            }
581
594
        } else if (INDEX_NAME.equalsIgnoreCase(vPath) && index) {
767
780
                                     getLocation());
768
781
        } finally {
769
782
            // Close the output stream.
770
 
            try {
771
 
                if (zOut != null) {
772
 
                    zOut.close();
773
 
                }
774
 
            } catch (IOException ex) {
775
 
                // Ignore close exception
776
 
            }
 
783
            FileUtils.close(zOut);
777
784
            createEmpty = false;
778
785
        }
779
786
        return true;
787
794
     */
788
795
    protected void cleanUp() {
789
796
        super.cleanUp();
 
797
        checkJarSpec();
790
798
 
791
799
        // we want to save this info if we are going to make another pass
792
 
        if (!doubleFilePass || (doubleFilePass && !skipWriting)) {
 
800
        if (!doubleFilePass || !skipWriting) {
793
801
            manifest = null;
794
802
            configuredManifest = savedConfiguredManifest;
795
803
            filesetManifest = null;
798
806
        rootEntries.removeAllElements();
799
807
    }
800
808
 
 
809
    // CheckStyle:LineLength OFF - Link is too long.
 
810
    /**
 
811
     * Check against packaging spec
 
812
     * @see http://java.sun.com/j2se/1.3/docs/guide/versioning/spec/VersioningSpecification.html#PackageVersioning
 
813
     */
 
814
    // CheckStyle:LineLength ON
 
815
    private void checkJarSpec() {
 
816
        String br = System.getProperty("line.separator");
 
817
        StringBuffer message = new StringBuffer();
 
818
        Section mainSection = (configuredManifest == null)
 
819
                            ? null
 
820
                            : configuredManifest.getMainSection();
 
821
 
 
822
        if (mainSection == null) {
 
823
            message.append("No Implementation-Title set.");
 
824
            message.append("No Implementation-Version set.");
 
825
            message.append("No Implementation-Vendor set.");
 
826
        } else {
 
827
            if (mainSection.getAttribute("Implementation-Title") == null) {
 
828
                message.append("No Implementation-Title set.");
 
829
            }
 
830
            if (mainSection.getAttribute("Implementation-Version") == null) {
 
831
                message.append("No Implementation-Version set.");
 
832
            }
 
833
            if (mainSection.getAttribute("Implementation-Vendor") == null) {
 
834
                message.append("No Implementation-Vendor set.");
 
835
            }
 
836
        }
 
837
 
 
838
        if (message.length() > 0) {
 
839
            message.append(br);
 
840
            message.append("Location: ").append(getLocation());
 
841
            message.append(br);
 
842
            if (strict.getValue().equalsIgnoreCase("fail")) {
 
843
                throw new BuildException(message.toString(), getLocation());
 
844
            } else {
 
845
                log(message.toString(), strict.getLogLevel());
 
846
            }
 
847
        }
 
848
    }
 
849
 
801
850
    /**
802
851
     * reset to default values.
803
852
     *
899
948
     * @return the matching entry, or null if the file is not found
900
949
     * @since Ant 1.6.2
901
950
     */
902
 
    protected static final String findJarName(String fileName,
 
951
    protected static String findJarName(String fileName,
903
952
                                              String[] classpath) {
904
953
        if (classpath == null) {
905
954
            return (new File(fileName)).getName();
946
995
     * @since Ant 1.7
947
996
     * @throws IOException on error
948
997
     */
949
 
    protected static final void grabFilesAndDirs(String file, List dirs,
 
998
    protected static void grabFilesAndDirs(String file, List dirs,
950
999
                                                 List files)
951
1000
        throws IOException {
952
1001
        org.apache.tools.zip.ZipFile zf = null;
982
1031
            }
983
1032
        }
984
1033
    }
 
1034
 
 
1035
    /** The strict enumerated type. */
 
1036
    public static class StrictMode extends EnumeratedAttribute {
 
1037
        /** Public no arg constructor. */
 
1038
        public StrictMode() {
 
1039
        }
 
1040
        /**
 
1041
         * Constructor with an arg.
 
1042
         * @param value the enumerated value as a string.
 
1043
         */
 
1044
        public StrictMode(String value) {
 
1045
            setValue(value);
 
1046
        }
 
1047
        /**
 
1048
         * Get List of valid strings.
 
1049
         * @return the list of values.
 
1050
         */
 
1051
        public String[] getValues() {
 
1052
            return new String[]{"fail", "warn", "ignore"};
 
1053
        }
 
1054
        /**
 
1055
         * @return The log level according to the strict mode.
 
1056
         */
 
1057
        public int getLogLevel() {
 
1058
            return (getValue().equals("ignore")) ? Project.MSG_VERBOSE : Project.MSG_WARN;
 
1059
        }
 
1060
    }
985
1061
}