KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjResourceClassLoader


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.util;
31
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.ByteArrayOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.jar.JarEntry JavaDoc;
40 import java.util.jar.JarInputStream JavaDoc;
41
42 import com.genimen.djeneric.structure.ResourceDefinition;
43
44 public class DjResourceClassLoader extends ClassLoader JavaDoc
45 {
46   private HashMap JavaDoc _resources; // the path to the location of the file that is going to be loaded
47
private ArrayList JavaDoc _extraJars = new ArrayList JavaDoc();
48
49   public DjResourceClassLoader(HashMap JavaDoc resources)
50   {
51     this._resources = resources;
52     Iterator JavaDoc it = resources.keySet().iterator();
53     while (it.hasNext())
54     {
55       String JavaDoc path = (String JavaDoc) it.next();
56       if (path.startsWith("/jars/"))
57       {
58         _extraJars.add(resources.get(path));
59       }
60     }
61   }
62
63   /////////////////////////////////////////////////////////////////////
64
// This method overrides the original method that is defined
65
// in the ClassLoader class. It will load a class file from the
66
// given location and try to make a class Object of it.
67
//
68
// name = The name of the class file that is to be loaded, without the
69
// ".class" extention.
70
// resolve = If true then resolve the class.
71
//////////////////////////////////////////////////////////////////////
72
protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve) throws ClassNotFoundException JavaDoc
73   {
74     // since all support classes of loaded class use same class loader
75
// must check subclass cache of classes for things like Object
76

77     Class JavaDoc c = findLoadedClass(name);
78
79     if (c == null)
80     {
81       try
82       {
83         c = findSystemClass(name);
84       }
85       catch (ClassNotFoundException JavaDoc e)
86       {
87         // don't do anything because this exception
88
// is thrown because the file was not found
89
// in the system classloader.
90
}
91     }
92
93     if (c == null)
94     {
95       ResourceDefinition res = (ResourceDefinition) _resources.get(name);
96       if (res != null)
97       {
98         byte data[] = res.getBytes();
99
100         // define a class Object of the class file in bytes
101
c = defineClass(name, data, 0, data.length);
102       }
103       else
104       {
105         String JavaDoc fileName = name.replace('.', '/') + ".class";
106         byte data[] = getBytesFromResourceJars(fileName);
107
108         if (data != null) c = defineClass(name, data, 0, data.length);
109       }
110
111       if (c == null) throw new ClassNotFoundException JavaDoc(name);
112     }
113
114     // check if the class has to be linked
115
if (resolve) resolveClass(c);
116
117     return c;
118   }
119
120   public InputStream JavaDoc getResourceAsStream(String JavaDoc name)
121   {
122     byte[] data = null;
123
124     ResourceDefinition res = (ResourceDefinition) _resources.get(name);
125     if (res != null)
126     {
127       data = res.getBytes();
128     }
129     else
130     {
131       data = getBytesFromResourceJars(name);
132     }
133
134     if (data == null) return null;
135     return new ByteArrayInputStream JavaDoc(data);
136
137   }
138
139   private byte[] getBytesFromResourceJars(String JavaDoc fileName)
140   {
141
142     try
143     {
144       Iterator JavaDoc it = _extraJars.iterator();
145       byte[] data = null;
146
147       while (it.hasNext() && data == null)
148       {
149         ResourceDefinition def = (ResourceDefinition) it.next();
150         if (def.isJar())
151         {
152           ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(def.getBytes());
153           JarInputStream JavaDoc jis = new JarInputStream JavaDoc(bis);
154           JarEntry JavaDoc entry;
155
156           while ((entry = jis.getNextJarEntry()) != null && data == null)
157           {
158             if (fileName.equals(entry.getName()))
159             {
160               ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc(2000);
161               DjFileUtil.copy(jis, bos);
162               data = bos.toByteArray();
163
164             }
165           }
166         }
167       }
168       return data;
169     }
170     catch (IOException JavaDoc e)
171     {
172       DjLogger.log(e);
173       return null;
174     }
175   }
176 }
Popular Tags