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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * %W% %E%
 *
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package sun.misc;

import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;

/**
 * This class is used to maintain mappings from packages, classes 
 * and resources to their enclosing JAR files. Mappings are kept
 * at the package level except for class or resource files that 
 * are located at the root directory. URLClassLoader uses the mapping
 * information to determine where to fetch an extension class or 
 * resource from. 
 *
 * @author  Zhenghua Li
 * @version %I%, %G%
 * @since   1.3
 */

public class JarIndex {

    /**
     * The hash map that maintains mappings from 
     * package/classe/resource to jar file list(s)
     */
    private HashMap indexMap;

    /** 
     * The hash map that maintains mappings from
     * jar file to package/class/resource lists
     */
    private HashMap jarMap;

    /*
     * An ordered list of jar file names.
     */
    private String[] jarFiles;
    
    /**
     * The index file name.
     */
    public static final String INDEX_NAME = "META-INF/INDEX.LIST";

    /**
     * true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.
     * If true, the names of the files in META-INF, and its subdirectories, will
     * be added to the index. Otherwise, just the directory names are added.
     */
    private static final boolean metaInfFilenames =
        "true".equals(System.getProperty("sun.misc.JarIndex.metaInfFilenames")) ? true : false;

    /**
     * Constructs a new, empty jar index.
     */
    public JarIndex() {
        indexMap = new HashMap();
        jarMap = new HashMap();
    }

    /**
     * Constructs a new index from the specified input stream.
     * 
     * @param is the input stream containing the index data
     */
    public JarIndex(InputStream is) throws IOException {
        this();
    read(is);
    }

    /**
     * Constructs a new index for the specified list of jar files.
     * 
     * @param files the list of jar files to construct the index from.
     */
    public JarIndex(String[] files) throws IOException {
        this();
        this.jarFiles = files;
        parseJars(files);
    }
    
    /**
     * Returns the jar index, or <code>null</code> if none.
     *
     * This single parameter version of the method is retained
     * for binary compatibility with earlier releases.
     *
     * @param jar the JAR file to get the index from.
     * @exception IOException if an I/O error has occurred.
     */
    public static JarIndex getJarIndex(JarFile jar) throws IOException {
    return getJarIndex(jar, null);
    }

    /**
     * Returns the jar index, or <code>null</code> if none.
     *
     * @param jar the JAR file to get the index from.
     * @exception IOException if an I/O error has occurred.
     */
    public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {
        JarIndex index = null;
    /* If metaIndex is not null, check the meta index to see
       if META-INF/INDEX.LIST is contained in jar file or not.
    */
    if (metaIndex != null &&
        !metaIndex.mayContain(INDEX_NAME)) {
        return null;
    }
        JarEntry e = jar.getJarEntry(INDEX_NAME);
        // if found, then load the index
        if (e != null) {
            index = new JarIndex(jar.getInputStream(e));
        }
        return index;
    }

    /**
     * Returns the jar files that are defined in this index.
     */
    public String[] getJarFiles() {
        return jarFiles;
    }
    
    /*
     * Add the key, value pair to the hashmap, the value will
     * be put in a linked list which is created if necessary.
     */
    private void addToList(String key, String value, HashMap t) {
        LinkedList list = (LinkedList)t.get(key);
        if (list == null) {
            list = new LinkedList();
            list.add(value);
            t.put(key, list);
        } else if (!list.contains(value)) {
            list.add(value);
        }
    }

    /**
     * Returns the list of jar files that are mapped to the file.
     *
     * @param fileName the key of the mapping
     */
    public LinkedList get(String fileName) {
        LinkedList jarFiles = null;
        if ((jarFiles = (LinkedList)indexMap.get(fileName)) == null) {
            /* try the package name again */
            int pos;
            if((pos = fileName.lastIndexOf("/")) != -1) {
                jarFiles = (LinkedList)indexMap.get(fileName.substring(0, pos));
            }
        }
        return jarFiles;
    }
    
    /**
     * Add the mapping from the specified file to the specified
     * jar file. If there were no mapping for the package of the 
     * specified file before, a new linked list will be created,
     * the jar file is added to the list and a new mapping from 
     * the package to the jar file list is added to the hashmap. 
     * Otherwise, the jar file will be added to the end of the 
     * existing list.
     *
     * @param fileName the file name
     * @param jarName the jar file that the file is mapped to
     *
     */
    public void add(String fileName, String jarName) {
        String packageName;
        int pos;
        if((pos = fileName.lastIndexOf("/")) != -1) {
            packageName = fileName.substring(0, pos);
        } else {
            packageName = fileName;
        }

        // add the mapping to indexMap
        addToList(packageName, jarName, indexMap);
        
        // add the mapping to jarMap
        addToList(jarName, packageName, jarMap);
    }

