1 22 package org.jboss.invocation.pooled.interfaces; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectStreamClass ; 28 import java.lang.ref.WeakReference ; 29 import java.lang.reflect.Method ; 30 import java.lang.reflect.Proxy ; 31 32 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 33 34 43 public class OptimizedObjectInputStream 44 extends ObjectInputStream 45 { 46 47 private static ConcurrentReaderHashMap classCache; 48 private static ConcurrentReaderHashMap objectStreamClassCache; 49 private static Method lookupStreamClass = null; 50 51 static 52 { 53 useClassCache(true); 54 try 55 { 56 lookupStreamClass = ObjectStreamClass .class.getDeclaredMethod("lookup", new Class []{Class .class, boolean.class}); 57 lookupStreamClass.setAccessible(true); 58 } 59 catch (Exception ex) 60 { 61 ex.printStackTrace(); 62 } 63 } 64 65 70 public static void useClassCache(boolean flag) 71 { 72 if (flag == true) 73 { 74 classCache = new ConcurrentReaderHashMap(); 75 objectStreamClassCache = new ConcurrentReaderHashMap(); 76 } 77 else 78 { 79 classCache = null; 80 objectStreamClassCache = null; 81 } 82 } 83 84 87 public static void flushClassCache() 88 { 89 classCache.clear(); 90 objectStreamClassCache.clear(); 91 } 92 93 private static Class forName(String className) throws ClassNotFoundException 94 { 95 Class clazz = null; 96 if (classCache != null) 97 { 98 WeakReference ref = (WeakReference ) classCache.get(className); 99 if (ref != null) clazz = (Class ) ref.get(); 100 if (clazz == null) 101 { 102 if (ref != null) classCache.remove(className); 103 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 104 try 105 { 106 clazz = loader.loadClass(className); 107 } 108 catch (ClassNotFoundException e) 109 { 110 114 clazz = Class.forName(className, false, loader); 115 } 116 classCache.put(className, new WeakReference (clazz)); 117 } 118 } 119 else 120 { 121 clazz = Thread.currentThread().getContextClassLoader().loadClass(className); 122 } 123 return clazz; 124 } 125 126 129 public OptimizedObjectInputStream(InputStream is) throws IOException 130 { 131 super(is); 132 } 133 134 protected static ObjectStreamClass lookup(Class clazz) 135 { 136 Object [] args = {clazz, Boolean.TRUE}; 137 try 138 { 139 return (ObjectStreamClass ) lookupStreamClass.invoke(null, args); 140 } 141 catch (Exception ex) 142 { 143 ex.printStackTrace(); 144 } 145 return null; 146 } 147 148 protected ObjectStreamClass readClassDescriptor() 149 throws IOException , ClassNotFoundException 150 { 151 if (CompatibilityVersion.pooledInvokerLegacy) 152 { 153 String className = readUTF(); 154 ObjectStreamClass osc = null; 155 if (objectStreamClassCache != null) 156 { 157 osc = (ObjectStreamClass ) objectStreamClassCache.get(className); 158 } 159 if (osc == null) 160 { 161 Class clazz = forName(className); 162 osc = ObjectStreamClass.lookup(clazz); 163 if (osc == null) osc = lookup(clazz); 164 if (osc == null) throw new IOException ("Unable to readClassDescriptor for class " + className); 165 if (objectStreamClassCache != null) objectStreamClassCache.put(className, osc); 166 } 167 return osc; 168 } 169 else 170 { 171 return super.readClassDescriptor(); 172 } 173 } 174 175 180 protected Class resolveClass(ObjectStreamClass v) 181 throws IOException , ClassNotFoundException 182 { 183 String className = v.getName(); 184 return forName(className); 185 } 186 187 protected Class resolveProxyClass(String [] interfaces) 188 throws IOException , ClassNotFoundException 189 { 190 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 192 Class [] ifaceClasses = new Class [interfaces.length]; 193 for (int i = 0; i < interfaces.length; i++) 194 { 195 String className = interfaces[i]; 196 Class iface = forName(className); 197 ifaceClasses[i] = iface; 198 } 199 200 return Proxy.getProxyClass(loader, ifaceClasses); 201 } 202 } 203 | Popular Tags |