1 16 package org.mortbay.util; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.EventListener ; 23 24 import org.apache.commons.logging.Log; 25 import org.mortbay.log.LogFactory; 26 import org.mortbay.http.HttpContext; 27 28 29 30 31 37 public abstract class Container implements LifeCycle,EventProvider,Serializable 38 { 39 private static Log log = LogFactory.getLog(Container.class); 40 41 private Object _eventListeners; 42 private Object _components; 43 44 private transient boolean _started; 45 private transient boolean _starting; 46 private transient boolean _stopping; 47 48 49 50 53 public synchronized final void start() 54 throws Exception 55 { 56 if (_started || _starting) 57 return; 58 59 _starting=true; 60 61 if (log.isDebugEnabled()) log.debug("Starting "+this); 62 LifeCycleEvent event = new LifeCycleEvent(this); 63 for(int i=0;i<LazyList.size(_eventListeners);i++) 64 { 65 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 66 if (listener instanceof LifeCycleListener) 67 ((LifeCycleListener)listener).lifeCycleStarting(event); 68 } 69 70 try 71 { 72 doStart(); 73 _started=true; 74 log.info("Started "+this); 75 for(int i=0;i<LazyList.size(_eventListeners);i++) 76 { 77 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 78 if (listener instanceof LifeCycleListener) 79 ((LifeCycleListener)listener).lifeCycleStarted(event); 80 } 81 } 82 catch(Throwable e) 83 { 84 LifeCycleEvent failed = new LifeCycleEvent(this,e); 85 for(int i=0;i<LazyList.size(_eventListeners);i++) 86 { 87 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 88 if (listener instanceof LifeCycleListener) 89 ((LifeCycleListener)listener).lifeCycleFailure(event); 90 } 91 if (e instanceof Exception ) 92 throw (Exception )e; 93 if (e instanceof RuntimeException ) 94 throw (RuntimeException )e; 95 if (e instanceof Error ) 96 throw (Error )e; 97 log.warn(LogSupport.EXCEPTION, e); 98 } 99 finally 100 { 101 _starting=false; 102 } 103 } 104 105 106 107 111 protected abstract void doStart() 112 throws Exception ; 113 114 115 public synchronized boolean isStarted() 116 { 117 return _started; 118 } 119 120 121 protected synchronized boolean isStarting() 122 { 123 return _starting; 124 } 125 126 127 protected synchronized boolean isStopping() 128 { 129 return _stopping; 130 } 131 132 133 136 public synchronized final void stop() 137 throws InterruptedException 138 { 139 if (!_started || _stopping) 140 return; 141 _stopping=true; 142 143 if (log.isDebugEnabled()) log.debug("Stopping "+this); 144 LifeCycleEvent event = new LifeCycleEvent(this); 145 for(int i=0;i<LazyList.size(_eventListeners);i++) 146 { 147 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 148 if (listener instanceof LifeCycleListener) 149 ((LifeCycleListener)listener).lifeCycleStopping(event); 150 } 151 152 try 153 { 154 doStop(); 155 _started=false; 156 log.info("Stopped "+this); 157 for(int i=0;i<LazyList.size(_eventListeners);i++) 158 { 159 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 160 if (listener instanceof LifeCycleListener) 161 ((LifeCycleListener)listener).lifeCycleStopped(event); 162 } 163 } 164 catch(Throwable e) 165 { 166 event = new LifeCycleEvent(this,e); 167 for(int i=0;i<LazyList.size(_eventListeners);i++) 168 { 169 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 170 if (listener instanceof LifeCycleListener) 171 ((LifeCycleListener)listener).lifeCycleFailure(event); 172 } 173 if (e instanceof InterruptedException ) 174 throw (InterruptedException )e; 175 if (e instanceof RuntimeException ) 176 throw (RuntimeException )e; 177 if (e instanceof Error ) 178 throw (Error )e; 179 log.warn(LogSupport.EXCEPTION, e); 180 } 181 finally 182 { 183 _stopping=false; 184 } 185 } 186 187 188 192 protected abstract void doStop() 193 throws Exception ; 194 195 196 protected void addComponent(Object o) 197 { 198 if (!LazyList.contains(_components,o)) 199 { 200 _components = LazyList.add(_components,o); 201 if(log.isDebugEnabled())log.debug("add component: "+o); 202 ComponentEvent event = new ComponentEvent(this,o); 203 for(int i=0;i<LazyList.size(_eventListeners);i++) 204 { 205 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 206 if (listener instanceof ComponentListener) 207 ((ComponentListener)listener).addComponent(event); 208 } 209 } 210 } 211 212 213 protected void removeComponent(Object o) 214 { 215 if (LazyList.contains(_components,o)) 216 { 217 _components=LazyList.remove(_components, o); 218 if(log.isDebugEnabled())log.debug("remove component: "+o); 219 ComponentEvent event = new ComponentEvent(this,o); 220 for(int i=0;i<LazyList.size(_eventListeners);i++) 221 { 222 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 223 if (listener instanceof ComponentListener) 224 ((ComponentListener)listener).removeComponent(event); 225 } 226 } 227 } 228 229 230 233 public void addEventListener(EventListener listener) 234 throws IllegalArgumentException 235 { 236 237 if (_eventListeners==null) 238 _eventListeners=new ArrayList (); 239 240 if (listener instanceof ComponentListener || 241 listener instanceof LifeCycleListener ) 242 { 243 if(log.isDebugEnabled())log.debug("addEventListener: "+listener); 244 _eventListeners=LazyList.add(_eventListeners,listener); 245 } 246 247 } 248 249 250 public void removeEventListener(EventListener listener) 251 { 252 if(log.isDebugEnabled())log.debug("removeEventListener: "+listener); 253 _eventListeners=LazyList.remove(_eventListeners,listener); 254 } 255 256 257 261 public void destroy() 262 { 263 if (isStarted()) 264 throw new IllegalStateException ("Started"); 265 266 if (_components!=null && _eventListeners!=null) 267 { 268 for (int c=0;c<LazyList.size(_components);c++) 269 { 270 Object o=LazyList.get(_components,c); 271 if (o instanceof HttpContext ) 272 ((HttpContext)o).destroy(); 273 274 ComponentEvent event = new ComponentEvent(this,o); 275 for(int i=0;i<LazyList.size(_eventListeners);i++) 276 { 277 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 278 if (listener instanceof ComponentListener) 279 ((ComponentListener)listener).removeComponent(event); 280 } 281 } 282 } 283 284 _components=null; 285 _eventListeners=null; 286 } 287 288 289 public Collection getComponents() 290 { 291 return LazyList.getList(_eventListeners, false); 292 } 293 294 295 private void readObject(java.io.ObjectInputStream in) 296 throws IOException , ClassNotFoundException 297 { 298 in.defaultReadObject(); 299 for (int c=0;c<LazyList.size(_components);c++) 300 { 301 Object o = LazyList.get(_components,c); 302 ComponentEvent event = new ComponentEvent(this,o); 303 for(int i=0;i<LazyList.size(_eventListeners);i++) 304 { 305 EventListener listener=(EventListener )LazyList.get(_eventListeners,i); 306 if (listener instanceof ComponentListener) 307 ((ComponentListener)listener).addComponent(event); 308 } 309 } 310 } 311 } 312 | Popular Tags |