0N/A/*
2362N/A * Copyright (c) 2005, 2006, 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.BufferedReader;
0N/Aimport java.io.FileReader;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/A
0N/A/*
0N/A * MetaIndex is intended to decrease startup time (in particular cold
0N/A * start, when files are not yet in the disk cache) by providing a
0N/A * quick reject mechanism for probes into jar files. The on-disk
0N/A * representation of the meta-index is a flat text file with per-jar
0N/A * entries indicating (generally speaking) prefixes of package names
0N/A * contained in the jar. As an example, here is an edited excerpt of
0N/A * the meta-index generated for jre/lib in the current build:
0N/A *
0N/A<PRE>
0N/A% VERSION 1
0N/A# charsets.jar
0N/Asun/
0N/A# jce.jar
0N/Ajavax/
0N/A! jsse.jar
0N/Asun/
0N/Acom/sun/net/
0N/Ajavax/
0N/Acom/sun/security/
0N/A@ resources.jar
0N/Acom/sun/xml/
0N/Acom/sun/rowset/
0N/Acom/sun/org/
0N/Asun/
0N/Acom/sun/imageio/
0N/Ajavax/
0N/Acom/sun/java/swing/
0N/AMETA-INF/services/
0N/Acom/sun/java/util/jar/pack/
0N/Acom/sun/corba/
0N/Acom/sun/jndi/
0N/A! rt.jar
0N/Aorg/w3c/
0N/Acom/sun/imageio/
0N/Ajavax/
0N/Asunw/util/
0N/Ajava/
0N/Asun/
0N/A...
0N/A</PRE>
0N/A * <p> A few notes about the design of the meta-index:
0N/A *
0N/A * <UL>
0N/A *
0N/A * <LI> It contains entries for multiple jar files. This is
0N/A * intentional, to reduce the number of disk accesses that need to be
0N/A * performed during startup.
0N/A *
0N/A * <LI> It is only intended to act as a fast reject mechanism to
0N/A * prevent application and other classes from forcing all jar files on
0N/A * the boot and extension class paths to be opened. It is not intended
0N/A * as a precise index of the contents of the jar.
0N/A *
0N/A * <LI> It should be as small as possible to reduce the amount of time
0N/A * required to parse it during startup. For example, adding on the
0N/A * secondary package element to java/ and javax/ packages
0N/A * ("javax/swing/", for example) causes the meta-index to grow
0N/A * significantly. This is why substrings of the packages have been
0N/A * chosen as the principal contents.
0N/A *
0N/A * <LI> It is versioned, and optional, to prevent strong dependencies
0N/A * between the JVM and JDK. It is also potentially applicable to more
0N/A * than just the boot and extension class paths.
0N/A *
0N/A * <LI> Precisely speaking, it plays different role in JVM and J2SE
0N/A * side. On the JVM side, meta-index file is used to speed up locating the
0N/A * class files only while on the J2SE side, meta-index file is used to speed
0N/A * up the resources file & class file.
0N/A * To help the JVM and J2SE code to better utilize the information in meta-index
0N/A * file, we mark the jar file differently. Here is the current rule we use.
0N/A * For jar file containing only class file, we put '!' before the jar file name;
0N/A * for jar file containing only resources file, we put '@' before the jar file name;
0N/A * for jar file containing both resources and class file, we put '#' before the
0N/A * jar name.
0N/A * Notice the fact that every jar file contains at least the manifest file, so when
0N/A * we say "jar file containing only class file", we don't include that file.
0N/A *
0N/A * </UL>
0N/A *
0N/A * <p> To avoid changing the behavior of the current application
0N/A * loader and other loaders, the current MetaIndex implementation in
0N/A * the JDK requires that the directory containing the meta-index be
0N/A * registered with the MetaIndex class before construction of the
0N/A * associated URLClassPath. This prevents the need for automatic
0N/A * searching for the meta-index in the URLClassPath code and potential
0N/A * changes in behavior for non-core ClassLoaders.
0N/A *
0N/A * This class depends on make/tools/MetaIndex/BuildMetaIndex.java and
0N/A * is used principally by sun.misc.URLClassPath.
0N/A */
0N/A
0N/Apublic class MetaIndex {
0N/A // Maps jar file names in registered directories to meta-indices
0N/A private static volatile Map<File, MetaIndex> jarMap;
0N/A
0N/A // List of contents of this meta-index
0N/A private String[] contents;
0N/A
0N/A // Indicate whether the coresponding jar file is a pure class jar file or not
0N/A private boolean isClassOnlyJar;
0N/A
0N/A //----------------------------------------------------------------------
0N/A // Registration of directories (which can cause parsing of the
0N/A // meta-index file if it is present), and fetching of parsed
0N/A // meta-indices
0N/A // jarMap is not strictly thread-safe when the meta index mechanism
0N/A // is extended for user-provided jar files in future.
0N/A
0N/A public static MetaIndex forJar(File jar) {
0N/A return getJarMap().get(jar);
0N/A }
0N/A
0N/A // 'synchronized' is added to protect the jarMap from being modified
0N/A // by multiple threads.
0N/A public static synchronized void registerDirectory(File dir) {
0N/A // Note that this does not currently check to see whether the
0N/A // directory has previously been registered, since the meta-index
0N/A // in a particular directory creates multiple entries in the
0N/A // jarMap. If this mechanism is extended beyond the boot and
0N/A // extension class paths (for example, automatically searching for
0N/A // meta-index files in directories containing jars which have been
0N/A // explicitly opened) then this code should be generalized.
0N/A //
0N/A // This method must be called from a privileged context.
0N/A File indexFile = new File(dir, "meta-index");
0N/A if (indexFile.exists()) {
0N/A try {
0N/A BufferedReader reader = new BufferedReader(new FileReader(indexFile));
0N/A String line = null;
0N/A String curJarName = null;
0N/A boolean isCurJarContainClassOnly = false;
0N/A List<String> contents = new ArrayList<String>();
0N/A Map<File, MetaIndex> map = getJarMap();
0N/A
0N/A /* Convert dir into canonical form. */
0N/A dir = dir.getCanonicalFile();
0N/A /* Note: The first line should contain the version of
0N/A * the meta-index file. We have to match the right version
0N/A * before trying to parse this file. */
0N/A line = reader.readLine();
0N/A if (line == null ||
0N/A !line.equals("% VERSION 2")) {
0N/A reader.close();
0N/A return;
0N/A }
0N/A while ((line = reader.readLine()) != null) {
0N/A switch (line.charAt(0)) {
0N/A case '!':
0N/A case '#':
0N/A case '@': {
0N/A // Store away current contents, if any
0N/A if ((curJarName != null) && (contents.size() > 0)) {
0N/A map.put(new File(dir, curJarName),
0N/A new MetaIndex(contents,
0N/A isCurJarContainClassOnly));
0N/A
0N/A contents.clear();
0N/A }
0N/A // Fetch new current jar file name
0N/A curJarName = line.substring(2);
0N/A if (line.charAt(0) == '!') {
0N/A isCurJarContainClassOnly = true;
0N/A } else if (isCurJarContainClassOnly) {
0N/A isCurJarContainClassOnly = false;
0N/A }
0N/A
0N/A break;
0N/A }
0N/A case '%':
0N/A break;
0N/A default: {
0N/A contents.add(line);
0N/A }
0N/A }
0N/A }
0N/A // Store away current contents, if any
0N/A if ((curJarName != null) && (contents.size() > 0)) {
0N/A map.put(new File(dir, curJarName),
0N/A new MetaIndex(contents, isCurJarContainClassOnly));
0N/A }
0N/A
0N/A reader.close();
0N/A
0N/A } catch (IOException e) {
0N/A // Silently fail for now (similar behavior to elsewhere in
0N/A // extension and core loaders)
0N/A }
0N/A }
0N/A }
0N/A
0N/A //----------------------------------------------------------------------
0N/A // Public APIs
0N/A //
0N/A
0N/A public boolean mayContain(String entry) {
0N/A // Ask non-class file from class only jar returns false
0N/A // This check is important to avoid some class only jar
0N/A // files such as rt.jar are opened for resource request.
0N/A if (isClassOnlyJar && !entry.endsWith(".class")){
0N/A return false;
0N/A }
0N/A
0N/A String[] conts = contents;
0N/A for (int i = 0; i < conts.length; i++) {
0N/A if (entry.startsWith(conts[i])) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A
0N/A //----------------------------------------------------------------------
0N/A // Implementation only below this point
0N/A // @IllegalArgumentException if entries is null.
0N/A private MetaIndex(List<String> entries, boolean isClassOnlyJar)
0N/A throws IllegalArgumentException {
0N/A if (entries == null) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A
0N/A contents = entries.toArray(new String[0]);
0N/A this.isClassOnlyJar = isClassOnlyJar;
0N/A }
0N/A
0N/A private static Map<File, MetaIndex> getJarMap() {
0N/A if (jarMap == null) {
0N/A synchronized (MetaIndex.class) {
0N/A if (jarMap == null) {
0N/A jarMap = new HashMap<File, MetaIndex>();
0N/A }
0N/A }
0N/A }
0N/A assert jarMap != null;
0N/A return jarMap;
0N/A }
0N/A}