answered
2014-06-17 20:12:42 +0800
chillworld 5367 ● 4 ● 9
https://github.com/chillw...
Hi there,
Quick thinking of me :
Create an abstract controller/viewmodel.
Get the timeout value from the authenticated user.
Set a timer with the timeout value.
If timer reaches tick => do logout.
Now you also need to set an actionlistener on you main component, so whenever you do an action, the timer is refreshed.
Don't forget to set the time out off in this case (or at max value for a user).
Edit :
As you can see I did not use the ZK Timer
cause this will only work when the timer is attached to a page, witch is in this case not true.
Zk.xml
<listener>
<description>ThreadLocal Synchronization Listener</description>
<listener-class>be.chillworld.DesktopInitImpl
</listener-class>
</listener>
DesktopInitImpl.java
package be.chillworld;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.util.DesktopInit;
/**
*
* @author cossaer.f
*/
public final class DesktopInitImpl implements DesktopInit {
public DesktopInitImpl() {
}
public void init(Desktop dsktp, Object o) throws Exception {
dsktp.addListener(new DesktopEventInterceptor(dsktp));
}
}
DesktopEventInterceptor.java
package be.chillworld;
import java.util.Timer;
import java.util.TimerTask;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.EventInterceptor;
/**
*
* @author cossaer.f
*/
public class DesktopEventInterceptor implements EventInterceptor {
private int userDelay = 10000;
private Timer timer = new Timer();
private final Desktop desktop;
public DesktopEventInterceptor(Desktop desktop) {
timer.schedule(new LogoutTask(), userDelay);
this.desktop = desktop;
}
private void logout() {
System.out.println("I DID A LOGOUT");
// It could be that desktop.getSession().invalidate(); could do the trick also in stead of sendRedirect.
// If session.invalidate works => no need to adding code to the viewmodel.
try {
Executions.activate(desktop);
} catch (Exception ex) {
}
Executions.sendRedirect("www.google.be");
Executions.deactivate(desktop);
}
private void resetTimer() {
System.out.println("TIMER RESET to : " + userDelay);
timer.cancel();
timer = new Timer();
timer.schedule(new LogoutTask(), userDelay);
}
public Event beforeSendEvent(Event event) {
return event;
}
public Event beforePostEvent(Event event) {
return event;
}
public Event beforeProcessEvent(Event event) {
return event;
}
public void afterProcessEvent(Event event) {
resetTimer();
}
private class LogoutTask extends TimerTask {
public void run() {
System.out.println("TIMER TICK");
logout();
}
}
}
For the Executions to work we have to enable server-push.
In your indexViewModel :
public IndexVm() {
Executions.getCurrent().getDesktop().enableServerPush(true);
// This won't work in the listener :(
// I suggest create an abstractVM where you add an init method (annotation @init).
// Put that line there and extend all your VM's to that abstractVM.
}
Remove or change the logging to default logging of your project.
A debug level would be perfect here.
Greetz chill.