View Javadoc

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  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  {
30      private static ThreadLocal currentWorkerContext = 
31          new ThreadLocal() {
32              protected synchronized Object initialValue() {
33                  return new Hashtable();
34              }
35          };
36      
37      private static ThreadLocal parallelRenderingMode =
38          new ThreadLocal() {
39              protected synchronized Object initialValue() {
40                  return new boolean [] { false };
41              }
42          };
43      
44      private CurrentWorkerContext()
45      {
46      }
47  
48      /***
49       * Returns an Enumeration containing the names of the attributes available to this Thread. 
50       * This method returns an empty Enumeration  if the thread has no attributes available to it.
51       */
52      public static Enumeration getAttributeNames()
53      {
54          return ((Hashtable) currentWorkerContext.get()).keys();
55      }
56  
57      /*** 
58       * @return an attribute in the current Thread
59       * @param attrName Locale for this Thread 
60       */
61      public static Object getAttribute(String name)
62      {
63          return ((Hashtable) currentWorkerContext.get()).get(name);
64      }
65  
66      /***
67       * Stores an attribute in this Thread.
68       * <br>
69       * @param name - a String specifying the name of the attribute
70       * @param o - the Object to be stored
71       */
72      public static void setAttribute(String name, Object o)
73      {
74          if (o != null) {
75              ((Hashtable) currentWorkerContext.get()).put(name, o);
76          } else {
77              removeAttribute(name);
78          }
79      }
80  
81      /***
82       * Removes an attribute from this Thread.
83       * <br>
84       * @param name - a String specifying the name of the attribute
85       */
86      public static void removeAttribute(String name)
87      {
88          ((Hashtable) currentWorkerContext.get()).remove(name);
89      }
90  
91      /***
92       * Removes all attributes from this Thread.
93       */
94      public static void removeAllAttributes()
95      {
96          ((Hashtable) currentWorkerContext.get()).clear();
97      }
98      
99      public static void setParallelRenderingMode(boolean parallelMode)
100     {
101         ((boolean []) parallelRenderingMode.get())[0] = parallelMode;
102     }
103 
104     public static boolean getParallelRenderingMode()
105     {
106         return ((boolean []) parallelRenderingMode.get())[0];
107     }
108 }