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

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/loading/ClassUtil.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
* JBoss, Home of Professional Open Source
 
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
 
4
* by the @authors tag. See the copyright.txt in the distribution for a
 
5
* full listing of individual contributors.
 
6
*
 
7
* This is free software; you can redistribute it and/or modify it
 
8
* under the terms of the GNU Lesser General Public License as
 
9
* published by the Free Software Foundation; either version 2.1 of
 
10
* the License, or (at your option) any later version.
 
11
*
 
12
* This software is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
* Lesser General Public License for more details.
 
16
*
 
17
* You should have received a copy of the GNU Lesser General Public
 
18
* License along with this software; if not, write to the Free
 
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 
21
*/
 
22
package org.jboss.remoting.loading;
 
23
 
 
24
import java.io.ByteArrayOutputStream;
 
25
import java.io.IOException;
 
26
import java.io.InputStream;
 
27
import java.security.AccessController;
 
28
import java.security.PrivilegedAction;
 
29
import java.util.HashSet;
 
30
import java.util.Set;
 
31
import org.jboss.logging.Logger;
 
32
 
 
33
 
 
34
/**
 
35
 * ClassUtil is a set of generic class utlities.
 
36
 *
 
37
 * @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
 
38
 * @author <a href="mailto:tom@jboss.org">Tom Elrod</a>
 
39
 * @version $Revision: 4145 $
 
40
 */
 
41
public class ClassUtil
 
42
{
 
43
   protected final static Logger log = Logger.getLogger(ClassUtil.class);
 
44
 
 
45
   public static Object deserialize(ClassBytes cb, ClassLoader cl)
 
46
         throws IOException, ClassNotFoundException
 
47
   {
 
48
      if(cb.getClassBytes() == null)
 
49
      {
 
50
         return null;
 
51
      }
 
52
      java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(cb.getClassBytes());
 
53
      java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl);
 
54
      Object result = ois.readObject();
 
55
      bis = null;
 
56
      ois = null;
 
57
      return result;
 
58
   }
 
59
 
 
60
   public static Object deserialize(byte buf[])
 
61
         throws IOException, ClassNotFoundException
 
62
   {
 
63
      ClassLoader cl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
 
64
      {
 
65
         public Object run()
 
66
         {
 
67
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
 
68
            if (loader == null) loader = ClassUtil.class.getClassLoader();
 
69
            return loader;
 
70
         }
 
71
      });
 
72
      
 
73
      return deserialize(buf, cl);
 
74
   }
 
75
 
 
76
   public static Object deserialize(byte buf[], ClassLoader cl)
 
77
         throws IOException, ClassNotFoundException
 
78
   {
 
79
      if(buf == null)
 
80
      {
 
81
         return null;
 
82
      }
 
83
      java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream(buf);
 
84
      java.io.ObjectInputStream ois = new ObjectInputStreamWithClassLoader(bis, cl);
 
85
      Object result = ois.readObject();
 
86
      bis = null;
 
87
      ois = null;
 
88
      return result;
 
89
   }
 
90
 
 
91
   public static byte[] serialize(Object obj)
 
92
         throws java.io.IOException
 
93
   {
 
94
      java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
 
95
      java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(bos);
 
96
      oos.writeObject(obj);
 
97
      oos.flush();
 
98
      bos.flush();
 
99
      byte buf[] = bos.toByteArray();
 
100
      bos = null;
 
101
      oos = null;
 
102
      return buf;
 
103
   }
 
104
 
 
105
   public static boolean isArrayClass(String className)
 
106
   {
 
107
      return (className.startsWith("[L") && className.endsWith(";"));
 
108
   }
 
109
 
 
110
   public static String getArrayClassPart(String className)
 
111
   {
 
112
      String cn = className;
 
113
      int i = className.indexOf("[L");
 
114
      if(i > -1)
 
115
      {
 
116
         cn = className.substring(i + 2, className.length() - 1);
 
117
      }
 
118
      return cn;
 
119
   }
 
120
 
 
121
   public static String getPackageName(Class cl)
 
122
   {
 
123
      String n = cl.getName();
 
124
      int i = n.lastIndexOf(".");
 
125
      return (i > -1) ? n.substring(0, i) : n;
 
126
   }
 
127
 
 
128
   public static String getShortClassName(Class cl)
 
129
   {
 
130
      String n = cl.getName();
 
131
      int i = n.lastIndexOf(".");
 
132
      return (i > -1) ? n.substring(i + 1) : n;
 
133
   }
 
134
 
 
135
   /**
 
136
    * given a class, recurse its dependency graph and find all its implemented interfaces
 
137
    *
 
138
    * @param clazz
 
139
    * @return array of interfaces
 
140
    */
 
141
   public static Class[] getInterfacesFor(Class clazz)
 
