01: /**************************************************************************************
02: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
03: * http://aspectwerkz.codehaus.org *
04: * ---------------------------------------------------------------------------------- *
05: * The software in this package is published under the terms of the LGPL license *
06: * a copy of which has been included with this distribution in the license.txt file. *
07: **************************************************************************************/package org.codehaus.aspectwerkz.util;
08:
09: import java.io.IOException;
10: import java.io.InputStream;
11: import java.io.ObjectInputStream;
12: import java.io.ObjectStreamClass;
13: import java.lang.reflect.Proxy;
14:
15: /**
16: * Fixes the ObjectInputStream class, which does not always resolve the class correctly in complex
17: * class loader hierarchies.
18: *
19: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr</a>
20: */
21: public class UnbrokenObjectInputStream extends ObjectInputStream {
22:
23: /**
24: * Creates a a new instance.
25: *
26: * @throws IOException
27: * @throws SecurityException
28: */
29: public UnbrokenObjectInputStream() throws IOException,
30: SecurityException {
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 in)
41: throws IOException {
42: super (in);
43: }
44:
45: /**
46: * Overrides the parents resolveClass method and resolves the class using the context class loader
47: * instead of Class.forName().
48: */
49: protected Class resolveClass(final ObjectStreamClass desc)
50: throws IOException, ClassNotFoundException {
51: try {
52: Class resolved = Class.forName(desc.getName(), false,
53: Thread.currentThread().getContextClassLoader());
54: return resolved;
55: } catch (ClassNotFoundException ex) {
56: return super .resolveClass(desc);
57: }
58: }
59:
60: /**
61: * Overrides the parents resolveClass method and resolves the class using the context class loader
62: * instead of Class.forName().
63: */
64: protected Class resolveProxyClass(String[] interfaces)
65: throws IOException, ClassNotFoundException {
66: try {
67: Class[] classObjs = new Class[interfaces.length];
68: for (int i = 0; i < interfaces.length; i++) {
69: classObjs[i] = Class.forName(interfaces[i], false,
70: Thread.currentThread().getContextClassLoader());
71: }
72: return Proxy.getProxyClass(Thread.currentThread()
73: .getContextClassLoader(), classObjs);
74: } catch (Exception e) {
75: e.printStackTrace();
76: return super.resolveProxyClass(interfaces);
77: }
78: }
79:
80: }
|