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.Endpoint;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.Processor;
026    import org.apache.camel.Route;
027    import org.apache.camel.StatefulService;
028    import org.apache.camel.spi.EventFactory;
029    import org.apache.camel.spi.EventNotifier;
030    import org.slf4j.Logger;
031    import org.slf4j.LoggerFactory;
032    
033    /**
034     * Helper for easily sending event notifications in a single line of code
035     *
036     * @version 
037     */
038    public final class EventHelper {
039    
040        private static final Logger LOG = LoggerFactory.getLogger(EventHelper.class);
041    
042        private EventHelper() {
043        }
044    
045        public static void notifyCamelContextStarting(CamelContext context) {
046            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
047            if (notifiers == null || notifiers.isEmpty()) {
048                return;
049            }
050    
051            for (EventNotifier notifier : notifiers) {
052                if (notifier.isIgnoreCamelContextEvents()) {
053                    continue;
054                }
055    
056                EventFactory factory = context.getManagementStrategy().getEventFactory();
057                if (factory == null) {
058                    return;
059                }
060                EventObject event = factory.createCamelContextStartingEvent(context);
061                if (event == null) {
062                    return;
063                }
064                doNotifyEvent(notifier, event);
065            }
066        }
067    
068        public static void notifyCamelContextStarted(CamelContext context) {
069            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
070            if (notifiers == null || notifiers.isEmpty()) {
071                return;
072            }
073    
074            for (EventNotifier notifier : notifiers) {
075                if (notifier.isIgnoreCamelContextEvents()) {
076                    continue;
077                }
078    
079                EventFactory factory = context.getManagementStrategy().getEventFactory();
080                if (factory == null) {
081                    return;
082                }
083                EventObject event = factory.createCamelContextStartedEvent(context);
084                if (event == null) {
085                    return;
086                }
087                doNotifyEvent(notifier, event);
088            }
089        }
090    
091        public static void notifyCamelContextStartupFailed(CamelContext context, Throwable cause) {
092            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
093            if (notifiers == null || notifiers.isEmpty()) {
094                return;
095            }
096    
097            for (EventNotifier notifier : notifiers) {
098                if (notifier.isIgnoreCamelContextEvents()) {
099                    continue;
100                }
101    
102                EventFactory factory = context.getManagementStrategy().getEventFactory();
103                if (factory == null) {
104                    return;
105                }
106                EventObject event = factory.createCamelContextStartupFailureEvent(context, cause);
107                if (event == null) {
108                    return;
109                }
110                doNotifyEvent(notifier, event);
111            }
112        }
113    
114        public static void notifyCamelContextStopping(CamelContext context) {
115            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
116            if (notifiers == null || notifiers.isEmpty()) {
117                return;
118            }
119    
120            for (EventNotifier notifier : notifiers) {
121                if (notifier.isIgnoreCamelContextEvents()) {
122                    continue;
123                }
124    
125                EventFactory factory = context.getManagementStrategy().getEventFactory();
126                if (factory == null) {
127                    return;
128                }
129                EventObject event = factory.createCamelContextStoppingEvent(context);
130                if (event == null) {
131                    return;
132                }
133                doNotifyEvent(notifier, event);
134            }
135        }
136    
137        public static void notifyCamelContextStopped(CamelContext context) {
138            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
139            if (notifiers == null || notifiers.isEmpty()) {
140                return;
141            }
142    
143            for (EventNotifier notifier : notifiers) {
144                if (notifier.isIgnoreCamelContextEvents()) {
145                    continue;
146                }
147    
148                EventFactory factory = context.getManagementStrategy().getEventFactory();
149                if (factory == null) {
150                    return;
151                }
152                EventObject event = factory.createCamelContextStoppedEvent(context);
153                if (event == null) {
154                    return;
155                }
156                doNotifyEvent(notifier, event);
157            }
158        }
159    
160        public static void notifyCamelContextStopFailed(CamelContext context, Throwable cause) {
161            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
162            if (notifiers == null || notifiers.isEmpty()) {
163                return;
164            }
165    
166            for (EventNotifier notifier : notifiers) {
167                if (notifier.isIgnoreCamelContextEvents()) {
168                    continue;
169                }
170    
171                EventFactory factory = context.getManagementStrategy().getEventFactory();
172                if (factory == null) {
173                    return;
174                }
175                EventObject event = factory.createCamelContextStopFailureEvent(context, cause);
176                if (event == null) {
177                    return;
178                }
179                doNotifyEvent(notifier, event);
180            }
181        }
182    
183        public static void notifyServiceStopFailure(CamelContext context, Object service, Throwable cause) {
184            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
185            if (notifiers == null || notifiers.isEmpty()) {
186                return;
187            }
188    
189            for (EventNotifier notifier : notifiers) {
190                if (notifier.isIgnoreServiceEvents()) {
191                    continue;
192                }
193    
194                EventFactory factory = context.getManagementStrategy().getEventFactory();
195                if (factory == null) {
196                    return;
197                }
198                EventObject event = factory.createServiceStopFailureEvent(context, service, cause);
199                if (event == null) {
200                    return;
201                }
202                doNotifyEvent(notifier, event);
203            }
204        }
205    
206        public static void notifyServiceStartupFailure(CamelContext context, Object service, Throwable cause) {
207            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
208            if (notifiers == null || notifiers.isEmpty()) {
209                return;
210            }
211    
212            for (EventNotifier notifier : notifiers) {
213                if (notifier.isIgnoreServiceEvents()) {
214                    continue;
215                }
216    
217                EventFactory factory = context.getManagementStrategy().getEventFactory();
218                if (factory == null) {
219                    return;
220                }
221                EventObject event = factory.createServiceStartupFailureEvent(context, service, cause);
222                if (event == null) {
223                    return;
224                }
225                doNotifyEvent(notifier, event);
226            }
227        }
228    
229        public static void notifyRouteStarted(CamelContext context, Route route) {
230            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
231            if (notifiers == null || notifiers.isEmpty()) {
232                return;
233            }
234    
235            for (EventNotifier notifier : notifiers) {
236                if (notifier.isIgnoreRouteEvents()) {
237                    continue;
238                }
239    
240                EventFactory factory = context.getManagementStrategy().getEventFactory();
241                if (factory == null) {
242                    return;
243                }
244                EventObject event = factory.createRouteStartedEvent(route);
245                if (event == null) {
246                    return;
247                }
248                doNotifyEvent(notifier, event);
249            }
250        }
251    
252        public static void notifyRouteStopped(CamelContext context, Route route) {
253            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
254            if (notifiers == null || notifiers.isEmpty()) {
255                return;
256            }
257    
258            for (EventNotifier notifier : notifiers) {
259                if (notifier.isIgnoreRouteEvents()) {
260                    continue;
261                }
262    
263                EventFactory factory = context.getManagementStrategy().getEventFactory();
264                if (factory == null) {
265                    return;
266                }
267                EventObject event = factory.createRouteStoppedEvent(route);
268                if (event == null) {
269                    return;
270                }
271                doNotifyEvent(notifier, event);
272            }
273        }
274    
275        public static void notifyExchangeCreated(CamelContext context, Exchange exchange) {
276            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
277                // do not generate events for an notify event
278                return;
279            }
280    
281            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
282            if (notifiers == null || notifiers.isEmpty()) {
283                return;
284            }
285    
286            for (EventNotifier notifier : notifiers) {
287                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCreatedEvent()) {
288                    continue;
289                }
290    
291                EventFactory factory = context.getManagementStrategy().getEventFactory();
292                if (factory == null) {
293                    return;
294                }
295                EventObject event = factory.createExchangeCreatedEvent(exchange);
296                if (event == null) {
297                    return;
298                }
299                doNotifyEvent(notifier, event);
300            }
301        }
302    
303        public static void notifyExchangeDone(CamelContext context, Exchange exchange) {
304            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
305                // do not generate events for an notify event
306                return;
307            }
308    
309            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
310            if (notifiers == null || notifiers.isEmpty()) {
311                return;
312            }
313    
314            for (EventNotifier notifier : notifiers) {
315                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeCompletedEvent()) {
316                    continue;
317                }
318    
319                EventFactory factory = context.getManagementStrategy().getEventFactory();
320                if (factory == null) {
321                    return;
322                }
323                EventObject event = factory.createExchangeCompletedEvent(exchange);
324                if (event == null) {
325                    return;
326                }
327                doNotifyEvent(notifier, event);
328            }
329        }
330    
331        public static void notifyExchangeFailed(CamelContext context, Exchange exchange) {
332            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
333                // do not generate events for an notify event
334                return;
335            }
336    
337            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
338            if (notifiers == null || notifiers.isEmpty()) {
339                return;
340            }
341    
342            for (EventNotifier notifier : notifiers) {
343                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) {
344                    continue;
345                }
346    
347                EventFactory factory = context.getManagementStrategy().getEventFactory();
348                if (factory == null) {
349                    return;
350                }
351                EventObject event = factory.createExchangeFailedEvent(exchange);
352                if (event == null) {
353                    return;
354                }
355                doNotifyEvent(notifier, event);
356            }
357        }
358    
359        public static void notifyExchangeFailureHandled(CamelContext context, Exchange exchange, Processor failureHandler,
360                                                        boolean deadLetterChannel) {
361            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
362                // do not generate events for an notify event
363                return;
364            }
365    
366            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
367            if (notifiers == null || notifiers.isEmpty()) {
368                return;
369            }
370    
371            for (EventNotifier notifier : notifiers) {
372                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) {
373                    continue;
374                }
375    
376                EventFactory factory = context.getManagementStrategy().getEventFactory();
377                if (factory == null) {
378                    return;
379                }
380                EventObject event = factory.createExchangeFailureHandledEvent(exchange, failureHandler, deadLetterChannel);
381                if (event == null) {
382                    return;
383                }
384                doNotifyEvent(notifier, event);
385            }
386        }
387    
388        public static void notifyExchangeRedelivery(CamelContext context, Exchange exchange, int attempt) {
389            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
390                // do not generate events for an notify event
391                return;
392            }
393    
394            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
395            if (notifiers == null || notifiers.isEmpty()) {
396                return;
397            }
398    
399            for (EventNotifier notifier : notifiers) {
400                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeFailedEvents()) {
401                    continue;
402                }
403    
404                EventFactory factory = context.getManagementStrategy().getEventFactory();
405                if (factory == null) {
406                    return;
407                }
408                EventObject event = factory.createExchangeRedeliveryEvent(exchange, attempt);
409                if (event == null) {
410                    return;
411                }
412                doNotifyEvent(notifier, event);
413            }
414        }
415    
416        public static void notifyExchangeSending(CamelContext context, Exchange exchange, Endpoint endpoint) {
417            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
418                // do not generate events for an notify event
419                return;
420            }
421    
422            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
423            if (notifiers == null || notifiers.isEmpty()) {
424                return;
425            }
426    
427            for (EventNotifier notifier : notifiers) {
428                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) {
429                    continue;
430                }
431    
432                EventFactory factory = context.getManagementStrategy().getEventFactory();
433                if (factory == null) {
434                    return;
435                }
436                EventObject event = factory.createExchangeSendingEvent(exchange, endpoint);
437                if (event == null) {
438                    return;
439                }
440                doNotifyEvent(notifier, event);
441            }
442        }
443    
444        public static void notifyExchangeSent(CamelContext context, Exchange exchange, Endpoint endpoint, long timeTaken) {
445            if (exchange.getProperty(Exchange.NOTIFY_EVENT, false, Boolean.class)) {
446                // do not generate events for an notify event
447                return;
448            }
449    
450            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
451            if (notifiers == null || notifiers.isEmpty()) {
452                return;
453            }
454    
455            for (EventNotifier notifier : notifiers) {
456                if (notifier.isIgnoreExchangeEvents() || notifier.isIgnoreExchangeSentEvents()) {
457                    continue;
458                }
459    
460                EventFactory factory = context.getManagementStrategy().getEventFactory();
461                if (factory == null) {
462                    return;
463                }
464                EventObject event = factory.createExchangeSentEvent(exchange, endpoint, timeTaken);
465                if (event == null) {
466                    return;
467                }
468                doNotifyEvent(notifier, event);
469            }
470        }
471    
472        public static void notifyCamelContextSuspending(CamelContext context) {
473            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
474            if (notifiers == null || notifiers.isEmpty()) {
475                return;
476            }
477    
478            for (EventNotifier notifier : notifiers) {
479                if (notifier.isIgnoreCamelContextEvents()) {
480                    continue;
481                }
482    
483                EventFactory factory = context.getManagementStrategy().getEventFactory();
484                if (factory == null) {
485                    return;
486                }
487                EventObject event = factory.createCamelContextSuspendingEvent(context);
488                if (event == null) {
489                    return;
490                }
491                doNotifyEvent(notifier, event);
492            }
493        }
494    
495        public static void notifyCamelContextSuspended(CamelContext context) {
496            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
497            if (notifiers == null || notifiers.isEmpty()) {
498                return;
499            }
500    
501            for (EventNotifier notifier : notifiers) {
502                if (notifier.isIgnoreCamelContextEvents()) {
503                    continue;
504                }
505    
506                EventFactory factory = context.getManagementStrategy().getEventFactory();
507                if (factory == null) {
508                    return;
509                }
510                EventObject event = factory.createCamelContextSuspendedEvent(context);
511                if (event == null) {
512                    return;
513                }
514                doNotifyEvent(notifier, event);
515            }
516        }
517    
518        public static void notifyCamelContextResuming(CamelContext context) {
519            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
520            if (notifiers == null || notifiers.isEmpty()) {
521                return;
522            }
523    
524            for (EventNotifier notifier : notifiers) {
525                if (notifier.isIgnoreCamelContextEvents()) {
526                    continue;
527                }
528    
529                EventFactory factory = context.getManagementStrategy().getEventFactory();
530                if (factory == null) {
531                    return;
532                }
533                EventObject event = factory.createCamelContextResumingEvent(context);
534                if (event == null) {
535                    return;
536                }
537                doNotifyEvent(notifier, event);
538            }
539        }
540    
541        public static void notifyCamelContextResumed(CamelContext context) {
542            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
543            if (notifiers == null || notifiers.isEmpty()) {
544                return;
545            }
546    
547            for (EventNotifier notifier : notifiers) {
548                if (notifier.isIgnoreCamelContextEvents()) {
549                    continue;
550                }
551    
552                EventFactory factory = context.getManagementStrategy().getEventFactory();
553                if (factory == null) {
554                    return;
555                }
556                EventObject event = factory.createCamelContextResumedEvent(context);
557                if (event == null) {
558                    return;
559                }
560                doNotifyEvent(notifier, event);
561            }
562        }
563    
564        public static void notifyCamelContextResumeFailed(CamelContext context, Throwable cause) {
565            List<EventNotifier> notifiers = context.getManagementStrategy().getEventNotifiers();
566            if (notifiers == null || notifiers.isEmpty()) {
567                return;
568            }
569    
570            for (EventNotifier notifier : notifiers) {
571                if (notifier.isIgnoreCamelContextEvents()) {
572                    continue;
573                }
574    
575                EventFactory factory = context.getManagementStrategy().getEventFactory();
576                if (factory == null) {
577                    return;
578                }
579                EventObject event = factory.createCamelContextResumeFailureEvent(context, cause);
580                if (event == null) {
581                    return;
582                }
583                doNotifyEvent(notifier, event);
584            }
585        }
586    
587        private static void doNotifyEvent(EventNotifier notifier, EventObject event) {
588            // only notify if notifier is started
589            boolean started = true;
590            if (notifier instanceof StatefulService) {
591                started = ((StatefulService) notifier).isStarted();
592            }
593            if (!started) {
594                LOG.debug("Ignoring notifying event {}. The EventNotifier has not been started yet: {}", event, notifier);
595                return;
596            }
597    
598            if (!notifier.isEnabled(event)) {
599                LOG.trace("Notification of event is disabled: {}", event);
600                return;
601            }
602    
603            try {
604                notifier.notify(event);
605            } catch (Throwable e) {
606                LOG.warn("Error notifying event " + event + ". This exception will be ignored. ", e);
607            }
608        }
609    
610    }