1 8 package org.codehaus.loom.components.util.infobuilder; 9 10 import java.lang.reflect.Method ; 11 import java.util.ArrayList ; 12 import java.util.List ; 13 14 import org.codehaus.loom.components.util.info.ComponentInfo; 15 import org.codehaus.loom.components.util.info.DependencyDescriptor; 16 import org.codehaus.loom.components.util.info.SchemaDescriptor; 17 import org.codehaus.loom.components.util.info.ServiceDescriptor; 18 import org.codehaus.metaclass.Attributes; 19 import org.codehaus.metaclass.model.Attribute; 20 21 28 public final class MetaClassBlockInfoReader implements BlockInfoReader 29 { 30 public ComponentInfo buildComponentInfo( final Class type ) 31 throws Exception 32 { 33 final Attribute attribute = 34 Attributes.getAttribute( type, "dna.component" ); 35 if( null == attribute ) 36 { 37 final String message = 38 "Type " + 39 type.getName() + 40 " does not specify the " + 41 "required MetaClass attributes to be a Component"; 42 throw new Exception ( message ); 43 } 44 final ServiceDescriptor[] services = buildServices( type ); 45 46 final DependencyDescriptor[] dependencies = 47 buildDependencies( type ); 48 49 final SchemaDescriptor schema = buildConfigurationSchema( type ); 50 51 return new ComponentInfo( type, 52 services, 53 dependencies, 54 schema ); 55 } 56 57 64 private SchemaDescriptor buildConfigurationSchema( final Class type ) 65 { 66 final Class [] types = 67 new Class []{org.apache.avalon.framework.configuration.Configuration.class}; 68 try 69 { 70 final Method method = type.getMethod( "configure", types ); 71 final Attribute attribute = 72 Attributes.getAttribute( method, "dna.configuration" ); 73 if( null == attribute ) 74 { 75 return null; 76 } 77 final String location = attribute.getParameter( "location" ); 78 final String schemaType = attribute.getParameter( "type" ); 79 return new SchemaDescriptor( location, schemaType ); 80 } 81 catch( NoSuchMethodException e ) 82 { 83 return null; 84 } 85 } 86 87 96 private DependencyDescriptor[] buildDependencies( final Class type ) 97 { 98 final Method method = getDependencyMethod( type ); 99 if( null == method ) 100 { 101 return DependencyDescriptor.EMPTY_SET; 102 } 103 final List deps = new ArrayList (); 104 105 final Attribute[] attributes = 106 Attributes.getAttributes( method, "dna.dependency" ); 107 for( int i = 0; i < attributes.length; i++ ) 108 { 109 final Attribute attribute = attributes[i]; 110 final String key = attribute.getParameter( "key" ); 111 final String depType = attribute.getParameter( "type" ); 112 final boolean optional = 113 attribute.getParameter( "optional", "" ).equals( "true" ); 114 deps.add( new DependencyDescriptor( key, depType, optional ) ); 115 } 116 117 return (DependencyDescriptor[])deps.toArray( DependencyDescriptor.EMPTY_SET ); 118 } 119 120 127 private Method getDependencyMethod( final Class type ) 128 { 129 try 130 { 131 final Class [] types1 = 132 new Class []{org.apache.avalon.framework.component.ComponentManager.class}; 133 return type.getMethod( "compose", types1 ); 134 } 135 catch( final NoSuchMethodException nsme ) 136 { 137 } 138 try 139 { 140 final Class [] types2 = 141 new Class []{org.apache.avalon.framework.service.ServiceManager.class}; 142 return type.getMethod( "service", types2 ); 143 } 144 catch( NoSuchMethodException e ) 145 { 146 } 147 return null; 148 } 149 150 159 private ServiceDescriptor[] buildServices( final Class type ) 160 { 161 final List services = new ArrayList (); 162 163 final Attribute[] attributes = 164 Attributes.getAttributes( type, "dna.service" ); 165 for( int i = 0; i < attributes.length; i++ ) 166 { 167 final Attribute attribute = attributes[i]; 168 final String serviceType = attribute.getParameter( "type" ); 169 services.add( new ServiceDescriptor( serviceType ) ); 170 } 171 172 return (ServiceDescriptor[])services.toArray( ServiceDescriptor.EMPTY_SET ); 173 } 174 } 175 | Popular Tags |