]> 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/SecureRcp.java
Adapt Security to RCP
[lgpl/argeo-commons.git] / security / eclipse / plugins / org.argeo.security.ui.rcp / src / main / java / org / argeo / security / ui / rcp / SecureRcp.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.argeo.security.equinox.CurrentUser;
8 import org.eclipse.core.runtime.IStatus;
9 import org.eclipse.core.runtime.Status;
10 import org.eclipse.equinox.app.IApplication;
11 import org.eclipse.equinox.app.IApplicationContext;
12 import org.eclipse.jface.dialogs.ErrorDialog;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.ui.IWorkbench;
15 import org.eclipse.ui.PlatformUI;
16
17 public class SecureRcp implements IApplication {
18 public Object start(IApplicationContext context) throws Exception {
19 String username = CurrentUser.getUsername();
20 Integer returnCode = null;
21 Display display = PlatformUI.createDisplay();
22 try {
23 returnCode = (Integer) Subject.doAs(CurrentUser.getSubject(),
24 getRunAction(display));
25 if (returnCode == PlatformUI.RETURN_RESTART)
26 return IApplication.EXIT_RESTART;
27 else
28 return IApplication.EXIT_OK;
29 } catch (Exception e) {
30 // e.printStackTrace();
31 IStatus status = new Status(IStatus.ERROR,
32 "org.eclipse.rap.security.demo", "Login failed", e);
33 ErrorDialog.openError(null, "Error", "Login failed", status);
34 } finally {
35 display.dispose();
36 }
37 return returnCode;
38 }
39
40 private PrivilegedAction getRunAction(final Display display) {
41 return new PrivilegedAction() {
42
43 public Object run() {
44 int result = PlatformUI.createAndRunWorkbench(display,
45 new SecureWorkbenchAdvisor());
46 return new Integer(result);
47 }
48 };
49 }
50
51 public void stop() {
52 final IWorkbench workbench;
53 try {
54 workbench = PlatformUI.getWorkbench();
55 } catch (Exception e) {
56 return;
57 }
58 if (workbench == null)
59 return;
60 final Display display = workbench.getDisplay();
61 display.syncExec(new Runnable() {
62
63 public void run() {
64 if (!display.isDisposed())
65 workbench.close();
66 }
67 });
68 }
69
70 }