1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.tomcat.util.threads;
19
20 import java.util.Hashtable;
21
22 /** Special thread that allows storing of attributes and notes.
23 * A guard is used to prevent untrusted code from accessing the
24 * attributes.
25 *
26 * This avoids hash lookups and provide something very similar
27 * with ThreadLocal ( but compatible with JDK1.1 and faster on
28 * JDK < 1.4 ).
29 *
30 * The main use is to store 'state' for monitoring ( like "processing
31 * request 'GET /' ").
32 */
33 public class ThreadWithAttributes extends Thread {
34
35 private Object control;
36 public static int MAX_NOTES=16;
37 private Object notes[]=new Object[MAX_NOTES];
38 private Hashtable attributes=new Hashtable();
39 private String currentStage;
40 private Object param;
41
42 private Object thData[];
43
44 public ThreadWithAttributes(Object control, Runnable r) {
45 super(r);
46 this.control=control;
47 }
48
49 public final Object[] getThreadData(Object control ) {
50 return thData;
51 }
52
53 public final void setThreadData(Object control, Object thData[] ) {
54 this.thData=thData;
55 }
56
57 /** Notes - for attributes that need fast access ( array )
58 * The application is responsible for id management
59 */
60 public final void setNote( Object control, int id, Object value ) {
61 if( this.control != control ) return;
62 notes[id]=value;
63 }
64
65 /** Information about the curent performed operation
66 */
67 public final String getCurrentStage(Object control) {
68 if( this.control != control ) return null;
69 return currentStage;
70 }
71
72 /** Information about the current request ( or the main object
73 * we are processing )
74 */
75 public final Object getParam(Object control) {
76 if( this.control != control ) return null;
77 return param;
78 }
79
80 public final void setCurrentStage(Object control, String currentStage) {
81 if( this.control != control ) return;
82 this.currentStage = currentStage;
83 }
84
85 public final void setParam( Object control, Object param ) {
86 if( this.control != control ) return;
87 this.param=param;
88 }
89
90 public final Object getNote(Object control, int id ) {
91 if( this.control != control ) return null;
92 return notes[id];
93 }
94
95 /** Generic attributes. You'll need a hashtable lookup -
96 * you can use notes for array access.
97 */
98 public final Hashtable getAttributes(Object control) {
99 return attributes;
100 }
101 }