View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.util;
17  
18  import org.kuali.rice.krad.UserSession;
19  
20  import java.util.HashMap;
21  import java.util.LinkedList;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.concurrent.Callable;
25  
26  /**
27   * Holds all of our thread local variables and accessors for those
28   *
29   * @author Kuali Rice Team (rice.collab@kuali.org)
30   */
31  public final class GlobalVariables {
32  
33      private static ThreadLocal<LinkedList<GlobalVariables>> GLOBAL_VARIABLES_STACK = new ThreadLocal<LinkedList<GlobalVariables>>() {
34          protected LinkedList<GlobalVariables> initialValue() {
35              LinkedList<GlobalVariables> globalVariablesStack = new LinkedList<GlobalVariables>();
36              globalVariablesStack.add(new GlobalVariables());
37              return globalVariablesStack;
38          }
39      };
40  
41      private static GlobalVariables getCurrentGlobalVariables() {
42          return GLOBAL_VARIABLES_STACK.get().getLast();
43      }
44  
45      static GlobalVariables pushGlobalVariables() {
46          GlobalVariables vars = new GlobalVariables();
47          GLOBAL_VARIABLES_STACK.get().add(vars);
48          return vars;
49      }
50  
51      static GlobalVariables popGlobalVariables() {
52          return GLOBAL_VARIABLES_STACK.get().removeLast();
53      }
54  
55      static void reset() {
56          LinkedList<GlobalVariables> stack = GLOBAL_VARIABLES_STACK.get();
57          stack.clear();
58          stack.add(new GlobalVariables());
59      }
60  
61      private UserSession userSession = null;
62      private String hideSessionFromTestsMessage = null;
63      private MessageMap messageMap = new MessageMap();
64      private Map<String,Object> requestCache = new HashMap<String, Object>();
65  
66      private GlobalVariables() {}
67  
68      /**
69       * @return the UserSession that has been assigned to this thread of execution it is important that this not be called by
70       *         anything that lives outside
71       */
72      public static UserSession getUserSession() {
73          GlobalVariables vars = getCurrentGlobalVariables();
74          String message = vars.hideSessionFromTestsMessage;
75          if (message != null) {
76              throw new RuntimeException(message);
77          }
78          return vars.userSession;
79      }
80  
81      /**
82       * Sets an error message for tests that try to use the session without declaring it.
83       * This method should be use by only KualiTestBase, not by other test code and especially not by production code.
84       *
85       * @param message the detail to throw, or null to allow access to the session
86       */
87      public static void setHideSessionFromTestsMessage(String message) {
88          GlobalVariables vars = getCurrentGlobalVariables();
89          vars.hideSessionFromTestsMessage = message;
90      }
91  
92      /**
93       * sets the userSession object into the global variable for this thread
94       *
95       * @param userSession
96       */
97      public static void setUserSession(UserSession userSession) {
98          GlobalVariables vars = getCurrentGlobalVariables();
99          vars.userSession = userSession;
100     }
101 
102     public static MessageMap getMessageMap() {
103         GlobalVariables vars = getCurrentGlobalVariables();
104         return vars.messageMap;
105     }
106 
107     /**
108      * Merges a message map into the global variables error map
109      * @param messageMap
110      */
111     public static void mergeErrorMap(MessageMap messageMap) {
112         getMessageMap().getErrorMessages().putAll(messageMap.getErrorMessages());
113         getMessageMap().getWarningMessages().putAll(messageMap.getWarningMessages());
114         getMessageMap().getInfoMessages().putAll(messageMap.getInfoMessages());
115     }
116 
117     /**
118      * Sets a new (clean) MessageMap
119      *
120      * @param messageMap
121      */
122     public static void setMessageMap(MessageMap messageMap) {
123         GlobalVariables vars = getCurrentGlobalVariables();
124         vars.messageMap = messageMap;
125     }
126 
127     public static Object getRequestCache(String cacheName) {
128         GlobalVariables vars = getCurrentGlobalVariables();
129         return vars.requestCache.get(cacheName);
130     }
131 
132     public static void setRequestCache(String cacheName, Object cacheObject) {
133         GlobalVariables vars = getCurrentGlobalVariables();
134         vars.requestCache.put(cacheName, cacheObject);
135     }
136 
137     /**
138      * Clears out GlobalVariable objects with the exception of the UserSession
139      */
140     public static void clear() {
141         GlobalVariables vars = getCurrentGlobalVariables();
142         vars.messageMap = new MessageMap();
143         vars.requestCache = new HashMap<String,Object>();
144     }
145 
146     /**
147      * Pushes a new GlobalVariables object onto the ThreadLocal GlobalVariables stack, invokes the runnable,
148      * and pops the GlobalVariables off in a finally clause
149      * @param callable the code to run under a new set of GlobalVariables
150      */
151     public static <T> T doInNewGlobalVariables(Callable<T> callable) throws Exception {
152         try {
153             pushGlobalVariables();
154             return callable.call();
155         } finally {
156             popGlobalVariables();
157         }
158     }
159 }