KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > security > propagation > jonas > JOnASSecurityCurrent


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: JOnASSecurityCurrent.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.security.propagation.jonas;
27
28 import java.lang.reflect.InvocationTargetException JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30
31 import org.objectweb.easybeans.security.api.EZBSecurityContext;
32 import org.objectweb.easybeans.security.api.EZBSecurityCurrent;
33
34 /**
35  * Allow to get the JOnAS security context.
36  * @author Florent Benoit
37  */

38 public class JOnASSecurityCurrent implements EZBSecurityCurrent {
39
40     /**
41      * Name of the JOnAS class.
42      */

43     private static final String JavaDoc JONAS_SECURITY_CURRENT = "org.objectweb.security.context.SecurityCurrent";
44
45     /**
46      * Name of the JOnAS context class.
47      */

48     private static final String JavaDoc JONAS_SECURITY_CONTEXT = "org.objectweb.security.context.SecurityContext";
49
50
51     /**
52      * Unique instance of the JOnAS security current object.
53      */

54     private static Object JavaDoc jonasSecurityCurrent = initCurrent();
55
56
57     /**
58      * Init the current object (if not already done).
59      * @return JOnAS security current object.
60      */

61     private static Object JavaDoc initCurrent() {
62
63         Class JavaDoc jonasSecurityCurrentClass = null;
64         try {
65             jonasSecurityCurrentClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_SECURITY_CURRENT);
66         } catch (ClassNotFoundException JavaDoc e) {
67             throw new IllegalStateException JavaDoc("Cannot load the '" + JONAS_SECURITY_CURRENT + "' class.", e);
68         }
69
70         Method JavaDoc m = null;
71         try {
72             m = jonasSecurityCurrentClass.getMethod("getCurrent");
73         } catch (SecurityException JavaDoc e) {
74             throw new IllegalStateException JavaDoc("Cannot get the method getCurrent on the JOnAS security context", e);
75         } catch (NoSuchMethodException JavaDoc e) {
76             throw new IllegalStateException JavaDoc("Cannot get the method getCurrent on the JOnAS security context", e);
77         }
78
79         try {
80             return m.invoke(null);
81         } catch (IllegalArgumentException JavaDoc e) {
82             throw new IllegalStateException JavaDoc("Cannot call getCallerPrincipal method on the JOnAS security context", e);
83         } catch (IllegalAccessException JavaDoc e) {
84             throw new IllegalStateException JavaDoc("Cannot call getCallerPrincipal method on the JOnAS security context", e);
85         } catch (InvocationTargetException JavaDoc e) {
86             throw new IllegalStateException JavaDoc("Cannot call getCallerPrincipal method on the JOnAS security context", e);
87         }
88     }
89
90
91
92     /**
93      * Gets the current context.
94      * @return SecurityContext return the Security context associated to the
95      * current thread or the JVM
96      */

97     public EZBSecurityContext getSecurityContext() {
98         Method JavaDoc m = null;
99         try {
100             m = jonasSecurityCurrent.getClass().getMethod("getSecurityContext");
101         } catch (SecurityException JavaDoc e) {
102             throw new IllegalStateException JavaDoc("Cannot get the method getSecurityContext on the JOnAS security context", e);
103         } catch (NoSuchMethodException JavaDoc e) {
104             throw new IllegalStateException JavaDoc("Cannot get the method getSecurityContext on the JOnAS security context", e);
105         }
106
107         // get current security context object
108
Object JavaDoc jonasSecurityContext = null;
109         try {
110             jonasSecurityContext = m.invoke(jonasSecurityCurrent);
111         } catch (IllegalArgumentException JavaDoc e) {
112             throw new IllegalStateException JavaDoc("Cannot call getSecurityContext method on the JOnAS security context", e);
113         } catch (IllegalAccessException JavaDoc e) {
114             throw new IllegalStateException JavaDoc("Cannot call getSecurityContext method on the JOnAS security context", e);
115         } catch (InvocationTargetException JavaDoc e) {
116             throw new IllegalStateException JavaDoc("Cannot call getSecurityContext method on the JOnAS security context", e);
117         }
118
119         // no security context ?
120
if (jonasSecurityContext == null) {
121             // load class
122
Class JavaDoc securityContextClass = null;
123             try {
124                 securityContextClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_SECURITY_CONTEXT);
125             } catch (ClassNotFoundException JavaDoc e) {
126                 throw new IllegalStateException JavaDoc("Cannot load the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
127             }
128             // build new object
129
try {
130                 jonasSecurityContext = securityContextClass.newInstance();
131             } catch (InstantiationException JavaDoc e) {
132                 throw new IllegalStateException JavaDoc("Cannot build an instance of the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
133             } catch (IllegalAccessException JavaDoc e) {
134                 throw new IllegalStateException JavaDoc("Cannot build an instance of the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
135             }
136         }
137
138
139         // wrap it
140
return new JOnASSecurityContext(jonasSecurityContext);
141
142     }
143
144     /**
145      * Associates the given security context to the current thread.
146      * @param securityContext Security context to associate to the current thread.
147      */

148     public void setSecurityContext(final EZBSecurityContext securityContext) {
149         // Do nothing, JOnAS interceptor will call JOnAS method.
150
}
151
152     /**
153      * Associates the given security context to all threads (JVM).
154      * @param securityContext Security context to associate to the JVM
155      */

156     public void setGlobalSecurityContext(final EZBSecurityContext securityContext) {
157         // Do nothing, JOnAS interceptor will call JOnAS method.
158
}
159
160 }
161
Popular Tags