    /**
     * Same as add(String,String) except that it doesn't strip off from the
     * last index of '/'. It just adds the filename.
     */
    private void addExplicit(String fileName, String jarName) {
        // add the mapping to indexMap
        addToList(fileName, jarName, indexMap);

        // add the mapping to jarMap
        addToList(jarName, fileName, jarMap);
    }
    
    /**
     * Go through all the jar files and construct the
     * index table.
     */
    private void parseJars(String[] files) throws IOException {
        if (files == null) {
            return;
        }

        String currentJar = null;
        
        for (int i = 0; i < files.length; i++) {
            currentJar = files[i];
            ZipFile zrf = new ZipFile(currentJar.replace
                                      ('/', File.separatorChar));

            Enumeration entries = zrf.entries();
            while(entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String fileName = entry.getName();

                // Skip the META-INF directory, the index, and manifest.
                // Any files in META-INF/ will be indexed explicitly
                if (fileName.equals("META-INF/") ||
                    fileName.equals(INDEX_NAME) ||
                    fileName.equals(JarFile.MANIFEST_NAME))
                    continue;

                if (!metaInfFilenames) {
                    add(fileName, currentJar);
                } else {
                    if (!fileName.startsWith("META-INF/")) {
                        add(fileName, currentJar);
                    } else if (!entry.isDirectory()) {
                        // Add files under META-INF explicitly so that certain services,
                        // like ServiceLoader, etc, can be located with greater accuracy.
                        // Directories can be skipped since each file will be added explicitly.
                        addExplicit(fileName, currentJar);
                    }
                }
            }
            zrf.close();
        }
    }

    /**
     * Writes the index to the specified OutputStream
     *
     * @param out the output stream
     * @exception IOException if an I/O error has occurred
     */
    public void write(OutputStream out) throws IOException {
        BufferedWriter bw = new BufferedWriter
            (new OutputStreamWriter(out, "UTF8"));
        bw.write("JarIndex-Version: 1.0\n\n");
        
        if (jarFiles != null) {
            for (int i = 0; i < jarFiles.length; i++) {
                /* print out the jar file name */
                String jar = jarFiles[i];
                bw.write(jar + "\n");
                LinkedList jarlist = (LinkedList)jarMap.get(jar);
                if (jarlist != null) {
                    Iterator listitr = jarlist.iterator();
                    while(listitr.hasNext()) {
                        bw.write((String)(listitr.next()) + "\n");
                    }
                }
                bw.write("\n");
            }
            bw.flush();
        }
    }
    

    /**
     * Reads the index from the specified InputStream.
     *
     * @param is the input stream 
     * @exception IOException if an I/O error has occurred
     */
    public void read(InputStream is) throws IOException {
        BufferedReader br = new BufferedReader
            (new InputStreamReader(is, "UTF8"));
        String line = null;
        String currentJar = null;
        
        /* an ordered list of jar file names */
        Vector jars = new Vector();
        
        /* read until we see a .jar line */
        while((line = br.readLine()) != null && !line.endsWith(".jar"));
        
        for(;line != null; line = br.readLine()) {
            if (line.length() == 0)
                continue;

            if (line.endsWith(".jar")) {
                currentJar = line;
                jars.add(currentJar);
            } else {
                String name = line;
                addToList(name, currentJar, indexMap);
                addToList(currentJar, name, jarMap);
            }
        }

        jarFiles = (String[])jars.toArray(new String[jars.size()]);
    }

    /**
     * Merges the current index into another index, taking into account
     * the relative path of the current index.
     *
     * @param toIndex The destination index which the current index will
     *                merge into.
     * @param path    The relative path of the this index to the destination
     *                index.
     *
     */
    public void merge(JarIndex toIndex, String path) {
        Iterator itr = indexMap.entrySet().iterator();
        while(itr.hasNext()) {
            Map.Entry e = (Map.Entry)itr.next();
            String packageName = (String)e.getKey();
            LinkedList from_list = (LinkedList)e.getValue();
            Iterator listItr = from_list.iterator();
            while(listItr.hasNext()) {
                String jarName = (String)listItr.next();
                if (path != null) {
                    jarName = path.concat(jarName);
                }
                toIndex.add(packageName, jarName);
            }
        }
    }
}
			
			

Browsed Source: [clear]