1 63 64 package com.jcorporate.expresso.core.registry; 65 66 import com.jcorporate.expresso.core.security.User; 67 68 import java.util.Stack ; 69 70 101 abstract public class RequestRegistry { 102 103 106 protected String dataContext = null; 107 108 111 protected User user = null; 112 113 114 119 private Stack superUserStack = null; 120 121 124 static private ThreadLocal threadLocal = new ThreadLocal (); 125 126 129 protected RequestRegistry() { 130 synchronized (threadLocal) { 131 threadLocal.set(this); 132 } 133 } 134 135 143 static public String getDataContext() { 144 return getInstance().dataContext; 145 } 146 147 156 protected static RequestRegistry getInstance() throws IllegalStateException { 157 RequestRegistry requestRegistry = (RequestRegistry) threadLocal.get(); 158 if (requestRegistry == null) { 159 throw new IllegalStateException ("No request registry has been associated with this thread yet." 160 + " If you are running inside a servlet engine, please add " 161 + "RequestRegistryFilter as a servlet filter within web.xml. See the " + 162 " web.xml in the Expresso source tree as an example."); 163 } 164 return requestRegistry; 165 } 166 167 175 static public User getUser() { 176 return getInstance().user; 177 } 178 179 187 static public void superUser(User newUser) { 188 RequestRegistry requestRegistry = getInstance(); 189 Stack userStack = requestRegistry.getUserStack(); 190 userStack.push(requestRegistry.user); 191 requestRegistry.user = newUser; 192 } 193 194 199 protected Stack getUserStack() { 200 if (superUserStack == null) { 201 superUserStack = new Stack (); 202 } 203 204 return superUserStack; 205 } 206 207 216 static public User revertUser() { 217 RequestRegistry requestRegistry = getInstance(); 218 Stack userStack = requestRegistry.getUserStack(); 219 if (userStack.isEmpty()) { 220 throw new java.lang.ArrayIndexOutOfBoundsException ("Stack is empty. " + 221 "You have mismatching superUser() and revertUser() calls"); 222 } 223 224 requestRegistry.user = (User) userStack.pop(); 225 return requestRegistry.user; 226 227 } 228 229 232 protected void releaseSettings() { 233 superUserStack = null; 234 user = null; 235 dataContext = null; 236 } 237 238 239 } 240 | Popular Tags |