1 22 package org.jboss.test.classloader.circularity.test; 23 24 import java.net.URL ; 25 26 import org.jboss.logging.Logger; 27 28 import org.jboss.mx.loading.UnifiedClassLoader; 29 import org.jboss.mx.loading.UnifiedClassLoader3; 30 import org.jboss.mx.loading.UnifiedLoaderRepository3; 31 import org.jboss.mx.loading.ClassLoadingTask; 32 import org.jboss.mx.loading.LoadMgr3; 33 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 34 35 39 public class DeadlockTests3 40 { 41 private static Logger log = Logger.getLogger(DeadlockTests3.class); 42 private CyclicBarrier t0Barrier = new CyclicBarrier(2); 43 private CyclicBarrier t1Barrier = new CyclicBarrier(2); 44 45 public DeadlockTests3() 46 { 47 } 48 49 62 public void testDeadlockCase1() throws Exception 63 { 64 log.info("Begin testDeadlockCase1"); 65 UnifiedLoaderRepository3 repository = new UnifiedLoaderRepository3(); 66 Class thisClass = getClass(); 67 UnifiedClassLoader thisUCL = (UnifiedClassLoader) thisClass.getClassLoader(); 68 URL origURL = thisUCL.getOrigURL(); 69 log.info("Service origURL="+origURL); 70 URL j0 = new URL (origURL, "dl10.jar"); 71 log.info("j0 = "+j0); 72 URL j1 = new URL (origURL, "dl11.jar"); 73 log.info("j1 = "+j1); 74 75 UnifiedClassLoader3 ucl0 = new UnifiedClassLoader3(j0); 76 repository.addClassLoader(ucl0); 77 UCL1 ucl1 = new UCL1(j1, t0Barrier, t1Barrier); 78 repository.addClassLoader(ucl1); 79 80 T1 t1 = new T1(ucl1); 81 t1.start(); 82 t0Barrier.barrier(); 83 log.info("Passed t0Barrier"); 84 85 T0 t0 = new T0(ucl0); 86 t0.start(); 87 t1Barrier.barrier(); 88 log.info("Passed t1Barrier"); 89 90 t1.join(5000); 91 if( t1.loadedClass == null ) 92 throw new Exception ("Thread1 failed to load Base"); 93 t0.join(5000); 94 if( t0.loadedClass == null ) 95 throw new Exception ("Thread0 failed to load Derived"); 96 log.info("End testDeadlockCase1"); 97 } 98 99 102 static class T0 extends Thread 103 { 104 Class loadedClass; 105 Throwable loadEx; 106 UnifiedClassLoader3 ucl0; 107 T0(UnifiedClassLoader3 ucl0) 108 { 109 super("Thread0:UCL0"); 110 this.ucl0 = ucl0; 111 } 112 public void run() 113 { 114 try 115 { 116 loadedClass = ucl0.loadClass("org.jboss.test.classloader.circularity.support.pkg0.Derived"); 117 } 118 catch(Throwable t) 119 { 120 loadEx = t; 121 log.error("T0 failed to load Derived", t); 122 } 123 } 124 } 125 127 static class T1 extends Thread 128 { 129 Class loadedClass; 130 Throwable loadEx; 131 UCL1 ucl1; 132 133 T1(UCL1 ucl1) 134 { 135 super("Thread1:UCL1"); 136 this.ucl1 = ucl1; 137 } 138 public void run() 139 { 140 try 141 { 142 loadedClass = ucl1.loadClass("org.jboss.test.classloader.circularity.support.Base"); 143 } 144 catch(Throwable t) 145 { 146 loadEx = t; 147 log.error("T1 failed to load Base", t); 148 } 149 } 150 } 151 152 static class MyClassLoadingTask extends ClassLoadingTask 153 { 154 MyClassLoadingTask(String classname, UnifiedClassLoader3 requestingClassLoader, 155 Thread requestingThread) 156 { 157 super(classname, requestingClassLoader, requestingThread); 158 } 159 int threadTaskCount() 160 { 161 return threadTaskCount; 162 } 163 int state() 164 { 165 return state; 166 } 167 Class loadedClass() 168 { 169 return loadedClass; 170 } 171 Throwable loadException() 172 { 173 return loadException; 174 } 175 } 176 177 public static class UCL1 extends UnifiedClassLoader3 178 { 179 private static final Logger log = Logger.getLogger(UCL1.class); 180 CyclicBarrier t0Barrier; 181 CyclicBarrier t1Barrier; 182 boolean passedBarriers; 183 184 public UCL1(URL url, CyclicBarrier t0Barrier, CyclicBarrier t1Barrier) 185 { 186 super(url); 187 this.t0Barrier = t0Barrier; 188 this.t1Barrier = t1Barrier; 189 } 190 191 193 public synchronized Class loadClass(String name, boolean resolve) 194 throws ClassNotFoundException 195 { 196 log.info("loadClass, name="+name); 197 boolean acquired = attempt(1); 198 if( acquired == false ) 199 throw new IllegalStateException ("Failed to acquire loadClass lock"); 200 log.info("Acquired loadClass lock"); 201 202 MyClassLoadingTask task = null; 203 try 204 { 205 Thread t = Thread.currentThread(); 206 if( loadLock.holds() == 1 ) 208 LoadMgr3.registerLoaderThread(this, t); 209 210 try 212 { 213 if( passedBarriers == false ) 214 t0Barrier.barrier(); 215 } 216 catch(InterruptedException e) 217 { 218 throw new IllegalStateException ("UCL1 failed to enter t0Barrier"); 219 } 220 log.info("Passed t0Barrier"); 221 try 222 { 223 if( passedBarriers == false ) 224 t1Barrier.barrier(); 225 } 226 catch(InterruptedException e) 227 { 228 throw new IllegalStateException ("UCL1 failed to enter t0Barrier"); 229 } 230 log.info("Passed t1Barrier"); 231 passedBarriers = true; 232 233 task = new MyClassLoadingTask(name, this, t); 235 238 UnifiedLoaderRepository3 ulr3 = (UnifiedLoaderRepository3) repository; 239 if( LoadMgr3.beginLoadTask(task, ulr3) == false ) 240 { 241 while( task.threadTaskCount() != 0 ) 242 { 243 try 244 { 245 LoadMgr3.nextTask(t, task, ulr3); 246 } 247 catch(InterruptedException e) 248 { 249 break; 251 } 252 } 253 } 254 } 255 finally 256 { 257 if( loadLock.holds() == 1 ) 259 LoadMgr3.endLoadTask(task); 260 this.release(); 262 this.notifyAll(); 263 } 264 265 if( task.loadedClass() == null ) 266 { 267 if( task.loadException() instanceof ClassNotFoundException ) 268 throw (ClassNotFoundException ) task.loadException(); 269 else if( task.loadException() != null ) 270 { 271 log.info("Unexpected error during load of:"+name, task.loadException()); 272 String msg = "Unexpected error during load of: "+name 273 + ", msg="+task.loadException().getMessage(); 274 throw new ClassNotFoundException (msg); 275 } 276 else 278 throw new IllegalStateException ("ClassLoadingTask.loadedTask is null, name: "+name); 279 } 280 281 return task.loadedClass(); 282 } 283 } 284 } 285 | Popular Tags |