001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.tomcat.util.threads;
018:
019: import java.util.Hashtable;
020:
021: /** Special thread that allows storing of attributes and notes.
022: * A guard is used to prevent untrusted code from accessing the
023: * attributes.
024: *
025: * This avoids hash lookups and provide something very similar
026: * with ThreadLocal ( but compatible with JDK1.1 and faster on
027: * JDK < 1.4 ).
028: *
029: * The main use is to store 'state' for monitoring ( like "processing
030: * request 'GET /' ").
031: */
032: public class ThreadWithAttributes extends Thread {
033:
034: private Object control;
035: public static int MAX_NOTES = 16;
036: private Object notes[] = new Object[MAX_NOTES];
037: private Hashtable attributes = new Hashtable();
038: private String currentStage;
039: private Object param;
040:
041: private Object thData[];
042:
043: public ThreadWithAttributes(Object control, Runnable r) {
044: super (r);
045: this .control = control;
046: }
047:
048: public final Object[] getThreadData(Object control) {
049: return thData;
050: }
051:
052: public final void setThreadData(Object control, Object thData[]) {
053: this .thData = thData;
054: }
055:
056: /** Notes - for attributes that need fast access ( array )
057: * The application is responsible for id management
058: */
059: public final void setNote(Object control, int id, Object value) {
060: if (this .control != control)
061: return;
062: notes[id] = value;
063: }
064:
065: /** Information about the curent performed operation
066: */
067: public final String getCurrentStage(Object control) {
068: if (this .control != control)
069: return null;
070: return currentStage;
071: }
072:
073: /** Information about the current request ( or the main object
074: * we are processing )
075: */
076: public final Object getParam(Object control) {
077: if (this .control != control)
078: return null;
079: return param;
080: }
081:
082: public final void setCurrentStage(Object control,
083: String currentStage) {
084: if (this .control != control)
085: return;
086: this .currentStage = currentStage;
087: }
088:
089: public final void setParam(Object control, Object param) {
090: if (this .control != control)
091: return;
092: this .param = param;
093: }
094:
095: public final Object getNote(Object control, int id) {
096: if (this .control != control)
097: return null;
098: return notes[id];
099: }
100:
101: /** Generic attributes. You'll need a hashtable lookup -
102: * you can use notes for array access.
103: */
104: public final Hashtable getAttributes(Object control) {
105: return attributes;
106: }
107: }
|