~ubuntu-branches/ubuntu/oneiric/libjboss-remoting-java/oneiric

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/marshall/dynamic/remote/classloaders/TestClassLoader2.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
/*
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.
7
 
*
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.
12
 
*
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.
17
 
*
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.
22
 
*/
23
 
package org.jboss.test.remoting.marshall.dynamic.remote.classloaders;
24
 
 
25
 
import java.io.ByteArrayInputStream;
26
 
import java.io.ByteArrayOutputStream;
27
 
import java.io.File;
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;
33
 
 
34
 
import org.apache.log4j.Logger;
35
 
 
36
 
/**
37
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
38
 
 * @version $Revision: 1.1 $
39
 
 * <p>
40
 
 * Copyright Aug 8, 2008
41
 
 * </p>
42
 
 */
43
 
public class TestClassLoader2 extends ClassLoader
44
 
{
45
 
   public boolean queriedForTarget;
46
 
   
47
 
   private static Logger log = Logger.getLogger(TestClassLoader2.class);
48
 
   
49
 
   private String jarFileName;
50
 
   private String targetClassName;
51
 
   private String targetClassFileName;
52
 
   
53
 
   
54
 
   public static void main(String[] args)
55
 
   {
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);
60
 
      try
61
 
      {
62
 
         Class c = tcl2.loadClass(className);
63
 
         Object o = c.newInstance();
64
 
         log.info("new object: " + o);
65
 
      }
66
 
      catch (Exception e)
67
 
      {
68
 
         e.printStackTrace();
69
 
      }
70
 
   }
71
 
   
72
 
   
73
 
   public TestClassLoader2(ClassLoader parent, String jarFileName, String targetClassName)
74
 
   {
75
 
      super(parent);
76
 
      this.jarFileName = jarFileName;
77
 
      this.targetClassName = targetClassName;
78
 
      this.targetClassFileName = targetClassName.replace('.', '/') + ".class";
79
 
   }
80
 
   
81
 
   
82
 
   public InputStream getResourceAsStream(String name) 
83
 
   {
84
 
      try
85
 
      {
86
 
         log.info(this + " queried for resource InputStream: " + name);
87
 
         if (targetClassFileName.equals(name))
88
 
         {
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);
94
 
         }
95
 
         else
96
 
         {
97
 
            return super.getResourceAsStream(name);
98
 
         }
99
 
      }
100
 
      catch (Exception e)
101
 
      {
102
 
         log.error(this + " unable to find resource: " + name);
103
 
         return null;
104
 
      }
105
 
   }
106
 
   
107
 
   
108
 
   protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException
109
 
   {
110
 
      try
111
 
      {
112
 
         log.info(this + " queried for class: " + name);
113
 
         if (targetClassName.equals(name))
114
 
         {
115
 
            log.info(this + " loading class: " + name);
116
 
            queriedForTarget = true;
117
 
            byte[] bytes = loadByteCodeFromClassName(name);
118
 
            Class c = defineClass(name, bytes, 0, bytes.length);
119
 
            if (resolve)
120
 
            {
121
 
               resolveClass(c);
122
 
            }
123
 
            return c;
124
 
         }
125
 
         else
126
 
         {
127
 
            return super.loadClass(name, resolve);
128
 
         }
129
 
      }
130
 
      catch (IOException e)
131
 
      {
132
 
         throw new ClassNotFoundException(name);
133
 
      }
134
 
   }
135
 
   
136
 
   
137
 
   private byte[] loadByteCodeFromClassName(String classname)
138
 
   throws ClassNotFoundException, IOException
139
 
   {
140
 
      String classFileName = classname.replace('.', '/') + ".class";
141
 
      return loadByteCodeFromClassFileName(classFileName);
142
 
   }
143
 
   
144
 
   
145
 
   private byte[] loadByteCodeFromClassFileName(String classFileName)
146
 
   throws ClassNotFoundException, IOException
147
 
   {
148
 
      File file = new File(jarFileName);
149
 
      JarInputStream jis = new JarInputStream(new FileInputStream(file));
150
 
      ZipEntry entry = jis.getNextEntry();
151
 
      
152
 
      try
153
 
      {
154
 
         while (entry != null)
155
 
         {
156
 
            log.info("name: " + entry.getName());
157
 
            if (classFileName.equals(entry.getName()))
158
 
               break;
159
 
            entry = jis.getNextEntry();
160
 
         }
161
 
         log.info("size: " + entry.getSize());
162
 
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
163
 
         byte[] tmp = new byte[1024];
164
 
         int read = 0;
165
 
         while( (read = jis.read(tmp)) > 0 )
166
 
         {
167
 
            baos.write(tmp, 0, read);
168
 
         }
169
 
         byte[] bytecode = baos.toByteArray();
170
 
         return bytecode;
171
 
      }
172
 
      finally
173
 
      {
174
 
         if( jis != null )
175
 
            jis.close();
176
 
      }
177
 
   }
178
 
}