jdk/src/share/classes/sun/misc/JarIndex.java
author alanb
Tue, 08 Feb 2011 19:31:44 +0000
changeset 8189 5fd29d2cbc4b
parent 8169 0dc88a086893
child 8387 f0fa7bbf889e
permissions -rw-r--r--
4421494: infinite loop while parsing double literal Reviewed-by: darcy, alanb Contributed-by: dmitry.nadezhin@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class is used to maintain mappings from packages, classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * and resources to their enclosing JAR files. Mappings are kept
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * at the package level except for class or resource files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * are located at the root directory. URLClassLoader uses the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * information to determine where to fetch an extension class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * resource from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Zhenghua Li
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class JarIndex {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * The hash map that maintains mappings from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * package/classe/resource to jar file list(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private HashMap indexMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The hash map that maintains mappings from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * jar file to package/class/resource lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private HashMap jarMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * An ordered list of jar file names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private String[] jarFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The index file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String INDEX_NAME = "META-INF/INDEX.LIST";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    70
     * true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    71
     * If true, the names of the files in META-INF, and its subdirectories, will
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    72
     * be added to the index. Otherwise, just the directory names are added.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    73
     */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    74
    private static final boolean metaInfFilenames =
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    75
        "true".equals(System.getProperty("sun.misc.JarIndex.metaInfFilenames"));
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    76
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
    77
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Constructs a new, empty jar index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public JarIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        indexMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        jarMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Constructs a new index from the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param is the input stream containing the index data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public JarIndex(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        read(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Constructs a new index for the specified list of jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param files the list of jar files to construct the index from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public JarIndex(String[] files) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        this();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        this.jarFiles = files;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        parseJars(files);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Returns the jar index, or <code>null</code> if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param jar the JAR file to get the index from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IOException if an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        JarIndex index = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* If metaIndex is not null, check the meta index to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
           if META-INF/INDEX.LIST is contained in jar file or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (metaIndex != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            !metaIndex.mayContain(INDEX_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        JarEntry e = jar.getJarEntry(INDEX_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        // if found, then load the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            index = new JarIndex(jar.getInputStream(e));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the jar files that are defined in this index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public String[] getJarFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return jarFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Add the key, value pair to the hashmap, the value will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * be put in a linked list which is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private void addToList(String key, String value, HashMap t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        LinkedList list = (LinkedList)t.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            list = new LinkedList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            list.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            t.put(key, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (!list.contains(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            list.add(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns the list of jar files that are mapped to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param fileName the key of the mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public LinkedList get(String fileName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        LinkedList jarFiles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if ((jarFiles = (LinkedList)indexMap.get(fileName)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            /* try the package name again */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if((pos = fileName.lastIndexOf("/")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                jarFiles = (LinkedList)indexMap.get(fileName.substring(0, pos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return jarFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Add the mapping from the specified file to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * jar file. If there were no mapping for the package of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * specified file before, a new linked list will be created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the jar file is added to the list and a new mapping from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the package to the jar file list is added to the hashmap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Otherwise, the jar file will be added to the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * existing list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param fileName the file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param jarName the jar file that the file is mapped to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public void add(String fileName, String jarName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        String packageName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if((pos = fileName.lastIndexOf("/")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            packageName = fileName.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            packageName = fileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // add the mapping to indexMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        addToList(packageName, jarName, indexMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // add the mapping to jarMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        addToList(jarName, packageName, jarMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   198
     * Same as add(String,String) except that it doesn't strip off from the
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   199
     * last index of '/'. It just adds the filename.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   200
     */
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   201
    private void addExplicit(String fileName, String jarName) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   202
        // add the mapping to indexMap
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   203
        addToList(fileName, jarName, indexMap);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   204
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   205
        // add the mapping to jarMap
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   206
        addToList(jarName, fileName, jarMap);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   207
     }
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   208
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   209
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Go through all the jar files and construct the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * index table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    private void parseJars(String[] files) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (files == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        String currentJar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        for (int i = 0; i < files.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            currentJar = files[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            ZipFile zrf = new ZipFile(currentJar.replace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                      ('/', File.separatorChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            Enumeration entries = zrf.entries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            while(entries.hasMoreElements()) {
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   227
                ZipEntry entry = (ZipEntry) entries.nextElement();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   228
                String fileName = entry.getName();
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   229
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   230
                // Skip the META-INF directory, the index, and manifest.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   231
                // Any files in META-INF/ will be indexed explicitly
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   232
                if (fileName.equals("META-INF/") ||
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   233
                    fileName.equals(INDEX_NAME) ||
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   234
                    fileName.equals(JarFile.MANIFEST_NAME))
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   235
                    continue;
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   236
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   237
                if (!metaInfFilenames) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    add(fileName, currentJar);
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   239
                } else {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   240
                    if (!fileName.startsWith("META-INF/")) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   241
                        add(fileName, currentJar);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   242
                    } else if (!entry.isDirectory()) {
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   243
                        // Add files under META-INF explicitly so that certain
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   244
                        // services, like ServiceLoader, etc, can be located
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   245
                        // with greater accuracy. Directories can be skipped
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   246
                        // since each file will be added explicitly.
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   247
                        addExplicit(fileName, currentJar);
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   248
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
8169
0dc88a086893 6887710: Jar index should avoid putting META-INF in the INDEX.LIST
chegar
parents: 5506
diff changeset
   251
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            zrf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Writes the index to the specified OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param out the output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void write(OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        BufferedWriter bw = new BufferedWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            (new OutputStreamWriter(out, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        bw.write("JarIndex-Version: 1.0\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (jarFiles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            for (int i = 0; i < jarFiles.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                /* print out the jar file name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                String jar = jarFiles[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                bw.write(jar + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                LinkedList jarlist = (LinkedList)jarMap.get(jar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                if (jarlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    Iterator listitr = jarlist.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    while(listitr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        bw.write((String)(listitr.next()) + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                bw.write("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            bw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Reads the index from the specified InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param is the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @exception IOException if an I/O error has occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public void read(InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        BufferedReader br = new BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            (new InputStreamReader(is, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        String line = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        String currentJar = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        /* an ordered list of jar file names */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        Vector jars = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /* read until we see a .jar line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        while((line = br.readLine()) != null && !line.endsWith(".jar"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for(;line != null; line = br.readLine()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if (line.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            if (line.endsWith(".jar")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                currentJar = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                jars.add(currentJar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                String name = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                addToList(name, currentJar, indexMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                addToList(currentJar, name, jarMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        jarFiles = (String[])jars.toArray(new String[jars.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Merges the current index into another index, taking into account
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the relative path of the current index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param toIndex The destination index which the current index will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *                merge into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @param path    The relative path of the this index to the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *                index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void merge(JarIndex toIndex, String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Iterator itr = indexMap.entrySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        while(itr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            Map.Entry e = (Map.Entry)itr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            String packageName = (String)e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            LinkedList from_list = (LinkedList)e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            Iterator listItr = from_list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            while(listItr.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                String jarName = (String)listItr.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                if (path != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    jarName = path.concat(jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                toIndex.add(packageName, jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
}