KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > threads > ThreadWithAttributes


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 JavaDoc;
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 JavaDoc {
34     
35     private Object JavaDoc control;
36     public static int MAX_NOTES=16;
37     private Object JavaDoc notes[]=new Object JavaDoc[MAX_NOTES];
38     private Hashtable JavaDoc attributes=new Hashtable JavaDoc();
39     private String JavaDoc currentStage;
40     private Object JavaDoc param;
41     
42     private Object JavaDoc thData[];
43
44     public ThreadWithAttributes(Object JavaDoc control, Runnable JavaDoc r) {
45         super(r);
46         this.control=control;
47     }
48     
49     public final Object JavaDoc[] getThreadData(Object JavaDoc control ) {
50         return thData;
51     }
52     
53     public final void setThreadData(Object JavaDoc control, Object JavaDoc 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 JavaDoc control, int id, Object JavaDoc 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 JavaDoc getCurrentStage(Object JavaDoc 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 JavaDoc getParam(Object JavaDoc control) {
76         if( this.control != control ) return null;
77         return param;
78     }
79
80     public final void setCurrentStage(Object JavaDoc control, String JavaDoc currentStage) {
81         if( this.control != control ) return;
82         this.currentStage = currentStage;
83     }
84
85     public final void setParam( Object JavaDoc control, Object JavaDoc param ) {
86         if( this.control != control ) return;
87         this.param=param;
88     }
89
90     public final Object JavaDoc getNote(Object JavaDoc 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 JavaDoc getAttributes(Object JavaDoc control) {
99         return attributes;
100     }
101 }
102
Popular Tags