View Javadoc

1   /**
2    * Copyright 2005-2011 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.Map;
22  
23  /**
24   * Holds all of our thread local variables and accessors for those
25   *
26   * @author Kuali Rice Team (rice.collab@kuali.org)
27   */
28  public final class GlobalVariables {
29  
30      private GlobalVariables() {
31          throw new UnsupportedOperationException("do not call");
32      }
33  
34      private static ThreadLocal<UserSession> userSessions = new ThreadLocal<UserSession>();
35      private static ThreadLocal<String> hideSessionFromTestsMessage = new ThreadLocal<String>();
36  
37      private static ThreadLocal<MessageMap> messageMaps = new ThreadLocal<MessageMap>()  {
38  		@Override
39  		protected MessageMap initialValue() {
40  			return new MessageMap();
41  		}
42  	};
43      
44      private static ThreadLocal<Map<String,Object>> requestCaches = new ThreadLocal<Map<String,Object>>() {
45      	@Override
46  		protected HashMap<String, Object> initialValue() {
47      		return new HashMap<String, Object>();
48      	}
49      };
50  
51      /**
52       * @return the UserSession that has been assigned to this thread of execution it is important that this not be called by
53       *         anything that lives outside
54       */
55      public static UserSession getUserSession() {
56          String message = hideSessionFromTestsMessage.get();
57          if (message != null) {
58              throw new RuntimeException(message);
59          }
60          return userSessions.get();
61      }
62  
63      /**
64       * Sets an error message for tests that try to use the session without declaring it.
65       * This method should be use by only KualiTestBase, not by other test code and especially not by production code.
66       *
67       * @param message the detail to throw, or null to allow access to the session
68       */
69      public static void setHideSessionFromTestsMessage(String message) {
70          hideSessionFromTestsMessage.set(message);
71      }
72  
73      /**
74       * sets the userSession object into the global variable for this thread
75       *
76       * @param userSession
77       */
78      public static void setUserSession(UserSession userSession) {
79          userSessions.set(userSession);
80      }
81      
82      public static MessageMap getMessageMap() {
83      	return messageMaps.get();
84      }
85  
86      /**
87       * Merges a message map into the global variables error map
88       * @param messageMap
89       */
90      public static void mergeErrorMap(MessageMap messageMap) {
91          getMessageMap().getErrorMessages().putAll(messageMap.getErrorMessages());
92          getMessageMap().getWarningMessages().putAll(messageMap.getWarningMessages());
93          getMessageMap().getInfoMessages().putAll(messageMap.getInfoMessages());
94      }
95      
96      /**
97       * Sets a new (clean) MessageMap
98       *
99       * @param messageMap
100      */
101     public static void setMessageMap(MessageMap messageMap) {
102     	messageMaps.set(messageMap);
103     }
104 
105     public static Object getRequestCache( String cacheName ) {
106     	return requestCaches.get().get(cacheName);
107     }
108 
109     public static void setRequestCache( String cacheName, Object cacheObject ) {
110     	requestCaches.get().put(cacheName, cacheObject);
111     }
112 
113     /**
114      * Clears out GlobalVariable objects with the exception of the UserSession
115      */
116     public static void clear() {
117         messageMaps.set(new MessageMap());
118         requestCaches.set(new HashMap<String,Object>() );
119     }
120 }