001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JOnASSecurityCurrent.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.security.propagation.jonas;
025:
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028:
029: import org.ow2.easybeans.security.api.EZBSecurityContext;
030: import org.ow2.easybeans.security.api.EZBSecurityCurrent;
031:
032: /**
033: * Allow to get the JOnAS security context.
034: * @author Florent Benoit
035: */
036: public class JOnASSecurityCurrent implements EZBSecurityCurrent {
037:
038: /**
039: * Name of the JOnAS class.
040: */
041: private static final String JONAS_SECURITY_CURRENT = "org.objectweb.security.context.SecurityCurrent";
042:
043: /**
044: * Name of the JOnAS context class.
045: */
046: private static final String JONAS_SECURITY_CONTEXT = "org.objectweb.security.context.SecurityContext";
047:
048: /**
049: * Unique instance of the JOnAS security current object.
050: */
051: private static Object jonasSecurityCurrent = initCurrent();
052:
053: /**
054: * Init the current object (if not already done).
055: * @return JOnAS security current object.
056: */
057: private static Object initCurrent() {
058:
059: Class jonasSecurityCurrentClass = null;
060: try {
061: jonasSecurityCurrentClass = Thread.currentThread()
062: .getContextClassLoader().loadClass(
063: JONAS_SECURITY_CURRENT);
064: } catch (ClassNotFoundException e) {
065: throw new IllegalStateException("Cannot load the '"
066: + JONAS_SECURITY_CURRENT + "' class.", e);
067: }
068:
069: Method m = null;
070: try {
071: m = jonasSecurityCurrentClass.getMethod("getCurrent");
072: } catch (SecurityException e) {
073: throw new IllegalStateException(
074: "Cannot get the method getCurrent on the JOnAS security context",
075: e);
076: } catch (NoSuchMethodException e) {
077: throw new IllegalStateException(
078: "Cannot get the method getCurrent on the JOnAS security context",
079: e);
080: }
081:
082: try {
083: return m.invoke(null);
084: } catch (IllegalArgumentException e) {
085: throw new IllegalStateException(
086: "Cannot call getCallerPrincipal method on the JOnAS security context",
087: e);
088: } catch (IllegalAccessException e) {
089: throw new IllegalStateException(
090: "Cannot call getCallerPrincipal method on the JOnAS security context",
091: e);
092: } catch (InvocationTargetException e) {
093: throw new IllegalStateException(
094: "Cannot call getCallerPrincipal method on the JOnAS security context",
095: e);
096: }
097: }
098:
099: /**
100: * Gets the current context.
101: * @return SecurityContext return the Security context associated to the
102: * current thread or the JVM
103: */
104: public EZBSecurityContext getSecurityContext() {
105: Method m = null;
106: try {
107: m = jonasSecurityCurrent.getClass().getMethod(
108: "getSecurityContext");
109: } catch (SecurityException e) {
110: throw new IllegalStateException(
111: "Cannot get the method getSecurityContext on the JOnAS security context",
112: e);
113: } catch (NoSuchMethodException e) {
114: throw new IllegalStateException(
115: "Cannot get the method getSecurityContext on the JOnAS security context",
116: e);
117: }
118:
119: // get current security context object
120: Object jonasSecurityContext = null;
121: try {
122: jonasSecurityContext = m.invoke(jonasSecurityCurrent);
123: } catch (IllegalArgumentException e) {
124: throw new IllegalStateException(
125: "Cannot call getSecurityContext method on the JOnAS security context",
126: e);
127: } catch (IllegalAccessException e) {
128: throw new IllegalStateException(
129: "Cannot call getSecurityContext method on the JOnAS security context",
130: e);
131: } catch (InvocationTargetException e) {
132: throw new IllegalStateException(
133: "Cannot call getSecurityContext method on the JOnAS security context",
134: e);
135: }
136:
137: // no security context ?
138: if (jonasSecurityContext == null) {
139: // load class
140: Class securityContextClass = null;
141: try {
142: securityContextClass = Thread.currentThread()
143: .getContextClassLoader().loadClass(
144: JONAS_SECURITY_CONTEXT);
145: } catch (ClassNotFoundException e) {
146: throw new IllegalStateException(
147: "Cannot load the class '"
148: + JONAS_SECURITY_CONTEXT + "'.", e);
149: }
150: // build new object
151: try {
152: jonasSecurityContext = securityContextClass
153: .newInstance();
154: } catch (InstantiationException e) {
155: throw new IllegalStateException(
156: "Cannot build an instance of the class '"
157: + JONAS_SECURITY_CONTEXT + "'.", e);
158: } catch (IllegalAccessException e) {
159: throw new IllegalStateException(
160: "Cannot build an instance of the class '"
161: + JONAS_SECURITY_CONTEXT + "'.", e);
162: }
163: }
164:
165: // wrap it
166: return new JOnASSecurityContext(jonasSecurityContext);
167:
168: }
169:
170: /**
171: * Associates the given security context to the current thread.
172: * @param securityContext Security context to associate to the current thread.
173: */
174: public void setSecurityContext(
175: final EZBSecurityContext securityContext) {
176: // Do nothing, JOnAS interceptor will call JOnAS method.
177: }
178:
179: /**
180: * Associates the given security context to all threads (JVM).
181: * @param securityContext Security context to associate to the JVM
182: */
183: public void setGlobalSecurityContext(
184: final EZBSecurityContext securityContext) {
185: // Do nothing, JOnAS interceptor will call JOnAS method.
186: }
187:
188: }
|