KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > common > SimpleClassLoader


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.common;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.zip.ZipEntry JavaDoc;
28 import java.util.zip.ZipFile JavaDoc;
29
30 import sync4j.syncclient.common.logging.Logger;
31
32 /**
33  * Simple classLoader for the loading of java class.
34  * Searches the classes in a given directory, in its subdirectory and
35  * in all jar files contained in the given directory and, recursively,
36  * in its subdirectory.
37  *
38  * @version $Id: SimpleClassLoader.java,v 1.4 2005/01/19 11:18:36 fabius Exp $
39  * @author Stefano Nichele
40  * @author Fabio Maggi
41  */

42
43 public class SimpleClassLoader extends ClassLoader JavaDoc {
44
45     // -------------------------------------------------------------Private data
46

47     /** This is the directory in which searching the java class */
48     private String JavaDoc workingDirectory = null;
49
50     private Logger logger = new Logger();
51
52     // -------------------------------------------------------------Constructors
53

54     /**
55      * Creates a new SimpleClassLoader with the given
56      * <code>workingDirectory</code>.
57      * @param workingDirectory directory in which the class
58      * loader searches the java class.
59      */

60     public SimpleClassLoader(String JavaDoc workingDirectory) {
61         File JavaDoc file = new File JavaDoc(workingDirectory);
62         this.workingDirectory = workingDirectory;
63
64         if (logger.isLoggable(Logger.INFO)) {
65                 logger.info("WorkingDirectory classloader: " +
66                              file.getAbsolutePath() );
67
68         }
69
70     }
71
72     // -----------------------------------------------------------Public methods
73

74
75     public String JavaDoc getWorkingDirectory() {
76         return this.workingDirectory;
77     }
78
79     //---------------------------------------
80
//Overrides java.lang.ClassLoader method
81
//---------------------------------------
82

83     public Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc {
84
85        byte[] b = null;
86
87        Class JavaDoc classLoaded = null;
88
89        try {
90            b = loadClassData(name);
91        } catch (Exception JavaDoc ex) {
92            throw new ClassNotFoundException JavaDoc("Class " + name + " not found: " + ex);
93        }
94
95        if (b==null) {
96            classLoaded = findSystemClass(name);
97        } else {
98            classLoaded = defineClass(name, b, 0, b.length);
99        }
100
101         return classLoaded;
102     }
103
104     // ----------------------------------------------------------Private methods
105

106     /**
107      * Returns the byte array of the java class searched.
108      * Searches for the class in <i>workingDirectory</i>
109      *
110      * @param name the name of the class.
111      * @return the byte array of the class. <code>null</code> if
112      * the class could not be found.
113      * @throw Exception if the <i>name</i> does not represent a
114      * valid java class name.
115      */

116     private byte[] loadClassData(String JavaDoc name) throws Exception JavaDoc {
117         byte[] content = null;
118
119         int index = name.lastIndexOf(".");
120
121         if (index == -1) {
122             throw new Exception JavaDoc("Java class name not recognized");
123         }
124
125         String JavaDoc sPackage = name.substring(0, index); // com.funambol.sl
126
name = name.substring(index+1, name.length()); // Setup
127

128         // Searches the class in the directory
129
content = loadInDirectory(workingDirectory, sPackage, name);
130
131         return content;
132     }
133
134     /**
135      * Searches the java class in the given directory
136      * @param dirName the directory name in which searching the java class
137      * @param packageClass package of the class to search
138      * @param javaClassName name of the class to search
139      * @return the byte array of the class. <code>null</code> if
140      * the class could not be found.
141      *
142      * @throws IOException
143      */

144     private byte[] loadInDirectory(String JavaDoc dirName,
145                                    String JavaDoc packageName, String JavaDoc javaClassName)
146     throws IOException JavaDoc {
147         byte[] content = null;
148
149         // decomposes the package in directory
150
StringTokenizer JavaDoc stPackage = new StringTokenizer JavaDoc(packageName, ".");
151
152         while (stPackage.hasMoreTokens()) {
153             dirName = dirName + File.separator + stPackage.nextToken();
154         }
155
156         // verify if the .class file exists
157
File JavaDoc fileClass = new File JavaDoc(dirName + File.separator
158                                   + javaClassName + ".class");
159         if (fileClass.isFile()) {
160
161             if (logger.isLoggable(Logger.INFO)) {
162                 logger.info("Class found in: " + dirName);
163             }
164
165             int size = (int)fileClass.length();
166             content = new byte[size];
167
168             FileInputStream JavaDoc fIstream = new FileInputStream JavaDoc(fileClass);
169             fIstream.read(content);
170             fIstream.close();
171         }
172
173         return content;
174     }
175
176 }
Popular Tags