1 17 18 package org.apache.avalon.fortress.util; 19 20 import org.apache.avalon.excalibur.logger.LoggerManager; 21 import org.apache.avalon.fortress.impl.DefaultContainer; 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.context.Context; 24 import org.apache.avalon.framework.context.ContextException; 25 import org.apache.avalon.framework.context.DefaultContext; 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.excalibur.event.Sink; 28 import org.apache.excalibur.event.command.ThreadManager; 29 import org.apache.excalibur.instrument.InstrumentManager; 30 import org.apache.excalibur.mpool.PoolManager; 31 32 import java.io.File ; 33 import java.net.URL ; 34 35 39 public final class FortressConfig 40 { 41 private final DefaultContext m_context; 42 43 57 public FortressConfig() 58 { 59 this( createDefaultConfig() ); 60 } 61 62 67 public FortressConfig( final Context parent ) 68 { 69 m_context = new OverridableContext( parent ); 70 } 71 72 75 public static final Context createDefaultConfig() 76 { 77 return createDefaultConfig( Thread.currentThread().getContextClassLoader() ); 78 } 79 80 83 public static final Context createDefaultConfig( final ClassLoader classLoader ) 84 { 85 final DefaultContext defaultContext = new DefaultContext(); 86 87 try 88 { 89 defaultContext.put( ContextManagerConstants.CONTAINER_CLASS, 90 DefaultContainer.class ); 91 defaultContext.put( ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS, 92 FortressCommandFailureHandler.class ); 93 } 94 catch ( Exception e ) 95 { 96 } 98 99 final File contextDir = new File ( System.getProperty( "user.dir" ) ); 100 final File workDir = new File ( System.getProperty( "java.io.tmpdir" ) ); 101 102 defaultContext.put( ContextManagerConstants.THREADS_CPU, new Integer ( 2 ) ); 103 defaultContext.put( ContextManagerConstants.THREAD_TIMEOUT, new Long ( 1000 ) ); 104 defaultContext.put( ContextManagerConstants.CONTEXT_DIRECTORY, contextDir ); 105 defaultContext.put( ContextManagerConstants.WORK_DIRECTORY, workDir ); 106 defaultContext.put( ContextManagerConstants.LOG_CATEGORY, "fortress" ); 107 defaultContext.put( ClassLoader .class.getName(), classLoader ); 108 defaultContext.put( ContextManagerConstants.CONFIGURATION_URI, "conf/system.xconf" ); 109 defaultContext.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, "conf/logkit.xconf" ); 110 111 defaultContext.makeReadOnly(); 112 113 return defaultContext; 114 } 115 116 119 public Context getContext() 120 { 121 m_context.makeReadOnly(); 122 return m_context; 123 } 124 125 public void setCommandSink( final Sink commandSink ) 126 { 127 m_context.put( Sink.ROLE, commandSink ); 128 } 129 130 public void setServiceManager( final ServiceManager componentManager ) 131 { 132 m_context.put( ContextManagerConstants.SERVICE_MANAGER, componentManager ); 133 } 134 135 public void setLifecycleExtensionManager( final LifecycleExtensionManager extensionManager ) 136 { 137 m_context.put( LifecycleExtensionManager.ROLE, extensionManager ); 138 } 139 140 public void setContainerClass( final String containerClass ) 141 throws ClassNotFoundException 142 { 143 ClassLoader classLoader; 144 try 145 { 146 classLoader = (ClassLoader ) m_context.get( ClassLoader .class.getName() ); 147 } 148 catch ( ContextException ce ) 149 { 150 classLoader = Thread.currentThread().getContextClassLoader(); 151 } 152 153 setContainerClass( classLoader.loadClass( containerClass ) ); 154 } 155 156 public void setContainerClass( final Class containerClass ) 157 { 158 m_context.put( ContextManagerConstants.CONTAINER_CLASS, containerClass ); 159 } 160 161 168 public void setCommandFailureHandlerClass( final String commandFailureHandlerClass ) 169 throws ClassNotFoundException 170 { 171 ClassLoader classLoader; 172 try 173 { 174 classLoader = (ClassLoader ) m_context.get( ClassLoader .class.getName() ); 175 } 176 catch ( ContextException ce ) 177 { 178 classLoader = Thread.currentThread().getContextClassLoader(); 179 } 180 181 setCommandFailureHandlerClass( classLoader.loadClass( commandFailureHandlerClass ) ); 182 } 183 184 191 public void setCommandFailureHandlerClass( final Class commandFailureHandlerClass ) 192 { 193 m_context.put( 194 ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS, commandFailureHandlerClass ); 195 } 196 197 public void setContainerConfiguration( final Configuration config ) 198 { 199 m_context.put( ContextManagerConstants.CONFIGURATION, config ); 200 m_context.put( ContextManagerConstants.CONFIGURATION_URI, null ); 201 } 202 203 public void setContainerConfiguration( final String location ) 204 { 205 m_context.put( ContextManagerConstants.CONFIGURATION_URI, location ); 206 } 207 208 public void setContextClassLoader( final ClassLoader loader ) 209 { 210 m_context.put( ClassLoader .class.getName(), loader ); 211 } 212 213 public void setContextDirectory( final File file ) 214 { 215 m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, file ); 216 } 217 218 public void setContextDirectory( final String directory ) 219 { 220 m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, new File ( directory ) ); 221 } 222 223 public void setContextRootURL( final URL url ) 224 { 225 m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, url ); 226 } 227 228 public void setLoggerCategory( final String category ) 229 { 230 m_context.put( ContextManagerConstants.LOG_CATEGORY, category ); 231 } 232 233 public void setLoggerManager( final LoggerManager logManager ) 234 { 235 m_context.put( LoggerManager.ROLE, logManager ); 236 m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION, null ); 237 m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, null ); 238 } 239 240 public void setLoggerManagerConfiguration( final Configuration config ) 241 { 242 m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION, config ); 243 m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, null ); 244 } 245 246 public void setLoggerManagerConfiguration( final String location ) 247 { 248 m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, location ); 249 } 250 251 public void setInstrumentManager( final InstrumentManager profiler ) 252 { 253 m_context.put( InstrumentManager.ROLE, profiler ); 254 m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION, null ); 255 m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, null ); 256 } 257 258 public void setInstrumentManagerConfiguration( final Configuration config ) 259 { 260 m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION, config ); 261 m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, null ); 262 } 263 264 public void setInstrumentManagerConfiguration( final String location ) 265 { 266 m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, location ); 267 } 268 269 public void setNumberOfThreadsPerCPU( final int numberOfThreads ) 270 { 271 m_context.put( ContextManagerConstants.THREADS_CPU, new Integer ( numberOfThreads ) ); 272 } 273 274 public void setPoolManager( final PoolManager poolManager ) 275 { 276 m_context.put( PoolManager.ROLE, poolManager ); 277 } 278 279 public void setRoleManager( final org.apache.avalon.fortress.RoleManager roleManager ) 280 { 281 m_context.put( org.apache.avalon.fortress.RoleManager.ROLE, roleManager ); 282 } 283 284 public void setRoleManagerConfiguration( final Configuration config ) 285 { 286 m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION, config ); 287 m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI, null ); 288 } 289 290 public void setRoleManagerConfiguration( final String location ) 291 { 292 m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI, location ); 293 } 294 295 public void setThreadTimeout( final long timeout ) 296 { 297 m_context.put( ContextManagerConstants.THREAD_TIMEOUT, new Long ( timeout ) ); 298 } 299 300 public void setWorkDirectory( final File file ) 301 { 302 m_context.put( ContextManagerConstants.WORK_DIRECTORY, file ); 303 } 304 305 public void setWorkDirectory( final String directory ) 306 { 307 setWorkDirectory( new File ( directory ) ); 308 } 309 310 public void setThreadManager( final ThreadManager threadManager ) 311 { 312 m_context.put( ThreadManager.ROLE, threadManager ); 313 } 314 } 315 | Popular Tags |