1 8 package org.apache.avalon.phoenix.components.kernel; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.InputStream ; 13 import java.io.FileNotFoundException ; 14 import java.util.HashMap ; 15 import org.apache.avalon.excalibur.i18n.ResourceManager; 16 import org.apache.avalon.excalibur.i18n.Resources; 17 import org.apache.avalon.framework.activity.Initializable; 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.logger.Logger; 22 import org.apache.avalon.framework.service.ServiceException; 23 import org.apache.avalon.framework.service.ServiceManager; 24 import org.apache.avalon.framework.service.Serviceable; 25 import org.apache.avalon.phoenix.components.util.ResourceUtil; 26 import org.apache.avalon.phoenix.interfaces.ApplicationContext; 27 import org.apache.avalon.phoenix.interfaces.ConfigurationRepository; 28 import org.apache.avalon.phoenix.interfaces.ConfigurationValidator; 29 import org.apache.avalon.phoenix.interfaces.Kernel; 30 import org.apache.avalon.phoenix.interfaces.ManagerException; 31 import org.apache.avalon.phoenix.interfaces.SystemManager; 32 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData; 33 import org.apache.avalon.phoenix.metadata.SarMetaData; 34 import org.apache.excalibur.threadcontext.ThreadContext; 35 import org.apache.excalibur.threadcontext.impl.DefaultThreadContextPolicy; 36 37 43 class DefaultApplicationContext 44 extends AbstractLogEnabled 45 implements ApplicationContext, Serviceable, Initializable 46 { 47 private static final Resources REZ = 48 ResourceManager.getPackageResources( DefaultApplicationContext.class ); 49 50 private final Logger m_hierarchy; 52 53 private final ClassLoader m_classLoader; 55 56 private final ThreadContext m_threadContext; 58 59 private ConfigurationRepository m_repository; 61 62 private ConfigurationValidator m_validator; 64 65 private SystemManager m_systemManager; 67 68 private SystemManager m_blockManager; 69 70 private final SarMetaData m_metaData; 71 private final File m_workDirectory; 72 73 76 private Kernel m_kernel; 77 78 protected DefaultApplicationContext( final SarMetaData metaData, 79 final File workDirectory, 80 final ClassLoader classLoader, 81 final Logger hierarchy ) 82 { 83 if( null == metaData ) 84 { 85 throw new NullPointerException ( "metaData" ); 86 } 87 if( null == classLoader ) 88 { 89 throw new NullPointerException ( "classLoader" ); 90 } 91 if( null == hierarchy ) 92 { 93 throw new NullPointerException ( "hierarchy" ); 94 } 95 if( null == workDirectory ) 96 { 97 throw new NullPointerException ( "workDirectory" ); 98 } 99 100 m_metaData = metaData; 101 m_classLoader = classLoader; 102 m_hierarchy = hierarchy; 103 m_workDirectory = workDirectory; 104 105 final DefaultThreadContextPolicy policy = new DefaultThreadContextPolicy(); 106 final HashMap map = new HashMap ( 1 ); 107 map.put( DefaultThreadContextPolicy.CLASSLOADER, m_classLoader ); 108 m_threadContext = new ThreadContext( policy, map ); 109 } 110 111 public void service( final ServiceManager serviceManager ) 112 throws ServiceException 113 { 114 m_repository = (ConfigurationRepository)serviceManager. 115 lookup( ConfigurationRepository.ROLE ); 116 m_systemManager = (SystemManager)serviceManager. 117 lookup( SystemManager.ROLE ); 118 m_validator = (ConfigurationValidator)serviceManager. 119 lookup( ConfigurationValidator.ROLE ); 120 m_kernel = (Kernel)serviceManager.lookup( Kernel.ROLE ); 121 } 122 123 public void initialize() 124 throws Exception 125 { 126 m_blockManager = getManagementContext(); 127 } 128 129 public InputStream getResourceAsStream( final String name ) 130 { 131 final File file = 132 ResourceUtil.getFileForResource( name, 133 m_metaData.getHomeDirectory(), 134 m_workDirectory ); 135 if( !file.exists() ) 136 { 137 return null; 138 } 139 else 140 { 141 try 142 { 143 return new FileInputStream ( file ); 144 } 145 catch( FileNotFoundException e ) 146 { 147 return null; 149 } 150 } 151 } 152 153 public SarMetaData getMetaData() 154 { 155 return m_metaData; 156 } 157 158 public ThreadContext getThreadContext() 159 { 160 return m_threadContext; 161 } 162 163 public void requestShutdown() 164 { 165 final Thread thread = new Thread ( "AppShutdown" ) 166 { 167 public void run() 168 { 169 schedulShutdown(); 170 } 171 }; 172 thread.start(); 173 } 174 175 private void schedulShutdown() 176 { 177 try 178 { 179 Thread.sleep( 2 ); 184 m_kernel.removeApplication( m_metaData.getName() ); 185 } 186 catch( Exception e ) 187 { 188 final String message = 189 REZ.getString( "applicationcontext.error.noremove", 190 m_metaData.getName() ); 191 getLogger().error( message, e ); 192 } 193 } 194 195 200 public ClassLoader getClassLoader() 201 { 202 return m_classLoader; 203 } 204 205 212 public Logger getLogger( final String category ) 213 { 214 return m_hierarchy.getChildLogger( category ); 215 } 216 217 226 public void exportObject( final String name, 227 final Class [] services, 228 final Object object ) 229 throws Exception 230 { 231 m_blockManager.register( name, object, services ); 232 } 233 234 239 public void unexportObject( final String name ) 240 throws Exception 241 { 242 m_blockManager.unregister( name ); 243 } 244 245 251 public Configuration getConfiguration( final String component ) 252 throws ConfigurationException 253 { 254 final Configuration configuration = 255 m_repository.getConfiguration( m_metaData.getName(), 256 component ); 257 258 if( hasBlockListener( component, this.m_metaData.getListeners() ) ) 260 { 261 return configuration; 262 } 263 else if( m_validator.isValid( m_metaData.getName(), 264 component, 265 configuration ) ) 266 { 267 268 return configuration; 269 } 270 else 271 { 272 final String message = 273 REZ.getString( "applicationcontext.error.invalidconfig", 274 component ); 275 throw new ConfigurationException( message ); 276 } 277 } 278 279 286 private boolean hasBlockListener( final String name, 287 final BlockListenerMetaData[] listeners ) 288 { 289 for( int i = 0; i < listeners.length; i++ ) 290 { 291 if( listeners[ i ].getName().equals( name ) ) 292 { 293 return true; 294 } 295 } 296 297 return false; 298 } 299 300 306 private SystemManager getManagementContext() 307 throws ManagerException 308 { 309 final SystemManager appContext = 310 m_systemManager.getSubContext( null, "application" ); 311 return appContext.getSubContext( m_metaData.getName(), "block" ); 312 } 313 } 314 | Popular Tags |