001: package com.jamonapi;
002:
003: import java.util.*;
004:
005: import com.jamonapi.utils.*;
006:
007: /** A class that can contain other listeners that can listen to jamon events of interest.
008: * These classes will all implement the JAMonListener interface too. This is an example of the
009: * Gang of 4 Composite design pattern.
010: * @author steve souza
011: *
012: */
013: public class CompositeListener implements JAMonListener, DetailData {
014:
015: // A variable that will hold the list of Listeners
016: private List listenerList = new ArrayList();
017: // The name of the composite listener
018: private String name;
019:
020: /** Uses the CompositeListener name */
021: public CompositeListener() {
022: this ("CompositeJAMonListener");
023: }
024:
025: /** Pass in a Listener name that allows you to differentiate this listener from others */
026: public CompositeListener(String name) {
027: this .name = name;
028: }
029:
030: /** Add a listener to the composite and return this object */
031: public CompositeListener addListener(JAMonListener listener) {
032: listenerList.add(listener);
033: return this ;
034: }
035:
036: /* Return the listener associated with the passed in name */
037: public JAMonListener getListener(String listenerName) {
038: int rows = getNumListeners();
039:
040: for (int i = 0; i < rows; i++) {
041: JAMonListener listener = (JAMonListener) listenerList
042: .get(i);
043: String name = listener.getName();
044: if (listenerName.equalsIgnoreCase(name))
045: return listener;
046: else if (listener instanceof CompositeListener) {
047: listener = ((CompositeListener) listener)
048: .getListener(listenerName);
049: if (listener != null)
050: return listener;
051: }
052: }
053:
054: return null;
055: }
056:
057: /** Return the listener associated with the index */
058: public JAMonListener getListener(int index) {
059: return (JAMonListener) listenerList.get(index);
060: }
061:
062: /** Remove the named listener from this CompositeListener */
063: public CompositeListener removeListener(String listenerName) {
064: int rows = getNumListeners();
065:
066: for (int i = 0; i < rows; i++) {
067: JAMonListener listener = (JAMonListener) listenerList
068: .get(i);
069: String name = listener.getName();
070: if (listenerName.equalsIgnoreCase(name)) {
071: listenerList.remove(i);
072: break;
073: } else if (listener instanceof CompositeListener
074: && ((CompositeListener) listener)
075: .hasListener(listenerName)) {
076: ((CompositeListener) listener)
077: .removeListener(listenerName);
078: break;
079: }
080:
081: }
082:
083: return this ;
084: }
085:
086: /** return true if the named listener exists */
087: public boolean hasListener(String listenerName) {
088: int rows = getNumListeners();
089:
090: for (int i = 0; i < rows; i++) {
091: JAMonListener listener = (JAMonListener) listenerList
092: .get(i);
093: String name = listener.getName();
094: if (listenerName.equalsIgnoreCase(name))
095: return true;
096: else if (listener instanceof CompositeListener) {
097: return ((CompositeListener) listener)
098: .hasListener(listenerName);
099: }
100: }
101:
102: return false;
103: }
104:
105: /** Return the number of listeners */
106: public int getNumListeners() {
107: return listenerList.size();
108: }
109:
110: /** Also returns the number of listeners */
111: public int getRowCount() {
112: return getNumListeners();
113: }
114:
115: /** Return the name of this instance */
116: public String getName() {
117: return name;
118: }
119:
120: public void setName(String name) {
121: this .name = name;
122:
123: }
124:
125: /** Notify all listeners that are part of this composite of a jamon event and pass them the
126: * monitor that triggered the event.
127: */
128: public void processEvent(Monitor mon) {
129: Iterator iter = listenerList.iterator();
130:
131: while (iter.hasNext()) {
132: JAMonListener listener = (JAMonListener) iter.next();
133: listener.processEvent(mon);
134: }
135:
136: }
137:
138: /** Get an iterator that will conatain the Composite's JAMonListener objects. The objects will
139: * be safe cast to JAMonListener */
140: public Iterator iterator() {
141: return listenerList.iterator();
142: }
143:
144: public boolean isEmpty() {
145: return listenerList.isEmpty();
146: }
147:
148: public boolean hasData() {
149: return !isEmpty();
150: }
151:
152: public Object[][] getData() {
153: if (isEmpty())
154: return null;
155:
156: int numListeners = getNumListeners();
157: List list = new ArrayList();
158: for (int i = 0; i < numListeners; i++) {
159: add(list, getListenerData(getListener(i)));
160: }
161:
162: return toArray(list);
163:
164: }
165:
166: private void add(List list, Object[][] data) {
167: int dataSize = (data == null) ? 0 : data.length;
168: for (int i = 0; i < dataSize; i++)
169: list.add(data[i]);
170:
171: }
172:
173: private Object[][] getListenerData(JAMonListener listener) {
174: if (listener instanceof CompositeListener)
175: return ((CompositeListener) listener).getData();
176: else
177: return new Object[][] { { listener.getName() } };
178: }
179:
180: private Object[][] toArray(List list) {
181: if (list.size() == 0)
182: return null;
183: else {
184: Object[][] listenerArray = new Object[list.size()][];
185: list.toArray(listenerArray);
186: return listenerArray;
187: }
188: }
189:
190: private static final String[] HEADER = { "ListenerName" };
191:
192: public String[] getHeader() {
193: return HEADER;
194: }
195:
196: public static Object[][] getData(JAMonListener listener) {
197: if (listener == null)
198: return null;
199: // don't need to wrap listener if it is already a compositelistener, but
200: // it makes the following code easier.
201: return new CompositeListener().addListener(listener).getData();
202: }
203:
204: public static String[] getHeader(JAMonListener listener) {
205: // At this point all headers are the same.
206: return HEADER;
207: }
208:
209: }
|