001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.util;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.ObjectInputStream;
022import java.io.ObjectStreamClass;
023import java.lang.reflect.Proxy;
024import java.util.HashMap;
025
026@SuppressWarnings("rawtypes")
027public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {
028
029    private static final ClassLoader FALLBACK_CLASS_LOADER =
030        ClassLoadingAwareObjectInputStream.class.getClassLoader();
031
032    /**
033     * Maps primitive type names to corresponding class objects.
034     */
035    private static final HashMap<String, Class> primClasses = new HashMap<String, Class>(8, 1.0F);
036
037    public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
038        super(in);
039    }
040
041    protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
042        ClassLoader cl = Thread.currentThread().getContextClassLoader();
043        return load(classDesc.getName(), cl);
044    }
045
046    protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
047        ClassLoader cl = Thread.currentThread().getContextClassLoader();
048        Class[] cinterfaces = new Class[interfaces.length];
049        for (int i = 0; i < interfaces.length; i++) {
050            cinterfaces[i] = load(interfaces[i], cl);
051        }
052
053        try {
054            return Proxy.getProxyClass(cl, cinterfaces);
055        } catch (IllegalArgumentException e) {
056            try {
057                return Proxy.getProxyClass(FALLBACK_CLASS_LOADER, cinterfaces);
058            } catch (IllegalArgumentException e1) {
059            }
060
061            throw new ClassNotFoundException(null, e);
062        }
063    }
064
065    private Class<?> load(String className, ClassLoader cl) throws ClassNotFoundException {
066        try {
067            return Class.forName(className, false, cl);
068        } catch (ClassNotFoundException e) {
069            final Class<?> clazz = (Class<?>) primClasses.get(className);
070            if (clazz != null) {
071                return clazz;
072            } else {
073                return Class.forName(className, false, FALLBACK_CLASS_LOADER);
074            }
075        }
076    }
077
078    static {
079        primClasses.put("boolean", boolean.class);
080        primClasses.put("byte", byte.class);
081        primClasses.put("char", char.class);
082        primClasses.put("short", short.class);
083        primClasses.put("int", int.class);
084        primClasses.put("long", long.class);
085        primClasses.put("float", float.class);
086        primClasses.put("double", double.class);
087        primClasses.put("void", void.class);
088    }
089}