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.camel.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/**
027 * This class is copied from the Apache ActiveMQ project.
028 */
029@SuppressWarnings("rawtypes")
030public class ClassLoadingAwareObjectInputStream extends ObjectInputStream {
031
032    private static final ClassLoader FALLBACK_CLASS_LOADER = ClassLoadingAwareObjectInputStream.class.getClassLoader();
033
034    /**
035     * Maps primitive type names to corresponding class objects.
036     */
037    private static final HashMap<String, Class> PRIM_CLASSES = new HashMap<>(8, 1.0F);
038
039    private final ClassLoader inLoader;
040
041    public ClassLoadingAwareObjectInputStream(InputStream in) throws IOException {
042        super(in);
043        inLoader = in.getClass().getClassLoader();
044    }
045
046    public ClassLoadingAwareObjectInputStream(ClassLoader classLoader, InputStream in) throws IOException {
047        super(in);
048        if (classLoader != null) {
049            inLoader = classLoader;
050        } else {
051            inLoader = in.getClass().getClassLoader();
052        }
053    }
054
055    @Override
056    protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
057        ClassLoader cl = Thread.currentThread().getContextClassLoader();
058        return load(classDesc.getName(), cl, inLoader);
059    }
060
061    @Override
062    protected Class<?> resolveProxyClass(String[] interfaces) throws IOException, ClassNotFoundException {
063        ClassLoader cl = Thread.currentThread().getContextClassLoader();
064        Class[] cinterfaces = new Class[interfaces.length];
065        for (int i = 0; i < interfaces.length; i++) {
066            cinterfaces[i] = load(interfaces[i], cl);
067        }
068
069        try {
070            return Proxy.getProxyClass(cl, cinterfaces);
071        } catch (IllegalArgumentException e) {
072            try {
073                return Proxy.getProxyClass(inLoader, cinterfaces);
074            } catch (IllegalArgumentException e1) {
075                // ignore
076            }
077            try {
078                return Proxy.getProxyClass(FALLBACK_CLASS_LOADER, cinterfaces);
079            } catch (IllegalArgumentException e2) {
080                // ignore
081            }
082
083            throw new ClassNotFoundException(null, e);
084        }
085    }
086
087    private Class<?> load(String className, ClassLoader... cl) throws ClassNotFoundException {
088        for (ClassLoader loader : cl) {
089            try {
090                return Class.forName(className, false, loader);
091            } catch (ClassNotFoundException e) {
092                // ignore
093            }
094        }
095        // fallback
096        final Class<?> clazz = PRIM_CLASSES.get(className);
097        if (clazz != null) {
098            return clazz;
099        } else {
100            return Class.forName(className, false, FALLBACK_CLASS_LOADER);
101        }
102    }
103
104    static {
105        PRIM_CLASSES.put("boolean", boolean.class);
106        PRIM_CLASSES.put("byte", byte.class);
107        PRIM_CLASSES.put("char", char.class);
108        PRIM_CLASSES.put("short", short.class);
109        PRIM_CLASSES.put("int", int.class);
110        PRIM_CLASSES.put("long", long.class);
111        PRIM_CLASSES.put("float", float.class);
112        PRIM_CLASSES.put("double", double.class);
113        PRIM_CLASSES.put("void", void.class);
114    }
115}