1 8 package org.apache.avalon.phoenix.tools.infobuilder; 9 10 import java.util.ArrayList ; 11 import org.apache.avalon.excalibur.i18n.ResourceManager; 12 import org.apache.avalon.excalibur.i18n.Resources; 13 import org.apache.avalon.framework.Version; 14 import org.apache.avalon.framework.configuration.Configuration; 15 import org.apache.avalon.framework.configuration.ConfigurationException; 16 import org.apache.avalon.framework.logger.AbstractLogEnabled; 17 import org.apache.avalon.phoenix.metainfo.BlockDescriptor; 18 import org.apache.avalon.phoenix.metainfo.BlockInfo; 19 import org.apache.avalon.phoenix.metainfo.DependencyDescriptor; 20 import org.apache.avalon.phoenix.metainfo.ServiceDescriptor; 21 22 30 public final class BlockInfoBuilder 31 extends AbstractLogEnabled 32 { 33 private static final Resources REZ = 34 ResourceManager.getPackageResources( BlockInfoBuilder.class ); 35 36 45 public BlockInfo build( final String classname, final Configuration info ) 46 throws Exception 47 { 48 if( getLogger().isDebugEnabled() ) 49 { 50 final String message = REZ.getString( "creating-blockinfo", classname ); 51 getLogger().debug( message ); 52 } 53 54 Configuration configuration = null; 55 56 configuration = info.getChild( "services" ); 57 final ServiceDescriptor[] services = buildServices( configuration ); 58 59 configuration = info.getChild( "management-access-points" ); 60 final ServiceDescriptor[] management = buildServices( configuration ); 61 62 configuration = info.getChild( "dependencies" ); 63 final DependencyDescriptor[] dependencies = buildDependencies( classname, configuration ); 64 65 configuration = info.getChild( "block" ); 66 final BlockDescriptor descriptor = buildBlockDescriptor( classname, configuration ); 67 68 if( getLogger().isDebugEnabled() ) 69 { 70 final String message = REZ.getString( "blockinfo-created", 71 classname, 72 new Integer ( services.length ), 73 new Integer ( dependencies.length ) ); 74 getLogger().debug( message ); 75 } 76 77 return new BlockInfo( descriptor, services, management, dependencies ); 78 } 79 80 89 private DependencyDescriptor[] buildDependencies( final String classname, 90 final Configuration configuration ) 91 throws ConfigurationException 92 { 93 final Configuration[] elements = configuration.getChildren( "dependency" ); 94 final ArrayList dependencies = new ArrayList (); 95 96 for( int i = 0; i < elements.length; i++ ) 97 { 98 final DependencyDescriptor dependency = 99 buildDependency( classname, elements[ i ] ); 100 dependencies.add( dependency ); 101 } 102 103 return (DependencyDescriptor[])dependencies.toArray( new DependencyDescriptor[ 0 ] ); 104 } 105 106 115 private DependencyDescriptor buildDependency( final String classname, 116 final Configuration dependency ) 117 throws ConfigurationException 118 { 119 final ServiceDescriptor service = buildService( dependency.getChild( "service" ) ); 120 String role = dependency.getChild( "role" ).getValue( null ); 121 122 if( null == role ) 124 { 125 role = service.getName(); 126 } 127 else 128 { 129 if( role.equals( service.getName() ) ) 132 { 133 final String message = REZ.getString( "redundent-role", classname, role ); 134 getLogger().warn( message ); 135 } 136 } 137 138 return new DependencyDescriptor( role, service ); 139 } 140 141 149 private ServiceDescriptor[] buildServices( final Configuration servicesSet ) 150 throws ConfigurationException 151 { 152 final Configuration[] elements = servicesSet.getChildren( "service" ); 153 final ArrayList services = new ArrayList (); 154 155 for( int i = 0; i < elements.length; i++ ) 156 { 157 final ServiceDescriptor service = buildService( elements[ i ] ); 158 services.add( service ); 159 } 160 161 return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] ); 162 } 163 164 172 private ServiceDescriptor buildService( final Configuration service ) 173 throws ConfigurationException 174 { 175 final String name = service.getAttribute( "name" ); 176 final String versionString = service.getAttribute( "version", "1.0" ); 177 final Version version = buildVersion( versionString ); 178 return new ServiceDescriptor( name, version ); 179 } 180 181 194 private BlockDescriptor buildBlockDescriptor( final String classname, 195 final Configuration block ) 196 throws ConfigurationException 197 { 198 if( 0 == block.getChildren().length ) 199 { 200 final String message = 201 REZ.getString( "missing-block", classname ); 202 getLogger().warn( message ); 203 System.err.println( message ); 204 return null; 205 } 206 207 final String name = block.getChild( "name" ).getValue( null ); 208 final Version version = buildVersion( block.getChild( "version" ).getValue() ); 209 final String schemaType = block.getChild( "schema-type" ).getValue( null ); 210 211 return new BlockDescriptor( name, classname, schemaType, version ); 212 } 213 214 220 private Version buildVersion( final String version ) 221 { 222 return Version.getVersion( version ); 223 } 224 } 225 | Popular Tags |