All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.ajax.InitPhaseListener Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * 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.ajax4jsf.framework.ajax;

import java.util.Iterator;

import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.StateManager;
import javax.faces.application.ViewHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * One time called listener, for initialize framework at first request.
 * @author shura
 *
 */
public class InitPhaseListener implements PhaseListener {
	
	private boolean removed= false;
	private boolean initialized = false;
	
	private static final Log log = LogFactory.getLog(InitPhaseListener.class);

	/* (non-Javadoc)
	 * @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
	 */
	public synchronized void afterPhase(PhaseEvent event) {
        if (!removed) {
        	if(log.isDebugEnabled()){
        		log.debug("Remove init phase listener from factories");
        	}
            LifecycleFactory factory = (LifecycleFactory)
                  FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
            for(Iterator iter = factory.getLifecycleIds(); iter.hasNext(); ) {
                Lifecycle lifecycle = factory.getLifecycle((String) iter.next());
                lifecycle.removePhaseListener(this);
            }
            removed = true;
        }
	}

	/* (non-Javadoc)
	 * @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
	 */
	public synchronized void beforePhase(PhaseEvent event) {
		if(!initialized){
			if(log.isDebugEnabled()){
				log.debug("Perform additional framework initialization on first request");
			}
			FacesContext facesContext = event.getFacesContext();
			Application application = facesContext.getApplication();
			StateManager stateManager = application.getStateManager();
			if(! (stateManager instanceof AjaxStateManager)){
				if(log.isDebugEnabled()){
					log.debug("Set AjaxStateManager on top of chain");
				}
				application.setStateManager(new AjaxStateManager(stateManager));
			}
			ViewHandler viewHandler = application.getViewHandler();
			if (!(viewHandler instanceof AjaxViewHandler)) {
				if(log.isDebugEnabled()){
					log.debug("Set AjaxViewHandler on top of chain");
				}
				application.setViewHandler(new AjaxViewHandler(viewHandler));
			}
			initialized = true;
		}
	}

	/* (non-Javadoc)
	 * @see javax.faces.event.PhaseListener#getPhaseId()
	 */
	public PhaseId getPhaseId() {
		// TODO Auto-generated method stub
		return PhaseId.ANY_PHASE;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy