]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KeyBasedSystemExecutionService.java
b5791c587f00ec2dd9ea021c4c06172cf3be7527
[lgpl/argeo-commons.git] / KeyBasedSystemExecutionService.java
1 package org.argeo.security.core;
2
3 import java.util.concurrent.Callable;
4 import java.util.concurrent.Executors;
5 import java.util.concurrent.Future;
6 import java.util.concurrent.FutureTask;
7
8 import org.argeo.ArgeoException;
9 import org.argeo.security.SystemExecutionService;
10
11 /**
12 * Implementation of a {@link SystemExecutionService} using a key-based
13 * {@link InternalAuthentication}
14 */
15 public class KeyBasedSystemExecutionService extends AbstractSystemExecution
16 implements SystemExecutionService {
17 public void execute(Runnable runnable) {
18 try {
19 wrapWithSystemAuthentication(Executors.callable(runnable)).call();
20 } catch (RuntimeException e) {
21 throw e;
22 } catch (Exception e) {
23 throw new ArgeoException(
24 "Exception when running system authenticated task", e);
25 }
26 }
27
28 public <T> Future<T> submit(Callable<T> task) {
29 FutureTask<T> future = new FutureTask<T>(
30 wrapWithSystemAuthentication(task));
31 future.run();
32 return future;
33 }
34
35 protected <T> Callable<T> wrapWithSystemAuthentication(
36 final Callable<T> runnable) {
37 return new Callable<T>() {
38
39 public T call() throws Exception {
40 authenticateAsSystem();
41 try {
42 return runnable.call();
43 } finally {
44 deauthenticateAsSystem();
45 }
46 }
47 };
48 }
49 }