KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > optim > lib > DirsCompileClassLoader


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, S. Chassande-Barrioz, A. Lefebvre
22  */

23
24 package org.objectweb.medor.optim.lib;
25
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * A class loader that always checks a list of special directories on the hard
35  * disk to load classes or Java source files when compilation if needed.
36  * <p> The
37  * principal method loadClass(name,resolve) is coded so that name represents a
38  * Class file name or a Java source file. In the second case, the Java source
39  * file is compiled and loaded. The class loaded are used in the current
40  * application, so loading the class referenced by these classes is delegated
41  * to the class system loader if they are already loaded.
42  */

43 public class DirsCompileClassLoader extends ClassLoader JavaDoc {
44
45     private long bytecount;
46     private Hashtable JavaDoc cache = new Hashtable JavaDoc();
47     private HashSet JavaDoc pathList = new HashSet JavaDoc();
48
49     public DirsCompileClassLoader(HashSet JavaDoc classpaths) {
50         super();
51         pathList = classpaths;
52     }
53
54     // load the class data from the class file in specified dir
55
private byte[] loadClassData(String JavaDoc name) throws ClassNotFoundException JavaDoc {
56
57         byte[] data = null;
58         try {
59             String JavaDoc path = getJavaPathClass(name);
60             File JavaDoc cf = new File JavaDoc(path + File.separatorChar +
61                 name.replace('.', File.separatorChar) + ".class");
62             FileInputStream JavaDoc in = new FileInputStream JavaDoc(cf);
63             bytecount = cf.length();
64             if (bytecount > 0) {
65                 data = new byte[(int) bytecount];
66                 in.read(data);
67                 in.close();
68             }
69         } catch (IOException JavaDoc ioexp) {
70             throw new ClassNotFoundException JavaDoc(name);
71         }
72         return (data);
73     }
74
75     public synchronized Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
76         throws ClassNotFoundException JavaDoc {
77         Class JavaDoc c = (Class JavaDoc) cache.get(name);
78         System.out.println("Loading ..." + name);
79         if (c == null) {
80             // the class has not been loaded yet
81
// When class to load need others classes, for example: java classes, they are directly loaded by delegation to
82
// the system class Loader. The directory must be empty to compile the java class.
83
try {
84                 System.out.println("First try default System loading class... ");
85                 c = this.findSystemClass(name);// don't load classes already loaded by the application class loader
86
} catch (ClassNotFoundException JavaDoc e) {
87                 System.err.println("System loading... failed when loading " + name);
88             }
89             if (c == null) {
90                 // In this case the name represents a java source name, so we must compile at first
91
if ((!isClass(name)) && (isJavaSource(name))) {
92                     Runtime JavaDoc rt = Runtime.getRuntime();
93                     Process JavaDoc p;
94                     // Compilation by javac or we can imagine an other compilator
95
try {
96                         System.out.println("Launching javac ..." + pathList);
97                         String JavaDoc classpath = "";
98                         Iterator JavaDoc i = pathList.iterator();
99                         if (i.hasNext()) classpath =
100                             classpath + ((String JavaDoc) i.next());
101                         while (i.hasNext()) {
102                             // Only For Windows classpath
103
classpath = classpath + ";" + ((String JavaDoc) i.next());
104                         }
105
106                         String JavaDoc cmd = "javac -classpath " + classpath + " " +
107                             getJavaSourcePath(name) + File.separatorChar +
108                             name.replace('.', File.separatorChar) + ".java";
109                         System.out.println(cmd);
110                         p = rt.exec(cmd);
111                         p.waitFor();
112                         System.out.println("Compilation finisched ...");
113
114                     } catch (IOException JavaDoc e) {
115                         System.err.println(" Compilation failed... " + e.getMessage());
116
117
118                     } catch (InterruptedException JavaDoc e) {
119                         System.err.println(" Compilation failed... " + e.getMessage());
120                     }
121
122                     c = loadClass(name, resolve);
123                 } else if (isClass(name)) {
124
125                     System.err.println("Try loading class data bytes... " + name);
126                     byte[] data = loadClassData(name);
127
128                     if (data != null) {
129                         System.out.println("Byte class loaded...try to define class now");
130                         c = defineClass(name, data, 0, (int) bytecount);
131                         if (resolve && c != null) {
132                             resolveClass(c);
133                         }
134                     }
135                 } else
136                     throw new ClassNotFoundException JavaDoc(name);
137                 // name is not boyh class or java source file
138
}
139         }
140
141
142         if (c != null)
143             cache.put(name, c);
144         else
145             throw new ClassNotFoundException JavaDoc(name);
146         return c;
147     }
148
149     private boolean isClass(String JavaDoc name) {
150         boolean isClass = false;
151         Iterator JavaDoc it = pathList.iterator();
152         String JavaDoc dir = null;
153         while ((it.hasNext()) & (!isClass)) {
154             dir = (String JavaDoc) it.next();
155             //System.out.println(dir + File.separatorChar + name.replace('.', File.separatorChar) + ".class");
156
File JavaDoc cf = new File JavaDoc(dir + File.separatorChar +
157                 name.replace('.', File.separatorChar) + ".class");
158             if (cf.exists()) isClass = true;
159
160         }
161         return isClass;
162     }
163
164     private boolean isJavaSource(String JavaDoc name) {
165         boolean isSource = false;
166         Iterator JavaDoc it = pathList.iterator();
167         String JavaDoc dir = null;
168         while (it.hasNext() && (!isSource)) {
169             dir = (String JavaDoc) it.next();
170             //System.out.println(dir + File.separatorChar + name.replace('.', File.separatorChar) + ".java");
171
File JavaDoc cf = new File JavaDoc(dir + File.separator +
172                 name.replace('.', File.separatorChar) + ".java");
173             if (cf.exists()) isSource = true;
174
175         }
176         return isSource;
177     }
178
179     private String JavaDoc getJavaSourcePath(String JavaDoc name) {
180         Iterator JavaDoc it = pathList.iterator();
181         String JavaDoc dir = null;
182         boolean trouv = false;
183         while ((it.hasNext()) && (!trouv)) {
184             dir = (String JavaDoc) it.next();
185             // Second case: name represents a java source file
186
File JavaDoc sf = new File JavaDoc(dir + File.separatorChar +
187                 name.replace('.', File.separatorChar) +
188                 ".java");
189             if (sf.exists()) trouv = true;
190         }
191         if (trouv)
192             return dir;
193         else
194             return null;
195     }
196
197     private String JavaDoc getJavaPathClass(String JavaDoc name) {
198         Iterator JavaDoc it = pathList.iterator();
199         String JavaDoc dir = null;
200         boolean trouv = false;
201         while ((it.hasNext()) && (!trouv)) {
202             dir = (String JavaDoc) it.next();
203             // Second case: name represents a java source file
204
File JavaDoc sf = new File JavaDoc(dir + File.separatorChar +
205                 name.replace('.', File.separatorChar) + ".class");
206             if (sf.exists()) trouv = true;
207         }
208         if (trouv)
209             return dir;
210         else
211             return null;
212     }
213 }
214
Popular Tags