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