KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@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  * Initial developer(s): Jeff Mesnil.
22  *
23  * --------------------------------------------------------------------------
24  * $Id: SecurityCurrent.java,v 1.5 2004/04/09 09:58:27 benoitf Exp $
25  * --------------------------------------------------------------------------
26  *
27  */

28
29 package org.objectweb.security.context;
30
31 /**
32  * For handling the association SecurityContext/ Thread
33  * @author Jeff Mesnil
34  */

35 public class SecurityCurrent {
36
37     /**
38      * Local thread
39      */

40     private static InheritableThreadLocal JavaDoc threadCtx;
41
42     /**
43      * Security Context for all the JVM
44      */

45     private static SecurityContext sctx = null;
46
47     /**
48      * Init the thread
49      */

50     static {
51         threadCtx = new InheritableThreadLocal JavaDoc();
52         threadCtx.set(new SecurityContext());
53     }
54
55     /**
56      * Unique instance
57      */

58     private static SecurityCurrent current = new SecurityCurrent();
59
60     /**
61      * Method getCurrent
62      * @return SecurityCurrent return the current
63      */

64     public static SecurityCurrent getCurrent() {
65         return current;
66     }
67
68     /**
69      * Method setSecurityContext
70      * @param ctx Security context to associate to the current thread
71      */

72     public void setSecurityContext(SecurityContext ctx) {
73         threadCtx.set(ctx);
74     }
75
76     /**
77      * Method setSecurityContext used for client container
78      * @param ctx Security context to associate to the JVM
79      */

80     public void setGlobalSecurityContext(SecurityContext ctx) {
81         sctx = ctx;
82     }
83
84     /**
85      * Method getSecurityContext
86      * @return SecurityContext return the Security context associated to the
87      * current thread
88      */

89     public SecurityContext getSecurityContext() {
90         if (sctx != null) {
91             return sctx;
92         } else {
93             return (SecurityContext) threadCtx.get();
94         }
95     }
96
97 }
Popular Tags