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.Semaphore; 34 35 39 public class RecursiveCCETests 40 { 41 private static Logger log = Logger.getLogger(RecursiveCCETests.class); 42 44 public RecursiveCCETests() 45 { 46 } 47 48 57 public void testRecursiveLoadMT() throws Exception 58 { 59 log.info("Begin testRecursiveLoadMT"); 60 UnifiedLoaderRepository3 repository = new UnifiedLoaderRepository3(); 61 Class thisClass = getClass(); 62 UnifiedClassLoader thisUCL = (UnifiedClassLoader) thisClass.getClassLoader(); 63 URL origURL = thisUCL.getOrigURL(); 64 log.info("Service origURL="+origURL); 65 URL j0 = new URL (origURL, "ha.jar"); 66 log.info("j0 = "+j0); 67 68 Semaphore s0 = new Semaphore(0); 69 MyUCL ucl0 = new MyUCL(j0, s0); 70 repository.addClassLoader(ucl0); 71 Semaphore s1 = new Semaphore(0); 72 MyUCL ucl1 = new MyUCL(origURL, s1); 73 repository.addClassLoader(ucl1); 74 String class0 = "org.jboss.test.classloader.circularity.support.HARMIServerImpl"; 75 MyThread t0 = new MyThread(ucl0, "testRecursiveLoadMT.T0", class0); 76 { 77 log.info("Starting T0"); 78 t0.start(); 79 log.info("Started T0, waiting on ucl="+System.identityHashCode(ucl0)); 80 s0.acquire(); 81 log.info("UCL0 notify received"); 82 } 83 84 String class1 = "org.jboss.test.classloader.circularity.support.HARMIServerImpl_Stub"; 85 MyThread t1 = new MyThread(ucl1, "testRecursiveLoadMT.T1", class1); 86 { 87 log.info("Starting T1"); 88 t1.start(); 89 log.info("Started T1, waiting on ucl="+System.identityHashCode(ucl1)); 90 s1.acquire(); 91 log.info("UCL1 notify received"); 92 } 93 94 t1.join(10000); 95 if( t1.loadedClass == null || t1.loadedClass.getName().equals(class1) == false ) 96 { 97 String msg = "Thread1 failed to load HARMIServerImpl_Stub, class="+t1.loadedClass; 98 log.error(msg, t1.loadEx); 99 throw new Exception (msg); 100 } 101 t0.join(5000); 102 if( t0.loadedClass == null || t0.loadedClass.getName().equals(class0) == false ) 103 { 104 String msg = "Thread0 failed to load HARMIServerImpl, class="+t0.loadedClass; 105 log.error(msg, t0.loadEx); 106 throw new Exception (msg); 107 } 108 log.info("End testRecursiveLoadMT"); 109 } 110 111 114 static class MyThread extends Thread 115 { 116 String className; 117 Class loadedClass; 118 Throwable loadEx; 119 UnifiedClassLoader3 ucl; 120 121 MyThread(UnifiedClassLoader3 ucl, String id, String className) 122 { 123 super(id); 124 this.className = className; 125 this.ucl = ucl; 126 } 127 public void run() 128 { 129 try 130 { 131 loadedClass = ucl.loadClass(className, false); 132 } 133 catch(Throwable t) 134 { 135 loadEx = t; 136 log.error("Failed to load: "+className, t); 137 } 138 } 139 } 140 141 public static class MyUCL extends UnifiedClassLoader3 142 { 143 private static final Logger log = Logger.getLogger(MyUCL.class); 144 Semaphore s; 145 boolean passedBarriers; 146 147 public MyUCL(URL url, Semaphore s) 148 { 149 super(url); 150 this.s = s; 151 } 152 153 155 public synchronized Class loadClass(String name, boolean resolve) 156 throws ClassNotFoundException 157 { 158 log.info("loadClass, name="+name); 159 boolean acquired = attempt(1); 160 if( acquired == false ) 161 throw new IllegalStateException ("Failed to acquire loadClass lock"); 162 log.info("Acquired loadClass lock"); 163 164 MyClassLoadingTask task = null; 165 try 166 { 167 Thread t = Thread.currentThread(); 168 if( loadLock.holds() == 1 ) 170 LoadMgr3.registerLoaderThread(this, t); 171 172 s.release(); 173 log.info("notifyAll, ucl="+System.identityHashCode(this)); 174 try 175 { 176 if( name.endsWith("HARMIServer") ) 177 { 178 t.sleep(5000); 179 log.info("Passed HARMIServer barrier"); 180 } 181 } 182 catch(InterruptedException e) 183 { 184 throw new IllegalStateException ("MyUCL failed to enter HARMIServer barrier"); 185 } 186 187 task = new MyClassLoadingTask(name, this, t); 189 192 UnifiedLoaderRepository3 ulr3 = (UnifiedLoaderRepository3) repository; 193 if( LoadMgr3.beginLoadTask(task, ulr3) == false ) 194 { 195 while( task.threadTaskCount() != 0 ) 196 { 197 try 198 { 199 LoadMgr3.nextTask(t, task, ulr3); 200 } 201 catch(InterruptedException e) 202 { 203 break; 205 } 206 } 207 } 208 } 209 finally 210 { 211 if( loadLock.holds() == 1 ) 213 LoadMgr3.endLoadTask(task); 214 this.release(); 216 this.notifyAll(); 217 } 218 219 if( task.loadedClass() == null ) 220 { 221 if( task.loadException() instanceof ClassNotFoundException ) 222 throw (ClassNotFoundException ) task.loadException(); 223 else if( task.loadException() != null ) 224 { 225 log.info("Unexpected error during load of:"+name, task.loadException()); 226 String msg = "Unexpected error during load of: "+name 227 + ", msg="+task.loadException().getMessage(); 228 throw new ClassNotFoundException (msg); 229 } 230 else 232 throw new IllegalStateException ("ClassLoadingTask.loadedTask is null, name: "+name); 233 } 234 235 return task.loadedClass(); 236 } 237 } 238 } 239 | Popular Tags |