01: /**
02: * JOnAS: Java(TM) Open Application Server
03: * Copyright (C) 1999 Bull S.A.
04: * Contact: jonas-team@objectweb.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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 4578 2004-04-09 09:58:27Z benoitf $
25: * --------------------------------------------------------------------------
26: *
27: */package org.objectweb.security.context;
28:
29: /**
30: * For handling the association SecurityContext/ Thread
31: * @author Jeff Mesnil
32: */
33: public class SecurityCurrent {
34:
35: /**
36: * Local thread
37: */
38: private static InheritableThreadLocal threadCtx;
39:
40: /**
41: * Security Context for all the JVM
42: */
43: private static SecurityContext sctx = null;
44:
45: /**
46: * Init the thread
47: */
48: static {
49: threadCtx = new InheritableThreadLocal();
50: threadCtx.set(new SecurityContext());
51: }
52:
53: /**
54: * Unique instance
55: */
56: private static SecurityCurrent current = new SecurityCurrent();
57:
58: /**
59: * Method getCurrent
60: * @return SecurityCurrent return the current
61: */
62: public static SecurityCurrent getCurrent() {
63: return current;
64: }
65:
66: /**
67: * Method setSecurityContext
68: * @param ctx Security context to associate to the current thread
69: */
70: public void setSecurityContext(SecurityContext ctx) {
71: threadCtx.set(ctx);
72: }
73:
74: /**
75: * Method setSecurityContext used for client container
76: * @param ctx Security context to associate to the JVM
77: */
78: public void setGlobalSecurityContext(SecurityContext ctx) {
79: sctx = ctx;
80: }
81:
82: /**
83: * Method getSecurityContext
84: * @return SecurityContext return the Security context associated to the
85: * current thread
86: */
87: public SecurityContext getSecurityContext() {
88: if (sctx != null) {
89: return sctx;
90: } else {
91: return (SecurityContext) threadCtx.get();
92: }
93: }
94:
95: }
|