]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.core/src/org/argeo/security/core/KeyBasedSystemExecutionService.java
Reduce CMS size
[lgpl/argeo-commons.git] / org.argeo.security.core / src / org / argeo / security / core / KeyBasedSystemExecutionService.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.core;
17
18 import java.util.concurrent.Callable;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.Future;
21 import java.util.concurrent.FutureTask;
22
23 import org.argeo.ArgeoException;
24 import org.argeo.security.SystemExecutionService;
25
26 /**
27 * Implementation of a {@link SystemExecutionService} using a key-based
28 * {@link InternalAuthentication}
29 */
30 public class KeyBasedSystemExecutionService extends AbstractSystemExecution
31 implements SystemExecutionService {
32 public void execute(Runnable runnable) {
33 try {
34 wrapWithSystemAuthentication(Executors.callable(runnable)).call();
35 } catch (RuntimeException e) {
36 throw e;
37 } catch (Exception e) {
38 throw new ArgeoException(
39 "Exception when running system authenticated task", e);
40 }
41 }
42
43 public <T> Future<T> submit(Callable<T> task) {
44 FutureTask<T> future = new FutureTask<T>(
45 wrapWithSystemAuthentication(task));
46 future.run();
47 return future;
48 }
49
50 protected <T> Callable<T> wrapWithSystemAuthentication(
51 final Callable<T> runnable) {
52 return new Callable<T>() {
53
54 public T call() throws Exception {
55 authenticateAsSystem();
56 try {
57 return runnable.call();
58 } finally {
59 // deauthenticateAsSystem();
60 }
61 }
62 };
63 }
64 }