/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.activemq.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.lang.reflect.Proxy;

public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {

    private static final ClassLoader FALLBACK_CLASS_LOADER = ClassLoadingAwareObjectInputStream.class.getClassLoader();

    public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
        super(in);
    }

    protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        return load(classDesc.getName(), cl);
    }

    protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        Class[] cinterfaces = new Class[interfaces.length];
        for (int i = 0; i < interfaces.length; i++) {
            cinterfaces[i] = load(interfaces[i], cl);
        }

        try {
            return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces);
        } catch (IllegalArgumentException e) {
            throw new ClassNotFoundException(null, e);
        }
    }

    private Class load(String className, ClassLoader cl) throws ClassNotFoundException {
        try {
            return Class.forName(className, false, cl);
        } catch (ClassNotFoundException e) {
            return Class.forName(className, false, FALLBACK_CLASS_LOADER);
        }
    }

}

@@ -987,16 +987,42 @@
 t.Proxy;
+%0Aimport java.util.HashMap;
 %0A%0Apublic
@@ -1212,17 +1212,191 @@
 ader();%0A
+    /** %3Cp%3EMaps primitive type names to corresponding class objects.%3C/p%3E */%0A    private static final HashMap%3CString, Class%3E primClasses = new HashMap%3CString, Class%3E(8, 1.0F);
 %0A
-
     publ
@@ -2360,16 +2360,28 @@
 ader cl)
+%0A           
  throws 
@@ -2536,80 +2536,701 @@
     
-return Class.forName(className, false, FALLBACK_CLASS_LOADER);%0A        %7D
+final Class clazz = (Class) primClasses.get(className);%0A            if (clazz != null) %7B%0A                return clazz;%0A            %7D else %7B%0A                return Class.forName(className, false, FALLBACK_CLASS_LOADER);%0A            %7D%0A        %7D%0A    %7D%0A    %0A    %0A    %0A    static %7B%0A        primClasses.put(%22boolean%22, boolean.class);%0A        primClasses.put(%22byte%22, byte.class);%0A        primClasses.put(%22char%22, char.class);%0A        primClasses.put(%22short%22, short.class);%0A        primClasses.put(%22int%22, int.class);%0A        primClasses.put(%22long%22, long.class);%0A        primClasses.put(%22float%22, float.class);%0A        primClasses.put(%22double%22, double.class);%0A        primClasses.put(%22void%22, void.class);
 %0A   

Apply patch for https://issues.apache.org/activemq/browse/AMQ-1584