1 8 package org.apache.avalon.phoenix.tools.assembler; 9 10 import java.io.File ; 11 import java.net.URL ; 12 import java.util.ArrayList ; 13 import org.apache.avalon.excalibur.i18n.ResourceManager; 14 import org.apache.avalon.excalibur.i18n.Resources; 15 import org.apache.avalon.framework.configuration.Configuration; 16 import org.apache.avalon.framework.configuration.ConfigurationException; 17 import org.apache.avalon.framework.logger.AbstractLogEnabled; 18 import org.apache.avalon.framework.logger.Logger; 19 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData; 20 import org.apache.avalon.phoenix.metadata.BlockMetaData; 21 import org.apache.avalon.phoenix.metadata.DependencyMetaData; 22 import org.apache.avalon.phoenix.metadata.SarMetaData; 23 import org.apache.avalon.phoenix.metainfo.BlockInfo; 24 import org.apache.avalon.phoenix.tools.configuration.ConfigurationBuilder; 25 import org.apache.avalon.phoenix.tools.infobuilder.BlockInfoBuilder; 26 27 35 public class Assembler 36 extends AbstractLogEnabled 37 { 38 private static final Resources REZ = 39 ResourceManager.getPackageResources( Assembler.class ); 40 41 private final BlockInfoBuilder m_builder = new BlockInfoBuilder(); 42 43 49 public void enableLogging( final Logger logger ) 50 { 51 super.enableLogging( logger ); 52 setupLogger( m_builder ); 53 } 54 55 67 public SarMetaData assembleSar( final String name, 68 final Configuration assembly, 69 final File directory, 70 final ClassLoader classLoader ) 71 throws AssemblyException 72 { 73 final Configuration[] blockConfig = assembly.getChildren( "block" ); 74 final BlockMetaData[] blocks = buildBlocks( blockConfig, classLoader ); 75 76 final Configuration[] listenerConfig = assembly.getChildren( "listener" ); 77 final BlockListenerMetaData[] listeners = buildBlockListeners( listenerConfig ); 78 79 return new SarMetaData( name, directory, blocks, listeners ); 80 } 81 82 90 private BlockMetaData[] buildBlocks( final Configuration[] blocks, 91 final ClassLoader classLoader ) 92 throws AssemblyException 93 { 94 final ArrayList blockSet = new ArrayList (); 95 for( int i = 0; i < blocks.length; i++ ) 96 { 97 final BlockMetaData blockMetaData = 98 buildBlock( blocks[ i ], classLoader ); 99 blockSet.add( blockMetaData ); 100 } 101 102 return (BlockMetaData[])blockSet.toArray( new BlockMetaData[ 0 ] ); 103 } 104 105 113 private BlockMetaData buildBlock( final Configuration block, 114 final ClassLoader classLoader ) 115 throws AssemblyException 116 { 117 try 118 { 119 final String name = block.getAttribute( "name" ); 120 final String classname = block.getAttribute( "class" ); 121 final Configuration[] provides = block.getChildren( "provide" ); 122 final Configuration proxy = block.getChild( "proxy" ); 123 final boolean disableProxy = 124 proxy.getAttributeAsBoolean( "disable", false ); 125 126 final DependencyMetaData[] roles = buildDependencyMetaDatas( provides ); 127 final BlockInfo info = getBlockInfo( name, classname, classLoader ); 128 129 130 return new BlockMetaData( name, roles, disableProxy, info ); 131 } 132 catch( final ConfigurationException ce ) 133 { 134 final String message = 135 REZ.getString( "block-entry-malformed", block.getLocation(), ce.getMessage() ); 136 throw new AssemblyException( message ); 137 } 138 } 139 140 150 private BlockInfo getBlockInfo( final String name, 151 final String classname, 152 final ClassLoader classLoader ) 153 throws AssemblyException 154 { 155 final String resourceName = classname.replace( '.', '/' ) + ".xinfo"; 156 157 final String notice = REZ.getString( "loading-blockinfo", resourceName ); 158 getLogger().debug( notice ); 159 160 final URL resource = classLoader.getResource( resourceName ); 161 if( null == resource ) 162 { 163 final String message = REZ.getString( "blockinfo-missing", name, resourceName ); 164 throw new AssemblyException( message ); 165 } 166 167 try 168 { 169 final Configuration info = ConfigurationBuilder.build( resource.toString() ); 170 171 return m_builder.build( classname, info ); 172 } 173 catch( final Exception e ) 174 { 175 final String message = 176 REZ.getString( "blockinfo-nocreate", name, resourceName, e.getMessage() ); 177 throw new AssemblyException( message, e ); 178 } 179 } 180 181 189 private BlockListenerMetaData[] buildBlockListeners( final Configuration[] listeners ) 190 throws AssemblyException 191 { 192 final ArrayList listenersMetaData = new ArrayList (); 193 for( int i = 0; i < listeners.length; i++ ) 194 { 195 final BlockListenerMetaData listener = buildBlockListener( listeners[ i ] ); 196 listenersMetaData.add( listener ); 197 } 198 199 return (BlockListenerMetaData[])listenersMetaData.toArray( new BlockListenerMetaData[ 0 ] ); 200 } 201 202 210 private BlockListenerMetaData buildBlockListener( final Configuration listener ) 211 throws AssemblyException 212 { 213 try 214 { 215 final String name = listener.getAttribute( "name" ); 216 final String className = listener.getAttribute( "class" ); 217 218 return new BlockListenerMetaData( name, className ); 219 } 220 catch( final ConfigurationException ce ) 221 { 222 final String message = 223 REZ.getString( "listener-entry-malformed", 224 listener.getLocation(), 225 ce.getMessage() ); 226 throw new AssemblyException( message ); 227 } 228 } 229 230 237 private DependencyMetaData[] buildDependencyMetaDatas( final Configuration[] provides ) 238 throws ConfigurationException 239 { 240 final ArrayList dependencies = new ArrayList (); 241 for( int j = 0; j < provides.length; j++ ) 242 { 243 final Configuration provide = provides[ j ]; 244 final String requiredName = provide.getAttribute( "name" ); 245 final String role = provide.getAttribute( "role" ); 246 247 dependencies.add( new DependencyMetaData( requiredName, role ) ); 248 } 249 250 return (DependencyMetaData[])dependencies.toArray( new DependencyMetaData[ 0 ] ); 251 } 252 } 253 | Popular Tags |