1 package org
.argeo
.security
.ui
.rcp
;
3 import java
.security
.PrivilegedAction
;
5 import javax
.security
.auth
.Subject
;
6 import javax
.security
.auth
.login
.LoginException
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.eclipse
.ui
.dialogs
.Error
;
11 import org
.eclipse
.core
.runtime
.IStatus
;
12 import org
.eclipse
.core
.runtime
.Status
;
13 import org
.eclipse
.equinox
.app
.IApplication
;
14 import org
.eclipse
.equinox
.app
.IApplicationContext
;
15 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
16 import org
.eclipse
.swt
.widgets
.Display
;
17 import org
.eclipse
.ui
.IWorkbench
;
18 import org
.eclipse
.ui
.PlatformUI
;
19 import org
.eclipse
.ui
.application
.WorkbenchAdvisor
;
22 * Common base class for authenticated access to the Eclipse UI framework (RAP
25 public abstract class AbstractSecureApplication
implements IApplication
{
26 private static final Log log
= LogFactory
27 .getLog(AbstractSecureApplication
.class);
29 protected abstract WorkbenchAdvisor
createWorkbenchAdvisor();
31 public Object
start(IApplicationContext context
) throws Exception
{
33 Integer returnCode
= null;
34 Display display
= PlatformUI
.createDisplay();
36 Subject subject
= null;
40 SecureApplicationActivator
.getLoginContext().login();
41 subject
= SecureApplicationActivator
.getLoginContext()
44 } catch (LoginException e
) {
45 Error
.show("Cannot login", e
);
47 } catch (Exception e
) {
48 Error
.show("Unexpected exception while trying to login", e
);
53 if (subject
== null) {
54 // IStatus status = new Status(IStatus.ERROR,
55 // "org.argeo.security.application", "Login is mandatory",
57 // ErrorDialog.openError(null, "Error", "Shutdown...", status);
58 // return status.getSeverity();
60 // TODO: log as anonymous
63 if (subject
!= null) {
64 returnCode
= (Integer
) Subject
.doAs(subject
,
65 getRunAction(display
));
66 SecureApplicationActivator
.getLoginContext().logout();
67 return processReturnCode(returnCode
);
71 } catch (Exception e
) {
72 // e.printStackTrace();
73 IStatus status
= new Status(IStatus
.ERROR
,
74 "org.argeo.security.rcp", "Login failed", e
);
75 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
82 protected Integer
processReturnCode(Integer returnCode
) {
86 @SuppressWarnings("rawtypes")
87 private PrivilegedAction
getRunAction(final Display display
) {
88 return new PrivilegedAction() {
91 int result
= createAndRunWorkbench(display
);
92 return new Integer(result
);
97 protected Integer
createAndRunWorkbench(Display display
) {
98 return PlatformUI
.createAndRunWorkbench(display
,
99 createWorkbenchAdvisor());
103 final IWorkbench workbench
;
105 workbench
= PlatformUI
.getWorkbench();
106 } catch (Exception e
) {
109 if (workbench
== null)
111 final Display display
= workbench
.getDisplay();
112 if (display
!= null && !display
.isDisposed())
113 display
.syncExec(new Runnable() {
116 if (!display
.isDisposed())
121 if (log
.isDebugEnabled())
122 log
.debug("workbench stopped");
123 // String username = CurrentUser.getUsername();
124 // if (log.isDebugEnabled())
125 // log.debug("workbench stopped, logged in as " + username);