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 final static String NODE_REPO_URI
= "argeo.node.repo.uri";
25 private static final Log log
= LogFactory
26 .getLog(AbstractSecureApplication
.class);
28 protected WorkbenchAdvisor
createWorkbenchAdvisor(String username
) {
29 return new SecureWorkbenchAdvisor(username
);
32 public Object
start(IApplicationContext context
) throws Exception
{
33 // wait for the system to be initialized
35 // Thread.sleep(3000);
36 // } catch (Exception e2) {
40 boolean remote
= System
.getProperty(NODE_REPO_URI
) != null;
42 // choose login context
43 final ILoginContext loginContext
;
45 loginContext
= SecureApplicationActivator
46 .createLoginContext(SecureApplicationActivator
.CONTEXT_REMOTE
);
48 if (OperatingSystem
.os
== OperatingSystem
.WINDOWS
)
49 loginContext
= SecureApplicationActivator
50 .createLoginContext(SecureApplicationActivator
.CONTEXT_WINDOWS
);
52 loginContext
= SecureApplicationActivator
53 .createLoginContext(SecureApplicationActivator
.CONTEXT_NIX
);
56 final Display display
= PlatformUI
.createDisplay();
59 Subject subject
= null;
62 subject
= loginContext
.getSubject();
63 } catch (LoginException e
) {
64 log
.error("Error when logging in.", e
);
68 } catch (InterruptedException e1
) {
74 // identify after successful login
75 if (log
.isDebugEnabled())
76 log
.debug("subject=" + subject
);
77 final String username
= subject
.getPrincipals().iterator().next()
79 if (log
.isDebugEnabled())
80 log
.debug(username
+ " logged in");
81 display
.disposeExec(new Runnable() {
83 log
.debug("Display disposed");
84 logout(loginContext
, username
);
89 PrivilegedAction
<?
> privilegedAction
= new PrivilegedAction
<Object
>() {
91 int result
= PlatformUI
.createAndRunWorkbench(display
,
92 createWorkbenchAdvisor(username
));
93 return new Integer(result
);
97 Integer returnCode
= (Integer
) Subject
.doAs(subject
,
99 logout(loginContext
, username
);
100 return processReturnCode(returnCode
);
101 } catch (Exception e
) {
103 logout(loginContext
, username
);
104 log
.error("Unexpected error", e
);
111 protected Integer
processReturnCode(Integer returnCode
) {
112 if (returnCode
== PlatformUI
.RETURN_RESTART
)
113 return IApplication
.EXIT_RESTART
;
115 return IApplication
.EXIT_OK
;
118 static void logout(ILoginContext secureContext
, String username
) {
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
);
129 final IWorkbench workbench
;
131 workbench
= PlatformUI
.getWorkbench();
132 } catch (Exception e
) {
135 if (workbench
== null)
137 final Display display
= workbench
.getDisplay();
138 if (display
!= null && !display
.isDisposed())
139 display
.syncExec(new Runnable() {
142 if (!display
.isDisposed())
147 if (log
.isDebugEnabled())
148 log
.debug("workbench stopped");