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

org.ajax4jsf.framework.resource.OneTimeRenderer 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.resource;

import java.io.IOException;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.ajax4jsf.framework.util.message.Messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Base class for resources, rendered only one time in page ( JavaScript, CSS )
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.3 $ $Date: 2006/07/12 18:14:11 $
 *
 */
public abstract class OneTimeRenderer extends BaseResourceRenderer {

	private static final Log log = LogFactory.getLog(OneTimeRenderer.class);
	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#encodeBegin(org.ajax4jsf.framework.resource.InternetResource, javax.faces.context.FacesContext, java.lang.Object, java.util.Map)
	 */
	public void encodeBegin(InternetResource resource, FacesContext context, Object data, Map attributes) throws IOException {
		
		if (!isRendered(resource,context)) {
			super.encodeBegin(resource, context, data, attributes);
		} else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODE_BEGIN_HTML_INFO_2, resource.getKey()));
		}
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.resource.BaseResourceRenderer#encodeEnd(org.ajax4jsf.framework.resource.InternetResource, javax.faces.context.FacesContext, java.lang.Object)
	 */
	public void encodeEnd(InternetResource resource, FacesContext context, Object data) throws IOException {
		// TODO Auto-generated method stub
		if (!isRendered(resource,context)) {
			super.encodeEnd(resource, context, data);
			context.getExternalContext().getRequestMap().put(resource.getKey(),Boolean.TRUE);
			if (log.isDebugEnabled()) {
					log.debug(Messages.getMessage(Messages.MARK_AS_RENDERED_INFO, resource.getKey()));
			}
		} else if (log.isDebugEnabled()) {
			log.debug(Messages.getMessage(Messages.SKIP_ENCODE_END_HTML_INFO_2, resource.getKey()));
		}
	}

	/**
	 * Detect for resource rendered status. For cacheable, show key parameter in request Map.
	 * @param resource
	 * @param context
	 * @return - true, if resource already rendered.
	 */
	private boolean isRendered(InternetResource resource, FacesContext context){
		return  null != context.getExternalContext().getRequestMap().get(resource.getKey()); // resource.isCacheable() &&
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy