jdk/src/share/classes/com/sun/media/sound/JSSecurityManager.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3453 55b3a9c935cd
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.sampled.AudioPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.misc.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/** Managing security in the Java Sound implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class contains all code that uses and is used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * SecurityManager.doPrivileged().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class JSSecurityManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** Prevent instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private JSSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /** Checks if the VM currently has a SecurityManager installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Note that this may change over time. So the result of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * should not be cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @return true if a SecurityManger is installed, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static boolean hasSecurityManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return (System.getSecurityManager() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    static void checkRecordPermission() throws SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            sm.checkPermission(new AudioPermission("record"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static void loadLibrary(final String libName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                if(Printer.debug) Printer.debug("using security manager to load library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                            System.loadLibrary(libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if(Printer.debug) Printer.debug("not using security manager to load library");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.loadLibrary(libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (Printer.debug) Printer.debug("loaded library " + libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (UnsatisfiedLinkError e2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (Printer.err)Printer.err("UnsatisfiedLinkError loading native library " + libName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static String getProperty(final String propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        String propertyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if(Printer.debug) Printer.debug("using JDK 1.2 security to get property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                return System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                propertyValue = (String) AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            } catch( Exception e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                propertyValue = System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            propertyValue = System.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return propertyValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /** Load properties from a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        This method tries to load properties from the filename give into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        the passed properties object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        If the file cannot be found or something else goes wrong,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        the method silently fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        @param properties The properties bundle to store the values of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        @param filename The filename of the properties file to load. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        filename is interpreted as relative to the subdirectory "lib" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        the JRE directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static void loadProperties(final Properties properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                               final String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if(hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // invoke the privileged action using 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                            loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                // try without using JDK 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            // not JDK 1.2 security, assume we already have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            loadPropertiesImpl(properties, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static void loadPropertiesImpl(Properties properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                           String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String fname = System.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (fname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                throw new Error("Can't find java.home ??");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            File f = new File(fname, "lib");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            f = new File(f, filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            fname = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            InputStream in = new FileInputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            BufferedInputStream bin = new BufferedInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                properties.load(bin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (Printer.trace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                System.err.println("Could not load properties file \"" + fname + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static ThreadGroup getTopmostThreadGroup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        ThreadGroup topmostThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if(hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                // invoke the privileged action using 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                        public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                return getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                topmostThreadGroup = (ThreadGroup) AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                // try without using JDK 1.2 security
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                topmostThreadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // not JDK 1.2 security, assume we already have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            topmostThreadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return topmostThreadGroup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    private static ThreadGroup getTopmostThreadGroupImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if(Printer.trace)Printer.trace(">> JSSecurityManager: getTopmostThreadGroupImpl()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        ThreadGroup g = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        while ((g.getParent() != null) && (g.getParent().getParent() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            g = g.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if(Printer.trace)Printer.trace("<< JSSecurityManager: getTopmostThreadGroupImpl() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /** Create a Thread in the topmost ThreadGroup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    static Thread createThread(final Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                               final String threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                               final boolean isDaemon, final int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                               final boolean doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        Thread thread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if(hasSecurityManager()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                            return createThreadImpl(runnable, threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                                    isDaemon, priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                                    doStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            thread = (Thread) AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if(Printer.debug) Printer.debug("created thread with JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if(Printer.debug)Printer.debug("not using JDK 1.2 security");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            thread = createThreadImpl(runnable, threadName, isDaemon, priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                      doStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private static Thread createThreadImpl(Runnable runnable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                           String threadName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                           boolean isDaemon, int priority,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                           boolean doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        ThreadGroup threadGroup = getTopmostThreadGroupImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        Thread thread = new Thread(threadGroup, runnable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (threadName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            thread.setName(threadName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        thread.setDaemon(isDaemon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (priority >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            thread.setPriority(priority);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (doStart) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            thread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    static List getProviders(final Class providerClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        PrivilegedAction action = new PrivilegedAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    List p = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    Iterator ps = Service.providers(providerClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    while (ps.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                            Object provider = ps.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                            if (providerClass.isInstance(provider)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                // $$mp 2003-08-22
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                // Always adding at the beginning reverses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                // order of the providers. So we no longer have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                // to do this in AudioSystem and MidiSystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                p.add(0, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            //$$fb 2002-11-07: do not fail on SPI not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            if (Printer.err) t.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        }                                                                  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        List providers = (List) AccessController.doPrivileged(action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}