- try {
- return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {
-
- @Override
- public T run() throws Exception {
- return action.call();
- }
-
- });
- } catch (PrivilegedActionException e) {
- throw new CompletionException("Failed to execute action for " + subject, e.getCause());
+ if (useThreadLocal) {
+ Subject previous = current();
+ current.set(subject);
+ try {
+ return action.call();
+ } catch (Exception e) {
+ throw new CompletionException("Failed to execute action for " + subject, e);
+ } finally {
+ current.set(previous);
+ }
+ } else {// legacy
+ try {
+ return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {
+
+ @Override
+ public T run() throws Exception {
+ return action.call();
+ }
+
+ });
+ } catch (PrivilegedActionException e) {
+ throw new CompletionException("Failed to execute action for " + subject, e.getCause());
+ } catch (Exception e) {
+ throw new CompletionException("Failed to execute action for " + subject, e);
+ }