| 1 87 package org.codehaus.loom.components.application; 88 89 import java.lang.reflect.Array ; 90 import java.util.ArrayList ; 91 import java.util.Collections ; 92 import java.util.HashMap ; 93 import java.util.Iterator ; 94 import java.util.List ; 95 import java.util.Map ; 96 import org.apache.avalon.framework.component.ComponentManager; 97 import org.apache.avalon.framework.component.WrapperComponentManager; 98 import org.apache.avalon.framework.configuration.Configuration; 99 import org.apache.avalon.framework.context.Context; 100 import org.apache.avalon.framework.logger.Logger; 101 import org.apache.avalon.framework.parameters.Parameters; 102 import org.apache.avalon.framework.service.DefaultServiceManager; 103 import org.apache.avalon.framework.service.ServiceManager; 104 import org.apache.excalibur.instrument.InstrumentManager; 105 import org.codehaus.dna.AbstractLogEnabled; 106 import org.codehaus.loom.components.util.info.ComponentInfo; 107 import org.codehaus.loom.components.util.info.DependencyDescriptor; 108 import org.codehaus.loom.components.util.lifecycle.ResourceProvider; 109 import org.codehaus.loom.components.util.metadata.ComponentTemplate; 110 import org.codehaus.loom.components.util.metadata.DependencyDirective; 111 import org.codehaus.loom.interfaces.Application; 112 import org.codehaus.loom.interfaces.ApplicationContext; 113 import org.codehaus.spice.alchemist.configuration.ConfigurationAlchemist; 114 115 121 class BlockResourceProvider 122 extends AbstractLogEnabled 123 implements ResourceProvider 124 { 125 126 private final ApplicationContext m_context; 127 128 132 private final Application m_application; 133 134 public BlockResourceProvider( final ApplicationContext context, 135 final Application application ) 136 { 137 if( null == context ) 138 { 139 throw new NullPointerException ( "context" ); 140 } 141 142 if( null == application ) 143 { 144 throw new NullPointerException ( "application" ); 145 } 146 147 m_context = context; 148 m_application = application; 149 } 150 151 157 public Object createObject( final Object entry ) 158 throws Exception  159 { 160 final Class clazz = getBlockEntry( entry ).getInfo().getType(); 161 return clazz.newInstance(); 162 } 163 164 171 public Logger createLogger( final Object entry ) 172 throws Exception  173 { 174 final String name = getBlockEntry( entry ).getName(); 175 return m_context.getLogger( name ); 176 } 177 178 185 public InstrumentManager createInstrumentManager( Object entry ) 186 throws Exception  187 { 188 return m_context.getInstrumentManager(); 189 } 190 191 198 public String createInstrumentableName( Object entry ) 199 throws Exception  200 { 201 final String name = getBlockEntry( entry ).getName(); 202 return m_context.getInstrumentableName( name ); 203 } 204 205 211 public Context createContext( final Object entry ) 212 throws Exception  213 { 214 return new DefaultBlockContext( getBlockEntry( entry ).getName(), 215 m_context ); 216 } 217 218 227 public ComponentManager createComponentManager( final Object entry ) 228 throws Exception  229 { 230 final ServiceManager serviceManager = createServiceManager( entry ); 231 return new WrapperComponentManager( serviceManager ); 232 } 233 234 243 public ServiceManager createServiceManager( final Object entry ) 244 throws Exception  245 { 246 final Map serviceMap = createServiceMap( entry ); 247 final DefaultServiceManager manager = new DefaultServiceManager(); 248 249 final Iterator iterator = serviceMap.keySet().iterator(); 250 while( iterator.hasNext() ) 251 { 252 final String key = (String )iterator.next(); 253 final Object value = serviceMap.get( key ); 254 manager.put( key, value ); 255 } 256 257 return manager; 258 } 259 260 private Map createServiceMap( final Object entry ) 261 throws Exception  262 { 263 final BlockEntry blockEntry = getBlockEntry( entry ); 264 final HashMap map = new HashMap (); 265 final HashMap sets = new HashMap (); 266 267 final ComponentInfo info = blockEntry.getInfo(); 268 final DependencyDirective[] roles = blockEntry.getTemplate() 269 .getDependencies(); 270 271 for( int i = 0; i < roles.length; i++ ) 272 { 273 final DependencyDirective role = roles[ i ]; 274 final Object dependency = m_application.getBlock( 275 role.getProviderName() ); 276 277 final String key = role.getKey(); 278 final DependencyDescriptor candidate = 279 info.getDependency( key ); 280 281 if( candidate.isArray() ) 282 { 283 ArrayList list = (ArrayList )sets.get( key ); 284 if( null == list ) 285 { 286 list = new ArrayList (); 287 sets.put( key, list ); 288 } 289 290 list.add( dependency ); 291 } 292 else if( candidate.isMap() ) 293 { 294 HashMap smap = (HashMap )sets.get( key ); 295 if( null == smap ) 296 { 297 smap = new HashMap (); 298 sets.put( key, smap ); 299 } 300 301 smap.put( role.getAlias(), dependency ); 302 } 303 else 304 { 305 map.put( key, dependency ); 306 } 307 } 308 309 final Iterator iterator = sets.keySet().iterator(); 310 while( iterator.hasNext() ) 311 { 312 final String key = (String )iterator.next(); 313 final Object value = sets.get( key ); 314 if( value instanceof List ) 315 { 316 final List list = (List )value; 317 final DependencyDescriptor dependency = info.getDependency( 318 key ); 319 320 final Object [] result = toArray( list, 321 dependency.getComponentType() ); 322 map.put( key, result ); 323 324 if( key.equals( dependency.getType() ) ) 325 { 326 final String classname = 327 "[L" + dependency.getComponentType() + ";"; 328 map.put( classname, result ); 329 } 330 } 331 else 332 { 333 final Map smap = 334 Collections.unmodifiableMap( (Map )value ); 335 map.put( key, smap ); 336 } 337 } 338 339 return map; 340 } 341 342 352 private Object [] toArray( final List list, final String type ) 353 throws ClassNotFoundException  354 { 355 final ClassLoader classLoader = 356 list.get( 0 ).getClass().getClassLoader(); 357 final Class clazz = classLoader.loadClass( type ); 358 final Object [] elements = 359 (Object [])Array.newInstance( clazz, list.size() ); 360 return list.toArray( elements ); 361 } 362 363 public Configuration createConfiguration( final Object entry ) 364 throws Exception  365 { 366 final ComponentTemplate template = getBlockEntry( entry ).getTemplate(); 367 return ConfigurationAlchemist.toAvalonConfiguration( 368 template.getConfiguration() ); 369 } 370 371 public Parameters createParameters( final Object entry ) 372 throws Exception  373 { 374 final Configuration configuration = 375 createConfiguration( entry ); 376 final Parameters parameters = 377 Parameters.fromConfiguration( configuration ); 378 parameters.makeReadOnly(); 379 return parameters; 380 } 381 382 private BlockEntry getBlockEntry( final Object entry ) 383 { 384 return (BlockEntry)entry; 385 } 386 } 387 | Popular Tags |