KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > jasperreports > util > JarsClassLoader


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
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 WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.engine.jasperreports.util;
22
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.jar.JarEntry JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import com.jaspersoft.jasperserver.api.metadata.common.domain.util.StreamUtils;
34
35 /**
36  * @author Lucian Chirita (lucianc@users.sourceforge.net)
37  * @version $Id: JarsClassLoader.java 3089 2006-04-14 14:13:09Z lucian $
38  */

39 public class JarsClassLoader extends ClassLoader JavaDoc {
40     private static final Log log = LogFactory.getLog(JarsClassLoader.class);
41
42     private final JarURLStreamHandler urlStreamHandler;
43
44     private final JarFile JavaDoc[] jars;
45
46     public JarsClassLoader(JarFile JavaDoc[] jars, ClassLoader JavaDoc parent) {
47         super(parent);
48
49         this.urlStreamHandler = new JarURLStreamHandler();
50         this.jars = jars;
51     }
52
53     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
54         String JavaDoc path = name.replace('.', '/').concat(".class");
55
56         JarFileEntry entry = findPath(path);
57
58         if (entry == null) {
59             throw new ClassNotFoundException JavaDoc(name);
60         }
61
62         //TODO certificates, package
63

64         byte[] classData;
65         try {
66             long size = entry.getSize();
67             if (size >= 0) {
68                 classData = StreamUtils.readData(entry.getInputStream(),
69                         (int) size);
70             } else {
71                 classData = StreamUtils.readData(entry.getInputStream());
72             }
73         } catch (IOException JavaDoc e) {
74             log.debug(e, e);
75             throw new ClassNotFoundException JavaDoc(name, e);
76         }
77
78         return defineClass(name, classData, 0, classData.length,
79                 JarsClassLoader.class.getProtectionDomain());
80     }
81
82     protected JarFileEntry findPath(String JavaDoc path) {
83         JarFileEntry entry = null;
84         for (int i = 0; i < jars.length && entry == null; i++) {
85             entry = getJarEntry(jars[i], path);
86         }
87         return entry;
88     }
89
90     protected URL JavaDoc findResource(String JavaDoc name) {
91         JarFileEntry entry = findPath(name);
92         return entry == null ? null : urlStreamHandler.createURL(entry);
93     }
94
95     protected Enumeration JavaDoc findResources(String JavaDoc name) throws IOException JavaDoc {
96         Vector JavaDoc urls = new Vector JavaDoc();
97         for (int i = 0; i < jars.length; i++) {
98             JarFileEntry entry = getJarEntry(jars[i], name);
99             if (entry != null) {
100                 urls.add(urlStreamHandler.createURL(entry));
101             }
102         }
103         return urls.elements();
104     }
105
106     protected static JarFileEntry getJarEntry(JarFile JavaDoc jar, String JavaDoc name) {
107         if (name.startsWith("/")) {
108             name = name.substring(1);
109         }
110
111         JarFileEntry jarEntry = null;
112         JarEntry JavaDoc entry = jar.getJarEntry(name);
113         if (entry != null) {
114             jarEntry = new JarFileEntry(jar, entry);
115         }
116
117         return jarEntry;
118     }
119 }
120
Popular Tags