Friday, October 24, 2014

[deltaspike] restricting the amount of windows

with codi it was possible to restrict the number of windows and old windows are dropped if the maximum was reached and a new window should get created.
deltaspike doesn't allow that before v1.1.0, however, it's very easy to implement it manually:
@SessionScoped
@JsfPhaseListener
public class WindowQuotaHandler implements PhaseListener
{
@Inject
private WindowContext windowContext;
private Stack<String> windowIdStack = new Stack<String>();
@Override
public synchronized /*no issue due to session-scoped instance*/
void afterPhase(PhaseEvent event)
{
String currentWindowId = windowContext.getCurrentWindowId();
if (currentWindowId == null)
{
return;
}
if (windowIdStack.contains(currentWindowId))
{
windowIdStack.remove(currentWindowId);
windowIdStack.push(currentWindowId);
}
else
{
windowIdStack.push(currentWindowId);
if (windowIdStack.size() > 30) //restricted to 30 windows
{
String windowIdToRemove = windowIdStack.remove(0);
windowContext.closeWindow(windowIdToRemove);
}
}
}
@Override
public void beforePhase(PhaseEvent event)
{
}
@Override
public PhaseId getPhaseId()
{
return PhaseId.RENDER_RESPONSE;
}
}