root / branches / v2_0_0_prep / applications / appgvSIG / src-test / test / TestClassLoader.java @ 23383
History | View | Annotate | Download (5.23 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
|
42 |
/* CVS MESSAGES:
|
43 |
*
|
44 |
* $Id: TestClassLoader.java 8590 2006-11-08 10:57:55Z jaume $
|
45 |
* $Log$
|
46 |
* Revision 1.1 2006-11-08 10:57:55 jaume
|
47 |
* remove unecessary imports
|
48 |
*
|
49 |
*
|
50 |
*/
|
51 |
package test; |
52 |
|
53 |
import java.io.DataInputStream; |
54 |
import java.io.File; |
55 |
import java.io.IOException; |
56 |
import java.io.InputStream; |
57 |
import java.net.MalformedURLException; |
58 |
import java.net.URL; |
59 |
import java.util.ArrayList; |
60 |
import java.util.Enumeration; |
61 |
import java.util.Hashtable; |
62 |
import java.util.jar.JarException; |
63 |
import java.util.zip.ZipEntry; |
64 |
import java.util.zip.ZipFile; |
65 |
|
66 |
public class TestClassLoader extends ClassLoader{ |
67 |
private Hashtable jarz = new Hashtable(); |
68 |
private Hashtable classes = new Hashtable(); |
69 |
private String baseDir; |
70 |
|
71 |
public TestClassLoader(String baseDir) throws IOException { |
72 |
this.baseDir = baseDir;
|
73 |
|
74 |
File dir = new File(baseDir); |
75 |
|
76 |
|
77 |
URL[] jars = (URL[]) getJarURLs(dir).toArray(new URL[0]); |
78 |
|
79 |
if (jars == null) { |
80 |
throw new IllegalArgumentException("jars cannot be null"); //$NON-NLS-1$ |
81 |
} |
82 |
|
83 |
//Se itera por las URLS que deben de ser jar's
|
84 |
ZipFile[] jarFiles = new ZipFile[jars.length]; |
85 |
|
86 |
for (int i = 0; i < jars.length; i++) { |
87 |
jarFiles[i] = new ZipFile(jars[i].getPath()); |
88 |
Enumeration files = jarFiles[i].entries();
|
89 |
while (files.hasMoreElements()) {
|
90 |
//Se obtiene la entrada
|
91 |
ZipEntry file = (ZipEntry) files.nextElement(); |
92 |
String fileName = file.getName();
|
93 |
|
94 |
//Se obtiene el nombre de la clase
|
95 |
if (!fileName.toLowerCase().endsWith(".class")) { //$NON-NLS-1$ |
96 |
continue;
|
97 |
} |
98 |
|
99 |
fileName = fileName.substring(0, fileName.length() - 6).replace('/', |
100 |
'.');
|
101 |
|
102 |
//Se cromprueba si ya hab?a una clase con dicho nombre
|
103 |
if (jarz.get(fileName) != null) { |
104 |
throw new JarException( |
105 |
"two or more classes with the same name in the jars: " +
|
106 |
fileName); |
107 |
} |
108 |
|
109 |
//Se registra la clase
|
110 |
jarz.put(fileName, jarFiles[i]); |
111 |
try {
|
112 |
loadClass(fileName, true);
|
113 |
} catch (ClassNotFoundException e) { |
114 |
// TODO Auto-generated catch block
|
115 |
e.printStackTrace(); |
116 |
} |
117 |
} |
118 |
|
119 |
} |
120 |
} |
121 |
|
122 |
private ArrayList getJarURLs(File file) { |
123 |
ArrayList jars = new ArrayList(); |
124 |
if (file.isDirectory()) {
|
125 |
String[] fileNames = file.list(); |
126 |
for (int i = 0; i < fileNames.length; i++) { |
127 |
File file1 = new File(file+ File.separator + fileNames[i]); |
128 |
if (file1.isDirectory()) {
|
129 |
jars.addAll(getJarURLs(file1)); |
130 |
} else {
|
131 |
if (file1.getAbsolutePath().endsWith(".jar")) { |
132 |
try {
|
133 |
jars.add(file1.toURL()); |
134 |
} catch (MalformedURLException e) { |
135 |
e.printStackTrace(); |
136 |
} |
137 |
} |
138 |
} |
139 |
} |
140 |
} else {
|
141 |
if (file.getAbsolutePath().endsWith(".jar")) { |
142 |
try {
|
143 |
jars.add(new URL(file.getAbsolutePath())); |
144 |
} catch (MalformedURLException e) { |
145 |
e.printStackTrace(); |
146 |
} |
147 |
} |
148 |
} |
149 |
return jars;
|
150 |
} |
151 |
|
152 |
protected synchronized Class loadClass(String className, boolean resolve) throws ClassNotFoundException { |
153 |
Class c;
|
154 |
System.out.print("Loading class ["+className+"]..."); |
155 |
try {
|
156 |
c = super.loadClass(className, resolve);
|
157 |
} catch (ClassNotFoundException e) { |
158 |
ZipFile file = (ZipFile) jarz.get(className); |
159 |
ZipEntry classFile = file.getEntry(className);
|
160 |
byte[] classBytes; |
161 |
try {
|
162 |
classBytes = loadClassBytes(classFile, file.getInputStream(classFile)); |
163 |
} catch (IOException e1) { |
164 |
throw new ClassNotFoundException(className); |
165 |
} |
166 |
c = defineClass(className, classBytes, 0, classBytes.length);
|
167 |
} |
168 |
System.out.println(" Ok!"); |
169 |
|
170 |
return c;
|
171 |
} |
172 |
|
173 |
protected Class findClass(String className) throws ClassNotFoundException { |
174 |
return loadClass(className);
|
175 |
} |
176 |
|
177 |
private byte[] loadClassBytes(ZipEntry classFile, InputStream is) |
178 |
throws IOException { |
179 |
// Get size of class file
|
180 |
int size = (int) classFile.getSize(); |
181 |
|
182 |
// Reserve space to read
|
183 |
byte[] buff = new byte[size]; |
184 |
|
185 |
// Get stream to read from
|
186 |
DataInputStream dis = new DataInputStream(is); |
187 |
|
188 |
// Read in data
|
189 |
dis.readFully(buff); |
190 |
|
191 |
// close stream
|
192 |
dis.close(); |
193 |
|
194 |
// return data
|
195 |
return buff;
|
196 |
} |
197 |
|
198 |
public static void main(String args[]) { |
199 |
try {
|
200 |
TestClassLoader cl = new TestClassLoader("lib-test/"); |
201 |
} catch (IOException e) { |
202 |
// TODO Auto-generated catch block
|
203 |
e.printStackTrace(); |
204 |
} |
205 |
} |
206 |
} |