KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > propagation > context > SecurityCurrent


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SecurityCurrent.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.security.propagation.context;
27
28 import org.objectweb.easybeans.security.api.EZBSecurityContext;
29 import org.objectweb.easybeans.security.api.EZBSecurityCurrent;
30
31 /**
32  * Manages the current security context associated to the current thread.
33  * @author Florent Benoit
34  */

35 public class SecurityCurrent implements EZBSecurityCurrent {
36
37     /**
38      * Inherited Local thread used to keep the security context.
39      */

40     private static InheritableThreadLocal JavaDoc<EZBSecurityContext> threadLocal;
41
42     /**
43      * Static Security Context that is applied on all threads (used for heavy
44      * client).
45      */

46     private static EZBSecurityContext globalContext = null;
47
48     /**
49      * Default security context.
50      */

51     private static final EZBSecurityContext DEFAULT_CTX = new SecurityContext();
52
53     /**
54      * Init the thread
55      */

56     static {
57         threadLocal = new InheritableThreadLocal JavaDoc<EZBSecurityContext>();
58         threadLocal.set(new SecurityContext());
59     }
60
61     /**
62      * Unique instance of this current object.
63      */

64     private static EZBSecurityCurrent unique = initCurrent();
65
66     /**
67      * Build the unique instance of the security current.<br>
68      * It can use JOnAS current object.
69      * @return unique instance.
70      */

71     @SuppressWarnings JavaDoc("unchecked")
72     private static EZBSecurityCurrent initCurrent() {
73         String JavaDoc externalPropertyClass = System.getProperty(SECURITY_CURRENT_PROPERTY);
74         if (externalPropertyClass != null) {
75             EZBSecurityCurrent current = null;
76             // use this external current object
77
Class JavaDoc<EZBSecurityCurrent> currentClass = null;
78             try {
79                 currentClass = (Class JavaDoc<EZBSecurityCurrent>) Thread.currentThread().getContextClassLoader().loadClass(
80                         externalPropertyClass);
81             } catch (ClassNotFoundException JavaDoc e) {
82                 throw new IllegalStateException JavaDoc("Cannot find the class '" + externalPropertyClass + "'", e);
83             } catch (ClassCastException JavaDoc e) {
84                 throw new IllegalStateException JavaDoc("'" + externalPropertyClass
85                         + "' class is not an instance of EZBSecurityCurrent interface.", e);
86             }
87
88             try {
89                 current = currentClass.newInstance();
90             } catch (InstantiationException JavaDoc e) {
91                 throw new IllegalStateException JavaDoc("Cannot create an instance of the class '" + externalPropertyClass + "'", e);
92             } catch (IllegalAccessException JavaDoc e) {
93                 throw new IllegalStateException JavaDoc("Cannot create an instance of the class '" + externalPropertyClass + "'", e);
94             }
95
96             return current;
97
98         }
99
100         // else return default implementation
101
return new SecurityCurrent();
102     }
103
104     /**
105      * Return the unique instance of this object.
106      * @return SecurityCurrent return the current
107      */

108     public static EZBSecurityCurrent getCurrent() {
109         return unique;
110     }
111
112     /**
113      * Associates the given security context to the current thread.
114      * @param securityContext Security context to associate to the current
115      * thread.
116      */

117     public void setSecurityContext(final EZBSecurityContext securityContext) {
118         threadLocal.set(securityContext);
119     }
120
121     /**
122      * Associates the given security context to all threads (JVM).
123      * @param securityContext Security context to associate to the JVM
124      */

125     public void setGlobalSecurityContext(final EZBSecurityContext securityContext) {
126         globalContext = securityContext;
127     }
128
129     /**
130      * Gets the current context.
131      * @return SecurityContext return the Security context associated to the
132      * current thread or the JVM
133      */

134     public EZBSecurityContext getSecurityContext() {
135         if (globalContext != null) {
136             return globalContext;
137         }
138         if (threadLocal.get() != null) {
139             return threadLocal.get();
140         }
141
142         // else, never null context.
143
return DEFAULT_CTX;
144     }
145
146 }
147
Popular Tags