Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/JDK13Services.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.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* Properties loaded from the properties file for default provider66* properties.67*/68private static Properties properties;6970/**71* Private, no-args constructor to ensure against instantiation.72*/73private JDK13Services() {74}7576/**77* Obtains a List containing installed instances of the providers for the78* requested service. The returned List is immutable.79*80* @param serviceClass The type of providers requested. This should be one81* of AudioFileReader.class, AudioFileWriter.class,82* FormatConversionProvider.class, MixerProvider.class,83* MidiDeviceProvider.class, MidiFileReader.class,84* MidiFileWriter.class or SoundbankReader.class.85*86* @return A List of providers of the requested type. This List is87* immutable.88*/89public static List<?> getProviders(final Class<?> serviceClass) {90final List<?> providers;91if (!MixerProvider.class.equals(serviceClass)92&& !FormatConversionProvider.class.equals(serviceClass)93&& !AudioFileReader.class.equals(serviceClass)94&& !AudioFileWriter.class.equals(serviceClass)95&& !MidiDeviceProvider.class.equals(serviceClass)96&& !SoundbankReader.class.equals(serviceClass)97&& !MidiFileWriter.class.equals(serviceClass)98&& !MidiFileReader.class.equals(serviceClass)) {99providers = new ArrayList<>(0);100} else {101providers = JSSecurityManager.getProviders(serviceClass);102}103return Collections.unmodifiableList(providers);104}105106/** Obtain the provider class name part of a default provider property.107@param typeClass The type of the default provider property. This108should be one of Receiver.class, Transmitter.class, Sequencer.class,109Synthesizer.class, SourceDataLine.class, TargetDataLine.class,110Clip.class or Port.class.111@return The value of the provider class name part of the property112(the part before the hash sign), if available. If the property is113not set or the value has no provider class name part, null is returned.114*/115public static synchronized String getDefaultProviderClassName(Class<?> typeClass) {116String value = null;117String defaultProviderSpec = getDefaultProvider(typeClass);118if (defaultProviderSpec != null) {119int hashpos = defaultProviderSpec.indexOf('#');120if (hashpos == 0) {121// instance name only; leave value as null122} else if (hashpos > 0) {123value = defaultProviderSpec.substring(0, hashpos);124} else {125value = defaultProviderSpec;126}127}128return value;129}130131/** Obtain the instance name part of a default provider property.132@param typeClass The type of the default provider property. This133should be one of Receiver.class, Transmitter.class, Sequencer.class,134Synthesizer.class, SourceDataLine.class, TargetDataLine.class,135Clip.class or Port.class.136@return The value of the instance name part of the property (the137part after the hash sign), if available. If the property is not set138or the value has no instance name part, null is returned.139*/140public static synchronized String getDefaultInstanceName(Class<?> typeClass) {141String value = null;142String defaultProviderSpec = getDefaultProvider(typeClass);143if (defaultProviderSpec != null) {144int hashpos = defaultProviderSpec.indexOf('#');145if (hashpos >= 0 && hashpos < defaultProviderSpec.length() - 1) {146value = defaultProviderSpec.substring(hashpos + 1);147}148}149return value;150}151152/** Obtain the value of a default provider property.153@param typeClass The type of the default provider property. This154should be one of Receiver.class, Transmitter.class, Sequencer.class,155Synthesizer.class, SourceDataLine.class, TargetDataLine.class,156Clip.class or Port.class.157@return The complete value of the property, if available.158If the property is not set, null is returned.159*/160private static synchronized String getDefaultProvider(Class<?> typeClass) {161if (!SourceDataLine.class.equals(typeClass)162&& !TargetDataLine.class.equals(typeClass)163&& !Clip.class.equals(typeClass)164&& !Port.class.equals(typeClass)165&& !Receiver.class.equals(typeClass)166&& !Transmitter.class.equals(typeClass)167&& !Synthesizer.class.equals(typeClass)168&& !Sequencer.class.equals(typeClass)) {169return null;170}171String name = typeClass.getName();172@SuppressWarnings("removal")173String value = AccessController.doPrivileged(174(PrivilegedAction<String>) () -> System.getProperty(name));175if (value == null) {176value = getProperties().getProperty(name);177}178if ("".equals(value)) {179value = null;180}181return value;182}183184/** Obtain a properties bundle containing property values from the185properties file. If the properties file could not be loaded,186the properties bundle is empty.187*/188private static synchronized Properties getProperties() {189if (properties == null) {190properties = new Properties();191JSSecurityManager.loadProperties(properties);192}193return properties;194}195}196197198