1 package org
.argeo
.security
.ui
.application
;
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
.jface
.dialogs
.ErrorSupportProvider
;
16 import org
.eclipse
.jface
.util
.Policy
;
17 import org
.eclipse
.jface
.window
.Window
.IExceptionHandler
;
18 import org
.eclipse
.swt
.widgets
.Composite
;
19 import org
.eclipse
.swt
.widgets
.Control
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.eclipse
.ui
.IWorkbench
;
22 import org
.eclipse
.ui
.PlatformUI
;
23 import org
.eclipse
.ui
.application
.WorkbenchAdvisor
;
24 import org
.eclipse
.ui
.internal
.statushandlers
.StackTraceSupportArea
;
25 import org
.eclipse
.ui
.statushandlers
.WorkbenchStatusDialogManager
;
27 public abstract class AbstractSecureApplication
implements IApplication
{
28 private static final Log log
= LogFactory
29 .getLog(AbstractSecureApplication
.class);
31 protected abstract WorkbenchAdvisor
createWorkbenchAdvisor();
33 public Object
start(IApplicationContext context
) throws Exception
{
35 Integer returnCode
= null;
36 Display display
= PlatformUI
.createDisplay();
41 String username
= null;
42 Exception loginException
= null;
44 username
= CurrentUser
.getUsername();
45 } catch (Exception e
) {
48 if (username
== null) {
49 IStatus status
= new Status(IStatus
.ERROR
,
50 "org.argeo.security.application", "Login is mandatory",
52 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
53 return status
.getSeverity();
55 if (log
.isDebugEnabled())
56 log
.debug("Logged in as " + username
);
57 returnCode
= (Integer
) Subject
.doAs(CurrentUser
.getSubject(),
58 getRunAction(display
));
59 if (log
.isDebugEnabled())
60 log
.debug("secure action completed");
62 return processReturnCode(returnCode
);
63 } catch (Exception e
) {
64 // e.printStackTrace();
65 IStatus status
= new Status(IStatus
.ERROR
,
66 "org.argeo.security.rcp", "Login failed", e
);
67 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
74 protected Integer
processReturnCode(Integer returnCode
) {
78 @SuppressWarnings("rawtypes")
79 private PrivilegedAction
getRunAction(final Display display
) {
80 return new PrivilegedAction() {
83 int result
= createAndRunWorkbench(display
);
84 return new Integer(result
);
89 protected Integer
createAndRunWorkbench(Display display
) {
90 return PlatformUI
.createAndRunWorkbench(display
,
91 createWorkbenchAdvisor());
95 final IWorkbench workbench
;
97 workbench
= PlatformUI
.getWorkbench();
98 } catch (Exception e
) {
101 if (workbench
== null)
103 final Display display
= workbench
.getDisplay();
104 if (display
!= null && !display
.isDisposed())
105 display
.syncExec(new Runnable() {
108 if (!display
.isDisposed())
113 if (log
.isDebugEnabled())
114 log
.debug("workbench stopped");
115 String username
= CurrentUser
.getUsername();
116 if (log
.isDebugEnabled())
117 log
.debug("workbench stopped, logged in as " + username
);