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     */
017    package org.apache.activemq.util;
018    
019    import java.io.IOException;
020    import java.io.InputStream;
021    import java.io.ObjectInputStream;
022    import java.io.ObjectStreamClass;
023    import java.lang.reflect.Proxy;
024    import java.util.HashMap;
025    
026    @SuppressWarnings("rawtypes")
027    public 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        private final ClassLoader inLoader;
038    
039        public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
040            super(in);
041            inLoader = in.getClass().getClassLoader();
042        }
043    
044        protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
045            ClassLoader cl = Thread.currentThread().getContextClassLoader();
046            return load(classDesc.getName(), cl, inLoader);
047        }
048    
049        protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
050            ClassLoader cl = Thread.currentThread().getContextClassLoader();
051            Class[] cinterfaces = new Class[interfaces.length];
052            for (int i = 0; i < interfaces.length; i++) {
053                cinterfaces[i] = load(interfaces[i], cl);
054            }
055    
056            try {
057                return Proxy.getProxyClass(cl, cinterfaces);
058            } catch (IllegalArgumentException e) {
059                try {
060                    return Proxy.getProxyClass(inLoader, cinterfaces);
061                } catch (IllegalArgumentException e1) {
062                    // ignore
063                }
064                try {
065                    return Proxy.getProxyClass(FALLBACK_CLASS_LOADER, cinterfaces);
066                } catch (IllegalArgumentException e2) {
067                    // ignore
068                }
069    
070                throw new ClassNotFoundException(null, e);
071            }
072        }
073    
074        private Class<?> load(String className, ClassLoader... cl) throws ClassNotFoundException {
075            for (ClassLoader loader : cl) {
076                try {
077                    return Class.forName(className, false, loader);
078                } catch (ClassNotFoundException e) {
079                    // ignore
080                }
081            }
082            // fallback
083            final Class<?> clazz = (Class<?>) primClasses.get(className);
084            if (clazz != null) {
085                return clazz;
086            } else {
087                return Class.forName(className, false, FALLBACK_CLASS_LOADER);
088            }
089        }
090    
091        static {
092            primClasses.put("boolean", boolean.class);
093            primClasses.put("byte", byte.class);
094            primClasses.put("char", char.class);
095            primClasses.put("short", short.class);
096            primClasses.put("int", int.class);
097            primClasses.put("long", long.class);
098            primClasses.put("float", float.class);
099            primClasses.put("double", double.class);
100            primClasses.put("void", void.class);
101        }
102    }