]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractSecureApplication.java
c3bf6b1131adf9adf933ec3e03c5f4c69e191e48
[lgpl/argeo-commons.git] / AbstractSecureApplication.java
1 package org.argeo.security.ui.application;
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
33 try {
34 String username = null;
35 Exception loginException = null;
36 try {
37 username = CurrentUser.getUsername();
38 } catch (Exception e) {
39 loginException = e;
40 }
41 if (username == null) {
42 IStatus status = new Status(IStatus.ERROR,
43 "org.argeo.security.application", "Login is mandatory",
44 loginException);
45 ErrorDialog.openError(null, "Error", "Shutdown...", status);
46 return status.getSeverity();
47 }
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");
54 CurrentUser.logout();
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);
61 return returnCode;
62 } finally {
63 display.dispose();
64 }
65 }
66
67 protected Integer processReturnCode(Integer returnCode) {
68 return returnCode;
69 }
70
71 @SuppressWarnings("rawtypes")
72 private PrivilegedAction getRunAction(final Display display) {
73 return new PrivilegedAction() {
74
75 public Object run() {
76 int result = createAndRunWorkbench(display);
77 return new Integer(result);
78 }
79 };
80 }
81
82 protected Integer createAndRunWorkbench(Display display) {
83 return PlatformUI.createAndRunWorkbench(display,
84 createWorkbenchAdvisor());
85 }
86
87 public void stop() {
88 final IWorkbench workbench;
89 try {
90 workbench = PlatformUI.getWorkbench();
91 } catch (Exception e) {
92 return;
93 }
94 if (workbench == null)
95 return;
96 final Display display = workbench.getDisplay();
97 if (display != null && !display.isDisposed())
98 display.syncExec(new Runnable() {
99
100 public void run() {
101 if (!display.isDisposed())
102 workbench.close();
103 }
104 });
105
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);
111
112 }
113
114 }