1 22 package org.jboss.mx.loading; 23 24 import java.security.CodeSource ; 25 import java.security.ProtectionDomain ; 26 import java.util.Comparator ; 27 import java.io.StringWriter ; 28 import java.io.PrintWriter ; 29 30 import org.jboss.logging.Logger; 31 32 36 public class ClassLoadingTaskDCL 37 { 38 protected static Logger log = Logger.getLogger(ClassLoadingTaskDCL.class); 39 protected static Comparator taskComparator = new ThreadTaskComparator(); 40 41 public static final int FOUND_CLASS_LOADER = 1; 42 public static final int NEXT_EVENT = 2; 43 public static final int WAIT_ON_EVENT = 3; 44 public static final int FINISHED = 4; 45 46 protected String classname; 47 protected Thread requestingThread; 48 protected DomainClassLoaderUCLImpl requestingClassLoader; 49 protected Class loadedClass; 50 protected int loadOrder = Integer.MAX_VALUE; 51 protected int stopOrder = Integer.MAX_VALUE; 52 protected Throwable loadException; 53 54 protected int threadTaskCount; 55 56 protected int state; 57 58 protected boolean trace; 59 60 protected int numCCE; 61 62 65 static class ThreadTaskComparator implements Comparator 66 { 67 public int compare(Object o1, Object o2) 68 { 69 ThreadTask t1 = (ThreadTask) o1; 70 ThreadTask t2 = (ThreadTask) o2; 71 int compare = t1.order - t2.order; 72 78 return compare; 79 } 80 } 81 82 85 class ThreadTask 86 { 87 88 DomainClassLoaderUCLImpl ucl; 89 90 Thread t; 91 94 int order; 95 boolean releaseInNextTask; 96 97 ThreadTask(DomainClassLoaderUCLImpl ucl, Thread t, int order, 98 boolean releaseInNextTask) 99 { 100 this.ucl = ucl; 101 this.t = t; 102 this.order = order; 103 this.releaseInNextTask = releaseInNextTask; 104 } 105 106 public String toString() 107 { 108 return "{t="+t+", ucl="+ucl+", name="+classname 109 +", requestingThread="+requestingThread 110 +", order="+order+", releaseInNextTask="+releaseInNextTask 111 +"}"; 112 } 113 114 String getClassname() 115 { 116 return classname; 117 } 118 Class getLoadedClass() 119 { 120 return loadedClass; 121 } 122 ClassLoadingTaskDCL getLoadTask() 123 { 124 return ClassLoadingTaskDCL.this; 125 } 126 127 void run() throws ClassNotFoundException 128 { 129 Class theClass = null; 130 try 131 { 132 if( loadedClass == null ) 133 { 134 theClass = ucl.loadClassLocally(classname, false); 135 setLoadedClass(theClass, order); 136 } 137 else if( trace ) 138 { 139 log.trace("Already found class("+loadedClass+"), skipping loadClassLocally"); 140 } 141 } 142 finally 143 { 144 ; } 146 } 147 } 148 149 protected ClassLoadingTaskDCL(String classname, DomainClassLoaderUCLImpl requestingClassLoader, 150 Thread requestingThread) 151 { 152 this(classname, requestingClassLoader, requestingThread, Integer.MAX_VALUE); 153 } 154 155 protected ClassLoadingTaskDCL(String classname, DomainClassLoaderUCLImpl requestingClassLoader, 156 Thread requestingThread, int stopAt) 157 { 158 this.requestingThread = requestingThread; 159 this.requestingClassLoader = requestingClassLoader; 160 this.classname = classname; 161 this.stopOrder = stopAt; 162 this.trace = log.isTraceEnabled(); 163 } 164 165 public String toString() 166 { 167 StringBuffer buffer = new StringBuffer (super.toString()); 168 buffer.append('{'); 169 buffer.append("classname: "+classname); 170 buffer.append(", requestingThread: "+requestingThread); 171 buffer.append(", requestingClassLoader: "+requestingClassLoader); 172 buffer.append(", loadedClass: "+loadedClass); 173 ClassToStringAction.toString(loadedClass, buffer); 174 buffer.append(", loadOrder: "+loadOrder); 175 buffer.append(", loadException: "+loadException); 176 buffer.append(", threadTaskCount: "+threadTaskCount); 177 buffer.append(", state: "+state); 178 buffer.append(", #CCE: "+numCCE); 179 buffer.append('}'); 180 if( trace && loadException != null ) 181 { 182 StringWriter sw = new StringWriter (); 183 PrintWriter pw = new PrintWriter (sw); 184 loadException.printStackTrace(pw); 185 buffer.append("loadException details:\n"); 186 buffer.append(sw.toString()); 187 } 188 return buffer.toString(); 189 } 190 191 ThreadTask newThreadTask(DomainClassLoaderUCLImpl ucl, Thread t, int order, 192 boolean reschedule, boolean releaseInNextTask) 193 { 194 if( reschedule == false ) 196 threadTaskCount ++; 197 return new ThreadTask(ucl, t, order, releaseInNextTask); 198 } 199 200 synchronized void setLoadError(Throwable t) 201 { 202 this.threadTaskCount--; 203 if( trace ) 204 log.trace("setLoadedError, error="+t); 205 loadException = t; 206 } 207 208 209 213 private synchronized void setLoadedClass(Class theClass, int order) 214 { 215 this.threadTaskCount --; 216 if( trace ) 217 log.trace("setLoadedClass, theClass="+theClass+", order="+order); 218 219 if( this.loadedClass != null && order == loadOrder && theClass != null ) 221 { 222 StringBuffer tmp = new StringBuffer ("Duplicate class found: "+classname); 223 tmp.append('\n'); 224 ProtectionDomain pd = this.loadedClass.getProtectionDomain(); 225 CodeSource cs = pd != null ? pd.getCodeSource() : null; 226 tmp.append("Current CS: "+cs); 227 tmp.append('\n'); 228 pd = theClass.getProtectionDomain(); 229 cs = pd != null ? pd.getCodeSource() : null; 230 tmp.append("Duplicate CS: "+cs); 231 log.warn(tmp.toString()); 232 } 233 234 if( theClass != null ) 236 { 237 if( loadedClass == null || order <= loadOrder ) 238 { 239 this.loadedClass = theClass; 240 this.loadOrder = order; 241 } 242 else 243 { 244 ProtectionDomain pd = this.loadedClass.getProtectionDomain(); 245 CodeSource cs = pd != null ? pd.getCodeSource() : null; 246 ProtectionDomain pd2 = theClass.getProtectionDomain(); 247 CodeSource cs2 = pd != null ? pd2.getCodeSource() : null; 248 log.debug("Ignoring source of: "+classname+" from CodeSource: "+cs2 249 +", due to order("+order+">="+loadOrder+"), " 250 +"accepted CodeSource: "+cs); 251 } 252 } 253 } 254 } 255 | Popular Tags |