tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / linuxtools / tmf / core / signal / TmfSignalManager.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Update register methods
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.signal;
15
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Executors;
24
25 import org.eclipse.linuxtools.internal.tmf.core.Activator;
26 import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer;
27
28 /**
29 * This class manages the set of signal listeners and the signals they are
30 * interested in. When a signal is broadcasted, the appropriate listeners
31 * signal handlers are invoked.
32 *
33 * @version 1.0
34 * @author Francois Chouinard
35 */
36 public class TmfSignalManager {
37
38 // The set of event listeners and their corresponding handler methods.
39 // Note: listeners could be restricted to ITmfComponents but there is no
40 // harm in letting anyone use this since it is not tied to anything but
41 // the signal data type.
42 private static Map<Object, Method[]> fListeners = new HashMap<>();
43 private static Map<Object, Method[]> fVIPListeners = new HashMap<>();
44
45 // The signal executor for asynchronous signals
46 private static final ExecutorService fExecutor = Executors.newSingleThreadExecutor();
47
48 // If requested, add universal signal tracer
49 // TODO: Temporary solution: should be enabled/disabled dynamically
50 private static boolean fTraceIsActive = false;
51 private static TmfSignalTracer fSignalTracer;
52
53 static {
54 if (fTraceIsActive) {
55 fSignalTracer = TmfSignalTracer.getInstance();
56 register(fSignalTracer);
57 }
58 }
59
60 /**
61 * Register an object to the signal manager. This object can then implement
62 * handler methods, marked with @TmfSignalHandler and with the expected
63 * signal type as parameter.
64 *
65 * @param listener
66 * The object that will be notified of new signals
67 */
68 public static synchronized void register(Object listener) {
69 deregister(listener); // make sure that listener is only registered once
70 Method[] methods = getSignalHandlerMethods(listener);
71 if (methods.length > 0) {
72 fListeners.put(listener, methods);
73 }
74 }
75
76 /**
77 * Register an object to the signal manager as a "VIP" listener. All VIP
78 * listeners will all receive the signal before the manager moves on to the
79 * lowly, non-VIP listeners.
80 *
81 * @param listener
82 * The object that will be notified of new signals
83 */
84 public static synchronized void registerVIP(Object listener) {
85 deregister(listener); // make sure that listener is only registered once
86 Method[] methods = getSignalHandlerMethods(listener);
87 if (methods.length > 0) {
88 fVIPListeners.put(listener, methods);
89 }
90 }
91
92 /**
93 * De-register a listener object from the signal manager. This means that
94 * its @TmfSignalHandler methods will no longer be called.
95 *
96 * @param listener
97 * The object to de-register
98 */
99 public static synchronized void deregister(Object listener) {
100 fVIPListeners.remove(listener);
101 fListeners.remove(listener);
102 }
103
104 /**
105 * Returns the list of signal handlers in the listener. Signal handler name
106 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
107 *
108 * @param listener
109 * @return
110 */
111 private static Method[] getSignalHandlerMethods(Object listener) {
112 List<Method> handlers = new ArrayList<>();
113 Method[] methods = listener.getClass().getMethods();
114 for (Method method : methods) {
115 if (method.isAnnotationPresent(TmfSignalHandler.class)) {
116 handlers.add(method);
117 }
118 }
119 return handlers.toArray(new Method[handlers.size()]);
120 }
121
122 static int fSignalId = 0;
123
124 /**
125 * Invokes the handling methods that listens to signals of a given type in
126 * the current thread.
127 *
128 * The list of handlers is built on-the-fly to allow for the dynamic
129 * creation/deletion of signal handlers. Since the number of signal handlers
130 * shouldn't be too high, this is not a big performance issue to pay for the
131 * flexibility.
132 *
133 * For synchronization purposes, the signal is bracketed by two synch
134 * signals.
135 *
136 * @param signal
137 * the signal to dispatch
138 */
139 public static synchronized void dispatchSignal(TmfSignal signal) {
140 int signalId = fSignalId++;
141 sendSignal(new TmfStartSynchSignal(signalId));
142 signal.setReference(signalId);
143 sendSignal(signal);
144 sendSignal(new TmfEndSynchSignal(signalId));
145 }
146
147 /**
148 * Invokes the handling methods that listens to signals of a given type
149 * in a separate thread which will call
150 * {@link TmfSignalManager#dispatchSignal(TmfSignal)}.
151 *
152 * If a signal is already processed the signal will be queued and
153 * dispatched after the ongoing signal finishes.
154 *
155 * @param signal
156 * the signal to dispatch
157 * @since 3.0
158 */
159 public static void dispatchSignalAsync(final TmfSignal signal) {
160 if (!fExecutor.isShutdown()) {
161 fExecutor.execute(new Runnable() {
162 @Override
163 public void run() {
164 dispatchSignal(signal);
165 }
166 });
167 }
168 }
169
170 /**
171 * Disposes the signal manager
172 * @since 3.0
173 */
174 public static void dispose() {
175 fExecutor.shutdown();
176 }
177
178 private static void sendSignal(TmfSignal signal) {
179 sendSignal(fVIPListeners, signal);
180 sendSignal(fListeners, signal);
181 }
182
183 private static void sendSignal(Map<Object, Method[]> listeners, TmfSignal signal) {
184
185 if (TmfCoreTracer.isSignalTraced()) {
186 TmfCoreTracer.traceSignal(signal, "(start)"); //$NON-NLS-1$
187 }
188
189 // Build the list of listener methods that are registered for this signal
190 Class<?> signalClass = signal.getClass();
191 Map<Object, List<Method>> targets = new HashMap<>();
192 targets.clear();
193 for (Map.Entry<Object, Method[]> entry : listeners.entrySet()) {
194 List<Method> matchingMethods = new ArrayList<>();
195 for (Method method : entry.getValue()) {
196 if (method.getParameterTypes()[0].isAssignableFrom(signalClass)) {
197 matchingMethods.add(method);
198 }
199 }
200 if (!matchingMethods.isEmpty()) {
201 targets.put(entry.getKey(), matchingMethods);
202 }
203 }
204
205 // Call the signal handlers
206 for (Map.Entry<Object, List<Method>> entry : targets.entrySet()) {
207 for (Method method : entry.getValue()) {
208 try {
209 method.invoke(entry.getKey(), new Object[] { signal });
210 if (TmfCoreTracer.isSignalTraced()) {
211 Object key = entry.getKey();
212 String hash = String.format("%1$08X", entry.getKey().hashCode()); //$NON-NLS-1$
213 String target = "[" + hash + "] " + key.getClass().getSimpleName() + ":" + method.getName(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
214 TmfCoreTracer.traceSignal(signal, target);
215 }
216 } catch (IllegalArgumentException e) {
217 Activator.logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
218 } catch (IllegalAccessException e) {
219 Activator.logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
220 } catch (InvocationTargetException e) {
221 Activator.logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
222 }
223 }
224 }
225
226 if (TmfCoreTracer.isSignalTraced()) {
227 TmfCoreTracer.traceSignal(signal, "(end)"); //$NON-NLS-1$
228 }
229 }
230
231 }
This page took 0.039037 seconds and 5 git commands to generate.