]> git.argeo.org Git - lgpl/argeo-commons.git/blob - KeyBasedSystemExecutionService.java
07ae046539c93ca5dd8f2deddf9c6ee62db4b7c6
[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 (Exception e) {
21 throw new ArgeoException(
22 "Exception when running system authenticated task", e);
23 }
24 }
25
26 public <T> Future<T> submit(Callable<T> task) {
27 FutureTask<T> future = new FutureTask<T>(
28 wrapWithSystemAuthentication(task));
29 future.run();
30 return future;
31 }
32
33 protected <T> Callable<T> wrapWithSystemAuthentication(
34 final Callable<T> runnable) {
35 return new Callable<T>() {
36
37 public T call() throws Exception {
38 authenticateAsSystem();
39 try {
40 return runnable.call();
41 } finally {
42 deauthenticateAsSystem();
43 }
44 }
45 };
46 }
47 }