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

org.ajax4jsf.framework.skin.SkinFactory 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.skin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.HashMap;
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 factory class ( implement Singleton design pattern ). Produce self
 * instance to build current skin configuration. At present, realised as lazy
 * creation factory. TODO - select point to initialize.
 * 
 * @author [email protected] (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.10 $ $Date: 2006/09/26 18:56:33 $
 * 
 */
public abstract class SkinFactory {

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

	private static final Log log = LogFactory.getLog(SkinFactory.class);


	public static void reset() {
		instances = Collections.synchronizedMap(new HashMap());
	}

	/**
	 * Initialize skin factory. TODO - make call from init() method of any
	 * servlet or custom faces element method ??? If exist resource
	 * META-INF/services/org.ajax4jsf.framework.skin.SkinFactory , create
	 * instance of class by name from first line of this file. If such class
	 * have constructor with {@link SkinFactory} parameter, instantiate it with
	 * instance of default factory ( as usual in JSF ). If any error occurs in
	 * instantiate custom factory, return default.
	 */
	public static final SkinFactory getInstance() {
	    ClassLoader loader = Thread.currentThread().getContextClassLoader();
	    SkinFactory instance = (SkinFactory) instances.get(loader);
			if (instance == null) {
				// Pluggable factories.
				InputStream input = null; // loader.getResourceAsStream(SERVICE_RESOURCE);
				input = loader.getResourceAsStream(SERVICE_RESOURCE);
				// have services file.
				if (input != null) {
					try {
						BufferedReader reader = new BufferedReader(
								new InputStreamReader(input));
						String factoryClassName = reader.readLine();
						if (log.isDebugEnabled()) {
							log.debug(Messages.getMessage(
									Messages.SET_SKIN_FACTORY_INFO,
									factoryClassName));
						}
						Class clazz = Class.forName(factoryClassName);
						try {
							// try construct factory chain.
							Constructor factoryConstructor = clazz
									.getConstructor(new Class[] { SkinFactory.class });
							instance = (SkinFactory) factoryConstructor
									.newInstance(new Object[] { instance });
						} catch (NoSuchMethodException e) {
							// no chain constructor - attempt default.
							instance = (SkinFactory) clazz.newInstance();
						}
					} catch (Exception e) {
						log
								.warn(
										Messages
												.getMessage(Messages.CREATING_SKIN_FACTORY_ERROR),
										e);
					} finally {
						try {
							input.close();
						} catch (IOException e) {
							// can be ignored
						}

					}
				}
				if (instance == null) {
					instance = new SkinFactoryImpl();
				}
			instances.put(loader, instance);	
			}

		return instance;
	}

	/**
	 * Get current {@link Skin} implementation.
	 * @param context
	 * @return
	 */
	public abstract Skin getSkin(FacesContext context);
	
//	public abstract SkinConfiguration getSkinConfiguration(FacesContext context);
	
	
	
	/**
	 * Get EL-enabled value. Return same string, if not el-expression.
	 * Otherthise, return parsed and evaluated expression.
	 * 
	 * @param context -
	 *            current Faces Context.
	 * @param value -
	 *            string to parse.
	 * @return - interpreted el or unmodified value.
	 */
	protected static boolean isValueReference(String value) {
		if (value == null)
			return false;

		int start = value.indexOf("#{");
		if (start >= 0) {
			int end = value.lastIndexOf('}');
			if (end >= 0 && start < end) {
				return true;
			}
		}
		return false;
	}
	/**
	 * Resource Uri for file with name of class for application-wide SkinFactory same as SPI definitions for common Java SAX, Jsf etc. factories
	 */
	public static final String SERVICE_RESOURCE = "META-INF/services/" + SkinFactory.class.getName();

	/**
	 * Name of web application init parameter for current skin . Can be simple
	 * String for non-modified name, or EL-expression for calculate current
	 * skin. If EL evaluated to String - used as skin name, if to
	 * instance of {@link Skin } - used this instance. by default -
	 * "org.exadel.chameleon.SKIN"
	 */
	public static final String SKIN_PARAMETER = "org.ajax4jsf.SKIN";

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy