]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.rcp/src/main/java/org/argeo/security/ui/rcp/AbstractSecureApplication.java
2a9dd0c2506d7f01033d27e30469e5cb07a084cb
[lgpl/argeo-commons.git] / security / 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 import javax.security.auth.login.LoginException;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.eclipse.ui.dialogs.Error;
11 import org.eclipse.core.runtime.IStatus;
12 import org.eclipse.core.runtime.Status;
13 import org.eclipse.equinox.app.IApplication;
14 import org.eclipse.equinox.app.IApplicationContext;
15 import org.eclipse.jface.dialogs.ErrorDialog;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.ui.IWorkbench;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.application.WorkbenchAdvisor;
20
21 /**
22 * Common base class for authenticated access to the Eclipse UI framework (RAP
23 * and RCP)
24 */
25 public abstract class AbstractSecureApplication implements IApplication {
26 private static final Log log = LogFactory
27 .getLog(AbstractSecureApplication.class);
28
29 protected abstract WorkbenchAdvisor createWorkbenchAdvisor();
30
31 public Object start(IApplicationContext context) throws Exception {
32
33 Integer returnCode = null;
34 Display display = PlatformUI.createDisplay();
35 try {
36 Subject subject = null;
37 Boolean retry = true;
38 while (retry) {
39 try {
40 SecureApplicationActivator.getLoginContext().login();
41 subject = SecureApplicationActivator.getLoginContext()
42 .getSubject();
43 retry = false;
44 } catch (LoginException e) {
45 Error.show("Cannot login", e);
46 retry = true;
47 } catch (Exception e) {
48 Error.show("Unexpected exception while trying to login", e);
49 retry = false;
50 }
51 }
52
53 if (subject == null) {
54 // IStatus status = new Status(IStatus.ERROR,
55 // "org.argeo.security.application", "Login is mandatory",
56 // loginException);
57 // ErrorDialog.openError(null, "Error", "Shutdown...", status);
58 // return status.getSeverity();
59
60 // TODO: log as anonymous
61 }
62
63 if (subject != null) {
64 returnCode = (Integer) Subject.doAs(subject,
65 getRunAction(display));
66 SecureApplicationActivator.getLoginContext().logout();
67 return processReturnCode(returnCode);
68 } else {
69 return -1;
70 }
71 } catch (Exception e) {
72 // e.printStackTrace();
73 IStatus status = new Status(IStatus.ERROR,
74 "org.argeo.security.rcp", "Login failed", e);
75 ErrorDialog.openError(null, "Error", "Shutdown...", status);
76 return returnCode;
77 } finally {
78 display.dispose();
79 }
80 }
81
82 protected Integer processReturnCode(Integer returnCode) {
83 return returnCode;
84 }
85
86 @SuppressWarnings("rawtypes")
87 private PrivilegedAction getRunAction(final Display display) {
88 return new PrivilegedAction() {
89
90 public Object run() {
91 int result = createAndRunWorkbench(display);
92 return new Integer(result);
93 }
94 };
95 }
96
97 protected Integer createAndRunWorkbench(Display display) {
98 return PlatformUI.createAndRunWorkbench(display,
99 createWorkbenchAdvisor());
100 }
101
102 public void stop() {
103 final IWorkbench workbench;
104 try {
105 workbench = PlatformUI.getWorkbench();
106 } catch (Exception e) {
107 return;
108 }
109 if (workbench == null)
110 return;
111 final Display display = workbench.getDisplay();
112 if (display != null && !display.isDisposed())
113 display.syncExec(new Runnable() {
114
115 public void run() {
116 if (!display.isDisposed())
117 workbench.close();
118 }
119 });
120
121 if (log.isDebugEnabled())
122 log.debug("workbench stopped");
123 // String username = CurrentUser.getUsername();
124 // if (log.isDebugEnabled())
125 // log.debug("workbench stopped, logged in as " + username);
126
127 }
128
129 }