KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > util > UnbrokenObjectInputStream


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package org.codehaus.aspectwerkz.util;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.ObjectInputStream JavaDoc;
13 import java.io.ObjectStreamClass JavaDoc;
14 import java.lang.reflect.Proxy JavaDoc;
15
16 /**
17  * Fixes the ObjectInputStream class, which does not always resolve the class correctly in complex
18  * class loader hierarchies.
19  *
20  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr</a>
21  */

22 public class UnbrokenObjectInputStream extends ObjectInputStream JavaDoc {
23
24     /**
25      * Creates a a new instance.
26      *
27      * @throws IOException
28      * @throws SecurityException
29      */

30     public UnbrokenObjectInputStream() throws IOException JavaDoc, SecurityException JavaDoc {
31         super();
32     }
33
34     /**
35      * Creates a new instance.
36      *
37      * @param in the input stream to deserialize the object from.
38      * @throws IOException
39      */

40     public UnbrokenObjectInputStream(final InputStream JavaDoc in) throws IOException JavaDoc {
41         super(in);
42     }
43
44     /**
45      * Overrides the parents resolveClass method and resolves the class using the context class loader
46      * instead of Class.forName().
47      */

48     protected Class JavaDoc resolveClass(final ObjectStreamClass JavaDoc desc) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
49         try {
50             Class JavaDoc resolved = Class.forName(desc.getName(), false, Thread.currentThread().getContextClassLoader());
51             return resolved;
52         } catch (ClassNotFoundException JavaDoc ex) {
53             return super.resolveClass(desc);
54         }
55     }
56
57     /**
58      * Overrides the parents resolveClass method and resolves the class using the context class loader
59      * instead of Class.forName().
60      */

61     protected Class JavaDoc resolveProxyClass(String JavaDoc[] interfaces) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
62         try {
63             Class JavaDoc[] classObjs = new Class JavaDoc[interfaces.length];
64             for (int i = 0; i < interfaces.length; i++) {
65                 classObjs[i] = Class.forName(interfaces[i], false, Thread.currentThread().getContextClassLoader());
66             }
67             return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classObjs);
68         } catch (Exception JavaDoc e) {
69             e.printStackTrace();
70             return super.resolveProxyClass(interfaces);
71         }
72     }
73
74 }
75
Popular Tags