1 package org
.argeo
.security
.ui
.rap
;
3 import java
.security
.PrivilegedAction
;
6 import javax
.security
.auth
.Subject
;
7 import javax
.security
.auth
.login
.LoginException
;
9 import org
.apache
.commons
.logging
.Log
;
10 import org
.apache
.commons
.logging
.LogFactory
;
11 import org
.argeo
.eclipse
.ui
.dialogs
.Error
;
12 import org
.eclipse
.core
.runtime
.IStatus
;
13 import org
.eclipse
.core
.runtime
.Status
;
14 import org
.eclipse
.jface
.dialogs
.ErrorDialog
;
15 import org
.eclipse
.rwt
.RWT
;
16 import org
.eclipse
.rwt
.lifecycle
.IEntryPoint
;
17 import org
.eclipse
.rwt
.service
.SessionStoreEvent
;
18 import org
.eclipse
.rwt
.service
.SessionStoreListener
;
19 import org
.eclipse
.swt
.widgets
.Display
;
20 import org
.eclipse
.ui
.PlatformUI
;
21 import org
.eclipse
.ui
.application
.IWorkbenchWindowConfigurer
;
22 import org
.eclipse
.ui
.application
.WorkbenchAdvisor
;
23 import org
.eclipse
.ui
.application
.WorkbenchWindowAdvisor
;
24 import org
.springframework
.security
.Authentication
;
25 import org
.springframework
.security
.context
.SecurityContextHolder
;
27 public class SecureEntryPoint
implements IEntryPoint
, SessionStoreListener
{
28 private Log log
= LogFactory
.getLog(SecureEntryPoint
.class);
30 private final static String SECURITY_CONTEXT_ATTRIBUTE
= "securityContextAttribute";
33 public int createUI() {
34 // log.debug("THREAD=" + Thread.currentThread().getId()
35 // + ", RWT.getSessionStore().getId()="
36 // + RWT.getSessionStore().getId());
38 Authentication authen
= (Authentication
) RWT
.getSessionStore()
39 .getAttribute(SECURITY_CONTEXT_ATTRIBUTE
);
41 SecurityContextHolder
.getContext().setAuthentication(authen
);
43 Integer returnCode
= null;
44 Display display
= PlatformUI
.createDisplay();
46 Subject subject
= null;
50 // if (authen == null)
51 // SecureRapActivator.getLoginContext().login();
52 subject
= SecureRapActivator
.getLoginContext().getSubject();
53 Set
<Authentication
> auths
= subject
54 .getPrincipals(Authentication
.class);
56 SecurityContextHolder
.getContext().setAuthentication(
57 auths
.iterator().next());
58 // authen = SecurityContextHolder.getContext()
59 // .getAuthentication();
60 // RWT.getSessionStore().setAttribute(
61 // SECURITY_CONTEXT_ATTRIBUTE, authen);
63 } catch (LoginException e
) {
64 Error
.show("Cannot login", e
);
66 } catch (Exception e
) {
67 Error
.show("Unexpected exception while trying to login", e
);
72 if (subject
== null) {
73 // IStatus status = new Status(IStatus.ERROR,
74 // "org.argeo.security.application", "Login is mandatory",
76 // ErrorDialog.openError(null, "Error", "Shutdown...", status);
77 // return status.getSeverity();
79 // TODO: log as anonymous
82 if (subject
!= null) {
83 returnCode
= (Integer
) Subject
.doAs(subject
,
84 getRunAction(display
));
85 SecureRapActivator
.getLoginContext().logout();
86 return processReturnCode(returnCode
);
90 } catch (Exception e
) {
91 // e.printStackTrace();
92 IStatus status
= new Status(IStatus
.ERROR
,
93 "org.argeo.security.rcp", "Login failed", e
);
94 ErrorDialog
.openError(null, "Error", "Shutdown...", status
);
101 @SuppressWarnings("rawtypes")
102 private PrivilegedAction
getRunAction(final Display display
) {
103 return new PrivilegedAction() {
105 public Object
run() {
106 int result
= createAndRunWorkbench(display
);
107 return new Integer(result
);
112 protected Integer
createAndRunWorkbench(Display display
) {
113 return PlatformUI
.createAndRunWorkbench(display
,
114 createWorkbenchAdvisor());
117 protected Integer
processReturnCode(Integer returnCode
) {
121 protected WorkbenchAdvisor
createWorkbenchAdvisor() {
122 return new SecureWorkbenchAdvisor() {
123 public WorkbenchWindowAdvisor
createWorkbenchWindowAdvisor(
124 IWorkbenchWindowConfigurer configurer
) {
125 return new RapSecureWorkbenchWindowAdvisor(configurer
);
132 public void beforeDestroy(SessionStoreEvent event
) {
133 if (log
.isDebugEnabled())
134 log
.debug("RWT session " + event
.getSessionStore().getId()
135 + " about to be destroyed. THREAD="
136 + Thread
.currentThread().getId());