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