001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.util;
018
019 import java.util.EventObject;
020 import java.util.List;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Processor;
025 import org.apache.camel.Route;
026 import org.apache.camel.impl.ServiceSupport;
027 import org.apache.camel.spi.EventFactory;
028 import org.apache.camel.spi.EventNotifier;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031
032 /**
033 * Helper for easily sending event notifications in a single line of code
034 *
035 * @version $Revision: 898746 $
036 */
037 public final class EventHelper {
038
039 private static final Log LOG = LogFactory.getLog(EventHelper.class);
040
041 private EventHelper() {
042 }
043
044 public static void notifyCamelContextStarting(CamelContext context) {
045 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
046 if (notifiers == null || notifiers.isEmpty()) {
047 return;
048 }
049
050 for (EventNotifier notifier : notifiers) {
051 if (notifier.isIgnoreCamelContextEvents()) {
052 continue;
053 }
054
055 EventFactory factory = context.getManagementStrategy().getEventFactory();
056 if (factory == null) {
057 return;
058 }
059 EventObject event = factory.createCamelContextStartingEvent(context);
060 if (event == null) {
061 return;
062 }
063 doNotifyEvent(notifier, event);
064 }
065 }
066
067 public static void notifyCamelContextStarted(CamelContext context) {
068 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
069 if (notifiers == null || notifiers.isEmpty()) {
070 return;
071 }
072
073 for (EventNotifier notifier : notifiers) {
074 if (notifier.isIgnoreCamelContextEvents()) {
075 continue;
076 }
077
078 EventFactory factory = context.getManagementStrategy().getEventFactory();
079 if (factory == null) {
080 return;
081 }
082 EventObject event = factory.createCamelContextStartedEvent(context);
083 if (event == null) {
084 return;
085 }
086 doNotifyEvent(notifier, event);
087 }
088 }
089
090 public static void notifyCamelContextStartupFailed(CamelContext context, Exception cause) {
091 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
092 if (notifiers == null || notifiers.isEmpty()) {
093 return;
094 }
095
096 for (EventNotifier notifier : notifiers) {
097 if (notifier.isIgnoreCamelContextEvents()) {
098 continue;
099 }
100
101 EventFactory factory = context.getManagementStrategy().getEventFactory();
102 if (factory == null) {
103 return;
104 }
105 EventObject event = factory.createCamelContextStartupFailureEvent(context, cause);
106 if (event == null) {
107 return;
108 }
109 doNotifyEvent(notifier, event);
110 }
111 }
112
113 public static void notifyCamelContextStopping(CamelContext context) {
114 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
115 if (notifiers == null || notifiers.isEmpty()) {
116 return;
117 }
118
119 for (EventNotifier notifier : notifiers) {
120 if (notifier.isIgnoreCamelContextEvents()) {
121 return;
122 }
123
124 EventFactory factory = context.getManagementStrategy().getEventFactory();
125 if (factory == null) {
126 return;
127 }
128 EventObject event = factory.createCamelContextStoppingEvent(context);
129 if (event == null) {
130 return;
131 }
132 doNotifyEvent(notifier, event);
133 }
134 }
135
136 public static void notifyCamelContextStopped(CamelContext context) {
137 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
138 if (notifiers == null || notifiers.isEmpty()) {
139 return;
140 }
141
142 for (EventNotifier notifier : notifiers) {
143 if (notifier.isIgnoreCamelContextEvents()) {
144 continue;
145 }
146
147 EventFactory factory = context.getManagementStrategy().getEventFactory();
148 if (factory == null) {
149 return;
150 }
151 EventObject event = factory.createCamelContextStoppedEvent(context);
152 if (event == null) {
153 return;
154 }
155 doNotifyEvent(notifier, event);
156 }
157 }
158
159 public static void notifyCamelContextStopFailed(CamelContext context, Exception cause) {
160 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
161 if (notifiers == null || notifiers.isEmpty()) {
162 return;
163 }
164
165 for (EventNotifier notifier : notifiers) {
166 if (notifier.isIgnoreCamelContextEvents()) {
167 continue;
168 }
169
170 EventFactory factory = context.getManagementStrategy().getEventFactory();
171 if (factory == null) {
172 return;
173 }
174 EventObject event = factory.createCamelContextStopFailureEvent(context, cause);
175 if (event == null) {
176 return;
177 }
178 doNotifyEvent(notifier, event);
179 }
180 }
181
182 public static void notifyServiceStopFailure(CamelContext context, Object service, Exception cause) {
183 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
184 if (notifiers == null || notifiers.isEmpty()) {
185 return;
186 }
187
188 for (EventNotifier notifier : notifiers) {
189 if (notifier.isIgnoreServiceEvents()) {
190 continue;
191 }
192
193 EventFactory factory = context.getManagementStrategy().getEventFactory();
194 if (factory == null) {
195 return;
196 }
197 EventObject event = factory.createServiceStopFailureEvent(context, service, cause);
198 if (event == null) {
199 return;
200 }
201 doNotifyEvent(notifier, event);
202 }
203 }
204
205 public static void notifyServiceStartupFailure(CamelContext context, Object service, Exception cause) {
206 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
207 if (notifiers == null || notifiers.isEmpty()) {
208 return;
209 }
210
211 for (EventNotifier notifier : notifiers) {
212 if (notifier.isIgnoreServiceEvents()) {
213 continue;
214 }
215
216 EventFactory factory = context.getManagementStrategy().getEventFactory();
217 if (factory == null) {
218 return;
219 }
220 EventObject event = factory.createServiceStartupFailureEvent(context, service, cause);
221 if (event == null) {
222 return;
223 }
224 doNotifyEvent(notifier, event);
225 }
226 }
227
228 public static void notifyRouteStarted(CamelContext context, Route route) {
229 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
230 if (notifiers == null || notifiers.isEmpty()) {
231 return;
232 }
233
234 for (EventNotifier notifier : notifiers) {
235 if (notifier.isIgnoreRouteEvents()) {
236 return;
237 }
238
239 EventFactory factory = context.getManagementStrategy().getEventFactory();
240 if (factory == null) {
241 return;
242 }
243 EventObject event = factory.createRouteStartedEvent(route);
244 if (event == null) {
245 return;
246 }
247 doNotifyEvent(notifier, event);
248 }
249 }
250
251 public static void notifyRouteStopped(CamelContext context, Route route) {
252 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
253 if (notifiers == null || notifiers.isEmpty()) {
254 return;
255 }
256
257 for (EventNotifier notifier : notifiers) {
258 if (notifier.isIgnoreRouteEvents()) {
259 continue;
260 }
261
262 EventFactory factory = context.getManagementStrategy().getEventFactory();
263 if (factory == null) {
264 return;
265 }
266 EventObject event = factory.createRouteStoppedEvent(route);
267 if (event == null) {
268 return;
269 }
270 doNotifyEvent(notifier, event);
271 }
272 }
273
274 public static void notifyExchangeCreated(CamelContext context, Exchange exchange) {
275 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
276 if (notifiers == null || notifiers.isEmpty()) {
277 return;
278 }
279
280 for (EventNotifier notifier : notifiers) {
281 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCreatedEvent()) {
282 continue;
283 }
284
285 EventFactory factory = context.getManagementStrategy().getEventFactory();
286 if (factory == null) {
287 return;
288 }
289 EventObject event = factory.createExchangeCreatedEvent(exchange);
290 if (event == null) {
291 return;
292 }
293 doNotifyEvent(notifier, event);
294 }
295 }
296
297 public static void notifyExchangeDone(CamelContext context, Exchange exchange) {
298 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
299 if (notifiers == null || notifiers.isEmpty()) {
300 return;
301 }
302
303 for (EventNotifier notifier : notifiers) {
304 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCompletedEvent()) {
305 continue;
306 }
307
308 EventFactory factory = context.getManagementStrategy().getEventFactory();
309 if (factory == null) {
310 return;
311 }
312 EventObject event = factory.createExchangeCompletedEvent(exchange);
313 if (event == null) {
314 return;
315 }
316 doNotifyEvent(notifier, event);
317 }
318 }
319
320 public static void notifyExchangeFailed(CamelContext context, Exchange exchange) {
321 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
322 if (notifiers == null || notifiers.isEmpty()) {
323 return;
324 }
325
326 for (EventNotifier notifier : notifiers) {
327 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailureEvents()) {
328 continue;
329 }
330
331 EventFactory factory = context.getManagementStrategy().getEventFactory();
332 if (factory == null) {
333 return;
334 }
335 EventObject event = factory.createExchangeFailureEvent(exchange);
336 if (event == null) {
337 return;
338 }
339 doNotifyEvent(notifier, event);
340 }
341 }
342
343 public static void notifyExchangeFailureHandled(CamelContext context, Exchange exchange, Processor failureHandler,
344 boolean deadLetterChannel) {
345 List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
346 if (notifiers == null || notifiers.isEmpty()) {
347 return;
348 }
349
350 for (EventNotifier notifier : notifiers) {
351 if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailureEvents()) {
352 continue;
353 }
354
355 EventFactory factory = context.getManagementStrategy().getEventFactory();
356 if (factory == null) {
357 return;
358 }
359 EventObject event = factory.createExchangeFailureHandledEvent(exchange, failureHandler, deadLetterChannel);
360 if (event == null) {
361 return;
362 }
363 doNotifyEvent(notifier, event);
364 }
365 }
366
367 private static void doNotifyEvent(EventNotifier notifier, EventObject event) {
368 // only notify if notifier is started
369 boolean started = true;
370 if (notifier instanceof ServiceSupport) {
371 started = ((ServiceSupport) notifier).isStarted();
372 }
373 if (!started) {
374 if (LOG.isDebugEnabled()) {
375 LOG.debug("Ignoring notifying event " + event + ". The EventNotifier has not been started yet: " + notifier);
376 }
377 return;
378 }
379
380 if (!notifier.isEnabled(event)) {
381 if (LOG.isTraceEnabled()) {
382 LOG.trace("Notification of event is disabled: " + event);
383 }
384 return;
385 }
386
387 try {
388 notifier.notify(event);
389 } catch (Exception e) {
390 LOG.warn("Error notifying event " + event + ". This exception will be ignored. ", e);
391 }
392 }
393
394 }