1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Update register methods
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.signal
;
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
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
24 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.TmfCoreTracer
;
27 * This class manages the set of signal listeners and the signals they are
28 * interested in. When a signal is broadcasted, the appropriate listeners
29 * signal handlers are invoked.
32 * @author Francois Chouinard
34 public class TmfSignalManager
{
36 // The set of event listeners and their corresponding handler methods.
37 // Note: listeners could be restricted to ITmfComponents but there is no
38 // harm in letting anyone use this since it is not tied to anything but
39 // the signal data type.
40 private static Map
<Object
, Method
[]> fListeners
= new HashMap
<Object
, Method
[]>();
41 private static Map
<Object
, Method
[]> fVIPListeners
= new HashMap
<Object
, Method
[]>();
43 // If requested, add universal signal tracer
44 // TODO: Temporary solution: should be enabled/disabled dynamically
45 private static boolean fTraceIsActive
= false;
46 private static TmfSignalTracer fSignalTracer
;
50 fSignalTracer
= TmfSignalTracer
.getInstance();
51 register(fSignalTracer
);
56 * Register an object to the signal manager. This object can then implement
57 * handler methods, marked with @TmfSignalHandler and with the expected
58 * signal type as parameter.
61 * The object that will be notified of new signals
63 public static synchronized void register(Object listener
) {
64 deregister(listener
); // make sure that listener is only registered once
65 Method
[] methods
= getSignalHandlerMethods(listener
);
66 if (methods
.length
> 0) {
67 fListeners
.put(listener
, methods
);
72 * Register an object to the signal manager as a "VIP" listener. All VIP
73 * listeners will all receive the signal before the manager moves on to the
74 * lowly, non-VIP listeners.
77 * The object that will be notified of new signals
79 public static synchronized void registerVIP(Object listener
) {
80 deregister(listener
); // make sure that listener is only registered once
81 Method
[] methods
= getSignalHandlerMethods(listener
);
82 if (methods
.length
> 0) {
83 fVIPListeners
.put(listener
, methods
);
88 * De-register a listener object from the signal manager. This means that
89 * its @TmfSignalHandler methods will no longer be called.
92 * The object to de-register
94 public static synchronized void deregister(Object listener
) {
95 fVIPListeners
.remove(listener
);
96 fListeners
.remove(listener
);
100 * Returns the list of signal handlers in the listener. Signal handler name
101 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
106 private static Method
[] getSignalHandlerMethods(Object listener
) {
107 List
<Method
> handlers
= new ArrayList
<Method
>();
108 Method
[] methods
= listener
.getClass().getMethods();
109 for (Method method
: methods
) {
110 if (method
.isAnnotationPresent(TmfSignalHandler
.class)) {
111 handlers
.add(method
);
114 return handlers
.toArray(new Method
[handlers
.size()]);
117 static int fSignalId
= 0;
120 * Invokes the handling methods that listens to signals of a given type.
122 * The list of handlers is built on-the-fly to allow for the dynamic
123 * creation/deletion of signal handlers. Since the number of signal handlers
124 * shouldn't be too high, this is not a big performance issue to pay for the
127 * For synchronization purposes, the signal is bracketed by two synch
131 * the signal to dispatch
133 public static synchronized void dispatchSignal(TmfSignal signal
) {
134 int signalId
= fSignalId
++;
135 sendSignal(new TmfStartSynchSignal(signalId
));
136 signal
.setReference(signalId
);
138 sendSignal(new TmfEndSynchSignal(signalId
));
141 private static void sendSignal(TmfSignal signal
) {
142 sendSignal(fVIPListeners
, signal
);
143 sendSignal(fListeners
, signal
);
146 private static void sendSignal(Map
<Object
, Method
[]> listeners
, TmfSignal signal
) {
148 if (TmfCoreTracer
.isSignalTraced()) {
149 TmfCoreTracer
.traceSignal(signal
, "(start)"); //$NON-NLS-1$
152 // Build the list of listener methods that are registered for this signal
153 Class
<?
> signalClass
= signal
.getClass();
154 Map
<Object
, List
<Method
>> targets
= new HashMap
<Object
, List
<Method
>>();
156 for (Map
.Entry
<Object
, Method
[]> entry
: listeners
.entrySet()) {
157 List
<Method
> matchingMethods
= new ArrayList
<Method
>();
158 for (Method method
: entry
.getValue()) {
159 if (method
.getParameterTypes()[0].isAssignableFrom(signalClass
)) {
160 matchingMethods
.add(method
);
163 if (!matchingMethods
.isEmpty()) {
164 targets
.put(entry
.getKey(), matchingMethods
);
168 // Call the signal handlers
169 for (Map
.Entry
<Object
, List
<Method
>> entry
: targets
.entrySet()) {
170 for (Method method
: entry
.getValue()) {
172 method
.invoke(entry
.getKey(), new Object
[] { signal
});
173 if (TmfCoreTracer
.isSignalTraced()) {
174 Object key
= entry
.getKey();
175 String hash
= String
.format("%1$08X", entry
.getKey().hashCode()); //$NON-NLS-1$
176 String target
= "[" + hash
+ "] " + key
.getClass().getSimpleName() + ":" + method
.getName(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
177 TmfCoreTracer
.traceSignal(signal
, target
);
179 } catch (IllegalArgumentException e
) {
180 Activator
.logError("Exception handling signal " + signal
+ " in method " + method
, e
); //$NON-NLS-1$ //$NON-NLS-2$
181 } catch (IllegalAccessException e
) {
182 Activator
.logError("Exception handling signal " + signal
+ " in method " + method
, e
); //$NON-NLS-1$ //$NON-NLS-2$
183 } catch (InvocationTargetException e
) {
184 Activator
.logError("Exception handling signal " + signal
+ " in method " + method
, e
); //$NON-NLS-1$ //$NON-NLS-2$
189 if (TmfCoreTracer
.isSignalTraced()) {
190 TmfCoreTracer
.traceSignal(signal
, "(end)"); //$NON-NLS-1$