1 8 package org.codehaus.loom.components.util.infobuilder; 9 10 import java.io.InputStream ; 11 import java.util.ArrayList ; 12 13 import org.codehaus.loom.components.util.ConfigurationBuilder; 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.spice.salt.i18n.ResourceManager; 19 import org.codehaus.spice.salt.i18n.Resources; 20 import org.codehaus.dna.AbstractLogEnabled; 21 import org.codehaus.dna.Configuration; 22 import org.codehaus.dna.ConfigurationException; 23 import org.codehaus.dna.Logger; 24 import org.xml.sax.InputSource ; 25 26 36 public final class LegacyBlockInfoReader 37 extends AbstractLogEnabled 38 implements BlockInfoReader 39 { 40 43 private static final Resources REZ = ResourceManager.getPackageResources( 44 LegacyBlockInfoReader.class ); 45 private static final String BLOCKINFO_SCHEMA = "-//PHOENIX/Block Info DTD Version 1.0//EN"; 46 private final ClassLoader m_classLoader; 47 48 public LegacyBlockInfoReader( final ClassLoader classLoader, 49 final Logger logger ) 50 { 51 if( null == classLoader ) 52 { 53 throw new NullPointerException ( "classLoader" ); 54 } 55 else if( null == logger ) 56 { 57 throw new NullPointerException ( "logger" ); 58 } 59 60 enableLogging( logger ); 61 62 m_classLoader = classLoader; 63 } 64 65 public ComponentInfo buildComponentInfo( final Class type ) 66 throws Exception 67 { 68 final String xinfo = type.getName().replace( '.', '/' ) + ".xinfo"; 69 final InputStream inputStream = m_classLoader.getResourceAsStream( 70 xinfo ); 71 72 if( null == inputStream ) 73 { 74 return null; 75 } 76 else 77 { 78 final InputSource input = new InputSource ( inputStream ); 79 final Configuration configuration = ConfigurationBuilder.build( 80 input, BLOCKINFO_SCHEMA, getLogger() ); 81 82 return build( type, configuration ); 83 } 84 } 85 86 95 private ComponentInfo build( final Class type, final Configuration info ) 96 throws Exception 97 { 98 if( getLogger().isDebugEnabled() ) 99 { 100 final String message = REZ.format( 101 "legacy.builder.creating-info.notice", type.getName() ); 102 getLogger().debug( message ); 103 } 104 105 final String topLevelName = info.getName(); 106 if( !topLevelName.equals( "blockinfo" ) ) 107 { 108 final String message = REZ.format( 109 "legacy.bad-toplevel-element.error", 110 type.getName(), 111 topLevelName ); 112 throw new ConfigurationException( message, 113 info.getPath(), 114 info.getLocation() ); 115 } 116 117 Configuration configuration; 118 119 final ServiceDescriptor[] services = buildServices( info ); 120 121 configuration = info.getChild( "dependencies" ); 122 final DependencyDescriptor[] dependencies = buildDependencies( 123 type.getName(), configuration ); 124 125 if( getLogger().isDebugEnabled() ) 126 { 127 final String message = REZ.format( "legacy.created-info.notice", 128 type.getName(), 129 new Integer ( services.length ), 130 new Integer ( 131 dependencies.length ) ); 132 getLogger().debug( message ); 133 } 134 135 configuration = info.getChild( "block" ); 136 final SchemaDescriptor schema = buildConfigurationSchema( 137 type.getName(), configuration ); 138 139 return new ComponentInfo( type, services, dependencies, schema ); 140 } 141 142 151 private SchemaDescriptor buildConfigurationSchema( final String classname, 152 final Configuration configuration ) 153 { 154 String schemaType = configuration.getChild( "schema-type" ).getValue( "" ); 155 if( "".equals( schemaType ) ) 156 { 157 return null; 158 } 159 if( "relax-ng".equals( schemaType ) ) 160 { 161 schemaType = "http://relaxng.org/ns/structure/1.0"; 162 } 163 final String location = LegacyUtil.getSchemaLocationFor( classname ); 164 return new SchemaDescriptor( location, schemaType ); 165 } 166 167 177 private DependencyDescriptor[] buildDependencies( final String classname, 178 final Configuration configuration ) 179 throws ConfigurationException 180 { 181 final Configuration[] elements = configuration.getChildren( 182 "dependency" ); 183 final ArrayList dependencies = new ArrayList (); 184 185 for( int i = 0; i < elements.length; i++ ) 186 { 187 final DependencyDescriptor dependency = buildDependency( classname, 188 elements[ i ] ); 189 dependencies.add( dependency ); 190 } 191 192 return (DependencyDescriptor[]) dependencies.toArray( 193 DependencyDescriptor.EMPTY_SET ); 194 } 195 196 205 private DependencyDescriptor buildDependency( final String classname, 206 final Configuration dependency ) 207 throws ConfigurationException 208 { 209 final String implementationKey = dependency.getChild( "service" ).getAttribute( 210 "name" ); 211 String key = dependency.getChild( "role" ).getValue( null ); 212 213 if( null == key ) 215 { 216 key = implementationKey; 217 } 218 else 219 { 220 if( key.equals( implementationKey ) ) 223 { 224 final String message = REZ.format( 225 "legacy.builder.redundent-key.notice", classname, key ); 226 getLogger().warn( message ); 227 } 228 } 229 230 return new DependencyDescriptor( key, implementationKey, false ); 231 } 232 233 241 private ServiceDescriptor[] buildServices( final Configuration info ) 242 throws ConfigurationException 243 { 244 final ArrayList services = new ArrayList (); 245 246 Configuration[] elements = info.getChild( "services" ).getChildren( 247 "service" ); 248 for( int i = 0; i < elements.length; i++ ) 249 { 250 final ServiceDescriptor service = buildService( elements[ i ], 251 false ); 252 services.add( service ); 253 } 254 elements = 255 info.getChild( "management-access-points" ).getChildren( "service" ); 256 for( int i = 0; i < elements.length; i++ ) 257 { 258 final ServiceDescriptor service = buildService( elements[ i ], 259 true ); 260 services.add( service ); 261 } 262 263 return (ServiceDescriptor[]) services.toArray( 264 ServiceDescriptor.EMPTY_SET ); 265 } 266 267 275 private ServiceDescriptor buildService( final Configuration service, 276 final boolean isManagement ) 277 throws ConfigurationException 278 { 279 return new ServiceDescriptor( service.getAttribute( "name" ) ); 282 } 283 } 284 | Popular Tags |