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; 11 import java.io.InputStream; 12 import java.io.ObjectInputStream; 13 import java.io.ObjectStreamClass; 14 import java.lang.reflect.Proxy; 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 { 23 24 /*** 25 * Creates a a new instance. 26 * 27 * @throws IOException 28 * @throws SecurityException 29 */ 30 public UnbrokenObjectInputStream() throws IOException, 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) throws IOException { 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 resolveClass(final ObjectStreamClass desc) throws IOException, ClassNotFoundException { 49 try { 50 Class resolved = Class.forName(desc.getName(), false, Thread.currentThread().getContextClassLoader()); 51 return resolved; 52 } catch (ClassNotFoundException 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 resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException { 62 try { 63 Class[] classObjs = new Class[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 e) { 69 e.printStackTrace(); 70 return super.resolveProxyClass(interfaces); 71 } 72 } 73 74 }