142
   {
 
143
      // use a set to eliminate duplicates, since you'll get a
 
144
      // java.lang.ClassFormatError: $Proxy8 (Repetitive interface name))
 
145
      Set set = new HashSet();
 
146
      addInterfaces(set, clazz);
 
147
      return (Class[]) set.toArray(new Class[set.size()]);
 
148
   }
 
149
 
 
150
   private static void addInterfaces(Set list, Class clazz)
 
151
   {
 
152
      if(clazz != null && clazz != Object.class)
 
153
      {
 
154
         if(clazz.isInterface() && list.contains(clazz) == false)
 
155
         {
 
156
            list.add(clazz);
 
157
         }
 
158
         Class interfaces[] = clazz.getInterfaces();
 
159
         if(interfaces != null && interfaces.length > 0)
 
160
         {
 
161
            for(int c = 0; c < interfaces.length; c++)
 
162
            {
 
163
               Class interfaceClass = interfaces[c];
 
164
               if(list.contains(interfaceClass) == false)
 
165
               {
 
166
                  list.add(interfaceClass);
 
167
               }
 
168
               addInterfaces(list, interfaceClass);
 
169
            }
 
170
         }
 
171
         addInterfaces(list, clazz.getSuperclass());
 
172
      }
 
173
   }
 
174
 
 
175
   /**
 
176
    * method is called to retrieve a byte array of a Class for a given class name
 
177
    *
 
178
    * @param className
 
179
    * @return
 
180
    */
 
181
   public static byte[] getClassBytes(String className, ClassLoader classbyteloader)
 
182
   {
 
183
      String cn = null;
 
184
      if(isArrayClass(className))
 
185
      {
 
186
         // if requesting an array, of course, that would be found in our class path, so we
 
187
         // need to strip the class data and just return the class part, the other side
 
188
         // will properly load the class as an array
 
189
         cn = getArrayClassPart(className).replace('.', '/') + ".class";
 
190
      }
 
191
      else
 
192
      {
 
193
         cn = className.replace('.', '/') + ".class";
 
194
      }
 
195
      if(log.isTraceEnabled())
 
196
      {
 
197
         log.trace("trying to load class: " + className + " from path: " + cn);
 
198
      }
 
199
      InputStream in = null;
 
200
      ClassLoader cl = classbyteloader;
 
201
 
 
202
      if(cl == null)
 
203
      {
 
204
         cl = (ClassLoader) AccessController.doPrivileged( new PrivilegedAction()
 
205
         {
 
206
            public Object run()
 
207
            {
 
208
               return ClassLoader.getSystemClassLoader();
 
209
            }
 
210
         });
 
211
      }
 
212
      if(cl != null)
 
213
      {
 
214
         try
 
215
         {
 
216
            final ClassLoader fcl = cl;
 
217
            final String fcn = cn;
 
218
            in = (InputStream) AccessController.doPrivileged( new PrivilegedAction()
 
219
            {
 
220
               public Object run()
 
221
               {
 
222
                  return fcl.getResourceAsStream(fcn);
 
223
               }
 
224
            });
 
225
         }
 
226
         catch (Exception e)
 
227
         {
 
228
            log.error("error getting resource " + cn, e);
 
229
         }
 
230
         if(in != null)
 
231
         {
 
232
            if(log.isTraceEnabled())
 
233
            {
 
234
               log.trace("looking for classes at: " + cl);
 
235
            }
 
236
            try
 
237
            {
 
238
               byte data[] = read(in);
 
239
               if(log.isTraceEnabled())
 
240
               {
 
241
                  log.trace("found class at classloader: " + cl);
 
242
               }
 
243
               return data;
 
244
            }
 
245
            catch(IOException io)
 
246
            {
 
247
            }
 
248
            finally
 
249
            {
 
250
               if(in != null)
 
251
               {
 
252
                  try
 
253
                  {
 
254
                     in.close();
 
255
                  }
 
256
                  catch(Exception ig)
 
257
                  {
 
258
                  }
 
259
                  in = null;
 
260
               }
 
261
            }
 
262
         }
 
263
      }
 
264
      return null;
 
265
   }
 
266
 
 
267
   /**
 
268
    * simple utility method for reading bytes from an input stream
 
269
    *
 
270
    * @param in
 
271
    * @return
 
272
    * @throws IOException
 
273
    */
 
274
   protected static byte[] read(InputStream in)
 
275
         throws IOException
 
276
   {
 
277
      ByteArrayOutputStream out = new ByteArrayOutputStream();
 
278
      byte buf[] = new byte[4096];
 
279
      while(true)
 
280
      {
 
281
         int c = in.read(buf);
 
282
         if(c < 0)
 
283
         {
 
284
            break;
 
285
         }
 
286
         out.write(buf, 0, c);
 
287
      }
 
288
      return out.toByteArray();
 
289
   }
 
290
 
 
291
}