KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > validation > AttributeValidatorTask


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.validation;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.InputStream;
23 import java.io.PrintWriter;
24 import java.io.PrintStream;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.jar.JarFile;
35 import java.util.jar.JarEntry;
36 import java.util.jar.JarOutputStream;
37
38 import org.apache.commons.attributes.AttributeRepositoryClass;
39 import org.apache.commons.attributes.Attributes;
40 import org.apache.commons.attributes.AttributeUtil;
41 import org.apache.commons.attributes.Indexed;
42 import org.apache.tools.ant.AntClassLoader;
43 import org.apache.tools.ant.BuildException;
44 import org.apache.tools.ant.Task;
45 import org.apache.tools.ant.types.FileSet;
46 import org.apache.tools.ant.types.Path;
47
48 /**
49  * Ant task that validates attributes. Usage:
50  *
51  * <pre><code>
52  * &lt;taskdef resource="org/apache/commons/attributes/anttasks.properties"/&gt;
53  *
54  * &lt;attribute-validator jarFile="myclasses.jar"&gt;
55  * &lt;classpath&gt;
56  * ...
57  * &lt;/classpath&gt;
58  * &lt;validator class="my.Validator"/&gt;
59  * &lt;validator class="my.other.Validator"/&gt;
60  * &lt;/attribute-validator&gt;
61  * </code></pre>
62  *
63  * The task will run the validator(s) with the classes the given jar file.
64  */

65 public class AttributeValidatorTask extends Task {
66     
67     private File jarFile;
68     private List classes = new ArrayList ();
69     private List validators = new ArrayList ();
70     private Path classPath;
71     private File baseName;
72     private boolean inMaven = false;
73     
74     public static class Validator {
75         
76         private String className;
77         
78         public void setClass (String className) {
79             this.className = className;
80         }
81         
82         public String getClassName () {
83             return className;
84         }
85     }
86     
87     public void setJarfile (File jarFile) {
88         this.jarFile = jarFile;
89     }
90     
91     public void setBaseName (File baseName) {
92         inMaven = true;
93         this.baseName = baseName;
94     }
95     
96     public Path createClasspath () {
97         this.classPath = new Path(project);
98         return classPath;
99     }
100     
101     public Validator createValidator () {
102         Validator validator = new Validator ();
103         validators.add (validator);
104         return validator;
105     }
106     
107     private static final String SUFFIX = "$__attributeRepository.class";
108     
109     protected void findJarFile () throws BuildException {
110         File[] allFiles = baseName.getParentFile ().listFiles ();
111         if (allFiles == null) {
112             throw new BuildException ("Unable to find any file with base name " + baseName.getName ()
113                 + " in " + baseName.getParentFile ().getPath ());
114         }
115         
116         long newestDate = 0;
117         for (int i = 0; i < allFiles.length; i++) {
118             String name = allFiles[i].getName ();
119             if (name.startsWith (baseName.getName ()) && name.endsWith (".jar") &&
120                 allFiles[i].lastModified () > newestDate) {
121                 jarFile = allFiles[i];
122                 newestDate = allFiles[i].lastModified ();
123             }
124         }
125         
126         if (jarFile == null) {
127             throw new BuildException ("Unable to find any file with base name " + baseName.getName ()
128                 + " in " + baseName.getParentFile ().getPath ());
129         }
130     }
131     
132     public void execute () throws BuildException {
133         if (inMaven) {
134             findJarFile ();
135         }
136         if (!jarFile.exists ()) {
137             log ("Can't find " + jarFile.getPath ());
138             return;
139         }
140         
141         try {
142             log ("Validating attributes in " + jarFile.getPath ());
143             
144             JarFile jar = new JarFile (jarFile);
145             try {
146                 Enumeration enum = jar.entries ();
147                 while (enum.hasMoreElements ()) {
148                     JarEntry entry = (JarEntry) enum.nextElement ();
149                     if (!entry.isDirectory ()) {
150                         String className = entry.getName ();
151                         if (className.endsWith (SUFFIX)) {
152                             className = className.replace ('/', '.').replace ('\\', '.').substring (0, className.length () - SUFFIX.length ());
153                             classes.add (className);
154                         }
155                     }
156                 }
157             } finally {
158                 jar.close ();
159             }
160             
161             AntClassLoader cl = new AntClassLoader (this.getClass ().getClassLoader (), project, classPath, true);
162             try {
163                 cl.addPathElement (jarFile.getPath ());
164                 
165                 HashSet classesToValidate = new HashSet ();
166                 Iterator attrs = classes.iterator ();
167                 while (attrs.hasNext ()) {
168                     String className = (String) attrs.next ();
169                     
170                     Class clazz = cl.loadClass (className);
171                     classesToValidate.add (clazz);
172                 }
173                 
174                 Iterator iter = validators.iterator ();
175                 while (iter.hasNext ()) {
176                     Validator validator = (Validator) iter.next ();
177                     Class validatorClass = cl.loadClass (validator.getClassName ());
178                     AttributeValidator attrValidator = (AttributeValidator) validatorClass.newInstance ();
179                     try {
180                         attrValidator.validate (classesToValidate);
181                     } catch (ValidationException ve) {
182                         throw new BuildException (ve.getInvalidClass () + " failed to validate: " + ve.getMessage ());
183                     }
184                 }
185             } finally {
186                 cl.cleanup ();
187             }
188         } catch (BuildException be) {
189             throw be;
190         } catch (Exception e) {
191             e.printStackTrace ();
192             throw new BuildException (e.toString ());
193         }
194     }
195 }
Popular Tags