01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.jetspeed.aggregator;
18:
19: import java.util.Enumeration;
20: import java.util.Hashtable;
21:
22: /**
23: * Maintains a context attributes for the current Thread
24: *
25: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
26: * @version $Id: $
27: */
28: public final class CurrentWorkerContext {
29: private static ThreadLocal currentWorkerContext = new ThreadLocal() {
30: protected synchronized Object initialValue() {
31: return new Hashtable();
32: }
33: };
34:
35: private static ThreadLocal parallelRenderingMode = new ThreadLocal() {
36: protected synchronized Object initialValue() {
37: return new boolean[] { false };
38: }
39: };
40:
41: private CurrentWorkerContext() {
42: }
43:
44: /**
45: * Returns an Enumeration containing the names of the attributes available to this Thread.
46: * This method returns an empty Enumeration if the thread has no attributes available to it.
47: */
48: public static Enumeration getAttributeNames() {
49: return ((Hashtable) currentWorkerContext.get()).keys();
50: }
51:
52: /**
53: * @return an attribute in the current Thread
54: * @param attrName Locale for this Thread
55: */
56: public static Object getAttribute(String name) {
57: return ((Hashtable) currentWorkerContext.get()).get(name);
58: }
59:
60: /**
61: * Stores an attribute in this Thread.
62: * <br>
63: * @param name - a String specifying the name of the attribute
64: * @param o - the Object to be stored
65: */
66: public static void setAttribute(String name, Object o) {
67: if (o != null) {
68: ((Hashtable) currentWorkerContext.get()).put(name, o);
69: } else {
70: removeAttribute(name);
71: }
72: }
73:
74: /**
75: * Removes an attribute from this Thread.
76: * <br>
77: * @param name - a String specifying the name of the attribute
78: */
79: public static void removeAttribute(String name) {
80: ((Hashtable) currentWorkerContext.get()).remove(name);
81: }
82:
83: /**
84: * Removes all attributes from this Thread.
85: */
86: public static void removeAllAttributes() {
87: ((Hashtable) currentWorkerContext.get()).clear();
88: }
89:
90: public static void setParallelRenderingMode(boolean parallelMode) {
91: ((boolean[]) parallelRenderingMode.get())[0] = parallelMode;
92: }
93:
94: public static boolean getParallelRenderingMode() {
95: return ((boolean[]) parallelRenderingMode.get())[0];
96: }
97: }
|