001: package com.sun.portal.util;
002:
003: import com.sun.portal.netlet.econnection.GWRunnable;
004: import com.sun.portal.util.TaskWrapper;
005: import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
006:
007: import java.util.HashMap;
008: import java.util.Map;
009:
010: /**
011: * author: Noble Paul
012: * Date: Jun 4, 2004, 12:38:48 PM
013: */
014: public class ThreadMonitorContext {
015: private static final int STARTED = 1;
016: public static final int WAITING_IN_QUEUE = 2;
017: public static final int RUNNING = 3;
018: public static final int WAITING_FOR_RESP = 4;
019:
020: public static final ThreadLocal threadLocal = new ThreadLocal();
021: private final Map attributeStore = new HashMap();
022: private Runnable task;
023: private boolean isTaskKnown = false;
024: private int state;
025: private long taskStartTime = -1;
026: private Thread this Thread;
027:
028: private static final Map threadMonitorContexts = new HashMap();
029:
030: public void setTask(Runnable runnable) {
031: this .task = runnable;
032: if (runnable == null)
033: return;
034: if (runnable instanceof TaskWrapper) {
035: TaskWrapper taskWrapper = (TaskWrapper) runnable;
036: if (taskWrapper.getTask() instanceof GWRunnable) {
037: isTaskKnown = true;
038: } else {
039: isTaskKnown = false;
040: }
041: }
042: }
043:
044: public Thread getThread() {
045: return this Thread;
046: }
047:
048: public static void startThread() {
049: ThreadMonitorContext context = new ThreadMonitorContext();
050: threadLocal.set(context);
051: synchronized (threadMonitorContexts) {
052: threadMonitorContexts.put(Thread.currentThread().getName(),
053: context);
054: }
055: context.state = STARTED;
056: context.this Thread = Thread.currentThread();
057: }
058:
059: public static void endThread() {
060: synchronized (threadMonitorContexts) {
061: threadMonitorContexts.remove(Thread.currentThread()
062: .getName());
063: }
064: threadLocal.set(null);
065:
066: }
067:
068: public static void waitingInQueue() {
069: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
070: .get();
071: if (context == null)
072: return;
073: context.state = WAITING_IN_QUEUE;
074: }
075:
076: public long getTaskStartTime() {
077: return taskStartTime;
078: }
079:
080: public static void newTask(Runnable task) {
081: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
082: .get();
083: if (context == null)
084: return;
085: context.setTask(task);
086: context.taskStartTime = System.currentTimeMillis();
087: context.state = RUNNING;
088: MonitoringSubsystem.handleEvent(SRAEvent.START_TASK, context);
089: }
090:
091: public static void taskEnds() {
092: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
093: .get();
094: if (context == null || context.state == WAITING_IN_QUEUE)
095: return;
096: context.state = WAITING_IN_QUEUE;
097: context.setTask(null);
098: context.taskStartTime = -1;
099: MonitoringSubsystem.handleEvent(SRAEvent.END_TASK, context);
100:
101: }
102:
103: public Runnable getTask() {
104: return task;
105: }
106:
107: public boolean isTaskKnown() {
108: return isTaskKnown;
109: }
110:
111: public int getState() {
112: return state;
113: }
114:
115: public static Map getThreadMonitorContexts() {
116: return new HashMap(threadMonitorContexts);
117: }
118:
119: public static ThreadMonitorContext getContext() {
120: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
121: .get();
122: return context;
123: }
124:
125: public static void storeAttribute(Object key, Object value) {
126: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
127: .get();
128: if (context == null)
129: return;
130: context.attributeStore.put(key, value);
131: }
132:
133: public static Object retrieveAttribute(Object key) {
134: ThreadMonitorContext context = (ThreadMonitorContext) threadLocal
135: .get();
136: if (context == null)
137: return null;
138: return context.attributeStore.get(key);
139: }
140:
141: }
|