1 package org
.argeo
.security
.ui
.rcp
;
3 import java
.security
.PrivilegedAction
;
5 import javax
.security
.auth
.Subject
;
6 import javax
.security
.auth
.login
.LoginException
;
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
;
20 * RCP workbench initialization
22 public abstract class AbstractSecureApplication
implements IApplication
{
23 private static final Log log
= LogFactory
24 .getLog(AbstractSecureApplication
.class);
26 protected WorkbenchAdvisor
createWorkbenchAdvisor(String username
) {
27 return new SecureWorkbenchAdvisor(username
);
30 public Object
start(IApplicationContext context
) throws Exception
{
31 // wait for the system to be initialized
34 } catch (Exception e2
) {
38 // choose login context
39 final ILoginContext loginContext
;
40 if (OperatingSystem
.os
== OperatingSystem
.WINDOWS
)
41 loginContext
= SecureApplicationActivator
42 .createLoginContext(SecureApplicationActivator
.CONTEXT_WINDOWS
);
44 loginContext
= SecureApplicationActivator
45 .createLoginContext(SecureApplicationActivator
.CONTEXT_NIX
);
47 final Display display
= PlatformUI
.createDisplay();
49 Subject subject
= null;
52 subject
= loginContext
.getSubject();
53 } catch (LoginException e
) {
54 log
.error("Error when logging in.", e
);
58 } catch (InterruptedException e1
) {
64 // identify after successful login
65 if (log
.isDebugEnabled())
66 log
.debug("subject=" + subject
);
67 final String username
= subject
.getPrincipals().iterator().next()
69 if (log
.isDebugEnabled())
70 log
.debug(username
+ " logged in");
71 display
.disposeExec(new Runnable() {
73 log
.debug("Display disposed");
74 logout(loginContext
, username
);
79 PrivilegedAction
<?
> privilegedAction
= new PrivilegedAction
<Object
>() {
81 int result
= PlatformUI
.createAndRunWorkbench(display
,
82 createWorkbenchAdvisor(username
));
83 return new Integer(result
);
87 Integer returnCode
= (Integer
) Subject
.doAs(subject
,
89 logout(loginContext
, username
);
90 return processReturnCode(returnCode
);
91 } catch (Exception e
) {
93 logout(loginContext
, username
);
94 log
.error("Unexpected error", e
);
101 protected Integer
processReturnCode(Integer returnCode
) {
102 if (returnCode
== PlatformUI
.RETURN_RESTART
)
103 return IApplication
.EXIT_RESTART
;
105 return IApplication
.EXIT_OK
;
108 static void logout(ILoginContext secureContext
, String username
) {
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
);
119 final IWorkbench workbench
;
121 workbench
= PlatformUI
.getWorkbench();
122 } catch (Exception e
) {
125 if (workbench
== null)
127 final Display display
= workbench
.getDisplay();
128 if (display
!= null && !display
.isDisposed())
129 display
.syncExec(new Runnable() {
132 if (!display
.isDisposed())
137 if (log
.isDebugEnabled())
138 log
.debug("workbench stopped");