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