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