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