001: package com.teamkonzept.lib;
002:
003: import java.util.Hashtable;
004: import java.util.Vector;
005:
006: /**
007: * The Configuration Manager is designed as a registry for Configuration
008: * Listeners. These listeners may register themselves in different arbitrary
009: * contexts. Notification of registered listeners is triggered by a dedicated
010: * method.
011: *
012: * @version 1.0
013: * @since 1.0
014: * @author © 2001 Team-Konzept
015: */
016: public class ConfigurationManager {
017:
018: // $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/lib/ConfigurationManager.java,v 1.3 2001/02/14 15:07:11 uli Exp $
019:
020: // Constants
021:
022: /**
023: * The singleton instance of the Configuration Manager.
024: */
025: private static ConfigurationManager SINGLETON = null;
026:
027: // Attributes
028:
029: /**
030: * The registry for the listeners.
031: */
032: private Hashtable registry = null;
033:
034: // Constructors
035:
036: /**
037: * Avoids external instantiation.
038: */
039: private ConfigurationManager() {
040: this .registry = new Hashtable();
041: }
042:
043: // Singleton access
044:
045: /**
046: * Returns the singleton instance of the Configuration Manager.
047: *
048: * @return the singleton instance of the Configuration Manager.
049: */
050: public static synchronized ConfigurationManager getInstance() {
051: if (SINGLETON == null) {
052: SINGLETON = new ConfigurationManager();
053: }
054:
055: return SINGLETON;
056: }
057:
058: // Method implementations
059:
060: /**
061: * Registers a configuration listener with the manager.
062: * <P>
063: * The method silently returns when either the listener or the
064: * context object is null.
065: *
066: * @param listener a configuration listener.
067: * @param context a registration context.
068: */
069: public synchronized void registerConfigurationListener(
070: ConfigurationListener listener, Object context) {
071: if (listener == null || context == null) {
072: // Fail fast and safe ;-)
073: return;
074: }
075:
076: // Read registry.
077: Vector registrees = (Vector) registry.get(context);
078:
079: if (registrees == null) {
080: // Create listeners container.
081: registrees = new Vector();
082: }
083:
084: // Add registree.
085: registrees.addElement(listener);
086:
087: // Update registry.
088: registry.put(context, registrees);
089: }
090:
091: /**
092: * Deregisters a configuration listener with the manager.
093: * <P>
094: * The method silently returns when either the listener or the
095: * context object is null.
096: *
097: * @param listener a configuration listener.
098: * @param context a registration context.
099: */
100: public synchronized void deregisterConfigurationListener(
101: ConfigurationListener listener, Object context) {
102: if (listener == null || context == null) {
103: // Fail fast and safe ;-)
104: return;
105: }
106:
107: // Read registry.
108: Vector registrees = (Vector) registry.get(context);
109:
110: if (registrees == null) {
111: // Terminate.
112: return;
113: }
114:
115: // Remove registree.
116: registrees.removeElement(listener);
117:
118: // Update registry.
119: if (registrees.size() > 0) {
120: registry.put(context, registrees);
121: } else {
122: registry.remove(context);
123: }
124: }
125:
126: /**
127: * Notifies all listeners filed under the registration context.
128: * <P>
129: * The method silently returns when the context object is null.
130: *
131: * @param context a registration context.
132: */
133: public synchronized void notifyListeners(Object context)
134: throws TKException {
135: if (context == null) {
136: // Fail fast and safe ;-)
137: return;
138: }
139:
140: // Read registry.
141: Vector registrees = (Vector) registry.get(context);
142:
143: if (registrees == null) {
144: // Terminate.
145: return;
146: }
147:
148: // Use index driven iteration instead of an enumeration
149: // to avoid short lived object creation.
150: for (int i = 0; i < registrees.size(); i++) {
151: // Notify listener.
152: ((ConfigurationListener) registrees.elementAt(i))
153: .configurationChanged();
154: }
155: }
156:
157: }
|