Patch for Bug287563 (signal handling)
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / signal / TmfSignalManager.java
CommitLineData
8c8bf09f 1/*******************************************************************************
50adc88e 2 * Copyright (c) 2009 Ericsson
8c8bf09f
ASL
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
13package org.eclipse.linuxtools.tmf.signal;
14
15import java.lang.reflect.InvocationTargetException;
16import java.lang.reflect.Method;
17import java.util.ArrayList;
18import java.util.HashMap;
19import java.util.List;
20import java.util.Map;
21
22/**
23 * <b><u>TmfSignalHandler</u></b>
24 * <p>
50adc88e 25 * TODO: Implement me. Please.
8c8bf09f 26 * <p>
50adc88e
FC
27 * TODO: Error/exception handling
28 * <p>
29 * Note: This code is a shameless simplification of Pawel Piech's excellent
30 * event handling work in DSF.
8c8bf09f
ASL
31 */
32public class TmfSignalManager {
33
50adc88e
FC
34 /**
35 * The set of event listeners and their corresponding handler methods.
36 *
37 * Note: The use of WeakHashMap is far from ideal but, if a listener goes
38 * out of scope without removing itself from this list, GC will eventually
39 * call the finalize() method
40 */
8c8bf09f
ASL
41 static private Map<Object, Method[]> fListeners = new HashMap<Object, Method[]>();
42
8c8bf09f 43
50adc88e 44 static public void addListener(Object listener) {
8c8bf09f
ASL
45 Method[] methods = getSignalHandlerMethods(listener);
46 if (methods.length > 0)
50adc88e
FC
47 synchronized(fListeners) {
48 fListeners.put(listener, methods);
49 }
8c8bf09f
ASL
50 }
51
50adc88e
FC
52 static public void removeListener(Object listener) {
53 synchronized(fListeners) {
54 fListeners.remove(listener);
8c8bf09f 55 }
8c8bf09f
ASL
56 }
57
58 /**
50adc88e 59 * Invokes the handling methods that expect this signal.
8c8bf09f
ASL
60 *
61 * The list of handlers is built on-the-fly to allow for the dynamic
62 * creation/deletion of signal handlers. Since the number of signal
63 * handlers shouldn't be too high, this is not a big performance issue
64 * to pay for the flexibility.
65 *
50adc88e 66 * @param signal
8c8bf09f 67 */
50adc88e 68 static public void dispatchSignal(Object signal) {
8c8bf09f
ASL
69
70 // Build the list of listener methods that are registered for this signal
71 Class<?> signalClass = signal.getClass();
72 Map<Object, List<Method>> listeners = new HashMap<Object, List<Method>>();
50adc88e 73 List<Method> matchingMethods = new ArrayList<Method>();
8c8bf09f 74 for (Map.Entry<Object, Method[]> entry : fListeners.entrySet()) {
8c8bf09f
ASL
75 for (Method method : entry.getValue()) {
76 if (method.getParameterTypes()[0].isAssignableFrom(signalClass)) {
77 matchingMethods.add(method);
78 }
79 }
80 if (!matchingMethods.isEmpty()) {
81 listeners.put(entry.getKey(), matchingMethods);
82 }
83 }
84
50adc88e 85 // Call the signal handlers
8c8bf09f
ASL
86 for (Map.Entry<Object, List<Method>> entry : listeners.entrySet()) {
87 for (Method method : entry.getValue()) {
88 try {
89 method.invoke(entry.getKey(), new Object[] { signal });
90 } catch (IllegalArgumentException e) {
91 // TODO Auto-generated catch block
50adc88e 92 e.printStackTrace();
8c8bf09f
ASL
93 } catch (IllegalAccessException e) {
94 // TODO Auto-generated catch block
50adc88e 95 e.printStackTrace();
8c8bf09f
ASL
96 } catch (InvocationTargetException e) {
97 // TODO Auto-generated catch block
50adc88e 98 e.printStackTrace();
8c8bf09f
ASL
99 }
100 }
101 }
102 }
103
50adc88e
FC
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 static private Method[] getSignalHandlerMethods(Object listener) {
112 List<Method> handlers = new ArrayList<Method>();
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}
This page took 0.028918 seconds and 5 git commands to generate.