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