use of java.security.AccessControlContext in project robovm by robovm.
the class myPrivilegedExceptionAction method test_doAsPrivileged_01.
/**
* javax.security.auth.Subject#doAsPrivileged(Subject subject,
* PrivilegedAction action,
* AccessControlContext acc)
*/
public void test_doAsPrivileged_01() {
Subject subj = new Subject();
PrivilegedAction<Object> pa = new myPrivilegedAction();
PrivilegedAction<Object> paNull = null;
AccessControlContext acc = AccessController.getContext();
try {
Object obj = Subject.doAsPrivileged(null, pa, acc);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
Object obj = Subject.doAsPrivileged(subj, pa, acc);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
Object obj = Subject.doAsPrivileged(subj, paNull, acc);
fail("NullPointerException wasn't thrown");
} catch (NullPointerException npe) {
}
}
use of java.security.AccessControlContext in project spring-framework by spring-projects.
the class AbstractBeanFactory method registerDisposableBeanIfNecessary.
/**
* Add the given bean to the list of disposable beans in this factory,
* registering its DisposableBean interface and/or the given destroy method
* to be called on factory shutdown (if applicable). Only applies to singletons.
* @param beanName the name of the bean
* @param bean the bean instance
* @param mbd the bean definition for the bean
* @see RootBeanDefinition#isSingleton
* @see RootBeanDefinition#getDependsOn
* @see #registerDisposableBean
* @see #registerDependentBean
*/
protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
if (mbd.isSingleton()) {
// Register a DisposableBean implementation that performs all destruction
// work for the given bean: DestructionAwareBeanPostProcessors,
// DisposableBean interface, custom destroy method.
registerDisposableBean(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
} else {
// A bean with a custom scope...
Scope scope = this.scopes.get(mbd.getScope());
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
}
scope.registerDestructionCallback(beanName, new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
}
}
}
use of java.security.AccessControlContext in project spring-framework by spring-projects.
the class CallbacksSecurityTests method testContainerPrivileges.
@Test
public void testContainerPrivileges() throws Exception {
AccessControlContext acc = provider.getAccessControlContext();
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
beanFactory.getBean("working-factory-method");
beanFactory.getBean("container-execution");
return null;
}
}, acc);
}
use of java.security.AccessControlContext in project spring-framework by spring-projects.
the class CallbacksSecurityTests method testSecuritySanity.
@Test
public void testSecuritySanity() throws Exception {
AccessControlContext acc = provider.getAccessControlContext();
try {
acc.checkPermission(new PropertyPermission("*", "read"));
fail("Acc should not have any permissions");
} catch (SecurityException se) {
// expected
}
final CustomCallbackBean bean = new CustomCallbackBean();
final Method method = bean.getClass().getMethod("destroy");
method.setAccessible(true);
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
method.invoke(bean);
return null;
}
}, acc);
fail("expected security exception");
} catch (Exception ex) {
}
final Class<ConstructorBean> cl = ConstructorBean.class;
try {
AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
@Override
public Object run() throws Exception {
return cl.newInstance();
}
}, acc);
fail("expected security exception");
} catch (Exception ex) {
}
}
use of java.security.AccessControlContext in project AsmackService by rtreffer.
the class Subject method doAs_PrivilegedExceptionAction.
// instantiates a new context and passes it to AccessController
@SuppressWarnings("unchecked")
private static Object doAs_PrivilegedExceptionAction(Subject subject, PrivilegedExceptionAction action, final AccessControlContext context) throws PrivilegedActionException {
AccessControlContext newContext;
final SubjectDomainCombiner combiner;
if (subject == null) {
// performance optimization
// if subject is null there is nothing to combine
combiner = null;
} else {
combiner = new SubjectDomainCombiner(subject);
}
PrivilegedAction<AccessControlContext> dccAction = new PrivilegedAction<AccessControlContext>() {
public AccessControlContext run() {
return new AccessControlContext(context, combiner);
}
};
newContext = AccessController.doPrivileged(dccAction);
return AccessController.doPrivileged(action, newContext);
}
Aggregations