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
33 // Thread.sleep(3000);
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();
50 Subject subject
= null;
53 subject
= loginContext
.getSubject();
54 } catch (LoginException e
) {
55 log
.error("Error when logging in.", e
);
59 } catch (InterruptedException e1
) {
65 // identify after successful login
66 if (log
.isDebugEnabled())
67 log
.debug("subject=" + subject
);
68 final String username
= subject
.getPrincipals().iterator().next()
70 if (log
.isDebugEnabled())
71 log
.debug(username
+ " logged in");
72 display
.disposeExec(new Runnable() {
74 log
.debug("Display disposed");
75 logout(loginContext
, username
);
80 PrivilegedAction
<?
> privilegedAction
= new PrivilegedAction
<Object
>() {
82 int result
= PlatformUI
.createAndRunWorkbench(display
,
83 createWorkbenchAdvisor(username
));
84 return new Integer(result
);
88 Integer returnCode
= (Integer
) Subject
.doAs(subject
,
90 logout(loginContext
, username
);
91 return processReturnCode(returnCode
);
92 } catch (Exception e
) {
94 logout(loginContext
, username
);
95 log
.error("Unexpected error", e
);
102 protected Integer
processReturnCode(Integer returnCode
) {
103 if (returnCode
== PlatformUI
.RETURN_RESTART
)
104 return IApplication
.EXIT_RESTART
;
106 return IApplication
.EXIT_OK
;
109 static void logout(ILoginContext secureContext
, String username
) {
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
);
120 final IWorkbench workbench
;
122 workbench
= PlatformUI
.getWorkbench();
123 } catch (Exception e
) {
126 if (workbench
== null)
128 final Display display
= workbench
.getDisplay();
129 if (display
!= null && !display
.isDisposed())
130 display
.syncExec(new Runnable() {
133 if (!display
.isDisposed())
138 if (log
.isDebugEnabled())
139 log
.debug("workbench stopped");