Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/JDK13Services.java
38924 views
/*1* Copyright (c) 1999, 2014, 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.security.AccessController;28import java.security.PrivilegedAction;29import java.util.ArrayList;30import java.util.Collections;31import java.util.List;32import java.util.Properties;3334import javax.sound.midi.Receiver;35import javax.sound.midi.Sequencer;36import javax.sound.midi.Synthesizer;37import javax.sound.midi.Transmitter;38import javax.sound.midi.spi.MidiDeviceProvider;39import javax.sound.midi.spi.MidiFileReader;40import javax.sound.midi.spi.MidiFileWriter;41import javax.sound.midi.spi.SoundbankReader;42import javax.sound.sampled.Clip;43import javax.sound.sampled.Port;44import javax.sound.sampled.SourceDataLine;45import javax.sound.sampled.TargetDataLine;46import javax.sound.sampled.spi.AudioFileReader;47import javax.sound.sampled.spi.AudioFileWriter;48import javax.sound.sampled.spi.FormatConversionProvider;49import javax.sound.sampled.spi.MixerProvider;505152/**53* JDK13Services uses the Service class in JDK 1.3 to discover a list of service54* providers installed in the system.55* <p>56* This class is public because it is called from javax.sound.midi.MidiSystem57* and javax.sound.sampled.AudioSystem. The alternative would be to make58* JSSecurityManager public, which is considered worse.59*60* @author Matthias Pfisterer61*/62public final class JDK13Services {6364/**65* Filename of the properties file for default provider properties. This66* file is searched in the subdirectory "lib" of the JRE directory (this67* behaviour is hardcoded).68*/69private static final String PROPERTIES_FILENAME = "sound.properties";7071/**72* Properties loaded from the properties file for default provider73* properties.74*/75private static Properties properties;7677/**78* Private, no-args constructor to ensure against instantiation.79*/80private JDK13Services() {81}8283/**84* Obtains a List containing installed instances of the providers for the85* requested service. The returned List is immutable.86*87* @param serviceClass The type of providers requested. This should be one88* of AudioFileReader.class, AudioFileWriter.class,89* FormatConversionProvider.class, MixerProvider.class,90* MidiDeviceProvider.class, MidiFileReader.class,91* MidiFileWriter.class or SoundbankReader.class.92*93* @return A List of providers of the requested type. This List is94* immutable.95*/96public static List<?> getProviders(final Class<?> serviceClass) {97final List<?> providers;98if (!MixerProvider.class.equals(serviceClass)99&& !FormatConversionProvider.class.equals(serviceClass)100&& !AudioFileReader.class.equals(serviceClass)101&& !AudioFileWriter.class.equals(serviceClass)102&& !MidiDeviceProvider.class.equals(serviceClass)103&& !SoundbankReader.class.equals(serviceClass)104&& !MidiFileWriter.class.equals(serviceClass)105&& !MidiFileReader.class.equals(serviceClass)) {106providers = new ArrayList<>(0);107} else {108providers = JSSecurityManager.getProviders(serviceClass);109}110return Collections.unmodifiableList(providers);111}112113/** Obtain the provider class name part of a default provider property.114@param typeClass The type of the default provider property. This115should be one of Receiver.class, Transmitter.class, Sequencer.class,116Synthesizer.class, SourceDataLine.class, TargetDataLine.class,117Clip.class or Port.class.118@return The value of the provider class name part of the property119(the part before the hash sign), if available. If the property is120not set or the value has no provider class name part, null is returned.121*/122public static synchronized String getDefaultProviderClassName(Class typeClass) {123String value = null;124String defaultProviderSpec = getDefaultProvider(typeClass);125if (defaultProviderSpec != null) {126int hashpos = defaultProviderSpec.indexOf('#');127if (hashpos == 0) {128// instance name only; leave value as null129} else if (hashpos > 0) {130value = defaultProviderSpec.substring(0, hashpos);131} else {132value = defaultProviderSpec;133}134}135return value;136}137138139/** Obtain the instance name part of a default provider property.140@param typeClass The type of the default provider property. This141should be one of Receiver.class, Transmitter.class, Sequencer.class,142Synthesizer.class, SourceDataLine.class, TargetDataLine.class,143Clip.class or Port.class.144@return The value of the instance name part of the property (the145part after the hash sign), if available. If the property is not set146or the value has no instance name part, null is returned.147*/148public static synchronized String getDefaultInstanceName(Class typeClass) {149String value = null;150String defaultProviderSpec = getDefaultProvider(typeClass);151if (defaultProviderSpec != null) {152int hashpos = defaultProviderSpec.indexOf('#');153if (hashpos >= 0 && hashpos < defaultProviderSpec.length() - 1) {154value = defaultProviderSpec.substring(hashpos + 1);155}156}157return value;158}159160161/** Obtain the value of a default provider property.162@param typeClass The type of the default provider property. This163should be one of Receiver.class, Transmitter.class, Sequencer.class,164Synthesizer.class, SourceDataLine.class, TargetDataLine.class,165Clip.class or Port.class.166@return The complete value of the property, if available.167If the property is not set, null is returned.168*/169private static synchronized String getDefaultProvider(Class typeClass) {170if (!SourceDataLine.class.equals(typeClass)171&& !TargetDataLine.class.equals(typeClass)172&& !Clip.class.equals(typeClass)173&& !Port.class.equals(typeClass)174&& !Receiver.class.equals(typeClass)175&& !Transmitter.class.equals(typeClass)176&& !Synthesizer.class.equals(typeClass)177&& !Sequencer.class.equals(typeClass)) {178return null;179}180String name = typeClass.getName();181String value = AccessController.doPrivileged(182(PrivilegedAction<String>) () -> System.getProperty(name));183if (value == null) {184value = getProperties().getProperty(name);185}186if ("".equals(value)) {187value = null;188}189return value;190}191192193/** Obtain a properties bundle containing property values from the194properties file. If the properties file could not be loaded,195the properties bundle is empty.196*/197private static synchronized Properties getProperties() {198if (properties == null) {199properties = new Properties();200JSSecurityManager.loadProperties(properties, PROPERTIES_FILENAME);201}202return properties;203}204}205206207