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