1 /* 2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.media.sound; 27 28 import java.io.BufferedInputStream; 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.InputStream; 32 import java.security.AccessController; 33 import java.security.PrivilegedAction; 34 import java.util.ArrayList; 35 import java.util.Iterator; 36 import java.util.List; 37 import java.util.Properties; 38 import java.util.ServiceLoader; 39 40 import javax.sound.sampled.AudioPermission; 41 42 /** Managing security in the Java Sound implementation. 43 * This class contains all code that uses and is used by 44 * SecurityManager.doPrivileged(). 45 * 46 * @author Matthias Pfisterer 47 */ 48 final class JSSecurityManager { 49 50 /** Prevent instantiation. 51 */ 52 private JSSecurityManager() { 53 } 54 55 /** Checks if the VM currently has a SecurityManager installed. 56 * Note that this may change over time. So the result of this method 57 * should not be cached. 58 * 59 * @return true if a SecurityManger is installed, false otherwise. 60 */ 61 private static boolean hasSecurityManager() { 62 return (System.getSecurityManager() != null); 63 } 64 65 static void checkRecordPermission() throws SecurityException { 66 if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()"); 67 SecurityManager sm = System.getSecurityManager(); 68 if (sm != null) { 69 sm.checkPermission(new AudioPermission("record")); 70 } 71 } 72 73 /** Load properties from a file. 74 This method tries to load properties from the filename give into 75 the passed properties object. 76 If the file cannot be found or something else goes wrong, 77 the method silently fails. 78 @param properties The properties bundle to store the values of the 79 properties file. 80 @param filename The filename of the properties file to load. This 81 filename is interpreted as relative to the subdirectory "conf" in 82 the JRE directory. 83 */ 84 static void loadProperties(final Properties properties, 85 final String filename) { 86 if(hasSecurityManager()) { 87 try { 88 // invoke the privileged action using 1.2 security 89 PrivilegedAction<Void> action = new PrivilegedAction<Void>() { 90 @Override 91 public Void run() { 92 loadPropertiesImpl(properties, filename); 93 return null; 94 } 95 }; 96 AccessController.doPrivileged(action); 97 if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security"); 98 } catch (Exception e) { 99 if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security"); 100 // try without using JDK 1.2 security 101 loadPropertiesImpl(properties, filename); 102 } 103 } else { 104 // not JDK 1.2 security, assume we already have permission 105 loadPropertiesImpl(properties, filename); 106 } 107 } 108 109 private static void loadPropertiesImpl(Properties properties, 110 String filename) { 111 if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()"); 112 String fname = System.getProperty("java.home"); 113 try { 114 if (fname == null) { 115 throw new Error("Can't find java.home ??"); 116 } 117 File f = new File(fname, "conf"); 118 f = new File(f, filename); 119 fname = f.getCanonicalPath(); 120 InputStream in = new FileInputStream(fname); 121 BufferedInputStream bin = new BufferedInputStream(in); 122 try { 123 properties.load(bin); 124 } finally { 125 if (in != null) { 126 in.close(); 127 } 128 } 129 } catch (Throwable t) { 130 if (Printer.trace) { 131 System.err.println("Could not load properties file \"" + fname + "\""); 132 t.printStackTrace(); 133 } 134 } 135 if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed"); 136 } 137 138 /** Create a Thread in the current ThreadGroup. 139 */ 140 static Thread createThread(final Runnable runnable, 141 final String threadName, 142 final boolean isDaemon, final int priority, 143 final boolean doStart) 144 { 145 String name = (threadName != null) ? threadName : "JSSM Thread"; 146 Thread thread = new Thread(null, runnable, threadName, 0, false); 147 148 thread.setDaemon(isDaemon); 149 if (priority >= 0) { 150 thread.setPriority(priority); 151 } 152 if (doStart) { 153 thread.start(); 154 } 155 return thread; 156 } 157 158 static synchronized <T> List<T> getProviders(final Class<T> providerClass) { 159 List<T> p = new ArrayList<>(7); 160 // ServiceLoader creates "lazy" iterator instance, but it ensures that 161 // next/hasNext run with permissions that are restricted by whatever 162 // creates the ServiceLoader instance, so it requires to be called from 163 // privileged section 164 final PrivilegedAction<Iterator<T>> psAction = 165 new PrivilegedAction<Iterator<T>>() { 166 @Override 167 public Iterator<T> run() { 168 return ServiceLoader.load(providerClass).iterator(); 169 } 170 }; 171 final Iterator<T> ps = AccessController.doPrivileged(psAction); 172 173 // the iterator's hasNext() method looks through classpath for 174 // the provider class names, so it requires read permissions 175 PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() { 176 @Override 177 public Boolean run() { 178 return ps.hasNext(); 179 } 180 }; 181 182 while (AccessController.doPrivileged(hasNextAction)) { 183 try { 184 // the iterator's next() method creates instances of the 185 // providers and it should be called in the current security 186 // context 187 T provider = ps.next(); 188 if (providerClass.isInstance(provider)) { 189 // $$mp 2003-08-22 190 // Always adding at the beginning reverses the 191 // order of the providers. So we no longer have 192 // to do this in AudioSystem and MidiSystem. 193 p.add(0, provider); 194 } 195 } catch (Throwable t) { 196 //$$fb 2002-11-07: do not fail on SPI not found 197 if (Printer.err) t.printStackTrace(); 198 } 199 } 200 return p; 201 } 202 }