1 17 18 19 package org.apache.catalina.util; 20 21 22 import org.apache.catalina.Lifecycle; 23 import org.apache.catalina.LifecycleEvent; 24 import org.apache.catalina.LifecycleListener; 25 26 27 34 35 public final class LifecycleSupport { 36 37 38 40 41 48 public LifecycleSupport(Lifecycle lifecycle) { 49 50 super(); 51 this.lifecycle = lifecycle; 52 53 } 54 55 56 58 59 62 private Lifecycle lifecycle = null; 63 64 65 68 private LifecycleListener listeners[] = new LifecycleListener[0]; 69 70 71 73 74 79 public void addLifecycleListener(LifecycleListener listener) { 80 81 synchronized (listeners) { 82 LifecycleListener results[] = 83 new LifecycleListener[listeners.length + 1]; 84 for (int i = 0; i < listeners.length; i++) 85 results[i] = listeners[i]; 86 results[listeners.length] = listener; 87 listeners = results; 88 } 89 90 } 91 92 93 97 public LifecycleListener[] findLifecycleListeners() { 98 99 return listeners; 100 101 } 102 103 104 112 public void fireLifecycleEvent(String type, Object data) { 113 114 LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); 115 LifecycleListener interested[] = null; 116 synchronized (listeners) { 117 interested = (LifecycleListener[]) listeners.clone(); 118 } 119 for (int i = 0; i < interested.length; i++) 120 interested[i].lifecycleEvent(event); 121 122 } 123 124 125 130 public void removeLifecycleListener(LifecycleListener listener) { 131 132 synchronized (listeners) { 133 int n = -1; 134 for (int i = 0; i < listeners.length; i++) { 135 if (listeners[i] == listener) { 136 n = i; 137 break; 138 } 139 } 140 if (n < 0) 141 return; 142 LifecycleListener results[] = 143 new LifecycleListener[listeners.length - 1]; 144 int j = 0; 145 for (int i = 0; i < listeners.length; i++) { 146 if (i != n) 147 results[j++] = listeners[i]; 148 } 149 listeners = results; 150 } 151 152 } 153 154 155 } 156 | Popular Tags |