本文整理匯總了Java中java.security.PrivilegedExceptionAction.run方法的典型用法代碼示例。如果您正苦於以下問題:Java PrivilegedExceptionAction.run方法的具體用法?Java PrivilegedExceptionAction.run怎麽用?Java PrivilegedExceptionAction.run使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.PrivilegedExceptionAction
的用法示例。
在下文中一共展示了PrivilegedExceptionAction.run方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: runFunction
import java.security.PrivilegedExceptionAction; //導入方法依賴的package包/類
/** Run the guarded business function, only if the current thread has access.
* This guarded function may throw a PrivilegedActionException which will contain
* the real exception
* @return Returns back the object that the guarded code returned
* @param functionName Name of the business function being guarded
* @param action An action object which will be executed, this should contain the guarded code
* @throws PrivilegedActionException This is the wrapped exception the the guarded code threw
* @throws AccessControlException This is thrown if the user doesn't have authorization for this function
*/
public static Object runFunction(String functionName, PrivilegedExceptionAction action)
throws PrivilegedActionException, AccessControlException {
if(hasAccess(functionName)) {
try {
return action.run();
} catch (Exception e) {
throw new PrivilegedActionException(e);
}
} else {
// Access Defined
log.info("Access Denied To Business Function: " + functionName);
throw new AccessControlException("Business Function:" + functionName);
}
}
示例2: shouldThrow
import java.security.PrivilegedExceptionAction; //導入方法依賴的package包/類
private void shouldThrow(PrivilegedExceptionAction<Object> action,
Class<? extends Throwable> except) {
try {
action.run();
Assert.fail("action did not throw " + except);
} catch (Throwable th) {
LOG.info("Caught an exception: ", th);
assertEquals("action threw wrong exception", except, th.getClass());
}
}
示例3: execute
import java.security.PrivilegedExceptionAction; //導入方法依賴的package包/類
@Override
public <T> T execute(PrivilegedExceptionAction<T> action)
throws Exception {
return action.run();
}
示例4: callable
import java.security.PrivilegedExceptionAction; //導入方法依賴的package包/類
/**
* Returns a {@link Callable} object that, when
* called, runs the given privileged exception action and returns
* its result.
* @param action the privileged exception action to run
* @return a callable object
* @throws NullPointerException if action null
*/
public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) {
if (action == null)
throw new NullPointerException();
return new Callable<Object>() {
public Object call() throws Exception { return action.run(); }};
}