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
026public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {
027
028    private static final ClassLoader FALLBACK_CLASS_LOADER = ClassLoadingAwareObjectInputStream.class.getClassLoader();
029    /** <p>Maps primitive type names to corresponding class objects.</p> */
030    private static final HashMap<String, Class> primClasses = new HashMap<String, Class>(8, 1.0F);
031    public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
032        super(in);
033    }
034
035    protected Class resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
036        ClassLoader cl = Thread.currentThread().getContextClassLoader();
037        return load(classDesc.getName(), cl);
038    }
039
040    protected Class resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
041        ClassLoader cl = Thread.currentThread().getContextClassLoader();
042        Class[] cinterfaces = new Class[interfaces.length];
043        for (int i = 0; i < interfaces.length; i++) {
044            cinterfaces[i] = load(interfaces[i], cl);
045        }
046
047        try {
048            return Proxy.getProxyClass(cinterfaces[0].getClassLoader(), cinterfaces);
049        } catch (IllegalArgumentException e) {
050            throw new ClassNotFoundException(null, e);
051        }
052    }
053
054    private Class load(String className, ClassLoader cl)
055            throws ClassNotFoundException {
056        try {
057            return Class.forName(className, false, cl);
058        } catch (ClassNotFoundException e) {
059            final Class clazz = (Class) primClasses.get(className);
060            if (clazz != null) {
061                return clazz;
062            } else {
063                return Class.forName(className, false, FALLBACK_CLASS_LOADER);
064            }
065        }
066    }
067    
068    
069    
070    static {
071        primClasses.put("boolean", boolean.class);
072        primClasses.put("byte", byte.class);
073        primClasses.put("char", char.class);
074        primClasses.put("short", short.class);
075        primClasses.put("int", int.class);
076        primClasses.put("long", long.class);
077        primClasses.put("float", float.class);
078        primClasses.put("double", double.class);
079        primClasses.put("void", void.class);
080    }
081
082}