org.ow2.easybeans.security.propagation.jonas.JOnASSecurityCurrent Maven / Gradle / Ivy
/**
* EasyBeans
* Copyright (C) 2006 Bull S.A.S.
* Contact: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
* --------------------------------------------------------------------------
* $Id: JOnASSecurityCurrent.java 5602 2010-10-01 15:49:31Z sauthieg $
* --------------------------------------------------------------------------
*/
package org.ow2.easybeans.security.propagation.jonas;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.ow2.easybeans.security.api.EZBSecurityContext;
import org.ow2.easybeans.security.api.EZBSecurityCurrent;
/**
* Allow to get the JOnAS security context.
* @author Florent Benoit
*/
public class JOnASSecurityCurrent implements EZBSecurityCurrent {
/**
* Name of the JOnAS class.
*/
private static final String JONAS_SECURITY_CURRENT = "org.objectweb.security.context.SecurityCurrent";
/**
* Name of the JOnAS context class.
*/
private static final String JONAS_SECURITY_CONTEXT = "org.objectweb.security.context.SecurityContext";
/**
* Unique instance of the JOnAS security current object.
*/
private static Object jonasSecurityCurrent = initCurrent();
/**
* Init the current object (if not already done).
* @return JOnAS security current object.
*/
private static Object initCurrent() {
Class> jonasSecurityCurrentClass = null;
try {
jonasSecurityCurrentClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_SECURITY_CURRENT);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot load the '" + JONAS_SECURITY_CURRENT + "' class.", e);
}
Method m = null;
try {
m = jonasSecurityCurrentClass.getMethod("getCurrent");
} catch (SecurityException e) {
throw new IllegalStateException("Cannot get the method getCurrent on the JOnAS security context", e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Cannot get the method getCurrent on the JOnAS security context", e);
}
try {
return m.invoke(null);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Cannot call getCallerPrincipal method on the JOnAS security context", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot call getCallerPrincipal method on the JOnAS security context", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException("Cannot call getCallerPrincipal method on the JOnAS security context", e);
}
}
/**
* Gets the current context.
* @return SecurityContext return the Security context associated to the
* current thread or the JVM
*/
public EZBSecurityContext getSecurityContext() {
Method m = null;
try {
m = jonasSecurityCurrent.getClass().getMethod("getSecurityContext");
} catch (SecurityException e) {
throw new IllegalStateException("Cannot get the method getSecurityContext on the JOnAS security context", e);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("Cannot get the method getSecurityContext on the JOnAS security context", e);
}
// get current security context object
Object jonasSecurityContext = null;
try {
jonasSecurityContext = m.invoke(jonasSecurityCurrent);
} catch (IllegalArgumentException e) {
throw new IllegalStateException("Cannot call getSecurityContext method on the JOnAS security context", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot call getSecurityContext method on the JOnAS security context", e);
} catch (InvocationTargetException e) {
throw new IllegalStateException("Cannot call getSecurityContext method on the JOnAS security context", e);
}
// no security context ?
if (jonasSecurityContext == null) {
// load class
Class> securityContextClass = null;
try {
securityContextClass = Thread.currentThread().getContextClassLoader().loadClass(JONAS_SECURITY_CONTEXT);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Cannot load the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
}
// build new object
try {
jonasSecurityContext = securityContextClass.newInstance();
} catch (InstantiationException e) {
throw new IllegalStateException("Cannot build an instance of the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
} catch (IllegalAccessException e) {
throw new IllegalStateException("Cannot build an instance of the class '" + JONAS_SECURITY_CONTEXT + "'.", e);
}
}
// wrap it
return new JOnASSecurityContext(jonasSecurityContext);
}
/**
* Associates the given security context to the current thread.
* @param securityContext Security context to associate to the current thread.
*/
public void setSecurityContext(final EZBSecurityContext securityContext) {
// Do nothing, JOnAS interceptor will call JOnAS method.
}
/**
* Associates the given security context to all threads (JVM).
* @param securityContext Security context to associate to the JVM
*/
public void setGlobalSecurityContext(final EZBSecurityContext securityContext) {
// Do nothing, JOnAS interceptor will call JOnAS method.
}
}