1
2
3
4
5
6
7
8
9
10
11 package org.mule.context.notification;
12
13 import org.mule.api.context.notification.ServerNotification;
14 import org.mule.api.context.notification.ServerNotificationListener;
15 import org.mule.config.i18n.CoreMessages;
16 import static org.mule.context.notification.ServerNotificationManager.*;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30
31
32 class Configuration
33 {
34
35 protected Log logger = LogFactory.getLog(getClass());
36 private Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> interfaceToTypes =
37 new HashMap<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>>();
38 private Set<ListenerSubscriptionPair> listenerSubscriptionPairs = new HashSet<ListenerSubscriptionPair>();
39 private Set<Class<? extends ServerNotificationListener>> disabledInterfaces = new HashSet<Class<? extends ServerNotificationListener>>();
40 private Set<Class<? extends ServerNotification>> disabledNotificationTypes = new HashSet<Class<? extends ServerNotification>>();
41 private boolean dirty = true;
42 private Policy policy;
43
44 synchronized void addInterfaceToType(Class<? extends ServerNotificationListener> iface, Class<? extends ServerNotification> type)
45 {
46 dirty = true;
47 if (!ServerNotification.class.isAssignableFrom(type))
48 {
49 throw new IllegalArgumentException(
50 CoreMessages.propertyIsNotSupportedType("type",
51 ServerNotification.class, type).getMessage());
52 }
53 if (!interfaceToTypes.containsKey(iface))
54 {
55 interfaceToTypes.put(iface, new HashSet<Class<? extends ServerNotification>>());
56 }
57 Set<Class<? extends ServerNotification>> events = interfaceToTypes.get(iface);
58 events.add(type);
59 if (logger.isDebugEnabled())
60 {
61 logger.debug("Registered event type: " + type);
62 logger.debug("Binding listener type '" + iface + "' to event type '" + type + "'");
63 }
64 }
65
66
67
68
69
70 synchronized void addAllInterfaceToTypes(Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> interfaceToTypes) throws ClassNotFoundException
71 {
72 dirty = true;
73
74 for (Iterator ifaces = interfaceToTypes.keySet().iterator(); ifaces.hasNext();)
75 {
76 Object iface = ifaces.next();
77 addInterfaceToType(toClass(iface), toClass(interfaceToTypes.get(iface)));
78 }
79 }
80
81 synchronized void addListenerSubscriptionPair(ListenerSubscriptionPair pair)
82 {
83 dirty = true;
84 if (!listenerSubscriptionPairs.add(pair))
85 {
86 logger.warn(CoreMessages.notificationListenerSubscriptionAlreadyRegistered(pair));
87 }
88 }
89
90 synchronized void addAllListenerSubscriptionPairs(Collection pairs)
91 {
92 dirty = true;
93 for (Iterator listener = pairs.iterator(); listener.hasNext();)
94 {
95 addListenerSubscriptionPair((ListenerSubscriptionPair) listener.next());
96 }
97 }
98
99 synchronized void removeListener(ServerNotificationListener listener)
100 {
101 dirty = true;
102 Set<ListenerSubscriptionPair> toRemove = new HashSet<ListenerSubscriptionPair>();
103 for (Iterator listeners = listenerSubscriptionPairs.iterator(); listeners.hasNext();)
104 {
105 ListenerSubscriptionPair pair = (ListenerSubscriptionPair) listeners.next();
106 if (pair.getListener().equals(listener))
107 {
108 toRemove.add(pair);
109 }
110 }
111 listenerSubscriptionPairs.removeAll(toRemove);
112 }
113
114 synchronized void removeAllListeners(Collection listeners)
115 {
116 dirty = true;
117 for (Iterator listener = listeners.iterator(); listener.hasNext();)
118 {
119 removeListener((ServerNotificationListener) listener.next());
120 }
121 }
122
123 synchronized void disableInterface(Class<? extends ServerNotificationListener> iface)
124 {
125 dirty = true;
126 disabledInterfaces.add(iface);
127 }
128
129 synchronized void disabledAllInterfaces(Collection<Class<? extends ServerNotificationListener>> interfaces) throws ClassNotFoundException
130 {
131 dirty = true;
132 for (Iterator iface = interfaces.iterator(); iface.hasNext();)
133 {
134 disableInterface(toClass(iface.next()));
135 }
136 }
137
138 synchronized void disableType(Class<? extends ServerNotification> type)
139 {
140 dirty = true;
141 disabledNotificationTypes.add(type);
142 }
143
144 synchronized void disableAllTypes(Collection types) throws ClassNotFoundException
145 {
146 dirty = true;
147 for (Iterator event = types.iterator(); event.hasNext();)
148 {
149 disableType(toClass(event.next()));
150 }
151 }
152
153 synchronized Policy getPolicy()
154 {
155 if (dirty)
156 {
157 policy = new Policy(interfaceToTypes, listenerSubscriptionPairs, disabledInterfaces, disabledNotificationTypes);
158 dirty = false;
159 }
160 return policy;
161 }
162
163
164
165 Map<Class<? extends ServerNotificationListener>, Set<Class<? extends ServerNotification>>> getInterfaceToTypes()
166 {
167 return Collections.unmodifiableMap(interfaceToTypes);
168 }
169
170 Set<ListenerSubscriptionPair> getListeners()
171 {
172 return Collections.unmodifiableSet(listenerSubscriptionPairs);
173 }
174
175 }