1 87 package org.codehaus.loom.components.kernel; 88 89 import java.io.File ; 90 import java.io.FileInputStream ; 91 import java.io.FileNotFoundException ; 92 import java.io.InputStream ; 93 import org.apache.avalon.framework.logger.Logger; 94 import org.apache.excalibur.instrument.InstrumentManager; 95 import org.codehaus.loom.components.util.ResourceUtil; 96 import org.codehaus.loom.components.util.profile.PartitionProfile; 97 import org.codehaus.loom.interfaces.ApplicationContext; 98 import org.codehaus.loom.interfaces.ContainerConstants; 99 import org.codehaus.loom.interfaces.Kernel; 100 import org.codehaus.loom.interfaces.LoomException; 101 import org.codehaus.loom.interfaces.SystemManager; 102 import org.codehaus.spice.alchemist.logger.LoggerAlchemist; 103 import org.codehaus.spice.loggerstore.LoggerStore; 104 import org.codehaus.spice.salt.i18n.ResourceManager; 105 import org.codehaus.spice.salt.i18n.Resources; 106 import org.codehaus.dna.AbstractLogEnabled; 107 import org.codehaus.dna.Active; 108 import org.codehaus.dna.Composable; 109 import org.codehaus.dna.MissingResourceException; 110 import org.codehaus.dna.ResourceLocator; 111 112 118 class DefaultApplicationContext 119 extends AbstractLogEnabled 120 implements ApplicationContext, Composable, Active 121 { 122 private static final Resources REZ = 123 ResourceManager.getPackageResources( DefaultApplicationContext.class ); 124 125 private final LoggerStore m_store; 127 128 private final ClassLoader m_classLoader; 130 131 private InstrumentManager m_instrumentManager; 133 134 private SystemManager m_systemManager; 136 137 private SystemManager m_blockManager; 138 139 private final PartitionProfile m_profile; 140 private final File m_workDirectory; 141 private final File m_homeDirectory; 142 143 144 private Kernel m_kernel; 145 146 protected DefaultApplicationContext( final PartitionProfile profile, 147 final File homeDirectory, 148 final File workDirectory, 149 final ClassLoader classLoader, 150 final LoggerStore store ) 151 { 152 if( null == profile ) 153 { 154 throw new NullPointerException ( "profile" ); 155 } 156 if( null == classLoader ) 157 { 158 throw new NullPointerException ( "classLoader" ); 159 } 160 if( null == store ) 161 { 162 throw new NullPointerException ( "store" ); 163 } 164 if( null == workDirectory ) 165 { 166 throw new NullPointerException ( "workDirectory" ); 167 } 168 if( null == homeDirectory ) 169 { 170 throw new NullPointerException ( "homeDirectory" ); 171 } 172 m_profile = profile; 173 m_classLoader = classLoader; 174 m_store = store; 175 m_workDirectory = workDirectory; 176 m_homeDirectory = homeDirectory; 177 } 178 179 184 public void compose( final ResourceLocator locator ) 185 throws MissingResourceException 186 { 187 m_systemManager = (SystemManager)locator. 188 lookup( SystemManager.class.getName() ); 189 m_kernel = (Kernel)locator.lookup( Kernel.class.getName() ); 190 m_instrumentManager = (InstrumentManager)locator. 191 lookup( InstrumentManager.class.getName() ); 192 } 193 194 public void initialize() 195 throws Exception  196 { 197 m_blockManager = getManagementContext(); 198 } 199 200 public void dispose() 201 throws Exception  202 { 203 } 204 205 public InputStream getResourceAsStream( final String name ) 206 { 207 final File file = 208 ResourceUtil.getFileForResource( name, 209 getHomeDirectory(), 210 m_workDirectory ); 211 if( !file.exists() ) 212 { 213 return null; 214 } 215 else 216 { 217 try 218 { 219 return new FileInputStream ( file ); 220 } 221 catch( FileNotFoundException e ) 222 { 223 return null; 225 } 226 } 227 } 228 229 public PartitionProfile getPartitionProfile() 230 { 231 return m_profile; 232 } 233 234 public void requestShutdown() 235 { 236 final Thread thread = new Thread ( "AppShutdown" ) 237 { 238 public void run() 239 { 240 scheduleShutdown(); 241 } 242 }; 243 thread.start(); 244 } 245 246 private void scheduleShutdown() 247 { 248 try 249 { 250 Thread.sleep( 2 ); 255 m_kernel.removeApplication( getName() ); 256 } 257 catch( Exception e ) 258 { 259 final String message = 260 REZ.format( "applicationcontext.error.noremove", 261 getName() ); 262 getLogger().error( message, e ); 263 } 264 } 265 266 public File getHomeDirectory() 267 { 268 return m_homeDirectory; 269 } 270 271 276 public ClassLoader getClassLoader() 277 { 278 return m_classLoader; 279 } 280 281 288 public Logger getLogger( final String category ) 289 throws Exception  290 { 291 return LoggerAlchemist.toAvalonLogger( m_store.getLogger( category ) ); 292 } 293 294 301 public void exportObject( final String name, 302 final Object object ) 303 throws Exception  304 { 305 m_blockManager.register( name, object ); 306 } 307 308 313 public void unexportObject( final String name ) 314 throws Exception  315 { 316 m_blockManager.unregister( name ); 317 } 318 319 325 private SystemManager getManagementContext() 326 throws LoomException 327 { 328 final SystemManager appContext = 329 m_systemManager.getSubContext( null, "application" ); 330 return appContext.getSubContext( getName(), "block" ); 331 } 332 333 338 public InstrumentManager getInstrumentManager() 339 { 340 return m_instrumentManager; 341 } 342 343 349 public String getInstrumentableName( String component ) 350 { 351 return ContainerConstants.ROOT_INSTRUMENT_CATEGORY + 352 "." + 353 getName() + 354 "." + 355 component; 356 } 357 358 private String getName() 359 { 360 return m_profile.getMetaData().getName(); 361 } 362 } 363 | Popular Tags |