1 22 package org.jboss.invocation; 23 24 import java.io.InputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectStreamClass ; 28 import java.lang.reflect.Proxy ; 29 import org.jboss.util.collection.WeakValueHashMap; 30 31 import org.jboss.logging.Logger; 32 33 41 public class MarshalledValueInputStream 42 extends ObjectInputStream 43 { 44 private static Logger log = Logger.getLogger(MarshalledValueInputStream.class); 45 46 private static WeakValueHashMap classCache; 47 48 53 public static void useClassCache(boolean flag) 54 { 55 if( flag == true ) 56 classCache = new WeakValueHashMap(); 57 else 58 classCache = null; 59 } 60 61 64 public static void flushClassCache() 65 { 66 classCache.clear(); 67 } 68 69 72 public MarshalledValueInputStream(InputStream is) throws IOException 73 { 74 super(is); 75 } 76 77 82 protected Class resolveClass(ObjectStreamClass v) 83 throws IOException , ClassNotFoundException 84 { 85 String className = v.getName(); 86 Class resolvedClass = null; 87 if( classCache != null ) 89 { 90 synchronized( classCache ) 91 { 92 resolvedClass = (Class ) classCache.get(className); 93 } 94 } 95 96 if( resolvedClass == null ) 97 { 98 ClassLoader loader = SecurityActions.getContextClassLoader(); 99 try 100 { 101 resolvedClass = loader.loadClass(className); 102 } 103 catch(ClassNotFoundException e) 104 { 105 109 resolvedClass = super.resolveClass(v); 110 } 111 if( classCache != null ) 112 { 113 synchronized( classCache ) 114 { 115 classCache.put(className, resolvedClass); 116 } 117 } 118 } 119 return resolvedClass; 120 } 121 122 protected Class resolveProxyClass(String [] interfaces) 123 throws IOException , ClassNotFoundException 124 { 125 if( log.isTraceEnabled() ) 126 { 127 StringBuffer tmp = new StringBuffer ("["); 128 for(int i = 0; i < interfaces.length; i ++) 129 { 130 if( i > 0 ) 131 tmp.append(','); 132 tmp.append(interfaces[i]); 133 } 134 tmp.append(']'); 135 log.trace("resolveProxyClass called, ifaces="+tmp.toString()); 136 } 137 138 ClassLoader loader = null; 140 Class [] ifaceClasses = new Class [interfaces.length]; 141 for (int i = 0; i < interfaces.length; i++) 142 { 143 Class iface = null; 144 String className = interfaces[i]; 145 if( classCache != null ) 147 { 148 synchronized( classCache ) 149 { 150 iface = (Class ) classCache.get(className); 151 } 152 } 153 154 if( iface == null ) 156 { 157 if( loader == null ) 158 loader = Thread.currentThread().getContextClassLoader(); 159 iface = loader.loadClass(className); 160 if( classCache != null ) 161 { 162 synchronized( classCache ) 163 { 164 classCache.put(className, iface); 165 } 166 } 167 } 168 ifaceClasses[i] = iface; 169 } 170 171 return Proxy.getProxyClass(loader, ifaceClasses); 172 } 173 } 174 | Popular Tags |