1 /*******************************************************************************
2 * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16 package it.jnrpe.events;
17
18 import it.jnrpe.JNRPEExecutionContext;
19
20 import java.util.Collection;
21
22 /**
23 * This is an utility class than can be used to send simply events to all the
24 * registered listeners.
25 *
26 * @author Massimiliano Ziccardi
27 */
28 public final class EventsUtil {
29
30 /**
31 * Private default constructor.
32 */
33 private EventsUtil() {
34
35 }
36
37 /**
38 * This method sends log events to the registered listeners. It is an.
39 * utility method that relieve the programmer from the need to create all.
40 * the EventParam object just to send log events. Supported event type are.
41 * TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.
42 *
43 * @param listenersList
44 * The list of all the listeners that will receive the event
45 * @param sender
46 * The sender of the event (usually <code>this</code>)
47 * @param evt
48 * The event type
49 * @param message
50 * The log message
51 */
52 public static void sendEvent(
53 final Collection<IJNRPEEventListener> listenersList,
54 final Object sender, final LogEvent evt, final String message) {
55 if (listenersList == null || listenersList.isEmpty()) {
56 return;
57 }
58
59 sendEvent(listenersList, sender, evt, message, null);
60 }
61
62 public static void sendEvent(final JNRPEExecutionContext context,
63 final Object sender, final LogEvent evt, final String message) {
64 sendEvent(context, sender, evt, message, null);
65 }
66
67 public static void sendEvent(final JNRPEExecutionContext context,
68 final Object sender, final LogEvent evt, final String message,
69 final Throwable exception) {
70 sendEvent(context.getListeners(), sender, evt, message, exception);
71 }
72
73 /**
74 * This method sends log events to the registered listeners. It is an.
75 * utility method that relieve the programmer from the need to create all.
76 * the EventParam object just to send log events. Supported event type are.
77 * TRACE, DEBUG, INFO, WARNING, ERROR, FATAL.
78 *
79 * @param listenerList
80 * The list of all the listeners that will receive the event
81 * @param sender
82 * The sender of the event (usually <code>this</code>)
83 * @param evt
84 * The event type
85 * @param message
86 * The log message
87 * @param exception
88 * The exception to be, eventually, logged (can be null).
89 */
90 public static void sendEvent(
91 final Collection<IJNRPEEventListener> listenerList,
92 final Object sender, final LogEvent evt, final String message,
93 final Throwable exception) {
94 if (listenerList == null || listenerList.isEmpty()) {
95 return;
96 }
97
98 if (sender == null || evt == null || message == null) {
99 throw new NullPointerException(
100 "The sender, evt and message parameter can't be null");
101 }
102
103 if (exception != null) {
104 sendEvent(listenerList, sender, evt.name(), new EventMessageParam(
105 message), new EventExceptionParam(exception));
106 } else {
107 sendEvent(listenerList, sender, evt.name(), new EventMessageParam(
108 message));
109 }
110 }
111
112 /**
113 * This method is used to send custom events to the registered listeners.
114 * The event type can be a freely chosen string. A custom listener should be
115 * instructed to handle such event with its parameters.
116 *
117 * @param listenerList
118 * The list of all the listeners that will receive the event
119 * @param sender
120 * The sender of the event (usually <code>this</code>)
121 * @param customEvtType
122 * The custom event type
123 * @param paramsList
124 * The event parameters
125 */
126 public static void sendEvent(
127 final Collection<IJNRPEEventListener> listenerList,
128 final Object sender, final String customEvtType,
129 final EventParam... paramsList) {
130 if (sender == null || customEvtType == null) {
131 throw new NullPointerException(
132 "The sender and event type parameter can't be null");
133 }
134
135 if (listenerList == null || listenerList.isEmpty()) {
136 return;
137 }
138
139 SimpleEvent se = new SimpleEvent(customEvtType, paramsList);
140
141 for (IJNRPEEventListener listener : listenerList) {
142 listener.receive(sender, se);
143 }
144 }
145 }