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
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
28
29
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
70
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
83
84
85
86
87 public static void setHideSessionFromTestsMessage(String message) {
88 GlobalVariables vars = getCurrentGlobalVariables();
89 vars.hideSessionFromTestsMessage = message;
90 }
91
92
93
94
95
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
109
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
119
120
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
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
148
149
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 }