jdk/src/share/classes/sun/misc/Launcher.java
author alanb
Tue, 08 Feb 2011 19:31:44 +0000
changeset 8189 5fd29d2cbc4b
parent 7668 d4a77089c587
child 8552 f36470084574
permissions -rw-r--r--
4421494: infinite loop while parsing double literal Reviewed-by: darcy, alanb Contributed-by: dmitry.nadezhin@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6882
diff changeset
     2
 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3958
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3958
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3958
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3958
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3958
diff changeset
    23
 * questions.
2
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.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FilePermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URLClassLoader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URLStreamHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.URLStreamHandlerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.AccessController;
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
    41
import java.security.AllPermission;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PermissionCollection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.Permissions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.security.CodeSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.util.SecurityConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.net.www.ParseUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * This class is used by the system to launch the main application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
Launcher */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class Launcher {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static URLStreamHandlerFactory factory = new Factory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static Launcher launcher = new Launcher();
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
    59
    private static String bootClassPath =
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
    60
        System.getProperty("sun.boot.class.path");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static Launcher getLauncher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return launcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private ClassLoader loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public Launcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // Create the extension class loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        ClassLoader extcl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            extcl = ExtClassLoader.getExtClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new InternalError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "Could not create extension class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // Now create the class loader to use to launch the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            loader = AppClassLoader.getAppClassLoader(extcl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new InternalError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                "Could not create application class loader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        // Also set the context class loader for the primordial thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Thread.currentThread().setContextClassLoader(loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // Finally, install a security manager if requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        String s = System.getProperty("java.security.manager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            SecurityManager sm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if ("".equals(s) || "default".equals(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                sm = new java.lang.SecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    sm = (SecurityManager)loader.loadClass(s).newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.setSecurityManager(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new InternalError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    "Could not create SecurityManager: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the class loader used to launch the main application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public ClassLoader getClassLoader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   120
    public static void addURLToAppClassLoader(URL u) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   121
        AccessController.checkPermission(new AllPermission());
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   122
        ClassLoader loader = Launcher.getLauncher().getClassLoader();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   123
        ((Launcher.AppClassLoader) loader).addAppURL(u);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   124
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   125
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   126
    public static void addURLToExtClassLoader(URL u) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   127
        AccessController.checkPermission(new AllPermission());
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   128
        ClassLoader loader = Launcher.getLauncher().getClassLoader();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   129
        ((Launcher.ExtClassLoader) loader.getParent()).addExtURL(u);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   130
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   131
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * The class loader used for loading installed extensions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static class ExtClassLoader extends URLClassLoader {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   136
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   137
        static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   138
            ClassLoader.registerAsParallelCapable();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   139
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * create an ExtClassLoader. The ExtClassLoader is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * within a context that limits which files it can read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public static ExtClassLoader getExtClassLoader() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            final File[] dirs = getExtDirs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // Prior implementations of this doPrivileged() block supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // aa synthesized ACC via a call to the private method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // ExtClassLoader.getContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   154
                return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   155
                    new PrivilegedExceptionAction<ExtClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   156
                        public ExtClassLoader run() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            int len = dirs.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                MetaIndex.registerDirectory(dirs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            return new ExtClassLoader(dirs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            } catch (java.security.PrivilegedActionException e) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   165
                throw (IOException) e.getException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        void addExtURL(URL url) {
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   170
            super.addURL(url);
2
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
         * Creates a new ExtClassLoader for the specified directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        public ExtClassLoader(File[] dirs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            super(getExtURLs(dirs), null, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        private static File[] getExtDirs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            String s = System.getProperty("java.ext.dirs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            File[] dirs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                StringTokenizer st =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    new StringTokenizer(s, File.pathSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                int count = st.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                dirs = new File[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    dirs[i] = new File(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                dirs = new File[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            return dirs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        private static URL[] getExtURLs(File[] dirs) throws IOException {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   198
            Vector<URL> urls = new Vector<URL>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            for (int i = 0; i < dirs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                String[] files = dirs[i].list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (files != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    for (int j = 0; j < files.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        if (!files[j].equals("meta-index")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            File f = new File(dirs[i], files[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                            urls.add(getFileURL(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            URL[] ua = new URL[urls.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            urls.copyInto(ua);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return ua;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
         * Searches the installed extension directories for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
         * library name. For each extension directory, we first look for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
         * the native library in the subdirectory whose name is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
         * of the system property <code>os.arch</code>. Failing that, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * look in the extension directory itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public String findLibrary(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            name = System.mapLibraryName(name);
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   224
            URL[] urls = super.getURLs();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   225
            File prevDir = null;
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   226
            for (int i = 0; i < urls.length; i++) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   227
                // Get the ext directory from the URL
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   228
                File dir = new File(urls[i].getPath()).getParentFile();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   229
                if (dir != null && !dir.equals(prevDir)) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   230
                    // Look in architecture-specific subdirectory first
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   231
                    // Read from the saved system properties to avoid deadlock
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   232
                    String arch = VM.getSavedProperty("os.arch");
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   233
                    if (arch != null) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   234
                        File file = new File(new File(dir, arch), name);
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   235
                        if (file.exists()) {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   236
                            return file.getAbsolutePath();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   237
                        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   238
                    }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   239
                    // Then check the extension directory
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   240
                    File file = new File(dir, name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    if (file.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        return file.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   245
                prevDir = dir;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   250
        protected Class findClass(String name) throws ClassNotFoundException {
3958
b8acd5ee4f4f 6612680: Remove classloader dependency on jkernel
mchung
parents: 3111
diff changeset
   251
            BootClassLoaderHook.preLoadClass(name);
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   252
            return super.findClass(name);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   253
        }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   254
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        private static AccessControlContext getContext(File[] dirs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            PathPermissions perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                new PathPermissions(dirs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            ProtectionDomain domain = new ProtectionDomain(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                new CodeSource(perms.getCodeBase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    (java.security.cert.Certificate[]) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                new AccessControlContext(new ProtectionDomain[] { domain });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * The class loader used for loading from java.class.path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * runs in a restricted security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    static class AppClassLoader extends URLClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   279
        static {
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   280
            ClassLoader.registerAsParallelCapable();
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   281
        }
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   282
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        public static ClassLoader getAppClassLoader(final ClassLoader extcl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            final String s = System.getProperty("java.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            final File[] path = (s == null) ? new File[0] : getClassPath(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // Note: on bugid 4256530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            // Prior implementations of this doPrivileged() block supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // a rather restrictive ACC via a call to the private method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // AppClassLoader.getContext(). This proved overly restrictive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // when loading  classes. Specifically it prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            // accessClassInPackage.sun.* grants from being honored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            //
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   296
            return AccessController.doPrivileged(
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   297
                new PrivilegedAction<AppClassLoader>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   298
                    public AppClassLoader run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    URL[] urls =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        (s == null) ? new URL[0] : pathToURLs(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    return new AppClassLoader(urls, extcl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * Creates a new AppClassLoader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        AppClassLoader(URL[] urls, ClassLoader parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            super(urls, parent, factory);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * Override loadClass so we can checkPackageAccess.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
2448
1e8128f3ff61 4735126: (cl) ClassLoader.loadClass locks all instances in chain when delegating
valeriep
parents: 715
diff changeset
   316
        public Class loadClass(String name, boolean resolve)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            throws ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        {
3958
b8acd5ee4f4f 6612680: Remove classloader dependency on jkernel
mchung
parents: 3111
diff changeset
   319
            BootClassLoaderHook.preLoadClass(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            int i = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    sm.checkPackageAccess(name.substring(0, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return (super.loadClass(name, resolve));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
         * allow any classes loaded from classpath to exit the VM.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        protected PermissionCollection getPermissions(CodeSource codesource)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            PermissionCollection perms = super.getPermissions(codesource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            perms.add(new RuntimePermission("exitVM"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * This class loader supports dynamic additions to the class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * at runtime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * @see java.lang.instrument.Instrumentation#appendToSystemClassPathSearch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        private void appendToClassPathForInstrumentation(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            assert(Thread.holdsLock(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            // addURL is a no-op if path already contains the URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            super.addURL( getFileURL(new File(path)) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * create a context that can read any directories (recursively)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * mentioned in the class path. In the case of a jar, it has to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         * be the directory containing the jar, not just the jar, as jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         * files might refer to other jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        private static AccessControlContext getContext(File[] cp)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throws java.net.MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            PathPermissions perms =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                new PathPermissions(cp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            ProtectionDomain domain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                new ProtectionDomain(new CodeSource(perms.getCodeBase(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    (java.security.cert.Certificate[]) null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            AccessControlContext acc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                new AccessControlContext(new ProtectionDomain[] { domain });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   376
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   377
        void addAppURL(URL url) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   378
            super.addURL(url);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   379
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   382
    private static class BootClassPathHolder {
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   383
        static final URLClassPath bcp;
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   384
        static {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   385
            URL[] urls;
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   386
            if (bootClassPath != null) {
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   387
                urls = AccessController.doPrivileged(
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   388
                    new PrivilegedAction<URL[]>() {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   389
                        public URL[] run() {
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   390
                            File[] classPath = getClassPath(bootClassPath);
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   391
                            int len = classPath.length;
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   392
                            Set<File> seenDirs = new HashSet<File>();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   393
                            for (int i = 0; i < len; i++) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   394
                                File curEntry = classPath[i];
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   395
                                // Negative test used to properly handle
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   396
                                // nonexistent jars on boot class path
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   397
                                if (!curEntry.isDirectory()) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   398
                                    curEntry = curEntry.getParentFile();
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   399
                                }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   400
                                if (curEntry != null && seenDirs.add(curEntry)) {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   401
                                    MetaIndex.registerDirectory(curEntry);
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   402
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                            }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   404
                            return pathToURLs(classPath);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   407
                );
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   408
            } else {
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   409
                urls = new URL[0];
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   410
            }
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   411
            bcp = new URLClassPath(urls, factory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
3111
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   413
    }
fefdeafb7ab9 6797688: Umbrella: Merge all JDK 6u4 - 6u12 deployment code into JDK7
herrick
parents: 2448
diff changeset
   414
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   415
    public static URLClassPath getBootstrapClassPath() {
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   416
        URLClassPath bcp = BootClassPathHolder.bcp;
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   417
        // if DownloadManager is installed, return the bootstrap class path
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   418
        // maintained by the Java kernel
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   419
        BootClassLoaderHook hook = BootClassLoaderHook.getHook();
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   420
        return hook == null ? bcp : hook.getBootstrapClassPath(bcp, factory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private static URL[] pathToURLs(File[] path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        URL[] urls = new URL[path.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        for (int i = 0; i < path.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            urls[i] = getFileURL(path[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        // DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        //for (int i = 0; i < urls.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        //  System.out.println("urls[" + i + "] = " + '"' + urls[i] + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return urls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static File[] getClassPath(String cp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        File[] path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (cp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            int count = 0, maxCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            int pos = 0, lastPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            // Count the number of separators first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                maxCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                lastPos = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            path = new File[maxCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            lastPos = pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            // Now scan for each path component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            while ((pos = cp.indexOf(File.pathSeparator, lastPos)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                if (pos - lastPos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                    path[count++] = new File(cp.substring(lastPos, pos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    // empty path component translates to "."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    path[count++] = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                lastPos = pos + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // Make sure we include the last path component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            if (lastPos < cp.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                path[count++] = new File(cp.substring(lastPos));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                path[count++] = new File(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            // Trim array to correct size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (count != maxCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                File[] tmp = new File[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                System.arraycopy(path, 0, tmp, 0, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                path = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            path = new File[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        //for (int i = 0; i < path.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        //  System.out.println("path[" + i + "] = " + '"' + path[i] + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private static URLStreamHandler fileHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    static URL getFileURL(File file) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            file = file.getCanonicalFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        } catch (IOException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return ParseUtil.fileToEncodedURL(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // Should never happen since we specify the protocol...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * The stream handler factory for loading system protocol handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private static class Factory implements URLStreamHandlerFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        private static String PREFIX = "sun.net.www.protocol";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        public URLStreamHandler createURLStreamHandler(String protocol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            String name = PREFIX + "." + protocol + ".Handler";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                Class c = Class.forName(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                return (URLStreamHandler)c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            } catch (InstantiationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            throw new InternalError("could not load " + protocol +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                    "system protocol handler");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
class PathPermissions extends PermissionCollection {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    // use serialVersionUID from JDK 1.2.2 for interoperability
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private static final long serialVersionUID = 8133287259134945693L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    private File path[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private Permissions perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    URL codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    PathPermissions(File path[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        this.path = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        this.perms = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        this.codeBase = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    URL getCodeBase()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        return codeBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    public void add(java.security.Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        throw new SecurityException("attempt to add a permission");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    private synchronized void init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (perms != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // this is needed to be able to create the classloader itself!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        perms.add(SecurityConstants.CREATE_CLASSLOADER_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        // add permission to read any "java.*" property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        perms.add(new java.util.PropertyPermission("java.*",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            SecurityConstants.PROPERTY_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   557
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   558
            public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                for (int i=0; i < path.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    File f = path[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        path = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        path = f.getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        codeBase = Launcher.getFileURL(new File(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        if (path.endsWith(File.separator)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                            perms.add(new FilePermission(path+"-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                            perms.add(new FilePermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                                path + File.separator+"-",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                                SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        int endIndex = path.lastIndexOf(File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        if (endIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                            path = path.substring(0, endIndex+1) + "-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                            perms.add(new FilePermission(path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                                SecurityConstants.FILE_READ_ACTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                            // XXX?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public boolean implies(java.security.Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        return perms.implies(permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   601
    public java.util.Enumeration<Permission> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        synchronized (perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            return perms.elements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        if (perms == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        return perms.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}