1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util;
17
18 import org.kuali.rice.krad.UserSession;
19 import org.kuali.rice.krad.uif.util.UifFormManager;
20 import org.kuali.rice.core.framework.util.ApplicationThreadLocal;
21
22 import java.util.HashMap;
23 import java.util.LinkedList;
24 import java.util.Map;
25 import java.util.concurrent.Callable;
26
27
28
29
30
31
32 public final class GlobalVariables {
33
34 private static ThreadLocal<LinkedList<GlobalVariables>> GLOBAL_VARIABLES_STACK = new ApplicationThreadLocal<LinkedList<GlobalVariables>>() {
35 protected LinkedList<GlobalVariables> initialValue() {
36 LinkedList<GlobalVariables> globalVariablesStack = new LinkedList<GlobalVariables>();
37 globalVariablesStack.add(new GlobalVariables());
38 return globalVariablesStack;
39 }
40 };
41
42 private static GlobalVariables getCurrentGlobalVariables() {
43 return GLOBAL_VARIABLES_STACK.get().getLast();
44 }
45
46 static GlobalVariables pushGlobalVariables() {
47 GlobalVariables vars = new GlobalVariables();
48 GLOBAL_VARIABLES_STACK.get().add(vars);
49 return vars;
50 }
51
52 static GlobalVariables popGlobalVariables() {
53 return GLOBAL_VARIABLES_STACK.get().removeLast();
54 }
55
56 static void reset() {
57 LinkedList<GlobalVariables> stack = GLOBAL_VARIABLES_STACK.get();
58 stack.clear();
59 stack.add(new GlobalVariables());
60 }
61
62 private UserSession userSession = null;
63 private String hideSessionFromTestsMessage = null;
64 private MessageMap messageMap = new MessageMap();
65 private Map<String,Object> requestCache = new HashMap<String, Object>();
66 private UifFormManager uifFormManager = null;
67
68 private GlobalVariables() {}
69
70
71
72
73
74 public static UserSession getUserSession() {
75 GlobalVariables vars = getCurrentGlobalVariables();
76 String message = vars.hideSessionFromTestsMessage;
77 if (message != null) {
78 throw new RuntimeException(message);
79 }
80 return vars.userSession;
81 }
82
83
84
85
86
87
88
89 public static void setHideSessionFromTestsMessage(String message) {
90 GlobalVariables vars = getCurrentGlobalVariables();
91 vars.hideSessionFromTestsMessage = message;
92 }
93
94
95
96
97
98
99 public static void setUserSession(UserSession userSession) {
100 GlobalVariables vars = getCurrentGlobalVariables();
101 vars.userSession = userSession;
102 }
103
104 public static MessageMap getMessageMap() {
105 GlobalVariables vars = getCurrentGlobalVariables();
106 return vars.messageMap;
107 }
108
109
110
111
112
113 public static void mergeErrorMap(MessageMap messageMap) {
114 getMessageMap().getErrorMessages().putAll(messageMap.getErrorMessages());
115 getMessageMap().getWarningMessages().putAll(messageMap.getWarningMessages());
116 getMessageMap().getInfoMessages().putAll(messageMap.getInfoMessages());
117 }
118
119
120
121
122
123
124 public static void setMessageMap(MessageMap messageMap) {
125 GlobalVariables vars = getCurrentGlobalVariables();
126 vars.messageMap = messageMap;
127 }
128
129 public static Object getRequestCache(String cacheName) {
130 GlobalVariables vars = getCurrentGlobalVariables();
131 return vars.requestCache.get(cacheName);
132 }
133
134 public static void setRequestCache(String cacheName, Object cacheObject) {
135 GlobalVariables vars = getCurrentGlobalVariables();
136 vars.requestCache.put(cacheName, cacheObject);
137 }
138
139
140
141
142
143
144
145 public static UifFormManager getUifFormManager() {
146 GlobalVariables vars = getCurrentGlobalVariables();
147 return vars.uifFormManager;
148 }
149
150
151
152
153
154
155 public static void setUifFormManager(UifFormManager uifFormManager) {
156 GlobalVariables vars = getCurrentGlobalVariables();
157 vars.uifFormManager = uifFormManager;
158 }
159
160
161
162
163 public static void clear() {
164 GlobalVariables vars = getCurrentGlobalVariables();
165 vars.messageMap = new MessageMap();
166 vars.requestCache = new HashMap<String,Object>();
167 }
168
169
170
171
172
173
174 public static <T> T doInNewGlobalVariables(Callable<T> callable) throws Exception {
175 return doInNewGlobalVariables(null, callable);
176 }
177
178
179
180
181
182
183
184
185 public static <T> T doInNewGlobalVariables(UserSession userSession, Callable<T> callable) throws Exception {
186 try {
187 GlobalVariables vars = pushGlobalVariables();
188 if (userSession != null) {
189 vars.userSession = userSession;
190 }
191 return callable.call();
192 } finally {
193 popGlobalVariables();
194 }
195 }
196 }