/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.myfaces.orchestra.viewController.jsf;
import java.util.Set;
import java.util.TreeSet;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import org.apache.myfaces.orchestra.viewController.ViewControllerManager;
/**
* Causes lifecycle methods to be invoked on backing beans that are associated with
* the current view.
* <p>
* Method executeInitView is invoked on the configured ViewControllerManager when ...
* <p>
*
* <p>
* See the javadoc for class ViewControllerManager on how to configure this.
* <p>
* Note that at the moment this does not supprt a ViewController bean for subviews
* (ie f:subView tags), which the Shale ViewController framework does provide.
* <p>
* It also might not invoke all the callbacks if exceptions are thrown by actionlisteners,
* etc (which again Shale guarantees). This is particularly important for an "endView"
* callback, where resources allocated on initView (such as database connections) might
* be released.
*/
public class ViewControllerPhaseListener implements PhaseListener
{
private static final long serialVersionUID = -3975277433747722402L;
/**
* @since 1.1
*/
public static class ViewControllerPhaseListenerState
{
private Set initedViews = new TreeSet();
protected ViewControllerPhaseListenerState()
{
}
}
public void beforePhase(PhaseEvent event)
{
if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId()) ||
PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
{
assertConversationState(event.getFacesContext());
if (event.getFacesContext().getResponseComplete())
{
// we have a redirect ... stop now
return;
}
}
// Try to init the view in every phase, just so we are sure to never miss it.
// This skips the actual call if init has already happened.
executeInitView(event.getFacesContext());
if (PhaseId.RENDER_RESPONSE.equals(event.getPhaseId()))
{
preRenderResponse(event.getFacesContext());
}
if (PhaseId.INVOKE_APPLICATION.equals(event.getPhaseId()))
{
preInvokeApplication(event.getFacesContext());
}
}
public void afterPhase(PhaseEvent event)
{
if (PhaseId.RESTORE_VIEW.equals(event.getPhaseId()))
{
assertConversationState(event.getFacesContext());
if (event.getFacesContext().getResponseComplete())
{
// we have a redirect ... stop now
return;
}
}
executeInitView(event.getFacesContext());
}
public PhaseId getPhaseId()
{
return PhaseId.ANY_PHASE;
}
protected String getViewId(FacesContext facesContext)
{
UIViewRoot viewRoot = facesContext.getViewRoot();
if (viewRoot == null)
{
return null;
}
return viewRoot.getViewId();
}
/**
* invoked multiple times during the lifecycle to ensure the conversation(s)
* to the associated viewController are running.
*
* @param facesContext
*/
protected void assertConversationState(FacesContext facesContext)
{
ViewControllerManager manager = ViewControllerVariableResolver.getViewControllerManager(facesContext);
if (manager == null)
{
return;
}
String viewId = getViewId(facesContext);
if (viewId == null)
{
return;
}
manager.assertConversationState(viewId);
}
/**
* invokes the preRenderView method on your view controller
*/
protected void preRenderResponse(FacesContext facesContext)
{
ViewControllerManager manager = ViewControllerVariableResolver.getViewControllerManager(facesContext);
if (manager == null)
{
return;
}
String viewId = getViewId(facesContext);
if (viewId == null)
{
return;
}
manager.executePreRenderView(viewId);
}
/**
* invokes the initView method on your view controller
* @since 1.1
*/
protected void executeInitView(FacesContext facesContext)
{
postRestoreView(facesContext);
}
/**
* @deprecated overload/use {@link #executeInitView(javax.faces.context.FacesContext)} instead
*/
protected void postRestoreView(FacesContext facesContext)
{
ViewControllerManager manager = ViewControllerVariableResolver.getViewControllerManager(facesContext);
if (manager == null)
{
return;
}
String viewId = getViewId(facesContext);
if (viewId == null)
{
return;
}
ViewControllerPhaseListenerState state = getState(facesContext);
if (state.initedViews.contains(viewId))
{
// already inited
return;
}
state.initedViews.add(viewId);
manager.executeInitView(viewId);
}
protected ViewControllerPhaseListenerState getState(FacesContext facesContext)
{
ViewControllerPhaseListenerState state = (ViewControllerPhaseListenerState)
facesContext.getExternalContext().getRequestMap()
.get(ViewControllerPhaseListenerState.class.getName());
if (state == null)
{
state = new ViewControllerPhaseListenerState();
facesContext.getExternalContext().getRequestMap().put(
ViewControllerPhaseListenerState.class.getName(), state);
}
return state;
}
/**
* invokes the preProcess method on your view controller
*/
protected void preInvokeApplication(FacesContext facesContext)
{
ViewControllerManager manager = ViewControllerVariableResolver.getViewControllerManager(facesContext);
if (manager == null)
{
return;
}
String viewId = getViewId(facesContext);
if (viewId == null)
{
return;
}
manager.executePreProcess(viewId);
}
}