1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.signal
;
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
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Tracer
;
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.
31 * @author Francois Chouinard
33 public class TmfSignalManager
{
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
[]>();
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
;
48 fSignalTracer
= TmfSignalTracer
.getInstance();
49 register(fSignalTracer
);
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.
59 * The object that will be notified of new signals
61 public static synchronized void register(Object listener
) {
62 Method
[] methods
= getSignalHandlerMethods(listener
);
63 if (methods
.length
> 0) {
64 fListeners
.put(listener
, methods
);
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.
74 * The object that will be notified of new signals
76 public static synchronized void registerVIP(Object listener
) {
77 Method
[] methods
= getSignalHandlerMethods(listener
);
78 if (methods
.length
> 0) {
79 fVIPListeners
.put(listener
, methods
);
84 * De-register a listener object from the signal manager. This means that
85 * its @TmfSignalHandler methods will no longer be called.
88 * The object to de-register
90 public static synchronized void deregister(Object listener
) {
91 fVIPListeners
.remove(listener
);
92 fListeners
.remove(listener
);
96 * Returns the list of signal handlers in the listener. Signal handler name
97 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
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
);
110 return handlers
.toArray(new Method
[handlers
.size()]);
113 static int fSignalId
= 0;
116 * Invokes the handling methods that listens to signals of a given type.
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.
123 * For synchronization purposes, the signal is bracketed by two synch signals.
125 * @param signal the signal to dispatch
127 static public synchronized void dispatchSignal(TmfSignal signal
) {
128 int signalId
= fSignalId
++;
129 sendSignal(new TmfStartSynchSignal(signalId
));
130 signal
.setReference(signalId
);
132 sendSignal(new TmfEndSynchSignal(signalId
));
135 static private void sendSignal(TmfSignal signal
) {
136 sendSignal(fVIPListeners
, signal
);
137 sendSignal(fListeners
, signal
);
140 static private void sendSignal(Map
<Object
, Method
[]> listeners
, TmfSignal signal
) {
142 if (Tracer
.isSignalTraced()) {
143 Tracer
.traceSignal(signal
, "(start)"); //$NON-NLS-1$
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
>>();
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
);
157 if (!matchingMethods
.isEmpty()) {
158 targets
.put(entry
.getKey(), matchingMethods
);
162 // Call the signal handlers
163 for (Map
.Entry
<Object
, List
<Method
>> entry
: targets
.entrySet()) {
164 for (Method method
: entry
.getValue()) {
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
);
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$
183 if (Tracer
.isSignalTraced()) {
184 Tracer
.traceSignal(signal
, "(end)"); //$NON-NLS-1$