001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.loading;
023:
024: import java.security.CodeSource;
025: import java.security.ProtectionDomain;
026: import java.util.Comparator;
027: import java.io.StringWriter;
028: import java.io.PrintWriter;
029: import org.jboss.logging.Logger;
030:
031: /** An encapsulation of a UCL3.loadClass task.
032: * @author Scott.Stark@jboss.org
033: * @version $Revision: 63208 $
034: */
035: public class ClassLoadingTask {
036: protected static Logger log = Logger
037: .getLogger(ClassLoadingTask.class);
038: protected static Comparator taskComparator = new ThreadTaskComparator();
039:
040: public static final int FOUND_CLASS_LOADER = 1;
041: public static final int NEXT_EVENT = 2;
042: public static final int WAIT_ON_EVENT = 3;
043: public static final int FINISHED = 4;
044:
045: protected String classname;
046: protected Thread requestingThread;
047: protected RepositoryClassLoader requestingClassLoader;
048: protected Class loadedClass;
049: protected int loadOrder = Integer.MAX_VALUE;
050: protected int stopOrder = Integer.MAX_VALUE;
051: protected Throwable loadException;
052: /** The number of ThreadTasks remaining */
053: protected int threadTaskCount;
054: /** The state of the requestingThread */
055: protected int state;
056: /** The Logger trace level flag */
057: protected boolean trace;
058:
059: protected int numCCE;
060:
061: /** Compare ThreadTask first based on their order ivar, and then the
062: * relative ordering with which their UCLs were added to the ULR.
063: */
064: static class ThreadTaskComparator implements Comparator {
065: public int compare(Object o1, Object o2) {
066: ThreadTask t1 = (ThreadTask) o1;
067: ThreadTask t2 = (ThreadTask) o2;
068: int compare = t1.order - t2.order;
069: if (compare == 0) {
070: compare = t1.ucl.getAddedOrder()
071: - t2.ucl.getAddedOrder();
072: }
073: return compare;
074: }
075: }
076:
077: /** An ecapsulation of a <Thread, UCL3> task used when requestingClassLoader
078: * needs to ask another UCL3 to perform the class loading.
079: */
080: class ThreadTask {
081: /** The class loader for the classname package */
082: RepositoryClassLoader ucl;
083: /** The thread that owns the ucl monitor */
084: Thread t;
085: /** The relative order of the task. If o0 < o1 then the class loaded
086: by task o0 is preferred to o1.
087: */
088: int order;
089: boolean releaseInNextTask;
090:
091: ThreadTask(RepositoryClassLoader ucl, Thread t, int order,
092: boolean releaseInNextTask) {
093: this .ucl = ucl;
094: this .t = t;
095: this .order = order;
096: this .releaseInNextTask = releaseInNextTask;
097: }
098:
099: public String toString() {
100: return "{t=" + t + ", ucl=" + ucl + ", name=" + classname
101: + ", requestingThread=" + requestingThread
102: + ", order=" + order + ", releaseInNextTask="
103: + releaseInNextTask + "}";
104: }
105:
106: String getClassname() {
107: return classname;
108: }
109:
110: Class getLoadedClass() {
111: return loadedClass;
112: }
113:
114: ClassLoadingTask getLoadTask() {
115: return ClassLoadingTask.this ;
116: }
117:
118: void run() throws ClassNotFoundException {
119: Class theClass = null;
120: try {
121: if (loadedClass == null) {
122: theClass = ucl.loadClassLocally(classname, false);
123: setLoadedClass(theClass, order);
124: } else if (trace) {
125: log.trace("Already found class(" + loadedClass
126: + "), skipping loadClassLocally");
127: }
128: } finally {
129: ;//setLoadedClass(theClass, order);
130: }
131: }
132: }
133:
134: protected ClassLoadingTask(String classname,
135: RepositoryClassLoader requestingClassLoader,
136: Thread requestingThread) {
137: this (classname, requestingClassLoader, requestingThread,
138: Integer.MAX_VALUE);
139: }
140:
141: protected ClassLoadingTask(String classname,
142: RepositoryClassLoader requestingClassLoader,
143: Thread requestingThread, int stopAt) {
144: this .requestingThread = requestingThread;
145: this .requestingClassLoader = requestingClassLoader;
146: this .classname = classname;
147: this .stopOrder = stopAt;
148: this .trace = log.isTraceEnabled();
149: }
150:
151: synchronized int incNumCCE() {
152: int cce = numCCE++;
153: return cce;
154: }
155:
156: public String toString() {
157: StringBuffer buffer = new StringBuffer(super .toString());
158: buffer.append('{');
159: buffer.append("classname: " + classname);
160: buffer.append(", requestingThread: " + requestingThread);
161: buffer.append(", requestingClassLoader: "
162: + requestingClassLoader);
163: buffer.append(", loadedClass: " + loadedClass);
164: ClassToStringAction.toString(loadedClass, buffer);
165: buffer.append(", loadOrder: " + loadOrder);
166: buffer.append(", loadException: " + loadException);
167: buffer.append(", threadTaskCount: " + threadTaskCount);
168: buffer.append(", state: " + state);
169: buffer.append(", #CCE: " + numCCE);
170: buffer.append('}');
171: if (trace && loadException != null) {
172: StringWriter sw = new StringWriter();
173: PrintWriter pw = new PrintWriter(sw);
174: loadException.printStackTrace(pw);
175: buffer.append("loadException details:\n");
176: buffer.append(sw.toString());
177: }
178: return buffer.toString();
179: }
180:
181: ThreadTask newThreadTask(RepositoryClassLoader ucl, Thread t,
182: int order, boolean reschedule, boolean releaseInNextTask) {
183: // Only update the threadTaskCount if this is not a reschedule
184: if (reschedule == false)
185: threadTaskCount++;
186: return new ThreadTask(ucl, t, order, releaseInNextTask);
187: }
188:
189: synchronized void setLoadError(Throwable t) {
190: this .threadTaskCount--;
191: if (trace)
192: log.trace("setLoadedError, error=" + t);
193: loadException = t;
194: }
195:
196: /** This is called from run on success or failure to mark the end
197: * of the load attempt. This must decrement the threadTaskCount or
198: * the ClassLoadingTask will never complete.
199: */
200: private synchronized void setLoadedClass(Class theClass, int order) {
201: this .threadTaskCount--;
202: if (trace)
203: log.trace("setLoadedClass, theClass=" + theClass
204: + ", order=" + order);
205:
206: // Warn about duplicate classes
207: if (this .loadedClass != null && order == loadOrder
208: && theClass != null) {
209: StringBuffer tmp = new StringBuffer(
210: "Duplicate class found: " + classname);
211: tmp.append('\n');
212: ProtectionDomain pd = this .loadedClass
213: .getProtectionDomain();
214: CodeSource cs = pd != null ? pd.getCodeSource() : null;
215: tmp.append("Current CS: " + cs);
216: tmp.append('\n');
217: pd = theClass.getProtectionDomain();
218: cs = pd != null ? pd.getCodeSource() : null;
219: tmp.append("Duplicate CS: " + cs);
220: log.warn(tmp.toString());
221: }
222:
223: // Accept the lowest order source of the class
224: if (theClass != null) {
225: if (loadedClass == null || order <= loadOrder) {
226: this .loadedClass = theClass;
227: this .loadOrder = order;
228: } else {
229: ProtectionDomain pd = this .loadedClass
230: .getProtectionDomain();
231: CodeSource cs = pd != null ? pd.getCodeSource() : null;
232: ProtectionDomain pd2 = theClass.getProtectionDomain();
233: CodeSource cs2 = pd != null ? pd2.getCodeSource()
234: : null;
235: log.debug("Ignoring source of: " + classname
236: + " from CodeSource: " + cs2
237: + ", due to order(" + order + ">=" + loadOrder
238: + "), " + "accepted CodeSource: " + cs);
239: }
240: }
241: }
242: }
|