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;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {
029
030    private static final Logger LOG = LoggerFactory.getLogger(ClassLoadingAwareObjectInputStream.class);
031    private static final ClassLoader FALLBACK_CLASS_LOADER =
032        ClassLoadingAwareObjectInputStream.class.getClassLoader();
033
034    private final ClassLoader inLoader;
035
036    public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
037        super(in);
038        inLoader = in.getClass().getClassLoader();
039    }
040
041    protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
042        ClassLoader cl = Thread.currentThread().getContextClassLoader();
043        return load(classDesc.getName(), cl, inLoader);
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(inLoader, cinterfaces);
058            } catch (IllegalArgumentException e1) {
059                // ignore
060            }
061            try {
062                return Proxy.getProxyClass(FALLBACK_CLASS_LOADER, cinterfaces);
063            } catch (IllegalArgumentException e2) {
064                // ignore
065            }
066
067            throw new ClassNotFoundException(null, e);
068        }
069    }
070
071    private Class<?> load(String className, ClassLoader... cl) throws ClassNotFoundException {
072        // check for simple types first
073        final Class<?> clazz = loadSimpleType(className);
074        if (clazz != null) {
075            LOG.trace("Loaded class: {} as simple type -> ", className, clazz);
076            return clazz;
077        }
078
079        // try the different class loaders
080        for (ClassLoader loader : cl) {
081            LOG.trace("Attempting to load class: {} using classloader: {}", className, cl);
082            try {
083                Class<?> answer = Class.forName(className, false, loader);
084                if (LOG.isTraceEnabled()) {
085                    LOG.trace("Loaded class: {} using classloader: {} -> ", new Object[]{className, cl, answer});
086                }
087                return answer;
088            } catch (ClassNotFoundException e) {
089                LOG.trace("Class not found: {} using classloader: {}", className, cl);
090                // ignore
091            }
092        }
093
094        // and then the fallback class loader
095        return Class.forName(className, false, FALLBACK_CLASS_LOADER);
096    }
097
098    /**
099     * Load a simple type
100     *
101     * @param name the name of the class to load
102     * @return the class or <tt>null</tt> if it could not be loaded
103     */
104    public static Class<?> loadSimpleType(String name) {
105        // code from ObjectHelper.loadSimpleType in Apache Camel
106
107        // special for byte[] or Object[] as its common to use
108        if ("java.lang.byte[]".equals(name) || "byte[]".equals(name)) {
109            return byte[].class;
110        } else if ("java.lang.Byte[]".equals(name) || "Byte[]".equals(name)) {
111            return Byte[].class;
112        } else if ("java.lang.Object[]".equals(name) || "Object[]".equals(name)) {
113            return Object[].class;
114        } else if ("java.lang.String[]".equals(name) || "String[]".equals(name)) {
115            return String[].class;
116            // and these is common as well
117        } else if ("java.lang.String".equals(name) || "String".equals(name)) {
118            return String.class;
119        } else if ("java.lang.Boolean".equals(name) || "Boolean".equals(name)) {
120            return Boolean.class;
121        } else if ("boolean".equals(name)) {
122            return boolean.class;
123        } else if ("java.lang.Integer".equals(name) || "Integer".equals(name)) {
124            return Integer.class;
125        } else if ("int".equals(name)) {
126            return int.class;
127        } else if ("java.lang.Long".equals(name) || "Long".equals(name)) {
128            return Long.class;
129        } else if ("long".equals(name)) {
130            return long.class;
131        } else if ("java.lang.Short".equals(name) || "Short".equals(name)) {
132            return Short.class;
133        } else if ("short".equals(name)) {
134            return short.class;
135        } else if ("java.lang.Byte".equals(name) || "Byte".equals(name)) {
136            return Byte.class;
137        } else if ("byte".equals(name)) {
138            return byte.class;
139        } else if ("java.lang.Float".equals(name) || "Float".equals(name)) {
140            return Float.class;
141        } else if ("float".equals(name)) {
142            return float.class;
143        } else if ("java.lang.Double".equals(name) || "Double".equals(name)) {
144            return Double.class;
145        } else if ("double".equals(name)) {
146            return double.class;
147        }
148
149        return null;
150    }
151
152}