1 17 18 package org.apache.avalon.fortress.impl; 19 20 import org.apache.avalon.fortress.ContainerManager; 21 import org.apache.avalon.fortress.ContainerManagerConstants; 22 import org.apache.avalon.fortress.InitializationException; 23 import org.apache.avalon.fortress.util.ContextManager; 24 import org.apache.avalon.framework.activity.Disposable; 25 import org.apache.avalon.framework.activity.Initializable; 26 import org.apache.avalon.framework.component.Composable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.container.ContainerUtil; 29 import org.apache.avalon.framework.context.Context; 30 import org.apache.avalon.framework.context.ContextException; 31 import org.apache.avalon.framework.logger.ConsoleLogger; 32 import org.apache.avalon.framework.logger.Loggable; 33 import org.apache.avalon.framework.logger.Logger; 34 import org.apache.avalon.framework.parameters.Parameters; 35 import org.apache.avalon.framework.service.ServiceManager; 36 import org.apache.avalon.excalibur.logger.LoggerManager; 37 38 46 public class DefaultContainerManager 47 implements Initializable, Disposable, ContainerManager, ContainerManagerConstants 48 { 49 private final ContextManager m_contextManager; 50 private final Logger m_logger; 51 private Object m_containerInstance; 52 53 public DefaultContainerManager( final ContextManager contextManager ) 54 { 55 this( contextManager, null ); 56 } 57 58 public DefaultContainerManager( final ContextManager contextManager, 59 final Logger logger ) 60 { 61 m_contextManager = contextManager; 62 m_logger = ( logger == null ? 63 createLoggerFromContext( m_contextManager.getContainerManagerContext() ) : logger ); 64 } 65 66 public DefaultContainerManager( final Context initParameters ) throws Exception 67 { 68 this( initParameters, null ); 69 } 70 71 public DefaultContainerManager( final Context initParameters, 72 final Logger logger ) throws Exception 73 { 74 this( getInitializedContextManager( initParameters, logger ), logger ); 75 } 76 77 82 private static ContextManager getInitializedContextManager( final Context initParameters, 83 Logger logger ) throws Exception 84 { 85 final ContextManager contextManager = new ContextManager( initParameters, logger ); 87 contextManager.initialize(); 88 return contextManager; 89 } 90 91 protected Logger createLoggerFromContext( final Context initParameters ) 92 { 93 try 94 { 95 final Logger logger = (Logger) initParameters.get( LOGGER ); 96 return logger; 97 } 98 catch ( ContextException ce ) 99 { 100 final Logger consoleLogger = new ConsoleLogger(); 101 consoleLogger.error( "ContainerManager could not obtain logger manager from context " 102 + "(this should not happen). Using console instead." ); 103 return consoleLogger; 104 } 105 } 106 107 110 public void initialize() throws Exception 111 { 112 initializeContainer(); 113 } 114 115 protected void initializeContainer() throws InitializationException 116 { 117 if ( null == m_containerInstance ) 118 { 119 createContainer(); 120 } 121 } 122 123 private void createContainer() 124 throws InitializationException 125 { 126 final Context managerContext = 127 m_contextManager.getContainerManagerContext(); 128 final Object instance; 129 try 130 { 131 final Class clazz = (Class ) managerContext.get( CONTAINER_CLASS ); 132 instance = clazz.newInstance(); 133 } 134 catch ( Exception e ) 135 { 136 final String message = 137 "Cannot set up impl. Unable to create impl class"; 138 throw new InitializationException( message, e ); 139 } 140 141 if ( instance instanceof Loggable ) 142 { 143 throw new InitializationException( "Loggable containers are not supported" ); 144 } 145 146 if ( instance instanceof Composable ) 147 { 148 throw new InitializationException( "Composable containers are not supported" ); 149 } 150 151 try 152 { 153 final Context implContext = m_contextManager.getChildContext(); 154 155 final ServiceManager serviceManager = 156 (ServiceManager) getContextEntry( managerContext, SERVICE_MANAGER ); 157 final LoggerManager loggerManager = 158 (LoggerManager) serviceManager.lookup( LoggerManager.ROLE ); 159 160 ContainerUtil.enableLogging( instance, loggerManager.getDefaultLogger() ); 161 ContainerUtil.contextualize( instance, implContext ); 162 163 ContainerUtil.service( instance, serviceManager ); 164 165 final Configuration config = 166 (Configuration) getContextEntry( managerContext, CONFIGURATION ); 167 ContainerUtil.configure( instance, config ); 168 169 final Parameters parameters = 170 (Parameters) getContextEntry( managerContext, PARAMETERS ); 171 ContainerUtil.parameterize( instance, parameters ); 172 173 ContainerUtil.initialize( instance ); 174 ContainerUtil.start( instance ); 175 176 m_containerInstance = instance; 177 } 178 catch ( Exception e ) 179 { 180 final String message = 181 "Cannot set up Container. Startup lifecycle failure"; 182 throw new InitializationException( message, e ); 183 } 184 } 185 186 193 private Object getContextEntry( final Context context, final String key ) 194 { 195 try 196 { 197 return context.get( key ); 198 } 199 catch ( ContextException e ) 200 { 201 return null; 202 } 203 } 204 205 protected void disposeContainer() 206 { 207 if ( null != m_containerInstance ) 208 { 209 try 210 { 211 ContainerUtil.stop( m_containerInstance ); 212 } 213 catch ( Exception e ) 214 { 215 if ( getLogger().isWarnEnabled() ) 216 { 217 getLogger().warn( "Caught an exception when stopping the Container, " 218 + "continuing with shutdown", e ); 219 } 220 } 221 222 ContainerUtil.dispose( m_containerInstance ); 223 m_containerInstance = null; 224 } 225 } 226 227 230 public void dispose() 231 { 232 disposeContainer(); 233 m_contextManager.dispose(); 234 } 235 236 240 public Object getContainer() 241 { 242 return m_containerInstance; 243 } 244 245 249 public final Logger getLogger() 250 { 251 return m_logger; 254 } 255 } 256 | Popular Tags |