package org.jboss.mx.loading;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Comparator;
import java.io.StringWriter;
import java.io.PrintWriter;
import org.jboss.logging.Logger;
public class ClassLoadingTask
{
protected static Logger log = Logger.getLogger(ClassLoadingTask.class);
protected static Comparator taskComparator = new ThreadTaskComparator();
public static final int FOUND_CLASS_LOADER = 1;
public static final int NEXT_EVENT = 2;
public static final int WAIT_ON_EVENT = 3;
public static final int FINISHED = 4;
protected String classname;
protected Thread requestingThread;
protected RepositoryClassLoader requestingClassLoader;
protected Class loadedClass;
protected int loadOrder = Integer.MAX_VALUE;
protected int stopOrder = Integer.MAX_VALUE;
protected Throwable loadException;
protected int threadTaskCount;
protected int state;
protected boolean trace;
protected int numCCE;
static class ThreadTaskComparator implements Comparator
{
public int compare(Object o1, Object o2)
{
ThreadTask t1 = (ThreadTask) o1;
ThreadTask t2 = (ThreadTask) o2;
int compare = t1.order - t2.order;
if( compare == 0 )
{
compare = t1.ucl.getAddedOrder() - t2.ucl.getAddedOrder();
}
return compare;
}
}
class ThreadTask
{
RepositoryClassLoader ucl;
Thread t;
int order;
boolean releaseInNextTask;
ThreadTask(RepositoryClassLoader ucl, Thread t, int order,
boolean releaseInNextTask)
{
this.ucl = ucl;
this.t = t;
this.order = order;
this.releaseInNextTask = releaseInNextTask;
}
public String toString()
{
return "{t="+t+", ucl="+ucl+", name="+classname
+", requestingThread="+requestingThread
+", order="+order+", releaseInNextTask="+releaseInNextTask
+"}";
}
String getClassname()
{
return classname;
}
Class getLoadedClass()
{
return loadedClass;
}
ClassLoadingTask getLoadTask()
{
return ClassLoadingTask.this;
}
void run() throws ClassNotFoundException
{
Class theClass = null;
try
{
if( loadedClass == null )
{
theClass = ucl.loadClassLocally(classname, false);
setLoadedClass(theClass, order);
}
else if( trace )
{
log.trace("Already found class("+loadedClass+"), skipping loadClassLocally");
}
}
finally
{
; }
}
}
protected ClassLoadingTask(String classname, RepositoryClassLoader requestingClassLoader,
Thread requestingThread)
{
this(classname, requestingClassLoader, requestingThread, Integer.MAX_VALUE);
}
protected ClassLoadingTask(String classname, RepositoryClassLoader requestingClassLoader,
Thread requestingThread, int stopAt)
{
this.requestingThread = requestingThread;
this.requestingClassLoader = requestingClassLoader;
this.classname = classname;
this.stopOrder = stopAt;
this.trace = log.isTraceEnabled();
}
public String toString()
{
StringBuffer buffer = new StringBuffer(super.toString());
buffer.append('{');
buffer.append("classname: "+classname);
buffer.append(", requestingThread: "+requestingThread);
buffer.append(", requestingClassLoader: "+requestingClassLoader);
buffer.append(", loadedClass: "+loadedClass);
ClassToStringAction.toString(loadedClass, buffer);
buffer.append(", loadOrder: "+loadOrder);
buffer.append(", loadException: "+loadException);
buffer.append(", threadTaskCount: "+threadTaskCount);
buffer.append(", state: "+state);
buffer.append(", #CCE: "+numCCE);
buffer.append('}');
if( trace && loadException != null )
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
loadException.printStackTrace(pw);
buffer.append("loadException details:\n");
buffer.append(sw.toString());
}
return buffer.toString();
}
ThreadTask newThreadTask(RepositoryClassLoader ucl, Thread t, int order,
boolean reschedule, boolean releaseInNextTask)
{
if( reschedule == false )
threadTaskCount ++;
return new ThreadTask(ucl, t, order, releaseInNextTask);
}
synchronized void setLoadError(Throwable t)
{
this.threadTaskCount--;
if( trace )
log.trace("setLoadedError, error="+t);
loadException = t;
}
private synchronized void setLoadedClass(Class theClass, int order)
{
this.threadTaskCount --;
if( trace )
log.trace("setLoadedClass, theClass="+theClass+", order="+order);
if( this.loadedClass != null && order == loadOrder && theClass != null )
{
StringBuffer tmp = new StringBuffer("Duplicate class found: "+classname);
tmp.append('\n');
ProtectionDomain pd = this.loadedClass.getProtectionDomain();
CodeSource cs = pd != null ? pd.getCodeSource() : null;
tmp.append("Current CS: "+cs);
tmp.append('\n');
pd = theClass.getProtectionDomain();
cs = pd != null ? pd.getCodeSource() : null;
tmp.append("Duplicate CS: "+cs);
log.warn(tmp.toString());
}
if( theClass != null )
{
if( loadedClass == null || order <= loadOrder )
{
this.loadedClass = theClass;
this.loadOrder = order;
}
else
{
ProtectionDomain pd = this.loadedClass.getProtectionDomain();
CodeSource cs = pd != null ? pd.getCodeSource() : null;
ProtectionDomain pd2 = theClass.getProtectionDomain();
CodeSource cs2 = pd != null ? pd2.getCodeSource() : null;
log.debug("Ignoring source of: "+classname+" from CodeSource: "+cs2
+", due to order("+order+">="+loadOrder+"), "
+"accepted CodeSource: "+cs);
}
}
}
}