Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/JarIndex.java
38829 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.misc;2627import java.io.*;28import java.security.AccessController;29import java.util.*;30import java.util.jar.*;31import java.util.zip.*;32import sun.security.action.GetPropertyAction;3334/**35* This class is used to maintain mappings from packages, classes36* and resources to their enclosing JAR files. Mappings are kept37* at the package level except for class or resource files that38* are located at the root directory. URLClassLoader uses the mapping39* information to determine where to fetch an extension class or40* resource from.41*42* @author Zhenghua Li43* @since 1.344*/4546public class JarIndex {4748/**49* The hash map that maintains mappings from50* package/classe/resource to jar file list(s)51*/52private HashMap<String,LinkedList<String>> indexMap;5354/**55* The hash map that maintains mappings from56* jar file to package/class/resource lists57*/58private HashMap<String,LinkedList<String>> jarMap;5960/*61* An ordered list of jar file names.62*/63private String[] jarFiles;6465/**66* The index file name.67*/68public static final String INDEX_NAME = "META-INF/INDEX.LIST";6970/**71* true if, and only if, sun.misc.JarIndex.metaInfFilenames is set to true.72* If true, the names of the files in META-INF, and its subdirectories, will73* be added to the index. Otherwise, just the directory names are added.74*/75private static final boolean metaInfFilenames =76"true".equals(AccessController.doPrivileged(77new GetPropertyAction("sun.misc.JarIndex.metaInfFilenames")));7879/**80* Constructs a new, empty jar index.81*/82public JarIndex() {83indexMap = new HashMap<>();84jarMap = new HashMap<>();85}8687/**88* Constructs a new index from the specified input stream.89*90* @param is the input stream containing the index data91*/92public JarIndex(InputStream is) throws IOException {93this();94read(is);95}9697/**98* Constructs a new index for the specified list of jar files.99*100* @param files the list of jar files to construct the index from.101*/102public JarIndex(String[] files) throws IOException {103this();104this.jarFiles = files;105parseJars(files);106}107108/**109* Returns the jar index, or <code>null</code> if none.110*111* This single parameter version of the method is retained112* for binary compatibility with earlier releases.113*114* @param jar the JAR file to get the index from.115* @exception IOException if an I/O error has occurred.116*/117public static JarIndex getJarIndex(JarFile jar) throws IOException {118return getJarIndex(jar, null);119}120121/**122* Returns the jar index, or <code>null</code> if none.123*124* @param jar the JAR file to get the index from.125* @exception IOException if an I/O error has occurred.126*/127public static JarIndex getJarIndex(JarFile jar, MetaIndex metaIndex) throws IOException {128JarIndex index = null;129/* If metaIndex is not null, check the meta index to see130if META-INF/INDEX.LIST is contained in jar file or not.131*/132if (metaIndex != null &&133!metaIndex.mayContain(INDEX_NAME)) {134return null;135}136JarEntry e = jar.getJarEntry(INDEX_NAME);137// if found, then load the index138if (e != null) {139index = new JarIndex(jar.getInputStream(e));140}141return index;142}143144/**145* Returns the jar files that are defined in this index.146*/147public String[] getJarFiles() {148return jarFiles;149}150151/*152* Add the key, value pair to the hashmap, the value will153* be put in a linked list which is created if necessary.154*/155private void addToList(String key, String value,156HashMap<String,LinkedList<String>> t) {157LinkedList<String> list = t.get(key);158if (list == null) {159list = new LinkedList<>();160list.add(value);161t.put(key, list);162} else if (!list.contains(value)) {163list.add(value);164}165}166167/**168* Returns the list of jar files that are mapped to the file.169*170* @param fileName the key of the mapping171*/172public LinkedList<String> get(String fileName) {173LinkedList<String> jarFiles = null;174if ((jarFiles = indexMap.get(fileName)) == null) {175/* try the package name again */176int pos;177if((pos = fileName.lastIndexOf("/")) != -1) {178jarFiles = indexMap.get(fileName.substring(0, pos));179}180}181return jarFiles;182}183184/**185* Add the mapping from the specified file to the specified186* jar file. If there were no mapping for the package of the187* specified file before, a new linked list will be created,188* the jar file is added to the list and a new mapping from189* the package to the jar file list is added to the hashmap.190* Otherwise, the jar file will be added to the end of the191* existing list.192*193* @param fileName the file name194* @param jarName the jar file that the file is mapped to195*196*/197public void add(String fileName, String jarName) {198String packageName;199int pos;200if((pos = fileName.lastIndexOf("/")) != -1) {201packageName = fileName.substring(0, pos);202} else {203packageName = fileName;204}205206addMapping(packageName, jarName);207}208209/**210* Same as add(String,String) except that it doesn't strip off from the211* last index of '/'. It just adds the jarItem (filename or package)212* as it is received.213*/214private void addMapping(String jarItem, String jarName) {215// add the mapping to indexMap216addToList(jarItem, jarName, indexMap);217218// add the mapping to jarMap219addToList(jarName, jarItem, jarMap);220}221222/**223* Go through all the jar files and construct the224* index table.225*/226private void parseJars(String[] files) throws IOException {227if (files == null) {228return;229}230231String currentJar = null;232233for (int i = 0; i < files.length; i++) {234currentJar = files[i];235ZipFile zrf = new ZipFile(currentJar.replace236('/', File.separatorChar));237238Enumeration<? extends ZipEntry> entries = zrf.entries();239while(entries.hasMoreElements()) {240ZipEntry entry = entries.nextElement();241String fileName = entry.getName();242243// Skip the META-INF directory, the index, and manifest.244// Any files in META-INF/ will be indexed explicitly245if (fileName.equals("META-INF/") ||246fileName.equals(INDEX_NAME) ||247fileName.equals(JarFile.MANIFEST_NAME))248continue;249250if (!metaInfFilenames || !fileName.startsWith("META-INF/")) {251add(fileName, currentJar);252} else if (!entry.isDirectory()) {253// Add files under META-INF explicitly so that certain254// services, like ServiceLoader, etc, can be located255// with greater accuracy. Directories can be skipped256// since each file will be added explicitly.257addMapping(fileName, currentJar);258}259}260261zrf.close();262}263}264265/**266* Writes the index to the specified OutputStream267*268* @param out the output stream269* @exception IOException if an I/O error has occurred270*/271public void write(OutputStream out) throws IOException {272BufferedWriter bw = new BufferedWriter273(new OutputStreamWriter(out, "UTF8"));274bw.write("JarIndex-Version: 1.0\n\n");275276if (jarFiles != null) {277for (int i = 0; i < jarFiles.length; i++) {278/* print out the jar file name */279String jar = jarFiles[i];280bw.write(jar + "\n");281LinkedList<String> jarlist = jarMap.get(jar);282if (jarlist != null) {283Iterator<String> listitr = jarlist.iterator();284while(listitr.hasNext()) {285bw.write(listitr.next() + "\n");286}287}288bw.write("\n");289}290bw.flush();291}292}293294295/**296* Reads the index from the specified InputStream.297*298* @param is the input stream299* @exception IOException if an I/O error has occurred300*/301public void read(InputStream is) throws IOException {302BufferedReader br = new BufferedReader303(new InputStreamReader(is, "UTF8"));304String line = null;305String currentJar = null;306307/* an ordered list of jar file names */308Vector<String> jars = new Vector<>();309310/* read until we see a .jar line */311while((line = br.readLine()) != null && !line.endsWith(".jar"));312313for(;line != null; line = br.readLine()) {314if (line.length() == 0)315continue;316317if (line.endsWith(".jar")) {318currentJar = line;319jars.add(currentJar);320} else {321String name = line;322addMapping(name, currentJar);323}324}325326jarFiles = jars.toArray(new String[jars.size()]);327}328329/**330* Merges the current index into another index, taking into account331* the relative path of the current index.332*333* @param toIndex The destination index which the current index will334* merge into.335* @param path The relative path of the this index to the destination336* index.337*338*/339public void merge(JarIndex toIndex, String path) {340Iterator<Map.Entry<String,LinkedList<String>>> itr = indexMap.entrySet().iterator();341while(itr.hasNext()) {342Map.Entry<String,LinkedList<String>> e = itr.next();343String packageName = e.getKey();344LinkedList<String> from_list = e.getValue();345Iterator<String> listItr = from_list.iterator();346while(listItr.hasNext()) {347String jarName = listItr.next();348if (path != null) {349jarName = path.concat(jarName);350}351toIndex.addMapping(packageName, jarName);352}353}354}355}356357358