KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > lib > ConfigurationManager


1 package com.teamkonzept.lib;
2
3 import java.util.Hashtable JavaDoc;
4 import java.util.Vector JavaDoc;
5
6 /**
7  * The Configuration Manager is designed as a registry for Configuration
8  * Listeners. These listeners may register themselves in different arbitrary
9  * contexts. Notification of registered listeners is triggered by a dedicated
10  * method.
11  *
12  * @version 1.0
13  * @since 1.0
14  * @author © 2001 Team-Konzept
15  */

16 public class ConfigurationManager
17 {
18
19     // $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/ConfigurationManager.java,v 1.3 2001/02/14 15:07:11 uli Exp $
20

21     // Constants
22

23     /**
24      * The singleton instance of the Configuration Manager.
25      */

26     private static ConfigurationManager SINGLETON = null;
27
28
29     // Attributes
30

31     /**
32      * The registry for the listeners.
33      */

34     private Hashtable JavaDoc registry = null;
35
36
37     // Constructors
38

39     /**
40      * Avoids external instantiation.
41      */

42     private ConfigurationManager ()
43     {
44         this.registry = new Hashtable JavaDoc();
45     }
46
47
48     // Singleton access
49

50     /**
51      * Returns the singleton instance of the Configuration Manager.
52      *
53      * @return the singleton instance of the Configuration Manager.
54      */

55     public static synchronized ConfigurationManager getInstance ()
56     {
57         if (SINGLETON == null)
58         {
59             SINGLETON = new ConfigurationManager();
60         }
61
62         return SINGLETON;
63     }
64
65
66     // Method implementations
67

68     /**
69      * Registers a configuration listener with the manager.
70      * <P>
71      * The method silently returns when either the listener or the
72      * context object is null.
73      *
74      * @param listener a configuration listener.
75      * @param context a registration context.
76      */

77     public synchronized void registerConfigurationListener (ConfigurationListener listener,
78                                                             Object JavaDoc context)
79     {
80         if (listener == null ||
81             context == null)
82         {
83             // Fail fast and safe ;-)
84
return;
85         }
86
87         // Read registry.
88
Vector JavaDoc registrees = (Vector JavaDoc) registry.get(context);
89
90         if (registrees == null)
91         {
92             // Create listeners container.
93
registrees = new Vector JavaDoc();
94         }
95
96         // Add registree.
97
registrees.addElement(listener);
98
99         // Update registry.
100
registry.put(context, registrees);
101     }
102
103     /**
104      * Deregisters a configuration listener with the manager.
105      * <P>
106      * The method silently returns when either the listener or the
107      * context object is null.
108      *
109      * @param listener a configuration listener.
110      * @param context a registration context.
111      */

112     public synchronized void deregisterConfigurationListener (ConfigurationListener listener,
113                                                               Object JavaDoc context)
114     {
115         if (listener == null ||
116             context == null)
117         {
118             // Fail fast and safe ;-)
119
return;
120         }
121
122         // Read registry.
123
Vector JavaDoc registrees = (Vector JavaDoc) registry.get(context);
124
125         if (registrees == null)
126         {
127             // Terminate.
128
return;
129         }
130
131         // Remove registree.
132
registrees.removeElement(listener);
133
134         // Update registry.
135
if (registrees.size() > 0)
136         {
137             registry.put(context, registrees);
138         }
139         else
140         {
141             registry.remove(context);
142         }
143     }
144
145     /**
146      * Notifies all listeners filed under the registration context.
147      * <P>
148      * The method silently returns when the context object is null.
149      *
150      * @param context a registration context.
151      */

152     public synchronized void notifyListeners (Object JavaDoc context)
153         throws TKException
154     {
155         if (context == null)
156         {
157             // Fail fast and safe ;-)
158
return;
159         }
160
161         // Read registry.
162
Vector JavaDoc registrees = (Vector JavaDoc) registry.get(context);
163
164         if (registrees == null)
165         {
166             // Terminate.
167
return;
168         }
169
170         // Use index driven iteration instead of an enumeration
171
// to avoid short lived object creation.
172
for (int i = 0; i < registrees.size(); i++)
173         {
174             // Notify listener.
175
((ConfigurationListener) registrees.elementAt(i)).configurationChanged();
176         }
177     }
178
179 }
180
Popular Tags