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;
023
024import org.apache.camel.CamelContext;
025
026public class CamelObjectInputStream extends ObjectInputStream {
027    private final ClassLoader classLoader;
028    
029    public CamelObjectInputStream(InputStream in, CamelContext context) throws IOException {
030        super(in);
031        if (context != null) {
032            this.classLoader = context.getApplicationContextClassLoader();
033        } else {
034            this.classLoader = null;
035        }
036    }
037    
038    protected Class<?> resolveClass(ObjectStreamClass desc) throws ClassNotFoundException, IOException {
039        if (classLoader != null) {
040            return Class.forName(desc.getName(), false, classLoader);
041        } else {
042            // If the application classloader is not set we just fallback to use old behaivor
043            return super.resolveClass(desc);
044        }
045    }
046}