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

Save This Page
Home » openjdk-7 » com.sun.media » sound » [javadoc | source]