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