]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.rap/src/main/java/org/argeo/security/ui/rap/SecureEntryPoint.java
Separate RCP and RAP application/entry point
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.rap / src / main / java / org / argeo / security / ui / rap / SecureEntryPoint.java
1 package org.argeo.security.ui.rap;
2
3 import java.security.PrivilegedAction;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginException;
7
8 import org.argeo.eclipse.ui.dialogs.Error;
9 import org.eclipse.core.runtime.IStatus;
10 import org.eclipse.core.runtime.Status;
11 import org.eclipse.jface.dialogs.ErrorDialog;
12 import org.eclipse.rwt.lifecycle.IEntryPoint;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.ui.PlatformUI;
15 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
16 import org.eclipse.ui.application.WorkbenchAdvisor;
17 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
18
19 public class SecureEntryPoint implements IEntryPoint {
20
21 @Override
22 public int createUI() {
23 Integer returnCode = null;
24 Display display = PlatformUI.createDisplay();
25 try {
26 Subject subject = null;
27 Boolean retry = true;
28 while (retry) {
29 try {
30 SecureRapActivator.getLoginContext().login();
31 subject = SecureRapActivator.getLoginContext()
32 .getSubject();
33 retry = false;
34 } catch (LoginException e) {
35 Error.show("Cannot login", e);
36 retry = true;
37 } catch (Exception e) {
38 Error.show("Unexpected exception while trying to login", e);
39 retry = false;
40 }
41 }
42
43 if (subject == null) {
44 // IStatus status = new Status(IStatus.ERROR,
45 // "org.argeo.security.application", "Login is mandatory",
46 // loginException);
47 // ErrorDialog.openError(null, "Error", "Shutdown...", status);
48 // return status.getSeverity();
49
50 // TODO: log as anonymous
51 }
52
53 if (subject != null) {
54 returnCode = (Integer) Subject.doAs(subject,
55 getRunAction(display));
56 SecureRapActivator.getLoginContext().logout();
57 return processReturnCode(returnCode);
58 } else {
59 return -1;
60 }
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 @SuppressWarnings("rawtypes")
73 private PrivilegedAction getRunAction(final Display display) {
74 return new PrivilegedAction() {
75
76 public Object run() {
77 int result = createAndRunWorkbench(display);
78 return new Integer(result);
79 }
80 };
81 }
82
83 protected Integer createAndRunWorkbench(Display display) {
84 return PlatformUI.createAndRunWorkbench(display,
85 createWorkbenchAdvisor());
86 }
87
88 protected Integer processReturnCode(Integer returnCode) {
89 return returnCode;
90 }
91
92 protected WorkbenchAdvisor createWorkbenchAdvisor() {
93 return new SecureWorkbenchAdvisor() {
94 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
95 IWorkbenchWindowConfigurer configurer) {
96 return new RapSecureWorkbenchWindowAdvisor(configurer);
97 }
98
99 };
100 }
101
102 }