tmf: Drop generics from ITmfTrace and TmfExperiment
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / signal / TmfSignalManager.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.signal;
14
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
22 import org.eclipse.linuxtools.internal.tmf.core.Activator;
23 import org.eclipse.linuxtools.internal.tmf.core.Tracer;
24
25 /**
26 * This class manages the set of signal listeners and the signals they are
27 * interested in. When a signal is broadcasted, the appropriate listeners
28 * signal handlers are invoked.
29 *
30 * @version 1.0
31 * @author Francois Chouinard
32 */
33 public class TmfSignalManager {
34
35 // The set of event listeners and their corresponding handler methods.
36 // Note: listeners could be restricted to ITmfComponents but there is no
37 // harm in letting anyone use this since it is not tied to anything but
38 // the signal data type.
39 static private Map<Object, Method[]> fListeners = new HashMap<Object, Method[]>();
40 static private Map<Object, Method[]> fVIPListeners = new HashMap<Object, Method[]>();
41
42 // If requested, add universal signal tracer
43 // TODO: Temporary solution: should be enabled/disabled dynamically
44 private static boolean fTraceIsActive = false;
45 private static TmfSignalTracer fSignalTracer;
46 static {
47 if (fTraceIsActive) {
48 fSignalTracer = TmfSignalTracer.getInstance();
49 register(fSignalTracer);
50 }
51 }
52
53 /**
54 * Register an object to the signal manager. This object can then implement
55 * handler methods, marked with @TmfSignalHandler and with the expected
56 * signal type as parameter.
57 *
58 * @param listener
59 * The object that will be notified of new signals
60 */
61 public static synchronized void register(Object listener) {
62 Method[] methods = getSignalHandlerMethods(listener);
63 if (methods.length > 0) {
64 fListeners.put(listener, methods);
65 }
66 }
67
68 /**
69 * Register an object to the signal manager as a "VIP" listener. All VIP
70 * listeners will all receive the signal before the manager moves on to the
71 * lowly, non-VIP listeners.
72 *
73 * @param listener
74 * The object that will be notified of new signals
75 */
76 public static synchronized void registerVIP(Object listener) {
77 Method[] methods = getSignalHandlerMethods(listener);
78 if (methods.length > 0) {
79 fVIPListeners.put(listener, methods);
80 }
81 }
82
83 /**
84 * De-register a listener object from the signal manager. This means that
85 * its @TmfSignalHandler methods will no longer be called.
86 *
87 * @param listener
88 * The object to de-register
89 */
90 public static synchronized void deregister(Object listener) {
91 fVIPListeners.remove(listener);
92 fListeners.remove(listener);
93 }
94
95 /**
96 * Returns the list of signal handlers in the listener. Signal handler name
97 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
98 *
99 * @param listener
100 * @return
101 */
102 static private Method[] getSignalHandlerMethods(Object listener) {
103 List<Method> handlers = new ArrayList<Method>();
104 Method[] methods = listener.getClass().getMethods();
105 for (Method method : methods) {
106 if (method.isAnnotationPresent(TmfSignalHandler.class)) {
107 handlers.add(method);
108 }
109 }
110 return handlers.toArray(new Method[handlers.size()]);
111 }
112
113 static int fSignalId = 0;
114
115 /**
116 * Invokes the handling methods that listens to signals of a given type.
117 *
118 * The list of handlers is built on-the-fly to allow for the dynamic
119 * creation/deletion of signal handlers. Since the number of signal
120 * handlers shouldn't be too high, this is not a big performance issue
121 * to pay for the flexibility.
122 *
123 * For synchronization purposes, the signal is bracketed by two synch signals.
124 *
125 * @param signal the signal to dispatch
126 */
127 static public synchronized void dispatchSignal(TmfSignal signal) {
128 int signalId = fSignalId++;
129 sendSignal(new TmfStartSynchSignal(signalId));
130 signal.setReference(signalId);
131 sendSignal(signal);
132 sendSignal(new TmfEndSynchSignal(signalId));
133 }
134
135 static private void sendSignal(TmfSignal signal) {
136 sendSignal(fVIPListeners, signal);
137 sendSignal(fListeners, signal);
138 }
139
140 static private void sendSignal(Map<Object, Method[]> listeners, TmfSignal signal) {
141
142 if (Tracer.isSignalTraced()) {
143 Tracer.traceSignal(signal, "(start)"); //$NON-NLS-1$
144 }
145
146 // Build the list of listener methods that are registered for this signal
147 Class<?> signalClass = signal.getClass();
148 Map<Object, List<Method>> targets = new HashMap<Object, List<Method>>();
149 targets.clear();
150 for (Map.Entry<Object, Method[]> entry : listeners.entrySet()) {
151 List<Method> matchingMethods = new ArrayList<Method>();
152 for (Method method : entry.getValue()) {
153 if (method.getParameterTypes()[0].isAssignableFrom(signalClass)) {
154 matchingMethods.add(method);
155 }
156 }
157 if (!matchingMethods.isEmpty()) {
158 targets.put(entry.getKey(), matchingMethods);
159 }
160 }
161
162 // Call the signal handlers
163 for (Map.Entry<Object, List<Method>> entry : targets.entrySet()) {
164 for (Method method : entry.getValue()) {
165 try {
166 method.invoke(entry.getKey(), new Object[] { signal });
167 if (Tracer.isSignalTraced()) {
168 Object key = entry.getKey();
169 String hash = String.format("%1$08X", entry.getKey().hashCode()); //$NON-NLS-1$
170 String target = "[" + hash + "] " + key.getClass().getSimpleName() + ":" + method.getName(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
171 Tracer.traceSignal(signal, target);
172 }
173 } catch (IllegalArgumentException e) {
174 Activator.getDefault().logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
175 } catch (IllegalAccessException e) {
176 Activator.getDefault().logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
177 } catch (InvocationTargetException e) {
178 Activator.getDefault().logError("Exception handling signal " + signal + " in method " + method, e); //$NON-NLS-1$ //$NON-NLS-2$
179 }
180 }
181 }
182
183 if (Tracer.isSignalTraced()) {
184 Tracer.traceSignal(signal, "(end)"); //$NON-NLS-1$
185 }
186 }
187
188 }
This page took 0.039665 seconds and 5 git commands to generate.