~gryle-devel/gryle/trunk-deleted

« back to all changes in this revision

Viewing changes to src/javazoom/jl/player/FactoryRegistry.java

  • Committer: Richard Leo Marsh Warburton
  • Date: 2007-01-13 22:08:02 UTC
  • mto: (1.1.6 gryle)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: rlmw@viglab-28-20070113220802-6cjjur0hdk1rce47
added src files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 for all the
 
30
 * 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
        static private FactoryRegistry instance = null;
 
40
 
 
41
        static synchronized public FactoryRegistry systemRegistry() {
 
42
                if (instance == null) {
 
43
                        instance = new FactoryRegistry();
 
44
                        instance.registerDefaultFactories();
 
45
                }
 
46
                return instance;
 
47
        }
 
48
 
 
49
        protected Hashtable factories = new Hashtable();
 
50
 
 
51
        /**
 
52
         * Registers an <code>AudioDeviceFactory</code> instance with this
 
53
         * registry.
 
54
         */
 
55
        public void addFactory(AudioDeviceFactory factory) {
 
56
                factories.put(factory.getClass(), factory);
 
57
        }
 
58
 
 
59
        public void removeFactoryType(Class cls) {
 
60
                factories.remove(cls);
 
61
        }
 
62
 
 
63
        public void removeFactory(AudioDeviceFactory factory) {
 
64
                factories.remove(factory.getClass());
 
65
        }
 
66
 
 
67
        public AudioDevice createAudioDevice() throws JavaLayerException {
 
68
                AudioDevice device = null;
 
69
                AudioDeviceFactory[] factories = getFactoriesPriority();
 
70
 
 
71
                if (factories == null)
 
72
                        throw new JavaLayerException(this + ": no factories registered");
 
73
 
 
74
                JavaLayerException lastEx = null;
 
75
                for (int i = 0; (device == null) && (i < factories.length); i++) {
 
76
                        try {
 
77
                                device = factories[i].createAudioDevice();
 
78
                        } catch (JavaLayerException ex) {
 
79
                                lastEx = ex;
 
80
                        }
 
81
                }
 
82
 
 
83
                if (device == null && lastEx != null) {
 
84
                        throw new JavaLayerException("Cannot create AudioDevice", lastEx);
 
85
                }
 
86
 
 
87
                return device;
 
88
        }
 
89
 
 
90
        protected AudioDeviceFactory[] getFactoriesPriority() {
 
91
                AudioDeviceFactory[] fa = null;
 
92
                synchronized (factories) {
 
93
                        int size = factories.size();
 
94
                        if (size != 0) {
 
95
                                fa = new AudioDeviceFactory[size];
 
96
                                int idx = 0;
 
97
                                Enumeration e = factories.elements();
 
98
                                while (e.hasMoreElements()) {
 
99
                                        AudioDeviceFactory factory = (AudioDeviceFactory) e
 
100
                                                        .nextElement();
 
101
                                        fa[idx++] = factory;
 
102
                                }
 
103
                        }
 
104
                }
 
105
                return fa;
 
106
        }
 
107
 
 
108
        protected void registerDefaultFactories() {
 
109
                addFactory(new JavaSoundAudioDeviceFactory());
 
110
        }
 
111
}