diff options
Diffstat (limited to 'songdbj/javazoom/jl/player/FactoryRegistry.java')
-rw-r--r-- | songdbj/javazoom/jl/player/FactoryRegistry.java | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/songdbj/javazoom/jl/player/FactoryRegistry.java b/songdbj/javazoom/jl/player/FactoryRegistry.java new file mode 100644 index 0000000000..8919995802 --- /dev/null +++ b/songdbj/javazoom/jl/player/FactoryRegistry.java | |||
@@ -0,0 +1,129 @@ | |||
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 | |||
21 | package javazoom.jl.player; | ||
22 | |||
23 | import java.util.Enumeration; | ||
24 | import java.util.Hashtable; | ||
25 | |||
26 | import javazoom.jl.decoder.JavaLayerException; | ||
27 | |||
28 | /** | ||
29 | * The <code>FactoryRegistry</code> class stores the factories | ||
30 | * for all the audio device implementations available in the system. | ||
31 | * <p> | ||
32 | * Instances of this class are thread-safe. | ||
33 | * | ||
34 | * @since 0.0.8 | ||
35 | * @author Mat McGowan | ||
36 | */ | ||
37 | |||
38 | public class FactoryRegistry extends AudioDeviceFactory | ||
39 | { | ||
40 | static private FactoryRegistry instance = null; | ||
41 | |||
42 | static synchronized public FactoryRegistry systemRegistry() | ||
43 | { | ||
44 | if (instance==null) | ||
45 | { | ||
46 | instance = new FactoryRegistry(); | ||
47 | instance.registerDefaultFactories(); | ||
48 | } | ||
49 | return instance; | ||
50 | } | ||
51 | |||
52 | |||
53 | protected Hashtable factories = new Hashtable(); | ||
54 | |||
55 | /** | ||
56 | * Registers an <code>AudioDeviceFactory</code> instance | ||
57 | * with this registry. | ||
58 | */ | ||
59 | public void addFactory(AudioDeviceFactory factory) | ||
60 | { | ||
61 | factories.put(factory.getClass(), factory); | ||
62 | } | ||
63 | |||
64 | public void removeFactoryType(Class cls) | ||
65 | { | ||
66 | factories.remove(cls); | ||
67 | } | ||
68 | |||
69 | public void removeFactory(AudioDeviceFactory factory) | ||
70 | { | ||
71 | factories.remove(factory.getClass()); | ||
72 | } | ||
73 | |||
74 | public AudioDevice createAudioDevice() throws JavaLayerException | ||
75 | { | ||
76 | AudioDevice device = null; | ||
77 | AudioDeviceFactory[] factories = getFactoriesPriority(); | ||
78 | |||
79 | if (factories==null) | ||
80 | throw new JavaLayerException(this+": no factories registered"); | ||
81 | |||
82 | JavaLayerException lastEx = null; | ||
83 | for (int i=0; (device==null) && (i<factories.length); i++) | ||
84 | { | ||
85 | try | ||
86 | { | ||
87 | device = factories[i].createAudioDevice(); | ||
88 | } | ||
89 | catch (JavaLayerException ex) | ||
90 | { | ||
91 | lastEx = ex; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | if (device==null && lastEx!=null) | ||
96 | { | ||
97 | throw new JavaLayerException("Cannot create AudioDevice", lastEx); | ||
98 | } | ||
99 | |||
100 | return device; | ||
101 | } | ||
102 | |||
103 | |||
104 | protected AudioDeviceFactory[] getFactoriesPriority() | ||
105 | { | ||
106 | AudioDeviceFactory[] fa = null; | ||
107 | synchronized (factories) | ||
108 | { | ||
109 | int size = factories.size(); | ||
110 | if (size!=0) | ||
111 | { | ||
112 | fa = new AudioDeviceFactory[size]; | ||
113 | int idx = 0; | ||
114 | Enumeration e = factories.elements(); | ||
115 | while (e.hasMoreElements()) | ||
116 | { | ||
117 | AudioDeviceFactory factory = (AudioDeviceFactory)e.nextElement(); | ||
118 | fa[idx++] = factory; | ||
119 | } | ||
120 | } | ||
121 | } | ||
122 | return fa; | ||
123 | } | ||
124 | |||
125 | protected void registerDefaultFactories() | ||
126 | { | ||
127 | addFactory(new JavaSoundAudioDeviceFactory()); | ||
128 | } | ||
129 | } | ||