KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ClassAvailableTask


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

8
9 import java.io.*;
10 import java.util.*;
11 import java.util.zip.*;
12 import org.apache.tools.ant.*;
13 import org.apache.tools.ant.taskdefs.*;
14 import org.apache.tools.ant.types.*;
15
16 /**
17  * Will set the given property if the requested class is available in the
18  * specified classpath. The found class is not loaded!
19  * This class is heavily based on the available task in the ant package:
20  * @author Stefano Mazzocchi <a HREF="mailto:stefano@apache.org">stefano@apache.org</a>
21  *
22  * This task searches only in the defined path but not in the parents path
23  * unless explicitly overridden by the value of ${build.sysclasspath}
24  * like the original available task does.
25  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
26  * @version CVS $Revision: 1.1 $ $Date: 2002/03/08 14:31:27 $
27  */

28
29 public class ClassAvailableTask extends Task {
30
31     /**
32      * A hashtable of zip files opened by the classloader
33      */

34     private Hashtable zipFiles = new Hashtable();
35
36     private String JavaDoc property;
37     private String JavaDoc classname;
38     private Path classpath;
39     private String JavaDoc value = "true";
40
41     public void setClasspath(Path classpath) {
42         createClasspath().append(classpath);
43     }
44
45     public Path createClasspath() {
46         if (this.classpath == null) {
47             this.classpath = new Path(this.project);
48         }
49         return this.classpath.createPath();
50     }
51
52     public void setClasspathRef(Reference r) {
53         createClasspath().setRefid(r);
54     }
55
56     public void setProperty(String JavaDoc property) {
57         this.property = property;
58     }
59
60     public void setValue(String JavaDoc value) {
61         this.value = value;
62     }
63
64     public void setClassname(String JavaDoc classname) {
65         if (!"".equals(classname)) {
66             this.classname = classname;
67         }
68     }
69
70     public void execute() throws BuildException {
71         if (property == null) {
72             throw new BuildException("property attribute is required", location);
73         }
74
75         if (eval()) {
76             this.project.setProperty(property, value);
77         }
78     }
79
80     public boolean eval() throws BuildException {
81         if (classname == null) {
82             throw new BuildException("At least one of (classname|file|resource) is required", location);
83         }
84
85         if (classpath != null) {
86             classpath.setProject(project);
87             classpath = classpath.concatSystemClasspath("ignore");
88         }
89
90         if (!findClassInComponents(classname)) {
91             log("Unable to load class " + classname + " to set property " + property, Project.MSG_VERBOSE);
92             return false;
93         }
94
95         return true;
96     }
97
98     /**
99      * Get an inputstream to a given resource in the given file which may
100      * either be a directory or a zip file.
101      *
102      * @param file the file (directory or jar) in which to search for the resource.
103      * @param resourceName the name of the resource for which a stream is required.
104      *
105      * @return a stream to the required resource or null if the resource cannot be
106      * found in the given file object
107      */

108     private boolean contains(File file, String JavaDoc resourceName) {
109         try {
110             if (!file.exists()) {
111                 return false;
112             }
113
114             if (file.isDirectory()) {
115                 File resource = new File(file, resourceName);
116
117                 if (resource.exists()) {
118                     return true;
119                 }
120             }
121             else {
122                 // is the zip file in the cache
123
ZipFile zipFile = (ZipFile)zipFiles.get(file);
124                 if (zipFile == null) {
125                     zipFile = new ZipFile(file);
126                     zipFiles.put(file, zipFile);
127                 }
128                 ZipEntry entry = zipFile.getEntry(resourceName);
129                 if (entry != null) {
130                     return true;
131                 }
132             }
133         }
134         catch (Exception JavaDoc e) {
135             log("Ignoring Exception " + e.getClass().getName() + ": " + e.getMessage() +
136                 " reading resource " + resourceName + " from " + file, Project.MSG_VERBOSE);
137         }
138
139         return false;
140     }
141
142     /**
143      * Find a class on the given classpath.
144      */

145     private boolean findClassInComponents(String JavaDoc name) {
146         // we need to search the components of the path to see if we can find the
147
// class we want.
148
final String JavaDoc classname = name.replace('.', '/') + ".class";
149         final String JavaDoc[] list = classpath.list();
150         boolean found = false;
151         int i = 0;
152         while (i < list.length && found == false) {
153             final File pathComponent = (File)project.resolveFile(list[i]);
154             found = this.contains(pathComponent, classname);
155             i++;
156         }
157         return found;
158     }
159 }
160
Popular Tags