KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > dna > tools > verifier > VerifyComponentsTask


1 /*
2  * Copyright (C) The Spice Group. All rights reserved.
3  *
4  * This software is published under the terms of the Spice
5  * Software License version 1.1, a copy of which has been included
6  * with this distribution in the LICENSE.txt file.
7  */

8 package org.codehaus.dna.tools.verifier;
9
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
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 /**
29  * Task to validate a set of components.
30  *
31  * @author Peter Donald
32  * @version $Revision: 1.1 $ $Date: 2004/04/18 20:13:45 $
33  */

34 public class VerifyComponentsTask
35     extends Task
36 {
37     /**
38      * IO used to read descriptors.
39      */

40     private static final MetaClassIOBinary IO = new MetaClassIOBinary();
41
42     /**
43      * Extension used to designate class files.
44      */

45     private static final String JavaDoc CLASS_EXT = ".class";
46
47     /**
48      * List of filesets to process.
49      */

50     private final List JavaDoc m_filesets = new ArrayList JavaDoc();
51
52     /**
53      * The Classpath to load components from.
54      */

55     private Path m_classpath;
56
57     /**
58      * Add fileset to list of files to be processed.
59      *
60      * @param fileSet fileset to list of files to be processed.
61      */

62     public void addFileset( final FileSet fileSet )
63     {
64         m_filesets.add( fileSet );
65         getClassPath().addFileset( fileSet );
66     }
67
68     /**
69      * Add a classpath element.
70      *
71      * @return the new path
72      */

73     public Path createClasspath()
74     {
75         return getClassPath().createPath();
76     }
77
78     /**
79      * Execute VerifyComponents.
80      */

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 JavaDoc classes = getClassesWithMetaData();
90         log( "Verifying " + classes.size() + " components." );
91         final Iterator JavaDoc iterator = classes.iterator();
92         while( iterator.hasNext() )
93         {
94             final String JavaDoc name = (String JavaDoc)iterator.next();
95             log( "Verifying " + name + ".", Project.MSG_DEBUG );
96             valid &= validateComponent( classLoader, name );
97         }
98
99         if( !valid )
100         {
101             final String JavaDoc message = "Not all components validated.";
102             throw new BuildException( message );
103         }
104     }
105
106     /**
107      * Validate specified component type.
108      *
109      * @param classLoader the classloader to load class from
110      * @param classname the name of type
111      * @return true if component is valid
112      */

113     boolean validateComponent( final AntClassLoader classLoader,
114                                final String JavaDoc classname )
115     {
116         boolean valid = true;
117         try
118         {
119             final Class JavaDoc 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 JavaDoc 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 JavaDoc e )
144         {
145             final String JavaDoc 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     /**
154      * Validate specified parameters.
155      */

156     void validateParameters()
157     {
158         if( null == m_classpath )
159         {
160             final String JavaDoc message = "User did not specify classpath";
161             throw new BuildException( message );
162         }
163     }
164
165     /**
166      * Setup list of files compiler will compile.
167      */

168     private List JavaDoc getClassesWithMetaData()
169     {
170         final List JavaDoc list = new ArrayList JavaDoc();
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     /**
181      * Add all files contained in fileset to compilers file list.
182      *
183      * @param fileSet the fileset
184      */

185     private void scanFileSetForClassesWithMetaData( final FileSet fileSet,
186                                                     final List JavaDoc list )
187     {
188         final File JavaDoc dir = fileSet.getDir( getProject() );
189         final DirectoryScanner directoryScanner =
190             fileSet.getDirectoryScanner( getProject() );
191         directoryScanner.scan();
192         final String JavaDoc[] includedFiles = directoryScanner.getIncludedFiles();
193         for( int j = 0; j < includedFiles.length; j++ )
194         {
195             final String JavaDoc name = includedFiles[ j ];
196             if( name.endsWith( CLASS_EXT ) )
197             {
198                 checkClass( name, dir, list );
199             }
200         }
201     }
202
203     /**
204      * Check if class represented by specified file
205      * is a DNA component and classname to list.
206      *
207      * @param name the name of .class file
208      * @param dir the base directory
209      * @param list the list of classnames
210      */

211     void checkClass( final String JavaDoc name,
212                      final File JavaDoc dir,
213                      final List JavaDoc list )
214     {
215         final String JavaDoc basename =
216             name.substring( 0, name.length() - CLASS_EXT.length() );
217         final String JavaDoc metaName = basename + MetaClassIOBinary.EXTENSION;
218         final File JavaDoc file = new File JavaDoc( dir, metaName );
219         final ClassDescriptor descriptor = loadDescriptor( file );
220         if( null != descriptor && isDNAComponent( descriptor ) )
221         {
222             list.add( descriptor.getName() );
223         }
224     }
225
226     /**
227      * Load descriptor from file.
228      * If descriptor can not be loaded then return null
229      *
230      * @param file the file
231      * @return the descriptor or null
232      */

233     ClassDescriptor loadDescriptor( final File JavaDoc file )
234     {
235         try
236         {
237             final FileInputStream JavaDoc input = new FileInputStream JavaDoc( file );
238             return IO.deserializeClass( input );
239         }
240         catch( final Exception JavaDoc e )
241         {
242             return null;
243         }
244     }
245
246     /**
247      * Return true if specified descriptor represents DNA component.
248      *
249      * @param descriptor the descriptor
250      * @return true if descriptor represents DNA component
251      */

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     /**
261      * Utility method to get a ClassPath instance.
262      *
263      * @return a Path object
264      */

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