View Javadoc

1   /*
2    * Copyright 2005-2007 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.kns.util;
17  
18  import java.util.HashMap;
19  import java.util.LinkedList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.kuali.rice.kew.engine.RouteContext;
24  import org.kuali.rice.kns.UserSession;
25  import org.kuali.rice.kns.web.struts.form.KualiForm;
26  
27  /**
28   * This class will hold all of our thread local variables and accessors for those
29   *
30   *
31   */
32  public class GlobalVariables {
33  
34      private static ThreadLocal<UserSession> userSessions = new ThreadLocal<UserSession>();
35      private static ThreadLocal<String> hideSessionFromTestsMessage = new ThreadLocal<String>();
36      private static ThreadLocal<KualiForm> kualiForms = new ThreadLocal<KualiForm>();
37      
38      private static ThreadLocal<MessageMap> messageMaps = new ThreadLocal<MessageMap>()  {
39  		protected MessageMap initialValue() {
40  			return new MessageMap();
41  		}
42  	};
43  	
44      // todo: generic collections
45      private static ThreadLocal<MessageList> messageLists = new ThreadLocal<MessageList>() {
46  		protected MessageList initialValue() {
47  			return new MessageList();
48  		}
49  	};
50  	
51      private static ThreadLocal<HashMap> auditErrorMaps = new ThreadLocal<HashMap>() {
52      	protected HashMap initialValue() {
53      		return new HashMap();
54      	}
55      };
56      
57      private static ThreadLocal<Map<String,Object>> requestCaches = new ThreadLocal<Map<String,Object>>() {
58      	protected HashMap<String, Object> initialValue() {
59      		return new HashMap<String, Object>();
60      	}
61      };
62  
63      /**
64       * @return the UserSession that has been assigned to this thread of execution it is important that this not be called by
65       *         anything that lives outside
66       */
67      public static UserSession getUserSession() {
68          String message = hideSessionFromTestsMessage.get();
69          if (message != null) {
70              throw new RuntimeException(message);
71          }
72          return userSessions.get();
73      }
74  
75      /**
76       * Sets an error message for tests that try to use the session without declaring it.
77       * This method should be use by only KualiTestBase, not by other test code and especially not by production code.
78       *
79       * @param message the detail to throw, or null to allow access to the session
80       */
81      public static void setHideSessionFromTestsMessage(String message) {
82          hideSessionFromTestsMessage.set(message);
83      }
84  
85      /**
86       * sets the userSession object into the global variable for this thread
87       *
88       * @param userSession
89       */
90      public static void setUserSession(UserSession userSession) {
91          userSessions.set(userSession);
92      }
93  
94      /**
95       * @deprecated use {@link #getMessageMap()} instead.
96       * 
97       * @return ErrorMap containing error messages.
98       */
99      @Deprecated
100     public static ErrorMap getErrorMap() {
101         return new ErrorMap(getMessageMap());
102     }
103     
104     public static MessageMap getMessageMap() {
105     	return messageMaps.get();
106     }
107 
108     /**
109      * Merges a message map into the global variables error map
110      * @param messageMap
111      */
112     public static void mergeErrorMap(MessageMap messageMap) {
113         getMessageMap().getErrorMessages().putAll(messageMap.getErrorMessages());
114         getMessageMap().getWarningMessages().putAll(messageMap.getWarningMessages());
115         getMessageMap().getInfoMessages().putAll(messageMap.getInfoMessages());
116     }
117     
118     /**
119      * Sets a new (clean) MessageMap
120      *
121      * @param messageMap
122      */
123     public static void setMessageMap(MessageMap messageMap) {
124     	messageMaps.set(messageMap);
125     }
126 
127     /**
128      * Sets a new (clean) ErrorMap
129      *
130      * @deprecated use {@link #setMessageMap(MessageMap)} instead
131      *
132      * @param errorMap
133      */
134     @Deprecated
135     public static void setErrorMap(ErrorMap errorMap) {
136     	setMessageMap(errorMap);
137     }
138 
139     /**
140      * @return ArrayList containing messages.
141      */
142     public static MessageList getMessageList() {
143         return messageLists.get();
144     }
145 
146     /**
147      * Sets a new message list
148      *
149      * @param messageList
150      */
151     public static void setMessageList(MessageList messageList) {
152         messageLists.set(messageList);
153     }
154 
155     /**
156      * @return ArrayList containing audit error messages.
157      */
158     public static HashMap getAuditErrorMap() {
159         return auditErrorMaps.get();
160     }
161 
162     /**
163      * Sets a new (clean) AuditErrorList
164      *
165      * @param errorMap
166      */
167     public static void setAuditErrorMap(HashMap errorMap) {
168         auditErrorMaps.set(errorMap);
169     }
170 
171     /**
172      * @return KualiForm that has been assigned to this thread of execution.
173      */
174     public static KualiForm getKualiForm() {
175         return kualiForms.get();
176     }
177 
178     /**
179      * sets the kualiForm object into the global variable for this thread
180      *
181      * @param kualiForm
182      */
183     public static void setKualiForm(KualiForm kualiForm) {
184     	kualiForms.set(kualiForm);
185     }
186 
187     public static Object getRequestCache( String cacheName ) {
188     	return requestCaches.get().get(cacheName);
189     }
190 
191     public static void setRequestCache( String cacheName, Object cacheObject ) {
192     	requestCaches.get().put(cacheName, cacheObject);
193     }
194 
195 
196     /**
197      * Clears out GlobalVariable objects with the exception of the UserSession
198      */
199     public static void clear() {
200         messageMaps.set(new MessageMap());
201         auditErrorMaps.set(new HashMap());
202         messageLists.set(new MessageList());
203         requestCaches.set(new HashMap<String,Object>() );
204         kualiForms.set(null);
205     }
206 }