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

org.ajax4jsf.framework.resource.InternetResourceBuilder 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.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

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

public abstract class InternetResourceBuilder {

	protected static Log log = LogFactory.getLog(InternetResourceBuilder.class);

	/**
	 * Get application start time for check resources modification time.
	 * @return application start time in msec's
	 */
	public abstract long getStartTime();

	/**
	 * @param cacheable
	 * @param session
	 * @param mime
	 * @return
	 * @throws FacesException
	 */
	public abstract InternetResource createUserResource(boolean cacheable,
			boolean session, String mime) throws FacesException;

	/**
	 * @param request
	 * @return
	 */
	public abstract String getFacesResourceKey(HttpServletRequest request);

	/**
	 * @param key
	 * @param resource
	 */
	public abstract void addResource(String key, InternetResource resource);

	/**
	 * @param path
	 * @return
	 * @throws ResourceNotFoundException
	 */
	public abstract InternetResource getResource(String path)
			throws ResourceNotFoundException;

	/**
	 * @param key
	 * @return
	 */
	public abstract Object getResourceDataForKey(String key);

	/**
	 * @param key
	 * @return
	 * @throws ResourceNotFoundException
	 */
	public abstract InternetResource getResourceForKey(String key)
			throws ResourceNotFoundException;

	/**
	 * @param resource
	 * @param context
	 * @param storeData
	 * @return
	 */
	public abstract String getUri(InternetResource resource,
			FacesContext context, Object storeData);

	/**
	 * @param base
	 * @param path
	 * @return
	 * @throws FacesException
	 */
	public abstract InternetResource createResource(Object base, String path)
			throws FacesException;

	/**
	 * @param servletContext
	 * @param filterName
	 * @throws ServletException
	 */
	public abstract void init(ServletContext servletContext, String filterName)
			throws ServletException;

	/**
	 * static instance variable.
	 */
	private static Map instances = Collections.synchronizedMap(new HashMap());

	/**
	 * Get ( or create if nessesary ) instance of builder for current loader.
	 * check content of file META-INF/services/org.ajax4jsf.framework.resource.InternetResourceBuilder for
	 * name of class to instantiate, othrthise create {@link ResourceBuilderImpl} instance.
	 * @return current builder instance.
	 */
	public static InternetResourceBuilder getInstance() {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		InternetResourceBuilder instance = (InternetResourceBuilder) instances
				.get(loader);
		if (null == instance) {
			try {
				String resource = "META-INF/services/"
						+ InternetResourceBuilder.class.getName();
				InputStream in = loader.getResourceAsStream(resource);
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(in));
				String serviceClassName = reader.readLine();
				reader.close();
				Class builderClass = loader.loadClass(serviceClassName);
				instance = (InternetResourceBuilder) builderClass.newInstance();
				if (log.isDebugEnabled()) {
					log.debug("Create instance of InternetBuilder from class "
							+ serviceClassName);
				}
			} catch (Exception e) {
				if (log.isDebugEnabled()) {
					log
							.debug(
									"Create default implementation instance of InternetBuilder");
				}
				instance = new ResourceBuilderImpl();
			}
			instances.put(loader, instance);
		}
		return instance;
	}

	/**
	 * Package-wide method for reset instance in Junit tests.
	 * 
	 * @param instance
	 */
	public static void setInstance(InternetResourceBuilder instance) {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		instances.put(loader, instance);
	}

	public InternetResourceBuilder() {
		super();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy