1 package org
.argeo
.security
.ui
.rap
;
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
.eclipse
.equinox
.security
.auth
.ILoginContext
;
11 import org
.eclipse
.jface
.dialogs
.Dialog
;
12 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
13 import org
.eclipse
.rwt
.RWT
;
14 import org
.eclipse
.rwt
.lifecycle
.IEntryPoint
;
15 import org
.eclipse
.rwt
.service
.SessionStoreEvent
;
16 import org
.eclipse
.rwt
.service
.SessionStoreListener
;
17 import org
.eclipse
.swt
.graphics
.Image
;
18 import org
.eclipse
.swt
.widgets
.Display
;
19 import org
.eclipse
.swt
.widgets
.Shell
;
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
;
25 public class SecureEntryPoint
implements IEntryPoint
, SessionStoreListener
{
26 private final static Log log
= LogFactory
.getLog(SecureEntryPoint
.class);
28 @SuppressWarnings("unchecked")
30 public int createUI() {
31 // 15 mins session timeout
32 RWT
.getRequest().getSession().setMaxInactiveInterval(15 * 60);
34 if (log
.isDebugEnabled())
35 log
.debug("THREAD=" + Thread
.currentThread().getId()
36 + ", sessionStore=" + RWT
.getSessionStore().getId());
38 final ILoginContext loginContext
= SecureRapActivator
39 .createLoginContext();
40 Integer returnCode
= null;
41 Display display
= PlatformUI
.createDisplay();
43 Subject subject
= null;
46 subject
= loginContext
.getSubject();
47 } catch (LoginException e
) {
48 log
.error("Error when logging in.", e
);
49 MessageDialog
.openInformation(display
.getActiveShell(),
50 "Login failed", "Login failed");
52 RWT
.getRequest().getSession().setMaxInactiveInterval(1);
55 } catch (InterruptedException e1
) {
58 // throw new RuntimeException("Login failed", e);
62 // identify after successful login
63 if (log
.isDebugEnabled())
64 log
.debug("subject=" + subject
);
65 final String username
= subject
.getPrincipals().iterator().next()
67 if (log
.isDebugEnabled())
68 log
.debug(username
+ " logged in");
69 display
.disposeExec(new Runnable() {
71 log
.debug("Display disposed");
72 logout(loginContext
, username
);
74 RWT
.getRequest().getSession().setMaxInactiveInterval(1);
77 } catch (InterruptedException e1
) {
84 returnCode
= (Integer
) Subject
.doAs(subject
, getRunAction(display
));
85 loginContext
.logout();
86 return processReturnCode(returnCode
);
87 } catch (Exception e
) {
89 logout(loginContext
, username
);
90 // RWT.getRequest().getSession().setMaxInactiveInterval(1);
91 log
.error("Unexpected error", e
);
92 // throw new ArgeoException("Cannot login", e);
99 static void logout(ILoginContext secureContext
, String username
) {
101 secureContext
.logout();
102 log
.info("Logged out " + (username
!= null ? username
: "")
103 + " (THREAD=" + Thread
.currentThread().getId() + ")");
104 } catch (LoginException e
) {
105 log
.error("Erorr when logging out", e
);
109 // static void closeWorkbench() {
110 // final IWorkbench workbench;
112 // workbench = PlatformUI.getWorkbench();
113 // } catch (Exception e) {
116 // if (workbench == null)
118 // final Display display = workbench.getDisplay();
119 // if (display != null && !display.isDisposed())
120 // display.syncExec(new Runnable() {
122 // public void run() {
123 // if (!display.isDisposed())
124 // workbench.close();
128 // if (log.isDebugEnabled())
129 // log.debug("Workbench closed");
132 static class FailedLogin
extends MessageDialog
{
134 public FailedLogin(Shell parentShell
, String dialogTitle
,
135 Image dialogTitleImage
, String dialogMessage
,
136 int dialogImageType
, String
[] dialogButtonLabels
,
138 super(parentShell
, "Failed ", dialogTitleImage
, dialogMessage
,
139 dialogImageType
, dialogButtonLabels
, defaultIndex
);
140 // TODO Auto-generated constructor stub
145 @SuppressWarnings("rawtypes")
146 private PrivilegedAction
getRunAction(final Display display
) {
147 return new PrivilegedAction() {
148 public Object
run() {
149 int result
= createAndRunWorkbench(display
);
150 return new Integer(result
);
155 protected Integer
createAndRunWorkbench(Display display
) {
156 return PlatformUI
.createAndRunWorkbench(display
,
157 createWorkbenchAdvisor());
160 protected Integer
processReturnCode(Integer returnCode
) {
164 protected WorkbenchAdvisor
createWorkbenchAdvisor() {
165 return new SecureWorkbenchAdvisor() {
166 public WorkbenchWindowAdvisor
createWorkbenchWindowAdvisor(
167 IWorkbenchWindowConfigurer configurer
) {
168 return new RapSecureWorkbenchWindowAdvisor(configurer
);
175 public void beforeDestroy(SessionStoreEvent event
) {
176 if (log
.isDebugEnabled())
177 log
.debug("RWT session " + event
.getSessionStore().getId()
178 + " about to be destroyed. THREAD="
179 + Thread
.currentThread().getId());