1 17 18 package org.apache.avalon.fortress.impl.handler; 19 20 import org.apache.avalon.excalibur.logger.LoggerManager; 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.activity.Initializable; 23 import org.apache.avalon.framework.container.ContainerUtil; 24 import org.apache.avalon.framework.logger.Logger; 25 import org.apache.avalon.framework.service.ServiceException; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.avalon.framework.service.Serviceable; 28 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable; 29 import org.apache.excalibur.instrument.CounterInstrument; 30 import org.apache.excalibur.instrument.Instrumentable; 31 import org.apache.excalibur.mpool.ObjectFactory; 32 33 41 public abstract class AbstractComponentHandler 42 extends AbstractLogEnabledInstrumentable 43 implements Serviceable, Initializable, Disposable, ComponentHandler 44 { 45 private CounterInstrument m_request = new CounterInstrument( "requests" ); 46 private CounterInstrument m_release = new CounterInstrument( "releases" ); 47 48 52 protected ObjectFactory m_factory; 53 54 58 protected boolean m_prepared; 59 60 64 protected boolean m_disposed; 65 66 67 protected Logger m_logger; 68 69 70 protected LoggerManager m_loggerManager; 71 72 75 public void service( final ServiceManager manager ) 76 throws ServiceException 77 { 78 m_loggerManager = 79 (LoggerManager) manager.lookup( LoggerManager.ROLE ); 80 m_factory = 81 (ObjectFactory) manager.lookup( ObjectFactory.ROLE ); 82 } 83 84 public void initialize() 85 throws Exception ![JavaDoc](../../../../../../../cmn/javadoc.gif) 86 { 87 final String classname = getClass().getName(); 88 final int index = classname.lastIndexOf( '.' ); 89 final String name = classname.substring( index + 1 ); 90 91 String loggerName = name.toLowerCase(); 92 if ( name.endsWith( "ComponentHandler" ) ) 93 { 94 final int endIndex = loggerName.length() - 16; 95 loggerName = loggerName.substring( 0, endIndex ); 96 } 97 98 final String categoryName = "system.handler." + loggerName; 99 m_logger = 100 m_loggerManager.getLoggerForCategory( categoryName ); 101 102 if ( m_factory instanceof Instrumentable ) 103 { 104 addChildInstrumentable( (Instrumentable) m_factory ); 105 } 106 107 addInstrument( m_request ); 108 addInstrument( m_release ); 109 110 setInstrumentableName( name ); 111 } 112 113 119 public Class getComponentClass() 120 { 121 return m_factory.getCreatedClass(); 122 } 123 124 130 public synchronized void prepareHandler() 131 throws Exception ![JavaDoc](../../../../../../../cmn/javadoc.gif) 132 { 133 if ( m_prepared ) 134 { 135 return; 136 } 137 138 if ( m_disposed ) 139 { 140 final String message = "Attempted to prepare disposed ComponentHandler for : " + 141 m_factory.getCreatedClass().getName(); 142 m_logger.warn( message ); 143 144 return; 145 } 146 147 doPrepare(); 148 149 if ( m_logger.isDebugEnabled() ) 150 { 151 final String message = "ComponentHandler initialized for: " + 152 m_factory.getCreatedClass().getName(); 153 m_logger.debug( message ); 154 } 155 156 m_prepared = true; 157 } 158 159 165 protected void doPrepare() throws Exception ![JavaDoc](../../../../../../../cmn/javadoc.gif) 166 {} 167 168 172 public Object get() 173 throws Exception ![JavaDoc](../../../../../../../cmn/javadoc.gif) 174 { 175 if ( !m_prepared ) 176 { 177 prepareHandler(); 178 } 179 180 if ( m_disposed ) 181 { 182 final String message = 183 "You cannot get a component from a disposed holder"; 184 throw new IllegalStateException ( message ); 185 } 186 187 if ( m_request.isActive() ) 188 { 189 m_request.increment(); 190 } 191 192 return doGet(); 193 } 194 195 202 protected abstract Object doGet() 203 throws Exception ; 204 205 209 public void put( final Object component ) 210 { 211 if ( !m_prepared ) 212 { 213 final String message = 214 "You cannot put a component in an uninitialized holder"; 215 throw new IllegalStateException ( message ); 216 } 217 218 if ( m_release.isActive() ) 219 { 220 m_release.increment(); 221 } 222 223 doPut( component ); 224 } 225 226 231 protected void doPut( final Object component ) 232 { 233 } 234 235 241 protected Object newComponent() 242 throws Exception ![JavaDoc](../../../../../../../cmn/javadoc.gif) 243 { 244 try 245 { 246 return m_factory.newInstance(); 247 } 248 catch ( final Exception e ) 249 { 250 if ( m_logger.isDebugEnabled() ) 251 { 252 final String message = "Unable to create new instance"; 253 m_logger.debug( message, e ); 254 } 255 256 throw e; 257 } 258 } 259 260 265 protected void disposeComponent( final Object component ) 266 { 267 if ( null == component ) 268 { 269 return; 270 } 271 try 272 { 273 m_factory.dispose( component ); 274 } 275 catch ( final Exception e ) 276 { 277 if ( m_logger.isWarnEnabled() ) 278 { 279 m_logger.warn( "Error disposing component", e ); 280 } 281 } 282 } 283 284 287 public void dispose() 288 { 289 doDispose(); 290 try 291 { 292 ContainerUtil.dispose( m_factory ); 293 } 294 catch ( RuntimeException e ) 295 { 296 if ( m_logger.isWarnEnabled() ) 297 { 298 final String message = "Error decommissioning component: " + 299 m_factory.getCreatedClass().getName(); 300 m_logger.warn( message, e ); 301 } 302 } 303 304 m_disposed = true; 305 } 306 307 311 protected void doDispose() 312 { 313 } 314 315 319 public String toString() 320 { 321 return getClass().getName() + "[for: " + m_factory.getCreatedClass().getName() + "]"; 322 } 323 } 324 | Popular Tags |