View Javadoc
1   package org.argeo.cms.e4;
2   
3   import java.security.AccessControlContext;
4   import java.security.AccessController;
5   import java.security.PrivilegedAction;
6   
7   import javax.security.auth.Subject;
8   
9   import org.eclipse.core.runtime.IProgressMonitor;
10  import org.eclipse.core.runtime.IStatus;
11  import org.eclipse.core.runtime.jobs.Job;
12  
13  /**
14   * Propagate authentication to an eclipse job. Typically to execute a privileged
15   * action outside the UI thread
16   */
17  public abstract class PrivilegedJob extends Job {
18  	private final Subject subject;
19  
20  	public PrivilegedJob(String jobName) {
21  		this(jobName, AccessController.getContext());
22  	}
23  
24  	public PrivilegedJob(String jobName,
25  			AccessControlContext accessControlContext) {
26  		super(jobName);
27  		subject = Subject.getSubject(accessControlContext);
28  
29  		// Must be called *before* the job is scheduled,
30  		// it is required for the progress window to appear
31  		setUser(true);
32  	}
33  
34  	@Override
35  	protected IStatus run(final IProgressMonitor progressMonitor) {
36  		PrivilegedAction<IStatus> privilegedAction = new PrivilegedAction<IStatus>() {
37  			public IStatus run() {
38  				return doRun(progressMonitor);
39  			}
40  		};
41  		return Subject.doAs(subject, privilegedAction);
42  	}
43  
44  	/**
45  	 * Implement here what should be executed with default context
46  	 * authentication
47  	 */
48  	protected abstract IStatus doRun(IProgressMonitor progressMonitor);
49  }