1 17 package org.apache.avalon.excalibur.component.servlet; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 import javax.servlet.GenericServlet ; 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletContext ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.ServletRequest ; 27 import javax.servlet.ServletResponse ; 28 import javax.servlet.UnavailableException ; 29 30 import org.apache.avalon.excalibur.component.ExcaliburComponentManagerCreator; 31 import org.apache.avalon.excalibur.logger.LoggerManager; 32 import org.apache.avalon.framework.component.ComponentManager; 33 import org.apache.avalon.framework.container.ContainerUtil; 34 import org.apache.avalon.framework.service.ServiceManager; 35 import org.apache.excalibur.instrument.InstrumentManager; 36 37 146 public class ExcaliburComponentManagerServlet 147 extends GenericServlet 148 { 149 private ExcaliburComponentManagerCreator m_componentManagerCreator; 150 151 152 private Latch m_latch; 153 154 157 158 161 171 public void init( ServletConfig servletConfig ) throws ServletException 172 { 173 super.init( servletConfig ); 174 175 ServletContext servletContext = getServletContext(); 177 178 InputStream loggerManagerConfigStream = null; 179 InputStream roleManagerConfigStream = null; 180 InputStream componentManagerConfigStream = null; 181 InputStream instrumentManagerConfigStream = null; 182 try 183 { 184 loggerManagerConfigStream = 185 getStreamFromParameter( servletConfig, "logkit", true ); 186 roleManagerConfigStream = 187 getStreamFromParameter( servletConfig, "roles", true ); 188 componentManagerConfigStream = 189 getStreamFromParameter( servletConfig, "components", true ); 190 instrumentManagerConfigStream = 191 getStreamFromParameter( servletConfig, "instrument", false ); 192 193 try 195 { 196 m_componentManagerCreator = new ExcaliburComponentManagerCreator( 197 null, 198 loggerManagerConfigStream, 199 roleManagerConfigStream, 200 componentManagerConfigStream, 201 instrumentManagerConfigStream ); 202 } 203 catch( Exception e ) 204 { 205 String msg = "Unable to create the ComponentManagerCreator. " 206 + "Most likely a comfiguration problem."; 207 throw new ServletException ( msg, e ); 208 } 209 } 210 finally 211 { 212 try 214 { 215 if( loggerManagerConfigStream != null ) 216 { 217 loggerManagerConfigStream.close(); 218 } 219 if( roleManagerConfigStream != null ) 220 { 221 roleManagerConfigStream.close(); 222 } 223 if( componentManagerConfigStream != null ) 224 { 225 componentManagerConfigStream.close(); 226 } 227 if( instrumentManagerConfigStream != null ) 228 { 229 instrumentManagerConfigStream.close(); 230 } 231 } 232 catch( IOException e ) 233 { 234 throw new ServletException ( "Encountered an error closing resource streams.", e ); 235 } 236 } 237 238 LoggerManager loggerManager = m_componentManagerCreator.getLoggerManager(); 239 240 246 m_latch = new Latch( m_componentManagerCreator ); 248 m_latch.enableLogging( loggerManager.getLoggerForCategory( "system.ecmservlet" ) ); 249 250 ReferenceProxy loggerManagerProxy = m_latch.createProxy( 252 loggerManager, "LoggerManager" ); 253 ReferenceProxy serviceManagerProxy = m_latch.createProxy( 254 m_componentManagerCreator.getServiceManager(), "ServiceManager" ); 255 ReferenceProxy componentManagerProxy = m_latch.createProxy( 256 m_componentManagerCreator.getComponentManager(), "ComponentManager" ); 257 ReferenceProxy instrumentManagerProxy = m_latch.createProxy( 258 m_componentManagerCreator.getInstrumentManager(), "InstrumentManager" ); 259 260 servletContext.setAttribute( LoggerManager.class.getName(), loggerManagerProxy ); 263 servletContext.setAttribute( ServiceManager.class.getName(), serviceManagerProxy ); 264 servletContext.setAttribute( ComponentManager.class.getName(), componentManagerProxy ); 265 servletContext.setAttribute( InstrumentManager.class.getName(), instrumentManagerProxy ); 266 267 } 269 270 273 public void destroy() 274 { 275 277 ServletContext servletContext = getServletContext(); 278 279 servletContext.removeAttribute( LoggerManager.class.getName() ); 281 servletContext.removeAttribute( ServiceManager.class.getName() ); 282 servletContext.removeAttribute( ComponentManager.class.getName() ); 283 servletContext.removeAttribute( InstrumentManager.class.getName() ); 284 285 m_latch.requestTrigger(); 287 288 } 290 291 299 public void service( ServletRequest servletRequest, ServletResponse servletResponse ) 300 throws UnavailableException 301 { 302 throw new UnavailableException ( getClass().getName() + " does not except service requests." ); 303 } 304 305 308 321 private InputStream getStreamFromParameter( ServletConfig servletConfig, 322 String resourceName, 323 boolean required ) 324 throws ServletException 325 { 326 327 String configFileName = servletConfig.getInitParameter( resourceName ); 328 329 if( configFileName == null ) 330 { 331 if( required ) 332 { 333 throw new ServletException ( resourceName 334 + " parameter must be provided in servlet configuration." ); 335 } 336 else 337 { 338 return null; 339 } 340 } 341 342 ServletContext servletContext = servletConfig.getServletContext(); 343 344 log( "Attempting to access resource: " + configFileName ); 345 346 InputStream is = servletContext.getResourceAsStream( configFileName ); 347 348 if( is == null ) 349 { 350 throw new ServletException ( "Resource '" + configFileName + "' is not available." ); 351 } 352 353 return is; 354 } 355 356 359 private static class Latch 360 extends AbstractReferenceProxyLatch 361 { 362 ExcaliburComponentManagerCreator m_componentManagerCreator; 363 364 367 373 Latch( ExcaliburComponentManagerCreator componentManagerCreator ) 374 { 375 m_componentManagerCreator = componentManagerCreator; 376 } 377 378 381 384 public void triggered() 385 throws Exception 386 { 387 ContainerUtil.shutdown( m_componentManagerCreator ); 389 } 391 } 392 } 393 | Popular Tags |