1 8 package org.apache.avalon.phoenix.components.application; 9 10 import org.apache.avalon.excalibur.i18n.ResourceManager; 11 import org.apache.avalon.excalibur.i18n.Resources; 12 import org.apache.avalon.framework.component.Component; 13 import org.apache.avalon.framework.component.ComponentManager; 14 import org.apache.avalon.framework.component.DefaultComponentManager; 15 import org.apache.avalon.framework.configuration.Configuration; 16 import org.apache.avalon.framework.configuration.ConfigurationException; 17 import org.apache.avalon.framework.context.Context; 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.logger.Logger; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.avalon.framework.service.DefaultServiceManager; 22 import org.apache.avalon.framework.service.ServiceManager; 23 import org.apache.avalon.phoenix.interfaces.Application; 24 import org.apache.avalon.phoenix.interfaces.ApplicationContext; 25 import org.apache.avalon.phoenix.metadata.BlockMetaData; 26 import org.apache.avalon.phoenix.metadata.DependencyMetaData; 27 import org.apache.excalibur.containerkit.lifecycle.ResourceProvider; 28 29 36 class BlockResourceProvider 37 extends AbstractLogEnabled 38 implements ResourceProvider 39 { 40 private static final Resources REZ = 41 ResourceManager.getPackageResources( BlockResourceProvider.class ); 42 43 46 private final ApplicationContext m_context; 47 48 52 private final Application m_application; 53 54 public BlockResourceProvider( final ApplicationContext context, 55 final Application application ) 56 { 57 if( null == context ) 58 { 59 throw new NullPointerException ( "context" ); 60 } 61 62 if( null == application ) 63 { 64 throw new NullPointerException ( "application" ); 65 } 66 67 m_context = context; 68 m_application = application; 69 } 70 71 78 public Object createObject( final Object entry ) 79 throws Exception 80 { 81 final BlockMetaData metaData = getMetaDataFor( entry ); 82 final ClassLoader classLoader = m_context.getClassLoader(); 83 String classname = metaData.getBlockInfo().getBlockDescriptor().getImplementationKey(); 84 final Class clazz = classLoader.loadClass( classname ); 85 return clazz.newInstance(); 86 } 87 88 95 public Logger createLogger( final Object entry ) 96 throws Exception 97 { 98 final BlockMetaData metaData = getMetaDataFor( entry ); 99 final String name = metaData.getName(); 100 return m_context.getLogger( name ); 101 } 102 103 109 public Context createContext( final Object entry ) 110 throws Exception 111 { 112 final BlockMetaData metaData = getMetaDataFor( entry ); 113 return new DefaultBlockContext( metaData.getName(), 114 m_context ); 115 } 116 117 127 public ComponentManager createComponentManager( final Object entry ) 128 throws Exception 129 { 130 final BlockMetaData metaData = getMetaDataFor( entry ); 131 final DefaultComponentManager componentManager = new DefaultComponentManager(); 132 final DependencyMetaData[] roles = metaData.getDependencies(); 133 134 for( int i = 0; i < roles.length; i++ ) 135 { 136 final DependencyMetaData role = roles[ i ]; 137 final Object dependency = m_application.getBlock( role.getName() ); 138 if( dependency instanceof Component ) 139 { 140 componentManager.put( role.getRole(), (Component)dependency ); 141 } 142 else 143 { 144 final String message = 145 REZ.getString( "lifecycle.nota-component.error", 146 metaData.getName(), 147 role.getRole(), 148 role.getName() ); 149 throw new Exception ( message ); 150 } 151 } 152 153 return componentManager; 154 } 155 156 166 public ServiceManager createServiceManager( final Object entry ) 167 throws Exception 168 { 169 final BlockMetaData metaData = getMetaDataFor( entry ); 170 final DefaultServiceManager manager = new DefaultServiceManager(); 171 final DependencyMetaData[] roles = metaData.getDependencies(); 172 173 for( int i = 0; i < roles.length; i++ ) 174 { 175 final DependencyMetaData role = roles[ i ]; 176 final Object dependency = m_application.getBlock( role.getName() ); 177 manager.put( role.getRole(), dependency ); 178 } 179 180 return manager; 181 } 182 183 public Configuration createConfiguration( final Object entry ) 184 throws Exception 185 { 186 final BlockMetaData metaData = getMetaDataFor( entry ); 187 final String name = metaData.getName(); 188 try 189 { 190 return m_context.getConfiguration( name ); 191 } 192 catch( final ConfigurationException ce ) 193 { 194 final String message = 197 REZ.getString( "missing-block-configuration", 198 name ); 199 throw new ConfigurationException( message, ce ); 200 } 201 } 202 203 public Parameters createParameters( final Object entry ) 204 throws Exception 205 { 206 final Configuration configuration = 207 createConfiguration( entry ); 208 final Parameters parameters = 209 Parameters.fromConfiguration( configuration ); 210 parameters.makeReadOnly(); 211 return parameters; 212 } 213 214 220 private BlockMetaData getMetaDataFor( final Object entry ) 221 { 222 return ((BlockEntry)entry).getMetaData(); 223 } 224 } 225
| Popular Tags
|