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