Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/JSSecurityManager.java
41161 views
/*1* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import java.io.Reader;28import java.nio.file.Files;29import java.nio.file.Path;30import java.nio.file.Paths;31import java.security.AccessController;32import java.security.PrivilegedAction;33import java.util.ArrayList;34import java.util.Iterator;35import java.util.List;36import java.util.Properties;37import java.util.ServiceLoader;3839import javax.sound.sampled.AudioPermission;4041/** Managing security in the Java Sound implementation.42* This class contains all code that uses and is used by43* SecurityManager.doPrivileged().44*45* @author Matthias Pfisterer46*/47final class JSSecurityManager {4849/** Prevent instantiation.50*/51private JSSecurityManager() {52}5354static void checkRecordPermission() throws SecurityException {55@SuppressWarnings("removal")56SecurityManager sm = System.getSecurityManager();57if (sm != null) {58sm.checkPermission(new AudioPermission("record"));59}60}6162/**63* Load properties from a file.64* <p>65* This method tries to load properties from the filename give into the66* passed properties object. If the file cannot be found or something else67* goes wrong, the method silently fails.68* <p>69* If the file referenced in "javax.sound.config.file" property exists and70* the user has an access to it, then it will be loaded, otherwise default71* configuration file "JAVA_HOME/conf/sound.properties" will be loaded.72*73* @param properties the properties bundle to store the values of the74* properties file75*/76@SuppressWarnings("removal")77static void loadProperties(final Properties properties) {78final String customFile = AccessController.doPrivileged(79(PrivilegedAction<String>) () -> System.getProperty(80"javax.sound.config.file"));81if (customFile != null) {82if (loadPropertiesImpl(properties, customFile)) {83return;84}85}86AccessController.doPrivileged((PrivilegedAction<Void>) () -> {87final String home = System.getProperty("java.home");88if (home == null) {89throw new Error("Can't find java.home ??");90}91loadPropertiesImpl(properties, home, "conf", "sound.properties");92return null;93});94}9596private static boolean loadPropertiesImpl(final Properties properties,97String first, String... more) {98final Path fname = Paths.get(first, more);99try (final Reader reader = Files.newBufferedReader(fname)) {100properties.load(reader);101return true;102} catch (final Throwable t) {103return false;104}105}106107/** Create a Thread in the current ThreadGroup.108*/109static Thread createThread(final Runnable runnable,110final String threadName,111final boolean isDaemon, final int priority,112final boolean doStart)113{114String name = (threadName != null) ? threadName : "JSSM Thread";115Thread thread = new Thread(null, runnable, threadName, 0, false);116117thread.setDaemon(isDaemon);118if (priority >= 0) {119thread.setPriority(priority);120}121if (doStart) {122thread.start();123}124return thread;125}126127@SuppressWarnings("removal")128static synchronized <T> List<T> getProviders(final Class<T> providerClass) {129List<T> p = new ArrayList<>(7);130// ServiceLoader creates "lazy" iterator instance, but it ensures that131// next/hasNext run with permissions that are restricted by whatever132// creates the ServiceLoader instance, so it requires to be called from133// privileged section134final PrivilegedAction<Iterator<T>> psAction =135new PrivilegedAction<Iterator<T>>() {136@Override137public Iterator<T> run() {138return ServiceLoader.load(providerClass).iterator();139}140};141final Iterator<T> ps = AccessController.doPrivileged(psAction);142143// the iterator's hasNext() method looks through classpath for144// the provider class names, so it requires read permissions145PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {146@Override147public Boolean run() {148return ps.hasNext();149}150};151152while (AccessController.doPrivileged(hasNextAction)) {153try {154// the iterator's next() method creates instances of the155// providers and it should be called in the current security156// context157T provider = ps.next();158if (providerClass.isInstance(provider)) {159// $$mp 2003-08-22160// Always adding at the beginning reverses the161// order of the providers. So we no longer have162// to do this in AudioSystem and MidiSystem.163p.add(0, provider);164}165} catch (Throwable t) {166//$$fb 2002-11-07: do not fail on SPI not found167if (Printer.err) t.printStackTrace();168}169}170return p;171}172}173174175