jdk/src/share/classes/com/sun/media/sound/JDK13Services.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 3452 e18ac2718a06
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.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.sound.sampled.spi.AudioFileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.sound.sampled.spi.AudioFileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.sound.sampled.spi.FormatConversionProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.sound.sampled.spi.MixerProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.sound.midi.spi.MidiFileReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.sound.midi.spi.MidiFileWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.midi.spi.SoundbankReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.midi.spi.MidiDeviceProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * JDK13Services uses the Service class in JDK 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * to discover a list of service providers installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * in the system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class is public because it is called from javax.sound.midi.MidiSystem
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * and javax.sound.sampled.AudioSystem. The alternative would be to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * JSSecurityManager public, which is considered worse.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Matthias Pfisterer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class JDK13Services {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** The default for the length of the period to hold the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        This value is given in milliseconds. It is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        1 minute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final long DEFAULT_CACHING_PERIOD = 60000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /** Filename of the properties file for default provider properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        This file is searched in the subdirectory "lib" of the JRE directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        (this behaviour is hardcoded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final String PROPERTIES_FILENAME = "sound.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** Cache for the providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Class objects of the provider type (MixerProvider, MidiDeviceProvider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ...) are used as keys. The values are instances of ProviderCache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static Map providersCacheMap = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /** The length of the period to hold the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        This value is given in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static long cachingPeriod = DEFAULT_CACHING_PERIOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** Properties loaded from the properties file for default provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static Properties properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Private, no-args constructor to ensure against instantiation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private JDK13Services() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** Set the period provider lists are cached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        This method is only intended for testing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static void setCachingPeriod(int seconds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        cachingPeriod = seconds * 1000L;
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
    /** Obtains a List containing installed instances of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        providers for the requested service.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        The List of providers is cached for the period of time given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        {@link #cachingPeriod cachingPeriod}. During this period, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        List instance is returned for the same type of provider. After this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        period, a new instance is constructed and returned. The returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        List is immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        @param serviceClass The type of providers requested. This should be one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        of AudioFileReader.class, AudioFileWriter.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        FormatConversionProvider.class, MixerProvider.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        MidiDeviceProvider.class, MidiFileReader.class, MidiFileWriter.class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        SoundbankReader.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        @return A List of providers of the requested type. This List is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static synchronized List getProviders(Class serviceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ProviderCache cache = (ProviderCache) providersCacheMap.get(serviceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (cache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            cache = new ProviderCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            providersCacheMap.put(serviceClass, cache);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (cache.providers == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.currentTimeMillis() > cache.lastUpdate + cachingPeriod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            cache.providers = Collections.unmodifiableList(JSSecurityManager.getProviders(serviceClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            cache.lastUpdate = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return cache.providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /** Obtain the provider class name part of a default provider property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        @param typeClass The type of the default provider property. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        should be one of Receiver.class, Transmitter.class, Sequencer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Clip.class or Port.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        @return The value of the provider class name part of the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        (the part before the hash sign), if available. If the property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        not set or the value has no provider class name part, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static synchronized String getDefaultProviderClassName(Class typeClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String defaultProviderSpec = getDefaultProvider(typeClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (defaultProviderSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int hashpos = defaultProviderSpec.indexOf('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (hashpos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // instance name only; leave value as null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else if (hashpos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                value = defaultProviderSpec.substring(0, hashpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                value = defaultProviderSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /** Obtain the instance name part of a default provider property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        @param typeClass The type of the default provider property. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        should be one of Receiver.class, Transmitter.class, Sequencer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Clip.class or Port.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        @return The value of the instance name part of the property (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        part after the hash sign), if available. If the property is not set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        or the value has no instance name part, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static synchronized String getDefaultInstanceName(Class typeClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String defaultProviderSpec = getDefaultProvider(typeClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (defaultProviderSpec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int hashpos = defaultProviderSpec.indexOf('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if (hashpos >= 0 && hashpos < defaultProviderSpec.length() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                value = defaultProviderSpec.substring(hashpos + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** Obtain the value of a default provider property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        @param typeClass The type of the default provider property. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        should be one of Receiver.class, Transmitter.class, Sequencer.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Clip.class or Port.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        @return The complete value of the property, if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        If the property is not set, null is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static synchronized String getDefaultProvider(Class typeClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        String propertyName = typeClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        value = JSSecurityManager.getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            value = getProperties().getProperty(propertyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ("".equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /** Obtain a properties bundle containing property values from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        properties file. If the properties file could not be loaded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        the properties bundle is empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private static synchronized Properties getProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (properties == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            properties = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            JSSecurityManager.loadProperties(properties, PROPERTIES_FILENAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    // INNER CLASSES
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static class ProviderCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // System time of the last update in milliseconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        public long lastUpdate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        // The providers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        public List providers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
}