]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AbstractSecureApplication.java
3da1bc7b4474ebfa8f6dc4bc29e4680d324282b9
[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 Subject subject = null;
50 try {
51 loginContext.login();
52 subject = loginContext.getSubject();
53 } catch (LoginException e) {
54 log.error("Error when logging in.", e);
55 display.dispose();
56 try {
57 Thread.sleep(2000);
58 } catch (InterruptedException e1) {
59 // silent
60 }
61 return null;
62 }
63
64 // identify after successful login
65 if (log.isDebugEnabled())
66 log.debug("subject=" + subject);
67 final String username = subject.getPrincipals().iterator().next()
68 .getName();
69 if (log.isDebugEnabled())
70 log.debug(username + " logged in");
71 display.disposeExec(new Runnable() {
72 public void run() {
73 log.debug("Display disposed");
74 logout(loginContext, username);
75 }
76 });
77
78 try {
79 PrivilegedAction<?> privilegedAction = new PrivilegedAction<Object>() {
80 public Object run() {
81 int result = PlatformUI.createAndRunWorkbench(display,
82 createWorkbenchAdvisor(username));
83 return new Integer(result);
84 }
85 };
86
87 Integer returnCode = (Integer) Subject.doAs(subject,
88 privilegedAction);
89 logout(loginContext, username);
90 return processReturnCode(returnCode);
91 } catch (Exception e) {
92 if (subject != null)
93 logout(loginContext, username);
94 log.error("Unexpected error", e);
95 } finally {
96 display.dispose();
97 }
98 return null;
99 }
100
101 protected Integer processReturnCode(Integer returnCode) {
102 if (returnCode == PlatformUI.RETURN_RESTART)
103 return IApplication.EXIT_RESTART;
104 else
105 return IApplication.EXIT_OK;
106 }
107
108 static void logout(ILoginContext secureContext, String username) {
109 try {
110 secureContext.logout();
111 log.info("Logged out " + (username != null ? username : "")
112 + " (THREAD=" + Thread.currentThread().getId() + ")");
113 } catch (LoginException e) {
114 log.error("Erorr when logging out", e);
115 }
116 }
117
118 public void stop() {
119 final IWorkbench workbench;
120 try {
121 workbench = PlatformUI.getWorkbench();
122 } catch (Exception e) {
123 return;
124 }
125 if (workbench == null)
126 return;
127 final Display display = workbench.getDisplay();
128 if (display != null && !display.isDisposed())
129 display.syncExec(new Runnable() {
130
131 public void run() {
132 if (!display.isDisposed())
133 workbench.close();
134 }
135 });
136
137 if (log.isDebugEnabled())
138 log.debug("workbench stopped");
139 }
140
141 }