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
.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();
34 String username
= null;
35 Exception loginException
= null;
37 username
= CurrentUser
.getUsername();
38 } catch (Exception e
) {
41 if (username
== null) {
42 IStatus status
= new Status(IStatus
.ERROR
,
43 "org.argeo.security.application", "Login is mandatory",
45 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
46 return status
.getSeverity();
48 if (log
.isDebugEnabled())
49 log
.debug("Logged in as " + username
);
50 returnCode
= (Integer
) Subject
.doAs(CurrentUser
.getSubject(),
51 getRunAction(display
));
52 if (log
.isDebugEnabled())
53 log
.debug("secure action completed");
55 return processReturnCode(returnCode
);
56 } catch (Exception e
) {
57 // e.printStackTrace();
58 IStatus status
= new Status(IStatus
.ERROR
,
59 "org.argeo.security.rcp", "Login failed", e
);
60 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
67 protected Integer
processReturnCode(Integer returnCode
) {
71 @SuppressWarnings("rawtypes")
72 private PrivilegedAction
getRunAction(final Display display
) {
73 return new PrivilegedAction() {
76 int result
= createAndRunWorkbench(display
);
77 return new Integer(result
);
82 protected Integer
createAndRunWorkbench(Display display
) {
83 return PlatformUI
.createAndRunWorkbench(display
,
84 createWorkbenchAdvisor());
88 final IWorkbench workbench
;
90 workbench
= PlatformUI
.getWorkbench();
91 } catch (Exception e
) {
94 if (workbench
== null)
96 final Display display
= workbench
.getDisplay();
97 if (display
!= null && !display
.isDisposed())
98 display
.syncExec(new Runnable() {
101 if (!display
.isDisposed())
106 if (log
.isDebugEnabled())
107 log
.debug("workbench stopped");
108 String username
= CurrentUser
.getUsername();
109 if (log
.isDebugEnabled())
110 log
.debug("workbench stopped, logged in as " + username
);