Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / test / java / test / TestClassLoader.java @ 40558

History | View | Annotate | Download (5.03 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: TestClassLoader.java 8590 2006-11-08 10:57:55Z jaume $
27
* $Log$
28
* Revision 1.1  2006-11-08 10:57:55  jaume
29
* remove unecessary imports
30
*
31
*
32
*/
33
package test;
34

    
35
import java.io.DataInputStream;
36
import java.io.File;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.net.MalformedURLException;
40
import java.net.URL;
41
import java.util.ArrayList;
42
import java.util.Enumeration;
43
import java.util.Hashtable;
44
import java.util.jar.JarException;
45
import java.util.zip.ZipEntry;
46
import java.util.zip.ZipFile;
47

    
48
public class TestClassLoader extends ClassLoader{
49
        private Hashtable jarz = new Hashtable();
50
        private Hashtable classes = new Hashtable();
51
        private String baseDir;
52

    
53
        public TestClassLoader(String baseDir) throws IOException {
54
                this.baseDir = baseDir;
55

    
56
                File dir = new File(baseDir);
57

    
58

    
59
                URL[] jars = (URL[]) getJarURLs(dir).toArray(new URL[0]);
60

    
61
                if (jars == null) {
62
                        throw new IllegalArgumentException("jars cannot be null"); //$NON-NLS-1$
63
                }
64

    
65
                //Se itera por las URLS que deben de ser jar's
66
                ZipFile[] jarFiles = new ZipFile[jars.length];
67

    
68
                for (int i = 0; i < jars.length; i++) {
69
                        jarFiles[i] = new ZipFile(jars[i].getPath());
70
                        Enumeration files = jarFiles[i].entries();
71
                        while (files.hasMoreElements()) {
72
                                //Se obtiene la entrada
73
                                ZipEntry file = (ZipEntry) files.nextElement();
74
                                String fileName = file.getName();
75

    
76
                                //Se obtiene el nombre de la clase
77
                                if (!fileName.toLowerCase().endsWith(".class")) { //$NON-NLS-1$
78
                                        continue;
79
                                }
80

    
81
                                fileName = fileName.substring(0, fileName.length() - 6).replace('/',
82
                                '.');
83

    
84
                                //Se cromprueba si ya hab?a una clase con dicho nombre
85
                                if (jarz.get(fileName) != null) {
86
                                        throw new JarException(
87
                                                        "two or more classes with the same name in the jars: " +
88
                                                        fileName);
89
                                }
90

    
91
                                //Se registra la clase
92
                                jarz.put(fileName, jarFiles[i]);
93
                                try {
94
                                        loadClass(fileName, true);
95
                                } catch (ClassNotFoundException e) {
96
                                        // TODO Auto-generated catch block
97
                                        e.printStackTrace();
98
                                }
99
                        }
100

    
101
                }
102
        }
103

    
104
        private ArrayList getJarURLs(File file) {
105
                ArrayList jars = new ArrayList();
106
                if (file.isDirectory()) {
107
                        String[] fileNames = file.list();
108
                        for (int i = 0; i < fileNames.length; i++) {
109
                                File file1 = new File(file+ File.separator + fileNames[i]);
110
                                if (file1.isDirectory()) {
111
                                        jars.addAll(getJarURLs(file1));
112
                                } else {
113
                                        if (file1.getAbsolutePath().endsWith(".jar")) {
114
                                                try {
115
                                                        jars.add(file1.toURL());
116
                                                } catch (MalformedURLException e) {
117
                                                        e.printStackTrace();
118
                                                }
119
                                        }
120
                                }
121
                        }
122
                } else {
123
                        if (file.getAbsolutePath().endsWith(".jar")) {
124
                                try {
125
                                        jars.add(new URL(file.getAbsolutePath()));
126
                                } catch (MalformedURLException e) {
127
                                        e.printStackTrace();
128
                                }
129
                        }
130
                }
131
                return jars;
132
        }
133

    
134
        protected synchronized Class loadClass(String className, boolean resolve) throws ClassNotFoundException {
135
                Class c;
136
                System.out.print("Loading class ["+className+"]...");
137
                try {
138
                        c = super.loadClass(className, resolve);
139
                } catch (ClassNotFoundException e) {
140
                        ZipFile file = (ZipFile) jarz.get(className);
141
                        ZipEntry classFile = file.getEntry(className);
142
                        byte[] classBytes;
143
                        try {
144
                                classBytes = loadClassBytes(classFile, file.getInputStream(classFile));
145
                        } catch (IOException e1) {
146
                                throw new ClassNotFoundException(className);
147
                        }
148
                        c = defineClass(className, classBytes, 0, classBytes.length);
149
                }
150
                System.out.println(" Ok!");
151

    
152
                return c;
153
        }
154

    
155
        protected Class findClass(String className) throws ClassNotFoundException {
156
                return loadClass(className);
157
        }
158

    
159
        private byte[] loadClassBytes(ZipEntry classFile, InputStream is)
160
        throws IOException {
161
                // Get size of class file
162
                int size = (int) classFile.getSize();
163

    
164
                // Reserve space to read
165
                byte[] buff = new byte[size];
166

    
167
                // Get stream to read from
168
                DataInputStream dis = new DataInputStream(is);
169

    
170
                // Read in data
171
                dis.readFully(buff);
172

    
173
                // close stream
174
                dis.close();
175

    
176
                // return data
177
                return buff;
178
        }
179

    
180
        public static void main(String args[]) {
181
                try {
182
                        TestClassLoader cl = new TestClassLoader("lib-test/");
183
                } catch (IOException e) {
184
                        // TODO Auto-generated catch block
185
                        e.printStackTrace();
186
                }
187
        }
188
}