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
.tmf
.core
.Tracer
;
25 * <b><u>TmfSignalHandler</u></b>
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 public class TmfSignalManager
{
34 // The set of event listeners and their corresponding handler methods.
35 // Note: listeners could be restricted to ITmfComponents but there is no
36 // harm in letting anyone use this since it is not tied to anything but
37 // the signal data type.
38 static private Map
<Object
, Method
[]> fListeners
= new HashMap
<Object
, Method
[]>();
39 static private Map
<Object
, Method
[]> fVIPListeners
= new HashMap
<Object
, Method
[]>();
41 // If requested, add universal signal tracer
42 // TODO: Temporary solution: should be enabled/disabled dynamically
43 private static boolean fTraceIsActive
= false;
44 private static TmfSignalTracer fSignalTracer
;
47 fSignalTracer
= TmfSignalTracer
.getInstance();
48 register(fSignalTracer
);
52 public static synchronized void register(Object listener
) {
53 Method
[] methods
= getSignalHandlerMethods(listener
);
54 if (methods
.length
> 0)
55 fListeners
.put(listener
, methods
);
58 public static synchronized void registerVIP(Object listener
) {
59 Method
[] methods
= getSignalHandlerMethods(listener
);
60 if (methods
.length
> 0)
61 fVIPListeners
.put(listener
, methods
);
64 public static synchronized void deregister(Object listener
) {
65 fVIPListeners
.remove(listener
);
66 fListeners
.remove(listener
);
70 * Returns the list of signal handlers in the listener. Signal handler name
71 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
76 static private Method
[] getSignalHandlerMethods(Object listener
) {
77 List
<Method
> handlers
= new ArrayList
<Method
>();
78 Method
[] methods
= listener
.getClass().getMethods();
79 for (Method method
: methods
) {
80 if (method
.isAnnotationPresent(TmfSignalHandler
.class)) {
84 return handlers
.toArray(new Method
[handlers
.size()]);
88 * Invokes the handling methods that listens to signals of a given type.
90 * The list of handlers is built on-the-fly to allow for the dynamic
91 * creation/deletion of signal handlers. Since the number of signal
92 * handlers shouldn't be too high, this is not a big performance issue
93 * to pay for the flexibility.
95 * For synchronization purposes, the signal is bracketed by two synch signals.
97 * @param signal the signal to dispatch
99 static int fSignalId
= 0;
100 static public synchronized void dispatchSignal(TmfSignal signal
) {
102 sendSignal(new TmfStartSynchSignal(fSignalId
));
103 signal
.setReference(fSignalId
);
105 sendSignal(new TmfEndSynchSignal(fSignalId
));
108 static private void sendSignal(TmfSignal signal
) {
109 sendSignal(fVIPListeners
, signal
);
110 sendSignal(fListeners
, signal
);
113 static private void sendSignal(Map
<Object
, Method
[]> listeners
, TmfSignal signal
) {
115 if (Tracer
.isSignalTraced()) Tracer
.traceSignal(signal
, "(start)"); //$NON-NLS-1$
117 // Build the list of listener methods that are registered for this signal
118 Class
<?
> signalClass
= signal
.getClass();
119 Map
<Object
, List
<Method
>> targets
= new HashMap
<Object
, List
<Method
>>();
121 for (Map
.Entry
<Object
, Method
[]> entry
: listeners
.entrySet()) {
122 List
<Method
> matchingMethods
= new ArrayList
<Method
>();
123 for (Method method
: entry
.getValue()) {
124 if (method
.getParameterTypes()[0].isAssignableFrom(signalClass
)) {
125 matchingMethods
.add(method
);
128 if (!matchingMethods
.isEmpty()) {
129 targets
.put(entry
.getKey(), matchingMethods
);
133 // Call the signal handlers
134 for (Map
.Entry
<Object
, List
<Method
>> entry
: targets
.entrySet()) {
135 for (Method method
: entry
.getValue()) {
137 method
.invoke(entry
.getKey(), new Object
[] { signal
});
138 if (Tracer
.isSignalTraced()) {
139 Object key
= entry
.getKey();
140 String hash
= String
.format("%1$08X", entry
.getKey().hashCode()); //$NON-NLS-1$
141 String target
= "[" + hash
+ "] " + key
.getClass().getSimpleName() + ":" + method
.getName(); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
142 Tracer
.traceSignal(signal
, target
);
144 } catch (IllegalArgumentException e
) {
145 } catch (IllegalAccessException e
) {
146 } catch (InvocationTargetException e
) {
151 if (Tracer
.isSignalTraced()) Tracer
.traceSignal(signal
, "(end)"); //$NON-NLS-1$