0N/A/*
3482N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/Aimport java.util.jar.*;
0N/Aimport java.util.zip.*;
0N/A
0N/A/**
0N/A * This class is used to maintain mappings from packages, classes
0N/A * and resources to their enclosing JAR files. Mappings are kept
0N/A * at the package level except for class or resource files that
0N/A * are located at the root directory. URLClassLoader uses the mapping
0N/A * information to determine where to fetch an extension class or
0N/A * resource from.
0N/A *
0N/A * @author Zhenghua Li
0N/A * @since 1.3
0N/A */
0N/A
0N/Apublic class JarIndex {
0N/A
0N/A /**
0N/A * The hash map that maintains mappings from
0N/A * package/classe/resource to jar file list(s)
0N/A */
0N/A private HashMap indexMap;
0N/A
0N/A /**
0N/A * The hash map that maintains mappings from
0N/A * jar file to package/class/resource lists
0N/A */
0N/A private HashMap jarMap;
0N/A
0N/A /*
0N/A * An ordered list of jar file names.
0N/A */
0N/A private String[] jarFiles;
0N/A
0N/A /**
0N/A * The index file name.
0N/A */
0N/A public static final String INDEX_NAME = "META-INF/INDEX.LIST";
0N/A
0N/A /**
3482N/A * true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.
3482N/A * If true, the names of the files in META-INF, and its subdirectories, will
3482N/A * be added to the index. Otherwise, just the directory names are added.
3482N/A */
3482N/A private static final boolean metaInfFilenames =
3482N/A "true".equals(System.getProperty("sun.misc.JarIndex.metaInfFilenames"));
3482N/A
3482N/A /**
0N/A * Constructs a new, empty jar index.
0N/A */
0N/A public JarIndex() {
0N/A indexMap = new HashMap();
0N/A jarMap = new HashMap();
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new index from the specified input stream.
0N/A *
0N/A * @param is the input stream containing the index data
0N/A */
0N/A public JarIndex(InputStream is) throws IOException {
0N/A this();
0N/A read(is);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new index for the specified list of jar files.
0N/A *
0N/A * @param files the list of jar files to construct the index from.
0N/A */
0N/A public JarIndex(String[] files) throws IOException {
0N/A this();
0N/A this.jarFiles = files;
0N/A parseJars(files);
0N/A }
0N/A
0N/A /**
0N/A * Returns the jar index, or <code>null</code> if none.
0N/A *
3570N/A * This single parameter version of the method is retained
3570N/A * for binary compatibility with earlier releases.
3570N/A *
3570N/A * @param jar the JAR file to get the index from.
3570N/A * @exception IOException if an I/O error has occurred.
3570N/A */
3570N/A public static JarIndex getJarIndex(JarFile jar) throws IOException {
3570N/A return getJarIndex(jar, null);
3570N/A }
3570N/A
3570N/A /**
3570N/A * Returns the jar index, or <code>null</code> if none.
3570N/A *
0N/A * @param jar the JAR file to get the index from.
0N/A * @exception IOException if an I/O error has occurred.
0N/A */
0N/A public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {
0N/A JarIndex index = null;
0N/A /* If metaIndex is not null, check the meta index to see
0N/A if META-INF/INDEX.LIST is contained in jar file or not.
0N/A */
0N/A if (metaIndex != null &&
0N/A !metaIndex.mayContain(INDEX_NAME)) {
0N/A return null;
0N/A }
0N/A JarEntry e = jar.getJarEntry(INDEX_NAME);
0N/A // if found, then load the index
0N/A if (e != null) {
0N/A index = new JarIndex(jar.getInputStream(e));
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Returns the jar files that are defined in this index.
0N/A */
0N/A public String[] getJarFiles() {
0N/A return jarFiles;
0N/A }
0N/A
0N/A /*
0N/A * Add the key, value pair to the hashmap, the value will
0N/A * be put in a linked list which is created if necessary.
0N/A */
0N/A private void addToList(String key, String value, HashMap t) {
0N/A LinkedList list = (LinkedList)t.get(key);
0N/A if (list == null) {
0N/A list = new LinkedList();
0N/A list.add(value);
0N/A t.put(key, list);
0N/A } else if (!list.contains(value)) {
0N/A list.add(value);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the list of jar files that are mapped to the file.
0N/A *
0N/A * @param fileName the key of the mapping
0N/A */
0N/A public LinkedList get(String fileName) {
0N/A LinkedList jarFiles = null;
0N/A if ((jarFiles = (LinkedList)indexMap.get(fileName)) == null) {
0N/A /* try the package name again */
0N/A int pos;
0N/A if((pos = fileName.lastIndexOf("/")) != -1) {
0N/A jarFiles = (LinkedList)indexMap.get(fileName.substring(0, pos));
0N/A }
0N/A }
0N/A return jarFiles;
0N/A }
0N/A
0N/A /**
0N/A * Add the mapping from the specified file to the specified
0N/A * jar file. If there were no mapping for the package of the
0N/A * specified file before, a new linked list will be created,
0N/A * the jar file is added to the list and a new mapping from
0N/A * the package to the jar file list is added to the hashmap.
0N/A * Otherwise, the jar file will be added to the end of the
0N/A * existing list.
0N/A *
0N/A * @param fileName the file name
0N/A * @param jarName the jar file that the file is mapped to
0N/A *
0N/A */
0N/A public void add(String fileName, String jarName) {
0N/A String packageName;
0N/A int pos;
0N/A if((pos = fileName.lastIndexOf("/")) != -1) {
0N/A packageName = fileName.substring(0, pos);
0N/A } else {
0N/A packageName = fileName;
0N/A }
0N/A
0N/A // add the mapping to indexMap
0N/A addToList(packageName, jarName, indexMap);
0N/A
0N/A // add the mapping to jarMap
0N/A addToList(jarName, packageName, jarMap);
0N/A }
0N/A
0N/A /**
3482N/A * Same as add(String,String) except that it doesn't strip off from the
3482N/A * last index of '/'. It just adds the filename.
3482N/A */
3482N/A private void addExplicit(String fileName, String jarName) {
3482N/A // add the mapping to indexMap
3482N/A addToList(fileName, jarName, indexMap);
3482N/A
3482N/A // add the mapping to jarMap
3482N/A addToList(jarName, fileName, jarMap);
3482N/A }
3482N/A
3482N/A /**
0N/A * Go through all the jar files and construct the
0N/A * index table.
0N/A */
0N/A private void parseJars(String[] files) throws IOException {
0N/A if (files == null) {
0N/A return;
0N/A }
0N/A
0N/A String currentJar = null;
0N/A
0N/A for (int i = 0; i < files.length; i++) {
0N/A currentJar = files[i];
0N/A ZipFile zrf = new ZipFile(currentJar.replace
0N/A ('/', File.separatorChar));
0N/A
0N/A Enumeration entries = zrf.entries();
0N/A while(entries.hasMoreElements()) {
3482N/A ZipEntry entry = (ZipEntry) entries.nextElement();
3482N/A String fileName = entry.getName();
3482N/A
3482N/A // Skip the META-INF directory, the index, and manifest.
3482N/A // Any files in META-INF/ will be indexed explicitly
3482N/A if (fileName.equals("META-INF/") ||
3482N/A fileName.equals(INDEX_NAME) ||
3482N/A fileName.equals(JarFile.MANIFEST_NAME))
3482N/A continue;
3482N/A
3482N/A if (!metaInfFilenames) {
0N/A add(fileName, currentJar);
3482N/A } else {
3482N/A if (!fileName.startsWith("META-INF/")) {
3482N/A add(fileName, currentJar);
3482N/A } else if (!entry.isDirectory()) {
3482N/A // Add files under META-INF explicitly so that certain
3482N/A // services, like ServiceLoader, etc, can be located
3482N/A // with greater accuracy. Directories can be skipped
3482N/A // since each file will be added explicitly.
3482N/A addExplicit(fileName, currentJar);
3482N/A }
0N/A }
0N/A }
3482N/A
0N/A zrf.close();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes the index to the specified OutputStream
0N/A *
0N/A * @param out the output stream
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public void write(OutputStream out) throws IOException {
0N/A BufferedWriter bw = new BufferedWriter
0N/A (new OutputStreamWriter(out, "UTF8"));
0N/A bw.write("JarIndex-Version: 1.0\n\n");
0N/A
0N/A if (jarFiles != null) {
0N/A for (int i = 0; i < jarFiles.length; i++) {
0N/A /* print out the jar file name */
0N/A String jar = jarFiles[i];
0N/A bw.write(jar + "\n");
0N/A LinkedList jarlist = (LinkedList)jarMap.get(jar);
0N/A if (jarlist != null) {
0N/A Iterator listitr = jarlist.iterator();
0N/A while(listitr.hasNext()) {
0N/A bw.write((String)(listitr.next()) + "\n");
0N/A }
0N/A }
0N/A bw.write("\n");
0N/A }
0N/A bw.flush();
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Reads the index from the specified InputStream.
0N/A *
0N/A * @param is the input stream
0N/A * @exception IOException if an I/O error has occurred
0N/A */
0N/A public void read(InputStream is) throws IOException {
0N/A BufferedReader br = new BufferedReader
0N/A (new InputStreamReader(is, "UTF8"));
0N/A String line = null;
0N/A String currentJar = null;
0N/A
0N/A /* an ordered list of jar file names */
0N/A Vector jars = new Vector();
0N/A
0N/A /* read until we see a .jar line */
0N/A while((line = br.readLine()) != null && !line.endsWith(".jar"));
0N/A
0N/A for(;line != null; line = br.readLine()) {
0N/A if (line.length() == 0)
0N/A continue;
0N/A
0N/A if (line.endsWith(".jar")) {
0N/A currentJar = line;
0N/A jars.add(currentJar);
0N/A } else {
0N/A String name = line;
0N/A addToList(name, currentJar, indexMap);
0N/A addToList(currentJar, name, jarMap);
0N/A }
0N/A }
0N/A
0N/A jarFiles = (String[])jars.toArray(new String[jars.size()]);
0N/A }
0N/A
0N/A /**
0N/A * Merges the current index into another index, taking into account
0N/A * the relative path of the current index.
0N/A *
0N/A * @param toIndex The destination index which the current index will
0N/A * merge into.
0N/A * @param path The relative path of the this index to the destination
0N/A * index.
0N/A *
0N/A */
0N/A public void merge(JarIndex toIndex, String path) {
0N/A Iterator itr = indexMap.entrySet().iterator();
0N/A while(itr.hasNext()) {
0N/A Map.Entry e = (Map.Entry)itr.next();
0N/A String packageName = (String)e.getKey();
0N/A LinkedList from_list = (LinkedList)e.getValue();
0N/A Iterator listItr = from_list.iterator();
0N/A while(listItr.hasNext()) {
0N/A String jarName = (String)listItr.next();
0N/A if (path != null) {
0N/A jarName = path.concat(jarName);
0N/A }
0N/A toIndex.add(packageName, jarName);
0N/A }
0N/A }
0N/A }
0N/A}