jdk/src/share/classes/com/sun/media/sound/JSSecurityManager.java
author chegar
Fri, 02 Dec 2011 11:39:48 +0000
changeset 11123 399112af8803
parent 5506 202f599c92aa
child 18215 b2afd66ce6db
permissions -rw-r--r--
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3453
diff changeset
     2
 * Copyright (c) 1999, 2009, 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: 3453
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: 3453
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: 3453
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3453
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3453
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 com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
    37
import java.util.ServiceLoader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.sampled.AudioPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/** Managing security in the Java Sound implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class contains all code that uses and is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * SecurityManager.doPrivileged().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
class JSSecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** Prevent instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private JSSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** Checks if the VM currently has a SecurityManager installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Note that this may change over time. So the result of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * should not be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return true if a SecurityManger is installed, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static boolean hasSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return (System.getSecurityManager() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static void checkRecordPermission() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            sm.checkPermission(new AudioPermission("record"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static void loadLibrary(final String libName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            if (hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                if(Printer.debug) Printer.debug("using security manager to load library");
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
    81
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
    82
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                            System.loadLibrary(libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if(Printer.debug) Printer.debug("not using security manager to load library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                System.loadLibrary(libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (Printer.debug) Printer.debug("loaded library " + libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch (UnsatisfiedLinkError e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (Printer.err)Printer.err("UnsatisfiedLinkError loading native library " + libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static String getProperty(final String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String propertyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if(Printer.debug) Printer.debug("using JDK 1.2 security to get property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            try{
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   105
                PrivilegedAction<String> action = new PrivilegedAction<String>() {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   106
                        public String run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                return System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    };
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   114
                propertyValue = AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } catch( Exception e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                propertyValue = System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            propertyValue = System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return propertyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** Load properties from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        This method tries to load properties from the filename give into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        the passed properties object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        If the file cannot be found or something else goes wrong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        the method silently fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        @param properties The properties bundle to store the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        @param filename The filename of the properties file to load. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        filename is interpreted as relative to the subdirectory "lib" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        the JRE directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    static void loadProperties(final Properties properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                               final String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if(hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                // invoke the privileged action using 1.2 security
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   143
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   144
                        public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                            loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // try without using JDK 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // not JDK 1.2 security, assume we already have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static void loadPropertiesImpl(Properties properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                           String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        String fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            f = new File(f, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            InputStream in = new FileInputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                properties.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (Printer.trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                System.err.println("Could not load properties file \"" + fname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static ThreadGroup getTopmostThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        ThreadGroup topmostThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if(hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                // invoke the privileged action using 1.2 security
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   198
                PrivilegedAction<ThreadGroup> action = new PrivilegedAction<ThreadGroup>() {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   199
                        public ThreadGroup run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                return getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    };
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   207
                topmostThreadGroup = AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                // try without using JDK 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                topmostThreadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // not JDK 1.2 security, assume we already have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            topmostThreadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return topmostThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    private static ThreadGroup getTopmostThreadGroupImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if(Printer.trace)Printer.trace(">> JSSecurityManager: getTopmostThreadGroupImpl()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        ThreadGroup g = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        while ((g.getParent() != null) && (g.getParent().getParent() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            g = g.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if(Printer.trace)Printer.trace("<< JSSecurityManager: getTopmostThreadGroupImpl() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /** Create a Thread in the topmost ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    static Thread createThread(final Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                               final String threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                               final boolean isDaemon, final int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                               final boolean doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Thread thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if(hasSecurityManager()) {
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   241
            PrivilegedAction<Thread> action = new PrivilegedAction<Thread>() {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   242
                    public Thread run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            return createThreadImpl(runnable, threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                                    isDaemon, priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                                    doStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                };
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   252
            thread = AccessController.doPrivileged(action);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if(Printer.debug) Printer.debug("created thread with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if(Printer.debug)Printer.debug("not using JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            thread = createThreadImpl(runnable, threadName, isDaemon, priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                      doStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static Thread createThreadImpl(Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                           String threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                           boolean isDaemon, int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                           boolean doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ThreadGroup threadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        Thread thread = new Thread(threadGroup, runnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (threadName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            thread.setName(threadName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        thread.setDaemon(isDaemon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (priority >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            thread.setPriority(priority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   283
    static <T> List<T> getProviders(final Class<T> providerClass) {
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   284
        List<T> p = new ArrayList<>();
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   285
        // ServiceLoader creates "lazy" iterator instance, so it doesn't,
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   286
        // require do be called from privileged section
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   287
        final Iterator<T> ps = ServiceLoader.load(providerClass).iterator();
3453
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   288
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   289
        // the iterator's hasNext() method looks through classpath for
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   290
        // the provider class names, so it requires read permissions
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   291
        PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   292
            public Boolean run() {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   293
                return ps.hasNext();
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   294
            }
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   295
        };
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   296
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   297
        while (AccessController.doPrivileged(hasNextAction)) {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   298
            try {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   299
                // the iterator's next() method creates instances of the
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   300
                // providers and it should be called in the current security
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   301
                // context
11123
399112af8803 7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents: 5506
diff changeset
   302
                T provider = ps.next();
3453
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   303
                if (providerClass.isInstance(provider)) {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   304
                    // $$mp 2003-08-22
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   305
                    // Always adding at the beginning reverses the
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   306
                    // order of the providers. So we no longer have
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   307
                    // to do this in AudioSystem and MidiSystem.
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   308
                    p.add(0, provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
3453
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   310
            } catch (Throwable t) {
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   311
                //$$fb 2002-11-07: do not fail on SPI not found
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   312
                if (Printer.err) t.printStackTrace();
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   313
            }
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   314
        }
55b3a9c935cd 6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents: 2
diff changeset
   315
        return p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}