From: Mathieu Baudier Date: Mon, 14 May 2018 18:09:04 +0000 (+0200) Subject: Introduce E4 privileged job X-Git-Tag: argeo-commons-2.1.74~37 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=3c14f9305b2c00fc76daf8769ade649140394806 Introduce E4 privileged job --- diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/PrivilegedJob.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/PrivilegedJob.java new file mode 100644 index 000000000..89055d2ff --- /dev/null +++ b/org.argeo.cms.e4/src/org/argeo/cms/e4/PrivilegedJob.java @@ -0,0 +1,49 @@ +package org.argeo.cms.e4; + +import java.security.AccessControlContext; +import java.security.AccessController; +import java.security.PrivilegedAction; + +import javax.security.auth.Subject; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.jobs.Job; + +/** + * Propagate authentication to an eclipse job. Typically to execute a privileged + * action outside the UI thread + */ +public abstract class PrivilegedJob extends Job { + private final Subject subject; + + public PrivilegedJob(String jobName) { + this(jobName, AccessController.getContext()); + } + + public PrivilegedJob(String jobName, + AccessControlContext accessControlContext) { + super(jobName); + subject = Subject.getSubject(accessControlContext); + + // Must be called *before* the job is scheduled, + // it is required for the progress window to appear + setUser(true); + } + + @Override + protected IStatus run(final IProgressMonitor progressMonitor) { + PrivilegedAction privilegedAction = new PrivilegedAction() { + public IStatus run() { + return doRun(progressMonitor); + } + }; + return Subject.doAs(subject, privilegedAction); + } + + /** + * Implement here what should be executed with default context + * authentication + */ + protected abstract IStatus doRun(IProgressMonitor progressMonitor); +}