1 50 51 package org.apache.avalon.meta.info.builder; 52 53 import java.io.InputStream ; 54 import java.util.ArrayList ; 55 import java.util.Properties ; 56 import org.apache.avalon.excalibur.i18n.ResourceManager; 57 import org.apache.avalon.excalibur.i18n.Resources; 58 import org.apache.avalon.framework.Version; 59 import org.apache.avalon.framework.configuration.Configuration; 60 import org.apache.avalon.framework.configuration.ConfigurationException; 61 import org.apache.avalon.meta.ConfigurationBuilder; 62 import org.apache.avalon.meta.info.InfoDescriptor; 63 import org.apache.avalon.meta.info.ContextDescriptor; 64 import org.apache.avalon.meta.info.EntryDescriptor; 65 import org.apache.avalon.meta.info.DependencyDescriptor; 66 import org.apache.avalon.meta.info.ExtensionDescriptor; 67 import org.apache.avalon.meta.info.CategoryDescriptor; 68 import org.apache.avalon.meta.info.ReferenceDescriptor; 69 import org.apache.avalon.meta.info.ServiceDescriptor; 70 import org.apache.avalon.meta.info.StageDescriptor; 71 import org.apache.avalon.meta.info.Type; 72 import org.apache.excalibur.configuration.ConfigurationUtil; 73 import org.xml.sax.InputSource ; 74 75 82 public class XMLLegacyCreator 83 extends XMLTypeCreator 84 implements TypeCreator 85 { 86 private static final Resources REZ = 87 ResourceManager.getPackageResources( XMLLegacyCreator.class ); 88 89 98 public Type createType( String implementationKey, 99 InputStream inputStream ) 100 throws Exception 101 { 102 if( inputStream == null ) 103 { 104 throw new NullPointerException ( "input" ); 105 } 106 final InputSource input = new InputSource ( inputStream ); 107 final String classname = implementationKey; 108 final Configuration xinfo = ConfigurationBuilder.build( input ); 109 return build( classname, xinfo ); 110 } 111 112 121 public Type createType( String classname, Configuration config ) 122 throws BuildException 123 { 124 return build( classname, config ); 125 } 126 127 136 private Type build( final String classname, final Configuration info ) 137 throws BuildException 138 { 139 final String topLevelName = info.getName(); 140 if( !topLevelName.equals( "blockinfo" ) ) 141 { 142 final String message = 143 REZ.getString( "builder.bad-toplevel-block-element.error", 144 classname, 145 topLevelName ); 146 throw new BuildException( message ); 147 } 148 149 Configuration configuration = null; 150 151 configuration = info.getChild( "block" ); 152 153 final InfoDescriptor descriptor = 154 buildInfoDescriptor( classname, configuration ); 155 156 configuration = info.getChild( "loggers" ); 157 final CategoryDescriptor[] loggers = new CategoryDescriptor[0]; 158 final ContextDescriptor context = 159 buildPhoenixContext(); 160 161 configuration = info.getChild( "services" ); 162 final ServiceDescriptor[] services = 163 buildBlockServices( configuration ); 164 165 configuration = info.getChild( "dependencies" ); 166 final DependencyDescriptor[] dependencies = 167 buildBlockDependencies( configuration ); 168 169 configuration = info.getChild( "stages" ); 170 final StageDescriptor[] phases = 171 buildStages( configuration ); 172 173 configuration = info.getChild( "extensions" ); 174 final ExtensionDescriptor[] extensions = 175 buildExtensions( configuration ); 176 177 return new Type( 178 descriptor, loggers, context, services, dependencies, 179 phases, extensions ); 180 } 181 182 191 public InfoDescriptor buildInfoDescriptor( 192 final String classname, final Configuration info ) 193 throws BuildException 194 { 195 final String name = 196 info.getChild( "name" ).getValue( null ); 197 final String schema = 198 info.getChild( "schema-type" ).getValue( null ); 199 final Version version = 200 buildVersion( info.getChild( "version" ).getValue( "1.0" ) ); 201 final Properties attributes = 202 buildAttributes( info.getChild( "attributes" ) ); 203 final String lifestyle = InfoDescriptor.SINGLETON; 204 return new InfoDescriptor( 205 name, classname, version, lifestyle, schema, attributes ); 206 } 207 208 private ContextDescriptor buildPhoenixContext() 209 { 210 EntryDescriptor name = 211 new EntryDescriptor( 212 ContextDescriptor.NAME_KEY, 213 "java.lang.String", false, false, "block.name" ); 214 EntryDescriptor partition = 215 new EntryDescriptor( 216 ContextDescriptor.PARTITION_KEY, 217 "java.lang.String", false, false, "app.name" ); 218 EntryDescriptor home = 219 new EntryDescriptor( 220 ContextDescriptor.HOME_KEY, 221 "java.io.File", false, false, "app.home" ); 222 223 return new ContextDescriptor( new EntryDescriptor[]{ name, partition, home } ); 224 } 225 226 234 protected DependencyDescriptor[] buildBlockDependencies( final Configuration depSet ) 235 throws BuildException 236 { 237 final Configuration[] deps = depSet.getChildren( "dependency" ); 238 final ArrayList dependencies = new ArrayList (); 239 for( int i = 0; i < deps.length; i++ ) 240 { 241 final DependencyDescriptor dependency = 242 buildBlockDependency( deps[ i ] ); 243 dependencies.add( dependency ); 244 } 245 return (DependencyDescriptor[])dependencies.toArray( 246 new DependencyDescriptor[ 0 ] ); 247 } 248 249 250 258 protected ServiceDescriptor[] buildBlockServices( final Configuration servicesSet ) 259 throws BuildException 260 { 261 final Configuration[] elements = servicesSet.getChildren( "service" ); 262 final ArrayList services = new ArrayList (); 263 264 for( int i = 0; i < elements.length; i++ ) 265 { 266 final ServiceDescriptor service = buildBlockService( elements[ i ] ); 267 services.add( service ); 268 } 269 270 return (ServiceDescriptor[])services.toArray( new ServiceDescriptor[ 0 ] ); 271 } 272 273 281 protected ServiceDescriptor buildBlockService( final Configuration service ) 282 throws BuildException 283 { 284 final ReferenceDescriptor designator = buildReferenceDescriptor( service ); 285 final Properties attributes = 286 buildAttributes( service.getChild( "attributes" ) ); 287 return new ServiceDescriptor( designator, attributes ); 288 } 289 290 298 protected ReferenceDescriptor buildReferenceDescriptor( final Configuration service ) 299 throws BuildException 300 { 301 try 302 { 303 final String type = service.getAttribute( "name" ); 304 final String versionString = service.getAttribute( "version", "1.0" ); 305 final Version version = buildVersion( versionString ); 306 return new ReferenceDescriptor( type, version ); 307 } 308 catch( Throwable e ) 309 { 310 final String error = 311 "Error occured while attempting to build reference descriptor from legacy blockinfo configuration: " 312 + ConfigurationUtil.list( service ); 313 throw new BuildException( error, e ); 314 } 315 } 316 317 325 protected DependencyDescriptor buildBlockDependency( final Configuration dependency ) 326 throws BuildException 327 { 328 try 329 { 330 Configuration serviceRef = dependency.getChild( "service" ); 331 final ReferenceDescriptor service = 332 buildReferenceDescriptor( serviceRef ); 333 final boolean optional = 334 dependency.getAttributeAsBoolean( "optional", false ); 335 final Properties attributes = 336 buildAttributes( dependency.getChild( "attributes" ) ); 337 String role = dependency.getChild( "role" ).getValue( null ); 338 339 if( null == role ) role = service.getClassname(); 341 return new DependencyDescriptor( role, service, optional, attributes ); 342 } 343 catch( Throwable e ) 344 { 345 final String error = 346 "Error occured while attempting to build dependency descriptor from legacy blockinfo descriptor: " 347 + ConfigurationUtil.list( dependency ); 348 throw new BuildException( error, e ); 349 } 350 } 351 352 } 353 | Popular Tags |