jdk/src/share/classes/sun/misc/URLClassPath.java
author martin
Mon, 10 Mar 2008 15:07:09 -0700
changeset 51 6fe31bc95bbc
parent 2 90ce3da70b43
child 495 d612e90c3ebc
permissions -rw-r--r--
6600143: Remove another 450 unnecessary casts Reviewed-by: alanb, iris, lmalvent, bristor, peterjones, darcy, wetmore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.LinkedList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.misc.JarIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.misc.InvalidJarIndexException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.zip.ZipEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.jar.JarEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.jar.Manifest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.jar.Attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.jar.Attributes.Name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.net.URLStreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.net.URLStreamHandlerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.security.AccessControlException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.security.CodeSigner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.misc.FileURLMapper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * This class is used to maintain a search path of URLs for loading classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * and resources from both JAR files and directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public class URLClassPath {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    final static String USER_AGENT_JAVA_VERSION = "UA-Java-Version";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    final static String JAVA_VERSION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final boolean DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        JAVA_VERSION = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            new sun.security.action.GetPropertyAction("java.version"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        DEBUG        = (java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            new sun.security.action.GetPropertyAction("sun.misc.URLClassPath.debug")) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* The original search path of URLs. */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    89
    private ArrayList<URL> path = new ArrayList<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /* The stack of unopened URLs */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    92
    Stack<URL> urls = new Stack<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /* The resulting search path of Loaders */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    95
    ArrayList<Loader> loaders = new ArrayList<Loader>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /* Map of each URL opened to its corresponding Loader */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    98
    HashMap<URL, Loader> lmap = new HashMap<URL, Loader>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /* The jar protocol handler to use when creating new URLs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private URLStreamHandler jarHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a new URLClassPath for the given URLs. The URLs will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * searched in the order specified for classes and resources. A URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * ending with a '/' is assumed to refer to a directory. Otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * the URL is assumed to refer to a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param urls the directory and JAR file URLs to search for classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *        and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param factory the URLStreamHandlerFactory to use when creating new URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public URLClassPath(URL[] urls, URLStreamHandlerFactory factory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            path.add(urls[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        push(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (factory != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            jarHandler = factory.createURLStreamHandler("jar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public URLClassPath(URL[] urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this(urls, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Appends the specified URL to the search path of directory and JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * file URLs from which to load classes and resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * If the URL specified is null or is already in the list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * URLs, then invoking this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public void addURL(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (url == null || path.contains(url))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            urls.add(0, url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            path.add(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Returns the original search path of URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public URL[] getURLs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        synchronized (urls) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   149
            return path.toArray(new URL[path.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Finds the resource with the specified name on the URL search path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * or null if not found or security check fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param name      the name of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param check     whether to perform a security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @return a <code>URL</code> for the resource, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * if the resource could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public URL findResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            URL url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Finds the first Resource on the URL search path which has the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * name. Returns null if no Resource could be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param name the name of the Resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param check     whether to perform a security check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the Resource, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public Resource getResource(String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            System.err.println("URLClassPath.getResource(\"" + name + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        for (int i = 0; (loader = getLoader(i)) != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            Resource res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns an enumeration of the URL objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @return an Enumeration of all the urls having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   203
    public Enumeration<URL> findResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                     final boolean check) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   205
        return new Enumeration<URL>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            private URL url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                        url = loader.findResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   228
            public URL nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                URL u = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                url = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public Resource getResource(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Finds all resources on the URL search path with the given name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns an enumeration of the Resource objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param name the resource name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return an Enumeration of all the resources having the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   250
    public Enumeration<Resource> getResources(final String name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                    final boolean check) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   252
        return new Enumeration<Resource>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            private int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            private Resource res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            private boolean next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    while ((loader = getLoader(index++)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        res = loader.getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                        if (res != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   275
            public Resource nextElement() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (!next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                Resource r = res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                res = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                return r;
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
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   286
    public Enumeration<Resource> getResources(final String name) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return getResources(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Returns the Loader at the specified position in the URL search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * path. The URLs are opened and expanded as needed. Returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * if the specified index is out of range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     private synchronized Loader getLoader(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         // Expand URL search path until the request can be satisfied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         // or the URL stack is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        while (loaders.size() < index + 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            // Pop the next URL from the URL stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (urls.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                } else {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   305
                    url = urls.pop();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // Skip this URL if it already has a Loader. (Loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // may be null in the case where URL has not been opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // but is referenced by a JAR index.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (lmap.containsKey(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // Otherwise, create a new Loader for the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            Loader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                loader = getLoader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                // If the loader defines a local class path then add the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                // URLs to the list of URLs to be opened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                URL[] urls = loader.getClassPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (urls != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    push(urls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                // Silently ignore for now...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            // Finally, add the Loader to the search path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            loaders.add(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            lmap.put(url, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   332
        return loaders.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Returns the Loader for the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private Loader getLoader(final URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        try {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   340
            return java.security.AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   341
                new java.security.PrivilegedExceptionAction<Loader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   342
                public Loader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    String file = url.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    if (file != null && file.endsWith("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        if ("file".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            return new FileLoader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                            return new Loader(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        return new JarLoader(url, jarHandler, lmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Pushes the specified URLs onto the list of unopened URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private void push(URL[] us) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        synchronized (urls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            for (int i = us.length - 1; i >= 0; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                urls.push(us[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Convert class path specification into an array of file URLs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * The path of the file is encoded before conversion into URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * form so that reserved characters can safely appear in the path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public static URL[] pathToURLs(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            File f = new File(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                f = new File(f.getCanonicalPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                // use the non-canonicalized filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                urls[count++] = ParseUtil.fileToEncodedURL(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            } catch (IOException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (urls.length != count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            URL[] tmp = new URL[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            System.arraycopy(urls, 0, tmp, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            urls = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Check whether the resource URL should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Return null on security check failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Called by java.net.URLClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public URL checkURL(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * Check whether the resource URL should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * Throw exception on failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Called internally within this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    static void check(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            URLConnection urlConnection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            Permission perm = urlConnection.getPermission();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (perm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    security.checkPermission(perm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    // fallback to checkRead/checkConnect for pre 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    // security managers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    if ((perm instanceof java.io.FilePermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        perm.getActions().indexOf("read") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        security.checkRead(perm.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    } else if ((perm instanceof
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        java.net.SocketPermission) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        perm.getActions().indexOf("connect") != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        URL locUrl = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        if (urlConnection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                            locUrl = ((JarURLConnection)urlConnection).getJarFileURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        security.checkConnect(locUrl.getHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                              locUrl.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                        throw se;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * Inner class used to represent a loader of resources and classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * from a base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private static class Loader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        private final URL base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * Creates a new Loader for the specified URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        Loader(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            base = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * Returns the base URL for this Loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        URL getBaseURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                 * For a HTTP connection we use the HEAD method to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                 * check if the resource exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                URLConnection uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                if (uc instanceof HttpURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    HttpURLConnection hconn = (HttpURLConnection)uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    hconn.setRequestMethod("HEAD");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    if (hconn.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    // our best guess for the other cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    InputStream is = url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                url = new URL(base, ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            final URLConnection uc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                uc = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                InputStream in = uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                public URL getCodeSourceURL() { return base; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                public InputStream getInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    return uc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                public int getContentLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    return uc.getContentLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         * Returns the Resource for the specified name, or null if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         * found or the caller does not have the permission to get the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        Resource getResource(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return getResource(name, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
         * Returns the local class path for this loader, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Inner class used to represent a Loader of resources from a JAR URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    static class JarLoader extends Loader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        private JarFile jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        private URL csu;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        private JarIndex index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        private MetaIndex metaIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        private URLStreamHandler handler;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   564
        private HashMap<URL, Loader> lmap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
         * Creates a new JarLoader for the specified URL referring to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   570
        JarLoader(URL url, URLStreamHandler jarHandler,
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   571
                  HashMap<URL, Loader> loaderMap)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            super(new URL("jar", "", -1, url + "!/", jarHandler));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            csu = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            handler = jarHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            lmap = loaderMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            if (!isOptimizable(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                 String fileName = url.getFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                if (fileName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    fileName = ParseUtil.decode(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    File f = new File(fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    metaIndex = MetaIndex.forJar(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    // If the meta index is found but the file is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    // installed, set metaIndex to null. A typical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    // senario is charsets.jar which won't be installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    // when the user is running in certain locale environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    // The side effect of null metaIndex will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    // ensureOpen get called so that IOException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    if (metaIndex != null && !f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        metaIndex = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                // metaIndex is null when either there is no such jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                // entry recorded in meta-index file or such jar file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                // missing in JRE. See bug 6340399.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                if (metaIndex == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                    ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        JarFile getJarFile () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            return jar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        private boolean isOptimizable(URL url) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            return "file".equals(url.getProtocol());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            if (jar == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   619
                        new java.security.PrivilegedExceptionAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   620
                            public Void run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                                    System.err.println("Opening " + csu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                                    Thread.dumpStack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                jar = getJarFile(csu);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                                index = JarIndex.getJarIndex(jar, metaIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                                    String[] jarfiles = index.getJarFiles();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                // Add all the dependent URLs to the lmap so that loaders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                                // will not be created for them by URLClassPath.getLoader(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                                // if the same URL occurs later on the main class path.  We set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                                // Loader to null here to avoid creating a Loader for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                // URL until we actually need to try to load something from them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                    for(int i = 0; i < jarfiles.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                            URL jarURL = new URL(csu, jarfiles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                                            // If a non-null loader already exists, leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                                            if (!lmap.containsKey(jarURL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                                                lmap.put(jarURL, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                                        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    throw (IOException)pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        private JarFile getJarFile(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // Optimize case where url refers to a local jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (isOptimizable(url)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                FileURLMapper p = new FileURLMapper (url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                if (!p.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                    throw new FileNotFoundException(p.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                return new JarFile (p.getPath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            URLConnection uc = getBaseURL().openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            uc.setRequestProperty(USER_AGENT_JAVA_VERSION, JAVA_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            return ((JarURLConnection)uc).getJarFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * Returns the index of this JarLoader if it exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        JarIndex getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                throw (InternalError) new InternalError().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
         * Creates the resource and if the check flag is set to true, checks if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
         * is its okay to return the resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Resource checkResource(final String name, boolean check,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            final JarEntry entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                if (check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                // throw new IllegalArgumentException("name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            } catch (AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                public String getName() { return name; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                public URL getURL() { return url; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                public URL getCodeSourceURL() { return csu; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                    { return jar.getInputStream(entry); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                public int getContentLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                    { return (int)entry.getSize(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                public Manifest getManifest() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                    { return jar.getManifest(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                public Certificate[] getCertificates()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                    { return entry.getCertificates(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                public CodeSigner[] getCodeSigners()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                    { return entry.getCodeSigners(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * Returns true iff atleast one resource in the jar file has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * package name as that of the specified resource name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        boolean validIndex(final String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            String packageName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            if((pos = name.lastIndexOf("/")) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                packageName = name.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            String entryName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            ZipEntry entry;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   736
            Enumeration<JarEntry> enum_ = jar.entries();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            while (enum_.hasMoreElements()) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   738
                entry = enum_.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                entryName = entry.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                if((pos = entryName.lastIndexOf("/")) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                    entryName = entryName.substring(0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                if (entryName.equals(packageName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * Returns the JAR Resource for the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (metaIndex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                if (!metaIndex.mayContain(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                throw (InternalError) new InternalError().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            final JarEntry entry = jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (entry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                return checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            if (index == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   782
            HashSet<URL> visited = new HashSet<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            return getResource(name, check, visited);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * Version of getResource() that tracks the jar files that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * visited by linking through the index files. This helper method uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * a HashSet to store the URLs of jar files that have been searched and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * uses it to avoid going into an infinite loop, looking for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         * non-existent resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        Resource getResource(final String name, boolean check,
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   794
                             Set<URL> visited) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            Resource res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            Object[] jarFiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            LinkedList jarFilesList = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            /* If there no jar files in the index that can potential contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
             * this resource then return immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            if((jarFilesList = index.get(name)) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                jarFiles = jarFilesList.toArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                int size = jarFilesList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                /* loop through the mapped jar file list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                while(count < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    String jarName = (String)jarFiles[count++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    JarLoader newLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        url = new URL(csu, jarName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        if ((newLoader = (JarLoader)lmap.get(url)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                            /* no loader has been set up for this jar file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                             * before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                             */
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   823
                            newLoader = AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   824
                                new PrivilegedExceptionAction<JarLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   825
                                    public JarLoader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                        return new JarLoader(url, handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                                            lmap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                            /* this newly opened jar file has its own index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                             * merge it into the parent's index, taking into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                             * account the relative path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                            JarIndex newIndex = newLoader.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                            if(newIndex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                int pos = jarName.lastIndexOf("/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                newIndex.merge(this.index, (pos == -1 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                    null : jarName.substring(0, pos + 1)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                            /* put it in the global hashtable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                            lmap.put(url, newLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                    } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    /* Note that the addition of the url to the list of visited
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                     * jars incorporates a check for presence in the hashmap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    boolean visitedURL = !visited.add(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    if (!visitedURL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                            newLoader.ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                            throw (InternalError) new InternalError().initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        final JarEntry entry = newLoader.jar.getJarEntry(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                            return newLoader.checkResource(name, check, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                        /* Verify that at least one other resource with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                         * same package name as the lookedup resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                         * present in the new jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        if (!newLoader.validIndex(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                            /* the mapping is wrong */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                            throw new InvalidJarIndexException("Invalid index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                    /* If newLoader is the current loader or if it is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                     * loader that has already been searched or if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                     * loader does not have an index then skip it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                     * and move on to the next loader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                    if (visitedURL || newLoader == this ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                            newLoader.getIndex() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    /* Process the index of the new loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    if((res = newLoader.getResource(name, check, visited))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                            != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                // Get the list of jar files again as the list could have grown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                // due to merging of index files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                jarFilesList = index.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            // If the count is unchanged, we are done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            } while(count < jarFilesList.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * Returns the JAR file local class path, or null if none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        URL[] getClassPath() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            if (index != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (metaIndex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            parseExtensionsDependencies();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            if (SharedSecrets.javaUtilJarAccess().jarFileHasClassPathAttribute(jar)) { // Only get manifest when necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                Manifest man = jar.getManifest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                if (man != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                    Attributes attr = man.getMainAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                        String value = attr.getValue(Name.CLASS_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                        if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                            return parseClassPath(csu, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * parse the standard extension dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        private void  parseExtensionsDependencies() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            ExtensionDependency.checkExtensionsDependencies(jar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         * Parses value of the Class-Path manifest attribute and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
         * an array of URLs relative to the specified base URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        private URL[] parseClassPath(URL base, String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            throws MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            StringTokenizer st = new StringTokenizer(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            URL[] urls = new URL[st.countTokens()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                String path = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                urls[i] = new URL(base, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * Inner class used to represent a loader of classes and resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * from a file URL that refers to a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    private static class FileLoader extends Loader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        private File dir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        FileLoader(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            super(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (!"file".equals(url.getProtocol())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                throw new IllegalArgumentException("url");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            String path = url.getFile().replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            path = ParseUtil.decode(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            dir = new File(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * Returns the URL for a resource with the specified name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        URL findResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            Resource rsc = getResource(name, check);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            if (rsc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                return rsc.getURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        Resource getResource(final String name, boolean check) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            final URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                URL normalizedBase = new URL(getBaseURL(), ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                url = new URL(getBaseURL(), ParseUtil.encodePath(name, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                if (url.getFile().startsWith(normalizedBase.getFile()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    // requested resource had ../..'s in path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                if (check)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    URLClassPath.check(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                final File file =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    new File(dir, name.replace('/', File.separatorChar));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                    return new Resource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                        public String getName() { return name; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                        public URL getURL() { return url; };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                        public URL getCodeSourceURL() { return getBaseURL(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                        public InputStream getInputStream() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                            { return new FileInputStream(file); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                        public int getContentLength() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                            { return (int)file.length(); };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
}