use of java.security.PrivilegedActionException in project hadoop by apache.
the class KerberosTestUtils method doAs.
public static <T> T doAs(String principal, final Callable<T> callable) throws Exception {
LoginContext loginContext = null;
try {
Set<Principal> principals = new HashSet<Principal>();
principals.add(new KerberosPrincipal(KerberosTestUtils.getClientPrincipal()));
Subject subject = new Subject(false, principals, new HashSet<Object>(), new HashSet<Object>());
loginContext = new LoginContext("", subject, null, new KerberosConfiguration(principal));
loginContext.login();
subject = loginContext.getSubject();
return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {
@Override
public T run() throws Exception {
return callable.call();
}
});
} catch (PrivilegedActionException ex) {
throw ex.getException();
} finally {
if (loginContext != null) {
loginContext.logout();
}
}
}
use of java.security.PrivilegedActionException in project groovy by apache.
the class MetaClassImpl method addProperties.
private void addProperties() {
BeanInfo info;
// introspect
try {
if (isBeanDerivative(theClass)) {
info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IntrospectionException {
return Introspector.getBeanInfo(theClass, Introspector.IGNORE_ALL_BEANINFO);
}
});
} else {
info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IntrospectionException {
return Introspector.getBeanInfo(theClass);
}
});
}
} catch (PrivilegedActionException pae) {
throw new GroovyRuntimeException("exception during bean introspection", pae.getException());
}
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
// build up the metaproperties based on the public fields, property descriptors,
// and the getters and setters
setupProperties(descriptors);
EventSetDescriptor[] eventDescriptors = info.getEventSetDescriptors();
for (EventSetDescriptor descriptor : eventDescriptors) {
Method[] listenerMethods = descriptor.getListenerMethods();
for (Method listenerMethod : listenerMethods) {
final MetaMethod metaMethod = CachedMethod.find(descriptor.getAddListenerMethod());
// we skip that here
if (metaMethod == null)
continue;
addToAllMethodsIfPublic(metaMethod);
String name = listenerMethod.getName();
if (listeners.containsKey(name)) {
listeners.put(name, AMBIGUOUS_LISTENER_METHOD);
} else {
listeners.put(name, metaMethod);
}
}
}
}
use of java.security.PrivilegedActionException in project robovm by robovm.
the class myPrivilegedExceptionAction method test_doAsPrivileged_02.
/**
* javax.security.auth.Subject#doAsPrivileged(Subject subject,
* PrivilegedExceptionAction action,
* AccessControlContext acc)
*/
public void test_doAsPrivileged_02() {
Subject subj = new Subject();
PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction();
PrivilegedExceptionAction<Object> peaNull = null;
AccessControlContext acc = AccessController.getContext();
try {
Object obj = Subject.doAsPrivileged(null, pea, acc);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
Object obj = Subject.doAsPrivileged(subj, pea, acc);
} catch (Exception e) {
fail("Unexpected exception: " + e);
}
try {
Object obj = Subject.doAsPrivileged(subj, peaNull, acc);
fail("NullPointerException wasn't thrown");
} catch (NullPointerException npe) {
} catch (Exception e) {
fail(e + " was thrown instead of NullPointerException");
}
try {
Subject.doAsPrivileged(subj, new PrivilegedExceptionAction<Object>() {
public Object run() throws PrivilegedActionException {
throw new PrivilegedActionException(null);
}
}, acc);
fail("PrivilegedActionException wasn't thrown");
} catch (PrivilegedActionException e) {
}
}
use of java.security.PrivilegedActionException in project AsmackService by rtreffer.
the class LoginContext method logoutImpl.
/**
* The real implementation of logout() method whose calls are wrapped into
* appropriate doPrivileged calls in logout().
*/
private void logoutImpl() throws LoginException {
if (subject == null) {
//$NON-NLS-1$
throw new LoginException("auth.38");
}
loggedIn = false;
Throwable firstProblem = null;
int total = 0;
for (Module module : modules) {
try {
module.module.logout();
++total;
} catch (Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
}
}
if (firstProblem != null || total == 0) {
if (firstProblem instanceof PrivilegedActionException && firstProblem.getCause() != null) {
firstProblem = firstProblem.getCause();
}
if (firstProblem instanceof LoginException) {
throw (LoginException) firstProblem;
}
//$NON-NLS-1$
throw (LoginException) new LoginException("auth.37").initCause(firstProblem);
}
}
use of java.security.PrivilegedActionException in project AsmackService by rtreffer.
the class LoginContext method loginImpl.
/**
* The real implementation of login() method whose calls are wrapped into
* appropriate doPrivileged calls in login().
*/
private void loginImpl() throws LoginException {
if (subject == null) {
subject = new Subject();
}
if (sharedState == null) {
sharedState = new HashMap<String, Object>();
}
// PHASE 1: Calling login()-s
Throwable firstProblem = null;
int[] logged = new int[4];
int[] total = new int[4];
for (Module module : modules) {
try {
// if a module fails during Class.forName(), then it breaks overall
// attempt - see catch() below
module.create(subject, callbackHandler, sharedState);
if (module.module.login()) {
++total[module.getFlag()];
++logged[module.getFlag()];
if (module.getFlag() == SUFFICIENT) {
break;
}
}
} catch (Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
if (module.klass == null) {
/*
* an exception occurred during class lookup - overall
* attempt must fail a little trick: increase the REQUIRED's
* number - this will look like a failed REQUIRED module
* later, so overall attempt will fail
*/
++total[REQUIRED];
break;
}
++total[module.getFlag()];
// something happened after the class was loaded
if (module.getFlag() == REQUISITE) {
// ... and no need to walk down anymore
break;
}
}
}
// end of PHASE1,
// Let's decide whether we have either overall success or a total failure
boolean fail = true;
// if any REQ* module failed - then it's failure
if (logged[REQUIRED] != total[REQUIRED] || logged[REQUISITE] != total[REQUISITE]) {
// fail = true;
} else {
if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
// must have at least one SUFFICIENT or OPTIONAL
if (logged[OPTIONAL] != 0 || logged[SUFFICIENT] != 0) {
fail = false;
}
//else { fail = true; }
} else {
fail = false;
}
}
int[] commited = new int[4];
// clear it
total[0] = total[1] = total[2] = total[3] = 0;
if (!fail) {
for (Module module : modules) {
if (module.klass != null) {
++total[module.getFlag()];
try {
module.module.commit();
++commited[module.getFlag()];
} catch (Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
}
}
}
}
// need to decide once again
fail = true;
if (commited[REQUIRED] != total[REQUIRED] || commited[REQUISITE] != total[REQUISITE]) {
//fail = true;
} else {
if (total[REQUIRED] == 0 && total[REQUISITE] == 0) {
/*
* neither REQUIRED nor REQUISITE was configured. must have at
* least one SUFFICIENT or OPTIONAL
*/
if (commited[OPTIONAL] != 0 || commited[SUFFICIENT] != 0) {
fail = false;
} else {
//fail = true;
}
} else {
fail = false;
}
}
if (fail) {
for (Module module : modules) {
try {
module.module.abort();
} catch (/*LoginException*/
Throwable ex) {
if (firstProblem == null) {
firstProblem = ex;
}
}
}
if (firstProblem instanceof PrivilegedActionException && firstProblem.getCause() != null) {
firstProblem = firstProblem.getCause();
}
if (firstProblem instanceof LoginException) {
throw (LoginException) firstProblem;
}
//$NON-NLS-1$
throw (LoginException) new LoginException("auth.37").initCause(firstProblem);
}
loggedIn = true;
}
Aggregations