1 package org
.argeo
.security
.ui
.rcp
;
3 import java
.security
.PrivilegedAction
;
5 import javax
.security
.auth
.Subject
;
7 import org
.apache
.commons
.logging
.Log
;
8 import org
.apache
.commons
.logging
.LogFactory
;
9 import org
.argeo
.security
.equinox
.CurrentUser
;
10 import org
.eclipse
.core
.runtime
.IStatus
;
11 import org
.eclipse
.core
.runtime
.Status
;
12 import org
.eclipse
.equinox
.app
.IApplication
;
13 import org
.eclipse
.equinox
.app
.IApplicationContext
;
14 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
15 import org
.eclipse
.swt
.widgets
.Display
;
16 import org
.eclipse
.ui
.IWorkbench
;
17 import org
.eclipse
.ui
.PlatformUI
;
18 import org
.eclipse
.ui
.application
.WorkbenchAdvisor
;
20 public abstract class AbstractSecureApplication
implements IApplication
{
21 private static final Log log
= LogFactory
22 .getLog(AbstractSecureApplication
.class);
24 protected abstract WorkbenchAdvisor
createWorkbenchAdvisor();
26 public Object
start(IApplicationContext context
) throws Exception
{
28 Integer returnCode
= null;
29 Display display
= PlatformUI
.createDisplay();
32 String username
= CurrentUser
.getUsername();
33 if (log
.isDebugEnabled())
34 log
.debug("Logged in as " + username
);
37 returnCode
= (Integer
) Subject
.doAs(CurrentUser
.getSubject(),
38 getRunAction(display
));
39 if (log
.isDebugEnabled())
40 log
.debug("secure action completed");
42 return processReturnCode(returnCode
);
43 } catch (Exception e
) {
44 // e.printStackTrace();
45 IStatus status
= new Status(IStatus
.ERROR
,
46 "org.argeo.security.rcp", "Login failed", e
);
47 ErrorDialog
.openError(null, "Error", "Login failed", status
);
54 protected Integer
processReturnCode(Integer returnCode
) {
58 @SuppressWarnings("rawtypes")
59 private PrivilegedAction
getRunAction(final Display display
) {
60 return new PrivilegedAction() {
63 int result
= createAndRunWorkbench(display
);
64 return new Integer(result
);
69 protected Integer
createAndRunWorkbench(Display display
) {
70 return PlatformUI
.createAndRunWorkbench(display
,
71 createWorkbenchAdvisor());
75 final IWorkbench workbench
;
77 workbench
= PlatformUI
.getWorkbench();
78 } catch (Exception e
) {
81 if (workbench
== null)
83 final Display display
= workbench
.getDisplay();
84 if (display
!= null && !display
.isDisposed())
85 display
.syncExec(new Runnable() {
88 if (!display
.isDisposed())
93 if (log
.isDebugEnabled())
94 log
.debug("workbench stopped");
95 String username
= CurrentUser
.getUsername();
96 if (log
.isDebugEnabled())
97 log
.debug("workbench stopped, logged in as " + username
);