1 8 package org.codehaus.dna.tools.verifier; 9 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 16 import org.apache.tools.ant.AntClassLoader; 17 import org.apache.tools.ant.BuildException; 18 import org.apache.tools.ant.DirectoryScanner; 19 import org.apache.tools.ant.Project; 20 import org.apache.tools.ant.Task; 21 import org.apache.tools.ant.types.FileSet; 22 import org.apache.tools.ant.types.Path; 23 import org.codehaus.metaclass.Attributes; 24 import org.codehaus.metaclass.io.MetaClassIOBinary; 25 import org.codehaus.metaclass.model.Attribute; 26 import org.codehaus.metaclass.model.ClassDescriptor; 27 28 34 public class VerifyComponentsTask 35 extends Task 36 { 37 40 private static final MetaClassIOBinary IO = new MetaClassIOBinary(); 41 42 45 private static final String CLASS_EXT = ".class"; 46 47 50 private final List m_filesets = new ArrayList (); 51 52 55 private Path m_classpath; 56 57 62 public void addFileset( final FileSet fileSet ) 63 { 64 m_filesets.add( fileSet ); 65 getClassPath().addFileset( fileSet ); 66 } 67 68 73 public Path createClasspath() 74 { 75 return getClassPath().createPath(); 76 } 77 78 81 public void execute() 82 { 83 validateParameters(); 84 85 final AntClassLoader classLoader = 86 new AntClassLoader( getProject(), m_classpath ); 87 boolean valid = true; 88 89 final List classes = getClassesWithMetaData(); 90 log( "Verifying " + classes.size() + " components." ); 91 final Iterator iterator = classes.iterator(); 92 while( iterator.hasNext() ) 93 { 94 final String name = (String )iterator.next(); 95 log( "Verifying " + name + ".", Project.MSG_DEBUG ); 96 valid &= validateComponent( classLoader, name ); 97 } 98 99 if( !valid ) 100 { 101 final String message = "Not all components validated."; 102 throw new BuildException( message ); 103 } 104 } 105 106 113 boolean validateComponent( final AntClassLoader classLoader, 114 final String classname ) 115 { 116 boolean valid = true; 117 try 118 { 119 final Class type = 120 classLoader.loadClass( classname ); 121 final ComponentVerifier verifier = new ComponentVerifier(); 122 final VerifyIssue[] issues = verifier.verifyType( type ); 123 for( int i = 0; i < issues.length; i++ ) 124 { 125 final VerifyIssue issue = issues[ i ]; 126 final String tail = " (" + classname + "): " + issue.getDescription(); 127 if( issue.isError() ) 128 { 129 log( "Error" + tail, Project.MSG_ERR ); 130 valid = false; 131 } 132 else if( issue.isWarning() ) 133 { 134 log( "Warning" + tail, Project.MSG_WARN ); 135 valid = false; 136 } 137 else 138 { 139 log( "Notice" + tail, Project.MSG_INFO ); 140 } 141 } 142 } 143 catch( final Exception e ) 144 { 145 final String message = 146 "Failed to validate " + classname + " due to " + e; 147 log( message, Project.MSG_ERR ); 148 valid = false; 149 } 150 return valid; 151 } 152 153 156 void validateParameters() 157 { 158 if( null == m_classpath ) 159 { 160 final String message = "User did not specify classpath"; 161 throw new BuildException( message ); 162 } 163 } 164 165 168 private List getClassesWithMetaData() 169 { 170 final List list = new ArrayList (); 171 final int count = m_filesets.size(); 172 for( int i = 0; i < count; i++ ) 173 { 174 final FileSet fileSet = (FileSet)m_filesets.get( i ); 175 scanFileSetForClassesWithMetaData( fileSet, list ); 176 } 177 return list; 178 } 179 180 185 private void scanFileSetForClassesWithMetaData( final FileSet fileSet, 186 final List list ) 187 { 188 final File dir = fileSet.getDir( getProject() ); 189 final DirectoryScanner directoryScanner = 190 fileSet.getDirectoryScanner( getProject() ); 191 directoryScanner.scan(); 192 final String [] includedFiles = directoryScanner.getIncludedFiles(); 193 for( int j = 0; j < includedFiles.length; j++ ) 194 { 195 final String name = includedFiles[ j ]; 196 if( name.endsWith( CLASS_EXT ) ) 197 { 198 checkClass( name, dir, list ); 199 } 200 } 201 } 202 203 211 void checkClass( final String name, 212 final File dir, 213 final List list ) 214 { 215 final String basename = 216 name.substring( 0, name.length() - CLASS_EXT.length() ); 217 final String metaName = basename + MetaClassIOBinary.EXTENSION; 218 final File file = new File ( dir, metaName ); 219 final ClassDescriptor descriptor = loadDescriptor( file ); 220 if( null != descriptor && isDNAComponent( descriptor ) ) 221 { 222 list.add( descriptor.getName() ); 223 } 224 } 225 226 233 ClassDescriptor loadDescriptor( final File file ) 234 { 235 try 236 { 237 final FileInputStream input = new FileInputStream ( file ); 238 return IO.deserializeClass( input ); 239 } 240 catch( final Exception e ) 241 { 242 return null; 243 } 244 } 245 246 252 boolean isDNAComponent( final ClassDescriptor descriptor ) 253 { 254 final Attribute[] attributes = descriptor.getAttributes(); 255 final Attribute attribute = 256 Attributes.getAttributeByName( attributes, "dna.component" ); 257 return null != attribute; 258 } 259 260 265 private Path getClassPath() 266 { 267 if( m_classpath == null ) 268 { 269 m_classpath = new Path( getProject() ); 270 } 271 return m_classpath; 272 } 273 } 274 | Popular Tags |