3
* JBoss, Home of Professional Open Source
4
* Copyright 2005, JBoss Inc., and individual contributors as indicated
5
* by the @authors tag. See the copyright.txt in the distribution for a
6
* full listing of individual contributors.
8
* This is free software; you can redistribute it and/or modify it
9
* under the terms of the GNU Lesser General Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* This software is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this software; if not, write to the Free
20
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.marshall.dynamic.remote.classloaders;
25
import java.io.ByteArrayInputStream;
26
import java.io.ByteArrayOutputStream;
28
import java.io.FileInputStream;
29
import java.io.IOException;
30
import java.io.InputStream;
31
import java.util.jar.JarInputStream;
32
import java.util.zip.ZipEntry;
34
import org.apache.log4j.Logger;
37
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
38
* @version $Revision: 1.1 $
40
* Copyright Aug 8, 2008
43
public class TestClassLoader2 extends ClassLoader
45
public boolean queriedForTarget;
47
private static Logger log = Logger.getLogger(TestClassLoader2.class);
49
private String jarFileName;
50
private String targetClassName;
51
private String targetClassFileName;
54
public static void main(String[] args)
56
String jarFileName = "C:/cygwin/home/rsigal/workspace/JBossRemoting-2.x/output/lib/jboss-remoting-loading-tests.jar";
57
String className = "org.jboss.test.remoting.marshall.dynamic.remote.classloaders.ResponseImpl";
58
ClassLoader cl = TestClassLoader2.class.getClassLoader();
59
TestClassLoader2 tcl2 = new TestClassLoader2(cl, jarFileName, className);
62
Class c = tcl2.loadClass(className);
63
Object o = c.newInstance();
64
log.info("new object: " + o);
73
public TestClassLoader2(ClassLoader parent, String jarFileName, String targetClassName)
76
this.jarFileName = jarFileName;
77
this.targetClassName = targetClassName;
78
this.targetClassFileName = targetClassName.replace('.', '/') + ".class";
82
public InputStream getResourceAsStream(String name)
86
log.info(this + " queried for resource InputStream: " + name);
87
if (targetClassFileName.equals(name))
89
log.info(this + " looking for resource InputStream: " + name);
90
queriedForTarget = true;
91
byte[] bytecode = loadByteCodeFromClassFileName(name);
92
log.info(this + " returning resource InputStream: " + name);
93
return new ByteArrayInputStream(bytecode);
97
return super.getResourceAsStream(name);
102
log.error(this + " unable to find resource: " + name);
108
protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
112
log.info(this + " queried for class: " + name);
113
if (targetClassName.equals(name))
115
log.info(this + " loading class: " + name);
116
queriedForTarget = true;
117
byte[] bytes = loadByteCodeFromClassName(name);
118
Class c = defineClass(name, bytes, 0, bytes.length);
127
return super.loadClass(name, resolve);
130
catch (IOException e)
132
throw new ClassNotFoundException(name);
137
private byte[] loadByteCodeFromClassName(String classname)
138
throws ClassNotFoundException, IOException
140
String classFileName = classname.replace('.', '/') + ".class";
141
return loadByteCodeFromClassFileName(classFileName);
145
private byte[] loadByteCodeFromClassFileName(String classFileName)
146
throws ClassNotFoundException, IOException
148
File file = new File(jarFileName);
149
JarInputStream jis = new JarInputStream(new FileInputStream(file));
150
ZipEntry entry = jis.getNextEntry();
154
while (entry != null)
156
log.info("name: " + entry.getName());
157
if (classFileName.equals(entry.getName()))
159
entry = jis.getNextEntry();
161
log.info("size: " + entry.getSize());
162
ByteArrayOutputStream baos = new ByteArrayOutputStream();
163
byte[] tmp = new byte[1024];
165
while( (read = jis.read(tmp)) > 0 )
167
baos.write(tmp, 0, read);
169
byte[] bytecode = baos.toByteArray();