]> git.argeo.org Git - lgpl/argeo-commons.git/blob - ui/rcp/AbstractSecureApplication.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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.OperatingSystem;
11 import org.eclipse.equinox.app.IApplication;
12 import org.eclipse.equinox.app.IApplicationContext;
13 import org.eclipse.equinox.security.auth.ILoginContext;
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 * RCP workbench initialization
21 */
22 public abstract class AbstractSecureApplication implements IApplication {
23 final static String NODE_REPO_URI = "argeo.node.repo.uri";
24
25 private static final Log log = LogFactory
26 .getLog(AbstractSecureApplication.class);
27
28 protected WorkbenchAdvisor createWorkbenchAdvisor(String username) {
29 return new SecureWorkbenchAdvisor(username);
30 }
31
32 public Object start(IApplicationContext context) throws Exception {
33 // wait for the system to be initialized
34 // try {
35 // Thread.sleep(3000);
36 // } catch (Exception e2) {
37 // // silent
38 // }
39
40 boolean remote = System.getProperty(NODE_REPO_URI) != null;
41
42 // choose login context
43 final ILoginContext loginContext;
44 if (remote) {
45 loginContext = SecureApplicationActivator
46 .createLoginContext(SecureApplicationActivator.CONTEXT_REMOTE);
47 } else {
48 if (OperatingSystem.os == OperatingSystem.WINDOWS)
49 loginContext = SecureApplicationActivator
50 .createLoginContext(SecureApplicationActivator.CONTEXT_WINDOWS);
51 else
52 loginContext = SecureApplicationActivator
53 .createLoginContext(SecureApplicationActivator.CONTEXT_NIX);
54 }
55
56 final Display display = PlatformUI.createDisplay();
57
58 // login
59 Subject subject = null;
60 try {
61 loginContext.login();
62 subject = loginContext.getSubject();
63 } catch (LoginException e) {
64 log.error("Error when logging in.", e);
65 display.dispose();
66 try {
67 Thread.sleep(2000);
68 } catch (InterruptedException e1) {
69 // silent
70 }
71 return null;
72 }
73
74 // identify after successful login
75 if (log.isDebugEnabled())
76 log.debug("subject=" + subject);
77 final String username = subject.getPrincipals().iterator().next()
78 .getName();
79 if (log.isDebugEnabled())
80 log.debug(username + " logged in");
81 display.disposeExec(new Runnable() {
82 public void run() {
83 log.debug("Display disposed");
84 logout(loginContext, username);
85 }
86 });
87
88 try {
89 PrivilegedAction<?> privilegedAction = new PrivilegedAction<Object>() {
90 public Object run() {
91 int result = PlatformUI.createAndRunWorkbench(display,
92 createWorkbenchAdvisor(username));
93 return new Integer(result);
94 }
95 };
96
97 Integer returnCode = (Integer) Subject.doAs(subject,
98 privilegedAction);
99 logout(loginContext, username);
100 return processReturnCode(returnCode);
101 } catch (Exception e) {
102 if (subject != null)
103 logout(loginContext, username);
104 log.error("Unexpected error", e);
105 } finally {
106 display.dispose();
107 }
108 return null;
109 }
110
111 protected Integer processReturnCode(Integer returnCode) {
112 if (returnCode == PlatformUI.RETURN_RESTART)
113 return IApplication.EXIT_RESTART;
114 else
115 return IApplication.EXIT_OK;
116 }
117
118 static void logout(ILoginContext secureContext, String username) {
119 try {
120 secureContext.logout();
121 log.info("Logged out " + (username != null ? username : "")
122 + " (THREAD=" + Thread.currentThread().getId() + ")");
123 } catch (LoginException e) {
124 log.error("Erorr when logging out", e);
125 }
126 }
127
128 public void stop() {
129 final IWorkbench workbench;
130 try {
131 workbench = PlatformUI.getWorkbench();
132 } catch (Exception e) {
133 return;
134 }
135 if (workbench == null)
136 return;
137 final Display display = workbench.getDisplay();
138 if (display != null && !display.isDisposed())
139 display.syncExec(new Runnable() {
140
141 public void run() {
142 if (!display.isDisposed())
143 workbench.close();
144 }
145 });
146
147 if (log.isDebugEnabled())
148 log.debug("workbench stopped");
149 }
150
151 }