1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.context.notification; |
12 | |
|
13 | |
import org.mule.api.context.WorkManager; |
14 | |
import org.mule.api.context.notification.BlockingServerEvent; |
15 | |
import org.mule.api.context.notification.ServerNotification; |
16 | |
import org.mule.api.context.notification.ServerNotificationHandler; |
17 | |
import org.mule.api.context.notification.ServerNotificationListener; |
18 | |
import org.mule.api.lifecycle.Disposable; |
19 | |
import org.mule.api.lifecycle.LifecycleException; |
20 | |
import org.mule.util.ClassUtils; |
21 | |
|
22 | |
import java.util.Collection; |
23 | |
import java.util.Collections; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import javax.resource.spi.work.Work; |
27 | |
import javax.resource.spi.work.WorkException; |
28 | |
import javax.resource.spi.work.WorkListener; |
29 | |
|
30 | |
import edu.emory.mathcs.backport.java.util.concurrent.BlockingDeque; |
31 | |
import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingDeque; |
32 | |
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; |
33 | |
|
34 | |
import org.apache.commons.logging.Log; |
35 | |
import org.apache.commons.logging.LogFactory; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | 1180 | public class ServerNotificationManager implements Work, Disposable, ServerNotificationHandler |
64 | |
{ |
65 | |
|
66 | |
public static final String NULL_SUBSCRIPTION = "NULL"; |
67 | 1180 | protected Log logger = LogFactory.getLog(getClass()); |
68 | 1180 | private boolean dynamic = false; |
69 | 1180 | private Configuration configuration = new Configuration(); |
70 | 1180 | private AtomicBoolean disposed = new AtomicBoolean(false); |
71 | 1180 | private BlockingDeque eventQueue = new LinkedBlockingDeque(); |
72 | |
|
73 | |
public boolean isNotificationDynamic() |
74 | |
{ |
75 | 530 | return dynamic; |
76 | |
} |
77 | |
|
78 | |
public void setNotificationDynamic(boolean dynamic) |
79 | |
{ |
80 | 2 | this.dynamic = dynamic; |
81 | 2 | } |
82 | |
|
83 | |
public void start(WorkManager workManager, WorkListener workListener) throws LifecycleException |
84 | |
{ |
85 | |
try |
86 | |
{ |
87 | 1160 | workManager.scheduleWork(this, WorkManager.INDEFINITE, null, workListener); |
88 | |
} |
89 | 0 | catch (WorkException e) |
90 | |
{ |
91 | 0 | throw new LifecycleException(e, this); |
92 | 1160 | } |
93 | 1160 | } |
94 | |
|
95 | |
public void addInterfaceToType(Class iface, Class event) |
96 | |
{ |
97 | 12816 | configuration.addInterfaceToType(iface, event); |
98 | 12816 | } |
99 | |
|
100 | |
public void setInterfaceToTypes(Map interfaceToEvents) throws ClassNotFoundException |
101 | |
{ |
102 | 0 | configuration.addAllInterfaceToTypes(interfaceToEvents); |
103 | 0 | } |
104 | |
|
105 | |
public void addListenerSubscriptionPair(ListenerSubscriptionPair pair) |
106 | |
{ |
107 | 0 | configuration.addListenerSubscriptionPair(pair); |
108 | 0 | } |
109 | |
|
110 | |
public void addListener(ServerNotificationListener listener) |
111 | |
{ |
112 | 14 | configuration.addListenerSubscriptionPair(new ListenerSubscriptionPair(listener)); |
113 | 14 | } |
114 | |
|
115 | |
public void addListenerSubscription(ServerNotificationListener listener, String subscription) |
116 | |
{ |
117 | 40 | configuration.addListenerSubscriptionPair(new ListenerSubscriptionPair(listener, subscription)); |
118 | 40 | } |
119 | |
|
120 | |
public void setAllListenerSubscriptionPairs(Collection pairs) |
121 | |
{ |
122 | 0 | configuration.addAllListenerSubscriptionPairs(pairs); |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
public void removeListener(ServerNotificationListener listener) |
129 | |
{ |
130 | 4 | configuration.removeListener(listener); |
131 | 4 | } |
132 | |
|
133 | |
public void removeAllListeners(Collection listeners) |
134 | |
{ |
135 | 0 | configuration.removeAllListeners(listeners); |
136 | 0 | } |
137 | |
|
138 | |
public void disableInterface(Class iface) throws ClassNotFoundException |
139 | |
{ |
140 | 2 | configuration.disableInterface(iface); |
141 | 2 | } |
142 | |
|
143 | |
public void setDisabledInterfaces(Collection interfaces) throws ClassNotFoundException |
144 | |
{ |
145 | 0 | configuration.disabledAllInterfaces(interfaces); |
146 | 0 | } |
147 | |
|
148 | |
public void disableType(Class type) throws ClassNotFoundException |
149 | |
{ |
150 | 8 | configuration.disableType(type); |
151 | 8 | } |
152 | |
|
153 | |
public void setDisabledTypes(Collection types) throws ClassNotFoundException |
154 | |
{ |
155 | 0 | configuration.disableAllTypes(types); |
156 | 0 | } |
157 | |
|
158 | |
public void fireNotification(ServerNotification notification) |
159 | |
{ |
160 | 10888 | if (!disposed.get()) |
161 | |
{ |
162 | 10888 | if (notification instanceof BlockingServerEvent) |
163 | |
{ |
164 | 10386 | notifyListeners(notification); |
165 | |
} |
166 | |
else |
167 | |
{ |
168 | |
try |
169 | |
{ |
170 | 502 | eventQueue.put(notification); |
171 | |
} |
172 | 0 | catch (InterruptedException e) |
173 | |
{ |
174 | 0 | if (!disposed.get()) |
175 | |
{ |
176 | 0 | logger.error("Failed to queue notification: " + notification, e); |
177 | |
} |
178 | 502 | } |
179 | |
} |
180 | |
} |
181 | |
else |
182 | |
{ |
183 | 0 | logger.warn("Notification not enqueued after ServerNotificationManager disposal: " + notification); |
184 | |
} |
185 | 10888 | } |
186 | |
|
187 | |
public boolean isNotificationEnabled(Class type) |
188 | |
{ |
189 | 654 | return configuration.getPolicy().isNotificationEnabled(type); |
190 | |
} |
191 | |
|
192 | |
public void dispose() |
193 | |
{ |
194 | 1144 | disposed.set(true); |
195 | 1144 | configuration = null; |
196 | 1144 | } |
197 | |
|
198 | |
protected void notifyListeners(ServerNotification notification) |
199 | |
{ |
200 | 10891 | if (!disposed.get()) |
201 | |
{ |
202 | 10887 | configuration.getPolicy().dispatch(notification); |
203 | |
} |
204 | |
else |
205 | |
{ |
206 | 4 | logger.warn("Notification not delivered after ServerNotificationManager disposal: " + notification); |
207 | |
} |
208 | 10891 | } |
209 | |
|
210 | |
public void release() |
211 | |
{ |
212 | 0 | dispose(); |
213 | 0 | } |
214 | |
|
215 | |
public void run() |
216 | |
{ |
217 | 2788 | while (!disposed.get()) |
218 | |
{ |
219 | |
try |
220 | |
{ |
221 | 1644 | ServerNotification notification = (ServerNotification) eventQueue.take(); |
222 | 495 | notifyListeners(notification); |
223 | |
} |
224 | 1133 | catch (InterruptedException e) |
225 | |
{ |
226 | |
|
227 | 1628 | } |
228 | |
} |
229 | 1144 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
public static Class toClass(Object value) throws ClassNotFoundException |
235 | |
{ |
236 | |
Class clazz; |
237 | 0 | if (value instanceof String) |
238 | |
{ |
239 | 0 | clazz = ClassUtils.loadClass(value.toString(), value.getClass()); |
240 | |
} |
241 | 0 | else if(value instanceof Class) |
242 | |
{ |
243 | 0 | clazz = (Class)value; |
244 | |
} |
245 | |
else |
246 | |
{ |
247 | 0 | throw new IllegalArgumentException("Notification types and listeners must be a Class with fully qualified class name. Value is: " + value); |
248 | |
} |
249 | 0 | return clazz; |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
Policy getPolicy() |
255 | |
{ |
256 | 6 | return configuration.getPolicy(); |
257 | |
} |
258 | |
|
259 | |
public Map getInterfaceToTypes() |
260 | |
{ |
261 | 0 | return Collections.unmodifiableMap(configuration.getInterfaceToTypes()); |
262 | |
} |
263 | |
|
264 | |
public Collection getListeners() |
265 | |
{ |
266 | 0 | return Collections.unmodifiableCollection(configuration.getListeners()); |
267 | |
} |
268 | |
|
269 | |
} |