01: package com.sun.portal.util;
02:
03: import java.util.LinkedList;
04: import java.util.List;
05:
06: /**
07: * author: Noble Paul
08: * Date: Feb 15, 2005, 11:00:03 AM
09: */
10: public class SRAEvent {
11: public static final SRAEvent DUMMY_EVENT = new SRAEvent();
12: public static final SRAEvent PLAIN_SOCKET_CREATED = new SRAEvent();
13: public static final SRAEvent PLAIN_SOCKET_DESTROYED = new SRAEvent();
14: public static final SRAEvent SSL_SOCKET_CREATED = new SRAEvent();
15: public static final SRAEvent SSL_SOCKET_DESTROYED = new SRAEvent();
16: public static final SRAEvent SERVER_SOCKET_CREATED = new SRAEvent();
17: public static final SRAEvent SERVER_SOCKET_DESTROYED = new SRAEvent();
18: public static final SRAEvent IS_LOGGING_START = new SRAEvent();
19: public static final SRAEvent IS_LOGGING_END = new SRAEvent();
20: public static final SRAEvent SSO_VALIDATION_START = new SRAEvent();
21: public static final SRAEvent SSO_VALIDATION_END = new SRAEvent();
22: public static final SRAEvent SSO_SET_PROPERTY_START = new SRAEvent();
23: public static final SRAEvent SSO_SET_PROPERTY_END = new SRAEvent();
24: public static final SRAEvent START_TASK = new SRAEvent();
25: public static final SRAEvent END_TASK = new SRAEvent();
26: public static final SRAEvent IS_NOTIFICATION = new SRAEvent();
27:
28: private SRAEventListener firstListener = null;//Most of the events have only one listener
29: private SRAEventListener secondListener = null;//at the most two listeners. So keep them handy.
30: private List allListeners = null;
31: boolean moreThanTwo = false;
32:
33: public void addListener(SRAEventListener listener) {
34: if (allListeners == null) {
35: allListeners = new LinkedList();
36: }
37: allListeners.add(listener);
38: rearrange();
39: }
40:
41: public void fireEvent(Object obj) {//This is the most frequently called method. Efficiency is important here.
42: if (firstListener == null)
43: return;
44: firstListener.handleEvent(this , obj);
45: if (secondListener == null)
46: return;
47: secondListener.handleEvent(this , obj);
48: if (!moreThanTwo)
49: return;
50: for (int i = 2; i < allListeners.size(); i++) {
51: SRAEventListener listener = (SRAEventListener) allListeners
52: .get(i);
53: listener.handleEvent(this , obj);
54: }
55: }
56:
57: private void rearrange() {
58: int size = allListeners.size();
59: moreThanTwo = false;
60: firstListener = secondListener = null;
61: if (size > 0) {
62: firstListener = (SRAEventListener) allListeners.get(0);
63: if (size > 1) {
64: secondListener = (SRAEventListener) allListeners.get(1);
65: if (size > 2)
66: moreThanTwo = true;
67: }
68: }
69: }
70:
71: public void removeListener(SRAEventListener listener) {
72: if (allListeners == null)
73: return;
74: allListeners.remove(listener);
75: rearrange();
76: }
77: }
|