KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > interceptor > InterceptorPermissionStack


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * InterceptorPermissionStack.java
20  *
21  * This class is responsible for keeping track of the thread permissions assigned
22  * to a thread and pushing and popping them to deal with threads being granted
23  * new permissions or releasing old permissions.
24  */

25
26 // package path
27
package com.rift.coad.lib.interceptor;
28
29 // java imports
30
import java.util.Stack JavaDoc;
31
32 // logging import
33
import org.apache.log4j.Logger;
34
35 // coadunation imports
36
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 /**
42  * This class is responsible for keeping track of the thread permissions assigned
43  * to a thread and pushing and popping them to deal with threads being granted
44  * new permissions or releasing old permissions.
45  *
46  * @author Brett Chaldecott
47  */

48 public class InterceptorPermissionStack {
49     
50     // the class log variable
51
protected Logger log =
52             Logger.getLogger(InterceptorPermissionStack.class.getName());
53     
54     // private member variables
55
private ThreadsPermissionContainer permissionContainer = null;
56     private ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc();
57     
58     
59     /**
60      * Creates a new instance of InterceptorPermissionStack
61      *
62      * @param permissionContainer The reference to the permission container
63      */

64     public InterceptorPermissionStack(
65             ThreadsPermissionContainer permissionContainer) {
66         this.permissionContainer = permissionContainer;
67     }
68     
69     
70     /**
71      * This method pushes the current user session onto a stack so it can
72      * be popped back on when processing is completed.
73      *
74      * @param userSession The user session to apply to the current thread
75      * session.
76      * @exception InterceptorException
77      */

78     public void push(UserSession userSession) throws InterceptorException {
79         try {
80             Stack JavaDoc stack = (Stack JavaDoc)threadLocal.get();
81             if (stack == null) {
82                 stack = new Stack JavaDoc();
83                 threadLocal.set(stack);
84             }
85             Thread JavaDoc currentThread = Thread.currentThread();
86             stack.push(
87                     permissionContainer.getSession(currentThread.getId()));
88             permissionContainer.putSession(currentThread.getId(),
89                     new ThreadPermissionSession(
90                     new Long JavaDoc(Thread.currentThread().getId()),userSession));
91         } catch (Exception JavaDoc ex) {
92             log.error("Failed to push an entry on the interceptor permission " +
93                     "stack : " + ex.getMessage(),ex);
94         }
95     }
96     
97     
98     /**
99      * This method pops the user permissions of the stack and applies them to
100      * the current thread. Thus reveting them to the permissions that existed
101      * before the call on the push.
102      *
103      * @exception InterceptorException
104      */

105     public void pop() throws InterceptorException {
106         try {
107             Stack JavaDoc stack = (Stack JavaDoc)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 JavaDoc 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 JavaDoc ex) {
125             log.error("Failed to pop an entry off the interceptor permission " +
126                     "stack : " + ex.getMessage(),ex);
127         }
128     }
129 }
130
Popular Tags