[290145] Preliminary patch uploaded
[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 27 * TODO: Error/exception handling
8c8bf09f
ASL
28 */
29public class TmfSignalManager {
30
50adc88e
FC
31 /**
32 * The set of event listeners and their corresponding handler methods.
50adc88e 33 */
8c8bf09f
ASL
34 static private Map<Object, Method[]> fListeners = new HashMap<Object, Method[]>();
35
82b08e62 36 // TODO: read from the preferences
8f965dd5 37 private static boolean fTraceIsActive = false;
82b08e62
FC
38 private static TmfSignalTrace fSignalTracer;
39
82b08e62
FC
40 static {
41 if (fTraceIsActive) {
42 fSignalTracer = new TmfSignalTrace();
43 addListener(fSignalTracer);
44 }
45 }
8c8bf09f 46
82b08e62 47 public static synchronized void addListener(Object listener) {
8c8bf09f
ASL
48 Method[] methods = getSignalHandlerMethods(listener);
49 if (methods.length > 0)
82b08e62 50 fListeners.put(listener, methods);
8c8bf09f
ASL
51 }
52
82b08e62
FC
53 public static synchronized void removeListener(Object listener) {
54 fListeners.remove(listener);
8c8bf09f
ASL
55 }
56
57 /**
50adc88e 58 * Invokes the handling methods that expect this signal.
8c8bf09f
ASL
59 *
60 * The list of handlers is built on-the-fly to allow for the dynamic
61 * creation/deletion of signal handlers. Since the number of signal
62 * handlers shouldn't be too high, this is not a big performance issue
63 * to pay for the flexibility.
64 *
50adc88e 65 * @param signal
8c8bf09f 66 */
82b08e62 67 static public synchronized void dispatchSignal(Object signal) {
8c8bf09f
ASL
68
69 // Build the list of listener methods that are registered for this signal
70 Class<?> signalClass = signal.getClass();
71 Map<Object, List<Method>> listeners = new HashMap<Object, List<Method>>();
82b08e62 72 listeners.clear();
8c8bf09f 73 for (Map.Entry<Object, Method[]> entry : fListeners.entrySet()) {
82b08e62 74 List<Method> matchingMethods = new ArrayList<Method>();
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()) {
8f965dd5
FC
88 try {
89 method.invoke(entry.getKey(), new Object[] { signal });
90 } catch (IllegalArgumentException e) {
91 // TODO Auto-generated catch block
8f965dd5
FC
92 } catch (IllegalAccessException e) {
93 // TODO Auto-generated catch block
8f965dd5
FC
94 } catch (InvocationTargetException e) {
95 // TODO Auto-generated catch block
8f965dd5 96 }
8c8bf09f
ASL
97 }
98 }
99 }
100
50adc88e
FC
101 /**
102 * Returns the list of signal handlers in the listener. Signal handler name
103 * is irrelevant; only the annotation (@TmfSignalHandler) is important.
104 *
105 * @param listener
106 * @return
107 */
108 static private Method[] getSignalHandlerMethods(Object listener) {
109 List<Method> handlers = new ArrayList<Method>();
110 Method[] methods = listener.getClass().getMethods();
111 for (Method method : methods) {
112 if (method.isAnnotationPresent(TmfSignalHandler.class)) {
113 handlers.add(method);
114 }
115 }
116 return handlers.toArray(new Method[handlers.size()]);
117 }
118
8f965dd5 119}
This page took 0.029859 seconds and 5 git commands to generate.