]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/eclipse/plugins/org.argeo.security.ui.application/src/main/java/org/argeo/security/ui/application/AbstractSecureApplication.java
Change password dialog
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.ui.application / src / main / java / org / argeo / security / ui / application / 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.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;
26
27 public abstract class AbstractSecureApplication implements IApplication {
28 private static final Log log = LogFactory
29 .getLog(AbstractSecureApplication.class);
30
31 protected abstract WorkbenchAdvisor createWorkbenchAdvisor();
32
33 public Object start(IApplicationContext context) throws Exception {
34
35 Integer returnCode = null;
36 Display display = PlatformUI.createDisplay();
37
38 // Force login
39
40 try {
41 String username = null;
42 Exception loginException = null;
43 try {
44 username = CurrentUser.getUsername();
45 } catch (Exception e) {
46 loginException = e;
47 }
48 if (username == null) {
49 IStatus status = new Status(IStatus.ERROR,
50 "org.argeo.security.application", "Login is mandatory",
51 loginException);
52 ErrorDialog.openError(null, "Error", "Shutdown...", status);
53 return status.getSeverity();
54 }
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");
61 CurrentUser.logout();
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);
68 return returnCode;
69 } finally {
70 display.dispose();
71 }
72 }
73
74 protected Integer processReturnCode(Integer returnCode) {
75 return returnCode;
76 }
77
78 @SuppressWarnings("rawtypes")
79 private PrivilegedAction getRunAction(final Display display) {
80 return new PrivilegedAction() {
81
82 public Object run() {
83 int result = createAndRunWorkbench(display);
84 return new Integer(result);
85 }
86 };
87 }
88
89 protected Integer createAndRunWorkbench(Display display) {
90 return PlatformUI.createAndRunWorkbench(display,
91 createWorkbenchAdvisor());
92 }
93
94 public void stop() {
95 final IWorkbench workbench;
96 try {
97 workbench = PlatformUI.getWorkbench();
98 } catch (Exception e) {
99 return;
100 }
101 if (workbench == null)
102 return;
103 final Display display = workbench.getDisplay();
104 if (display != null && !display.isDisposed())
105 display.syncExec(new Runnable() {
106
107 public void run() {
108 if (!display.isDisposed())
109 workbench.close();
110 }
111 });
112
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);
118
119 }
120
121 }