FactoryRegistry.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * 11/19/04 1.0 moved to LGPL.
  3. * 29/01/00 Initial version. mdm@techie.com
  4. *-----------------------------------------------------------------------
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Library General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. *----------------------------------------------------------------------
  19. */
  20. package javazoom.jl.player;
  21. import java.util.Enumeration;
  22. import java.util.Hashtable;
  23. import javazoom.jl.decoder.JavaLayerException;
  24. /**
  25. * The <code>FactoryRegistry</code> class stores the factories
  26. * for all the audio device implementations available in the system.
  27. * <p>
  28. * Instances of this class are thread-safe.
  29. *
  30. * @since 0.0.8
  31. * @author Mat McGowan
  32. */
  33. public class FactoryRegistry extends AudioDeviceFactory
  34. {
  35. static private FactoryRegistry instance = null;
  36. static synchronized public FactoryRegistry systemRegistry()
  37. {
  38. if (instance==null)
  39. {
  40. instance = new FactoryRegistry();
  41. instance.registerDefaultFactories();
  42. }
  43. return instance;
  44. }
  45. protected Hashtable factories = new Hashtable();
  46. /**
  47. * Registers an <code>AudioDeviceFactory</code> instance
  48. * with this registry.
  49. */
  50. public void addFactory(AudioDeviceFactory factory)
  51. {
  52. factories.put(factory.getClass(), factory);
  53. }
  54. public void removeFactoryType(Class cls)
  55. {
  56. factories.remove(cls);
  57. }
  58. public void removeFactory(AudioDeviceFactory factory)
  59. {
  60. factories.remove(factory.getClass());
  61. }
  62. public AudioDevice createAudioDevice() throws JavaLayerException
  63. {
  64. AudioDevice device = null;
  65. AudioDeviceFactory[] factories = getFactoriesPriority();
  66. if (factories==null)
  67. throw new JavaLayerException(this+": no factories registered");
  68. JavaLayerException lastEx = null;
  69. for (int i=0; (device==null) && (i<factories.length); i++)
  70. {
  71. try
  72. {
  73. device = factories[i].createAudioDevice();
  74. }
  75. catch (JavaLayerException ex)
  76. {
  77. lastEx = ex;
  78. }
  79. }
  80. if (device==null && lastEx!=null)
  81. {
  82. throw new JavaLayerException("Cannot create AudioDevice", lastEx);
  83. }
  84. return device;
  85. }
  86. protected AudioDeviceFactory[] getFactoriesPriority()
  87. {
  88. AudioDeviceFactory[] fa = null;
  89. synchronized (factories)
  90. {
  91. int size = factories.size();
  92. if (size!=0)
  93. {
  94. fa = new AudioDeviceFactory[size];
  95. int idx = 0;
  96. Enumeration e = factories.elements();
  97. while (e.hasMoreElements())
  98. {
  99. AudioDeviceFactory factory = (AudioDeviceFactory)e.nextElement();
  100. fa[idx++] = factory;
  101. }
  102. }
  103. }
  104. return fa;
  105. }
  106. protected void registerDefaultFactories()
  107. {
  108. addFactory(new JavaSoundAudioDeviceFactory());
  109. }
  110. }