From 6b3ffc999e1ba5719420b861c7fe411bbbdfa9e2 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Sun, 17 Apr 2011 07:58:19 +0000 Subject: [PATCH] Introduce system callables NEW - bug 17: Generalize agent management and registration beyond JMS https://bugzilla.argeo.org/show_bug.cgi?id=17 git-svn-id: https://svn.argeo.org/commons/trunk@4442 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../security/SystemExecutionService.java | 11 +++++++- .../core/KeyBasedSystemExecutionService.java | 27 +++++++++++++++---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SystemExecutionService.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SystemExecutionService.java index b67eb27b1..e03f27ab2 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SystemExecutionService.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/SystemExecutionService.java @@ -1,6 +1,8 @@ package org.argeo.security; +import java.util.concurrent.Callable; import java.util.concurrent.Executor; +import java.util.concurrent.Future; /** * Allows to execute code authenticated as a system user (that is not a real @@ -10,9 +12,16 @@ import java.util.concurrent.Executor; */ public interface SystemExecutionService extends Executor { /** - * Executes this Runnable within a system authenticated context. + * Executes this {@link Runnable} within a system authenticated context. * Implementations should make sure that this method is properly secured via * Java permissions since it could access to everything without credentials. */ public void execute(Runnable runnable); + + /** + * Executes this {@link Callable} within a system authenticated context. + * Implementations should make sure that this method is properly secured via + * Java permissions since it could access to everything without credentials. + */ + public Future submit(Callable task); } diff --git a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java index 392371515..b2dfc4a24 100644 --- a/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java +++ b/security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/core/KeyBasedSystemExecutionService.java @@ -1,6 +1,10 @@ package org.argeo.security.core; import java.security.AccessController; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.FutureTask; import javax.security.auth.Subject; @@ -20,13 +24,26 @@ public class KeyBasedSystemExecutionService implements SystemExecutionService { private String systemAuthenticationKey; public void execute(Runnable runnable) { - wrapWithSystemAuthentication(runnable).run(); + try { + wrapWithSystemAuthentication(Executors.callable(runnable)).call(); + } catch (Exception e) { + throw new ArgeoException( + "Exception when running system authenticated task", e); + } } - protected Runnable wrapWithSystemAuthentication(final Runnable runnable) { - return new Runnable() { + public Future submit(Callable task) { + FutureTask future = new FutureTask( + wrapWithSystemAuthentication(task)); + future.run(); + return future; + } + + protected Callable wrapWithSystemAuthentication( + final Callable runnable) { + return new Callable() { - public void run() { + public T call() throws Exception { SecurityContext securityContext = SecurityContextHolder .getContext(); Authentication currentAuth = securityContext @@ -51,7 +68,7 @@ public class KeyBasedSystemExecutionService implements SystemExecutionService { systemAuthenticationKey)); securityContext.setAuthentication(auth); try { - runnable.run(); + return runnable.call(); } finally { // remove the authentication securityContext.setAuthentication(null); -- 2.30.2