Reputation: 1315
We have an Eclipse RCP based application that uses Eclipse 3.6 plug-ins and Java 1.8. I have a need to implement a login session timeout for the application. I am not too familiar with RCP. I googled around a bit and found a solution here. My implementation does not seem to work if there is another pop up window open.
I created the following class:
public class SessionManager {
private long timeStampLastMs;
private long timeOutTimeMs = 20000L;
private static SessionManager sessionManager = null;
private SessionManager() {
}
public static SessionManager getSessionManager() {
if (sessionManager == null) {
sessionManager = new SessionManager();
}
return sessionManager;
}
public synchronized boolean isActivityTimeOut() {
boolean timeout = false;
long now = System.currentTimeMillis();
if (timeOutTimeMs < (now - timeStampLastMs)) {
timeout = true;
}
return timeout;
}
public synchronized void resetActivityTimeOut() {
timeStampLastMs = System.currentTimeMillis();
}
public void timeoutScreen() {
int defaultIndex = 0;
MessageDialog dialog = new MessageDialog(null, "Information", null,
"Login session expired. Please close this window and restart application", MessageDialog.INFORMATION,
new String[] { IDialogConstants.OK_LABEL }, defaultIndex);
dialog.open();
}
}
Added the following method to the ApplicationWorkbenchAdvisor class which extends WorkbenchAdvisor
@Override
public void eventLoopIdle(Display display) {
SessionManager sessionManager = SessionManager.getSessionManager();
if (sessionManager.isActivityTimeOut()) {
sessionManager.timeoutScreen();
display.dispose();
System.exit(0);
}
super.eventLoopIdle(display);
}
To reset the timer, I made the changes to the 'run' method of the Application class that implements IPlatformRunnable:
public Object run(Object args) throws Exception {
Listener uiListener = new Listener() {
@Override
public void handleEvent (Event event) {
SessionManager.getSessionManager().resetActivityTimeOut();
}
};
Display display = PlatformUI.createDisplay();
display.addFilter(SWT.KeyUp, uiListener);
display.addFilter(SWT.MouseUp, uiListener);
display.addFilter(SWT.Activate, uiListener);
try {
final Session session = Session.getInstance();
Platform.endSplash();
if (!login(session))
return IPlatformRunnable.EXIT_OK;
int returnCode = PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
if (returnCode == PlatformUI.RETURN_RESTART) {
return IPlatformRunnable.EXIT_RESTART;
}
return IPlatformRunnable.EXIT_OK;
} finally {
display.dispose();
}
}
This works OK with the main window with the execution of eventLoopIdle(). But when popup dialogs are open eventLoopIdle() does not seem to get executed and the timeout does not happen.
What do I need to do to get this to work?
Upvotes: 0
Views: 35