1 22 package org.jboss.iiop.rmi; 23 24 25 import java.lang.ref.SoftReference ; 26 27 import java.lang.reflect.Method ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.WeakHashMap ; 34 35 36 55 class WorkCacheManager 56 { 57 59 61 63 private static final org.jboss.logging.Logger logger = 64 org.jboss.logging.Logger.getLogger(WorkCacheManager.class); 65 66 68 73 WorkCacheManager(Class cls) 74 { 75 logger.debug("Class: " + cls.getName()); 76 try { 78 constructor = cls.getDeclaredConstructor(new Class []{Class .class}); 79 initializer = cls.getDeclaredMethod("doAnalyze", null); 80 } catch (NoSuchMethodException ex) { 81 throw new IllegalArgumentException ("Bad Class: " + ex.toString()); 82 } 83 84 workDone = new WeakHashMap (); 85 workInProgress = new HashMap (); 86 } 87 88 89 91 96 ContainerAnalysis getAnalysis(Class cls) 97 throws RMIIIOPViolationException 98 { 99 ContainerAnalysis ret; 100 101 synchronized (this) { 102 ret = lookupDone(cls); 103 if (ret != null) 104 return ret; 105 106 InProgress inProgress = (InProgress)workInProgress.get(cls); 108 if (inProgress != null) { 109 if (inProgress.thread == Thread.currentThread()) 110 return inProgress.analysis; 112 } 115 116 ret = createWorkInProgress(cls); 117 } 118 119 doTheWork(cls, ret); 121 122 synchronized (this) { 124 workInProgress.remove(cls); 125 workDone.put(cls, new SoftReference (ret)); 126 notifyAll(); 127 } 128 129 return ret; 130 } 131 132 134 138 Constructor constructor; 139 140 144 Method initializer; 145 146 150 Map workDone; 151 152 156 Map workInProgress; 157 158 161 private ContainerAnalysis lookupDone(Class cls) 162 { 163 SoftReference ref = (SoftReference )workDone.get(cls); 164 if (ref == null) 165 return null; 166 ContainerAnalysis ret = (ContainerAnalysis)ref.get(); 167 if (ret == null) 168 workDone.remove(cls); return ret; 170 } 171 172 175 private ContainerAnalysis createWorkInProgress(Class cls) 176 { 177 ContainerAnalysis analysis; 178 try { 179 analysis = (ContainerAnalysis)constructor.newInstance(new Object []{cls}); 180 } catch (InstantiationException ex) { 181 throw new RuntimeException (ex.toString()); 182 } catch (IllegalAccessException ex) { 183 throw new RuntimeException (ex.toString()); 184 } catch (InvocationTargetException ex) { 185 throw new RuntimeException (ex.toString()); 186 } 187 188 workInProgress.put(cls, new InProgress(analysis, Thread.currentThread())); 189 190 return analysis; 191 } 192 193 private void doTheWork(Class cls, ContainerAnalysis ret) 194 throws RMIIIOPViolationException 195 { 196 try { 197 initializer.invoke(ret, new Object []{}); 198 } catch (Throwable t) { 199 synchronized (this) { 200 workInProgress.remove(cls); 201 } 202 if (t instanceof InvocationTargetException ) t = ((InvocationTargetException )t).getTargetException(); 204 205 if (t instanceof RMIIIOPViolationException) 206 throw (RMIIIOPViolationException)t; 207 if (t instanceof RuntimeException ) 208 throw (RuntimeException )t; 209 if (t instanceof Error ) 210 throw (Error )t; 211 throw new RuntimeException (t.toString()); 212 } 213 } 214 215 218 private static class InProgress 219 { 220 ContainerAnalysis analysis; 221 Thread thread; 222 223 InProgress(ContainerAnalysis analysis, Thread thread) 224 { 225 this.analysis = analysis; 226 this.thread = thread; 227 } 228 } 229 } 230 231 | Popular Tags |