1 25 26 package com.rift.coad.lib.interceptor; 28 29 import java.util.Stack ; 31 32 import org.apache.log4j.Logger; 34 35 import com.rift.coad.lib.security.ThreadsPermissionContainer; 37 import com.rift.coad.lib.security.ThreadPermissionSession; 38 import com.rift.coad.lib.security.UserSession; 39 40 41 48 public class InterceptorPermissionStack { 49 50 protected Logger log = 52 Logger.getLogger(InterceptorPermissionStack.class.getName()); 53 54 private ThreadsPermissionContainer permissionContainer = null; 56 private ThreadLocal threadLocal = new ThreadLocal (); 57 58 59 64 public InterceptorPermissionStack( 65 ThreadsPermissionContainer permissionContainer) { 66 this.permissionContainer = permissionContainer; 67 } 68 69 70 78 public void push(UserSession userSession) throws InterceptorException { 79 try { 80 Stack stack = (Stack )threadLocal.get(); 81 if (stack == null) { 82 stack = new Stack (); 83 threadLocal.set(stack); 84 } 85 Thread currentThread = Thread.currentThread(); 86 stack.push( 87 permissionContainer.getSession(currentThread.getId())); 88 permissionContainer.putSession(currentThread.getId(), 89 new ThreadPermissionSession( 90 new Long (Thread.currentThread().getId()),userSession)); 91 } catch (Exception ex) { 92 log.error("Failed to push an entry on the interceptor permission " + 93 "stack : " + ex.getMessage(),ex); 94 } 95 } 96 97 98 105 public void pop() throws InterceptorException { 106 try { 107 Stack stack = (Stack )threadLocal.get(); 108 if (stack == null) { 109 throw new InterceptorException( 110 "The interceptors are not getting used correctly there" + 111 "is not session for this thread."); 112 } 113 Thread currentThread = Thread.currentThread(); 114 ThreadPermissionSession permission = 115 (ThreadPermissionSession)stack.pop(); 116 if (permission != null) { 117 permissionContainer.putSession(currentThread.getId(), 118 permission); 119 } else { 120 permissionContainer.removeSession(currentThread.getId()); 121 } 122 } catch (InterceptorException ex) { 123 throw ex; 124 } catch (Exception ex) { 125 log.error("Failed to pop an entry off the interceptor permission " + 126 "stack : " + ex.getMessage(),ex); 127 } 128 } 129 } 130 | Popular Tags |