]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/eclipse/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java
Improve Eclipse security
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.ui.rcp / src / main / java / org / argeo / security / ui / rcp / AbstractSecureApplication.java
1 package org.argeo.security.ui.rcp;
2
3 import java.security.PrivilegedAction;
4
5 import javax.security.auth.Subject;
6
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;
19
20 public abstract class AbstractSecureApplication implements IApplication {
21 private static final Log log = LogFactory
22 .getLog(AbstractSecureApplication.class);
23
24 protected abstract WorkbenchAdvisor createWorkbenchAdvisor();
25
26 public Object start(IApplicationContext context) throws Exception {
27
28 Integer returnCode = null;
29 Display display = PlatformUI.createDisplay();
30
31 // Force login
32 String username = CurrentUser.getUsername();
33 if (log.isDebugEnabled())
34 log.debug("Logged in as " + username);
35
36 try {
37 returnCode = (Integer) Subject.doAs(CurrentUser.getSubject(),
38 getRunAction(display));
39 if (log.isDebugEnabled())
40 log.debug("secure action completed");
41 CurrentUser.logout();
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);
48 return returnCode;
49 } finally {
50 display.dispose();
51 }
52 }
53
54 protected Integer processReturnCode(Integer returnCode) {
55 return returnCode;
56 }
57
58 @SuppressWarnings("rawtypes")
59 private PrivilegedAction getRunAction(final Display display) {
60 return new PrivilegedAction() {
61
62 public Object run() {
63 int result = createAndRunWorkbench(display);
64 return new Integer(result);
65 }
66 };
67 }
68
69 protected Integer createAndRunWorkbench(Display display) {
70 return PlatformUI.createAndRunWorkbench(display,
71 createWorkbenchAdvisor());
72 }
73
74 public void stop() {
75 final IWorkbench workbench;
76 try {
77 workbench = PlatformUI.getWorkbench();
78 } catch (Exception e) {
79 return;
80 }
81 if (workbench == null)
82 return;
83 final Display display = workbench.getDisplay();
84 if (display != null && !display.isDisposed())
85 display.syncExec(new Runnable() {
86
87 public void run() {
88 if (!display.isDisposed())
89 workbench.close();
90 }
91 });
92
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);
98
99 }
100
101 }