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

com.sun.faces.application.ApplicationImpl Maven / Gradle / Ivy

Go to download

This is the master POM file for Sun's Implementation of the JSF 1.2 Specification.

The newest version!
/*
 * $Id: ApplicationImpl.java,v 1.94.4.10 2010/05/17 19:51:05 edburns Exp $
 */

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 1997-2010 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.sun.faces.application;

import com.sun.faces.RIConstants;
import com.sun.faces.config.WebConfiguration;
import com.sun.faces.el.ChainTypeCompositeELResolver;
import com.sun.faces.el.DemuxCompositeELResolver;
import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.RegisterConverterPropertyEditors;
import com.sun.faces.el.ELUtils;
import com.sun.faces.el.FacesCompositeELResolver;
import com.sun.faces.el.PropertyResolverImpl;
import com.sun.faces.el.VariableResolverImpl;
import com.sun.faces.lifecycle.ELResolverInitPhaseListener;
import com.sun.faces.util.MessageUtils;
import com.sun.faces.util.ReflectionUtils;
import com.sun.faces.util.Util;
import com.sun.faces.util.FacesLogger;

import javax.el.ELContextListener;
import javax.el.ELException;
import javax.el.ELResolver;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.application.StateManager;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.el.MethodBinding;
import javax.faces.el.PropertyResolver;
import javax.faces.el.ReferenceSyntaxException;
import javax.faces.el.ValueBinding;
import javax.faces.el.VariableResolver;
import javax.faces.event.ActionListener;
import javax.faces.validator.Validator;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.Constructor;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.el.CompositeELResolver;


/**
 * 

Application represents a per-web-application * singleton object where applications based on JavaServer Faces (or * implementations wishing to provide extended functionality) can * register application-wide singletons that provide functionality * required by JavaServer Faces. */ public class ApplicationImpl extends Application { // Log instance for this class private static final Logger logger = FacesLogger.APPLICATION.getLogger(); private static final ELContextListener[] EMPTY_EL_CTX_LIST_ARRAY = { }; private static final Map[]> STANDARD_CONV_ID_TO_TYPE_MAP = new HashMap[]>(8, 1.0f); private static final Map,String> STANDARD_TYPE_TO_CONV_ID_MAP = new HashMap,String>(16, 1.0f); static { STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Byte", new Class[] { Byte.TYPE, Byte.class}); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Boolean", new Class[] { Boolean.TYPE, Boolean.class}); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Character", new Class[] { Character.TYPE, Character.class}); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Short", new Class[] { Short.TYPE, Short.class }); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Integer", new Class[] { Integer.TYPE, Integer.class }); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Long", new Class[] { Long.TYPE, Long.class }); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Float", new Class[] { Float.TYPE, Float.class }); STANDARD_CONV_ID_TO_TYPE_MAP.put("javax.faces.Double", new Class[] { Double.TYPE, Double.class }); for (Map.Entry[]> entry : STANDARD_CONV_ID_TO_TYPE_MAP.entrySet()) { Class[] types = entry.getValue(); String key = entry.getKey(); for (Class clazz : types) { STANDARD_TYPE_TO_CONV_ID_MAP.put(clazz, key); } } } // Relationship Instance Variables private ApplicationAssociate associate = null; private volatile ActionListener actionListener = null; private volatile NavigationHandler navigationHandler = null; private volatile PropertyResolverImpl propertyResolver = null; volatile VariableResolverImpl variableResolver = null; private volatile ViewHandler viewHandler = null; private volatile StateManager stateManager = null; private volatile ArrayList supportedLocales = null; private volatile Locale defaultLocale = null; // // This map stores reference expression | value binding instance // mappings. // // // These three maps store store "identifier" | "class name" // mappings. // private Map componentMap = null; private Map converterIdMap = null; private Map converterTypeMap = null; private Map validatorMap = null; private volatile String messageBundle = null; private ArrayList elContextListeners = null; CompositeELResolver elResolvers = null; FacesCompositeELResolver compositeELResolver = null; private boolean registerPropertyEditors; /** * Constructor */ public ApplicationImpl() { super(); associate = new ApplicationAssociate(this); componentMap = new ConcurrentHashMap(); converterIdMap = new ConcurrentHashMap(); converterTypeMap = new ConcurrentHashMap(); validatorMap = new ConcurrentHashMap(); navigationHandler = new NavigationHandlerImpl(associate); propertyResolver = new PropertyResolverImpl(); variableResolver = new VariableResolverImpl(); elResolvers = new CompositeELResolver(); WebConfiguration webConfig = WebConfiguration.getInstance(); registerPropertyEditors = webConfig.isOptionEnabled(RegisterConverterPropertyEditors); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Created Application instance "); } } public void addELContextListener(ELContextListener listener) { if (listener != null) { if (elContextListeners == null) { //noinspection CollectionWithoutInitialCapacity elContextListeners = new ArrayList(); } elContextListeners.add(listener); } } public void removeELContextListener(ELContextListener listener) { if (listener != null && elContextListeners != null) { elContextListeners.remove(listener); } } public ELContextListener [] getELContextListeners() { if (elContextListeners != null ) { return (elContextListeners.toArray( new ELContextListener[elContextListeners.size()])); } else { return (EMPTY_EL_CTX_LIST_ARRAY); } } public ExpressionFactory getExpressionFactory() { return associate.getExpressionFactory(); } public Object evaluateExpressionGet(FacesContext context, String expression, Class expectedType) throws ELException { ValueExpression ve = getExpressionFactory().createValueExpression(context.getELContext(), expression,expectedType); return (ve.getValue(context.getELContext())); } public UIComponent createComponent(ValueExpression componentExpression, FacesContext context, String componentType) throws FacesException { if (null == componentExpression) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentExpression"); throw new NullPointerException(message); } if (null == context) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"); throw new NullPointerException(message); } if (null == componentType) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentType"); throw new NullPointerException(message); } Object result; boolean createOne = false; try { if (null != (result = componentExpression.getValue(context.getELContext()))) { // if the result is not an instance of UIComponent createOne = (!(result instanceof UIComponent)); // we have to create one. } if (null == result || createOne) { result = this.createComponent(componentType); componentExpression.setValue((context.getELContext()), result); } } catch (Exception ex) { throw new FacesException(ex); } return (UIComponent) result; } public ELResolver getELResolver() { if (compositeELResolver == null) { performOneTimeELInitialization(); } return compositeELResolver; } public void addELResolver(ELResolver resolver) { if (associate.hasRequestBeenServiced()) { throw new IllegalStateException( MessageUtils.getExceptionMessageString( MessageUtils.APPLICATION_INIT_COMPLETE_ERROR_ID)); } elResolvers.add(resolver); } public CompositeELResolver getApplicationELResolvers() { return elResolvers; } public ActionListener getActionListener() { return actionListener; } public ViewHandler getViewHandler() { return viewHandler; } public synchronized void setViewHandler(ViewHandler handler) { if (handler == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "handler"); throw new NullPointerException(message); } if (associate.isResponseRendered()) { // at least one response has been rendered. if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "jsf.illegal_attempt_setting_viewhandler_error"); } throw new IllegalStateException(MessageUtils.getExceptionMessageString( MessageUtils.ILLEGAL_ATTEMPT_SETTING_VIEWHANDLER_ID)); } viewHandler = handler; if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, MessageFormat.format("set ViewHandler Instance to ''{0}''", viewHandler.getClass().getName())); } } public StateManager getStateManager() { return stateManager; } public synchronized void setStateManager(StateManager manager) { if (manager == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "manager"); throw new NullPointerException(message); } if (associate.isResponseRendered()) { // at least one response has been rendered. if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "jsf.illegal_attempt_setting_statemanager_error"); } throw new IllegalStateException(MessageUtils.getExceptionMessageString( MessageUtils.ILLEGAL_ATTEMPT_SETTING_STATEMANAGER_ID)); } stateManager = manager; if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, MessageFormat.format("set StateManager Instance to ''{0}''", stateManager.getClass().getName())); } } public synchronized void setActionListener(ActionListener listener) { if (listener == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "listener"); throw new NullPointerException(message); } this.actionListener = listener; if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("set ActionListener Instance to ''{0}''", actionListener.getClass().getName())); } } /** * Return the NavigationHandler instance * installed present in this application instance. If * an instance does not exist, it will be created. */ public NavigationHandler getNavigationHandler() { return navigationHandler; } /** * Set a NavigationHandler instance for this * application instance. * * @param handler The NavigationHandler instance. */ public synchronized void setNavigationHandler(NavigationHandler handler) { if (handler == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "handler"); throw new NullPointerException(message); } this.navigationHandler = handler; if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("set NavigationHandler Instance to ''{0}''", navigationHandler.getClass().getName())); } } @SuppressWarnings("deprecation") public PropertyResolver getPropertyResolver() { if (compositeELResolver == null) { performOneTimeELInitialization(); } return propertyResolver; } public ResourceBundle getResourceBundle(FacesContext context, String var) { if (null == context || null == var) { throw new FacesException("context or var is null."); } return associate.getResourceBundle(context, var); } @SuppressWarnings("deprecation") public void setPropertyResolver(PropertyResolver resolver) { // Throw Illegal State Exception if a PropertyResolver is set after // a request has been processed. if (associate.hasRequestBeenServiced()) { throw new IllegalStateException( MessageUtils.getExceptionMessageString( MessageUtils.APPLICATION_INIT_COMPLETE_ERROR_ID)); } if (resolver == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "resolver"); throw new NullPointerException(message); } propertyResolver.setDelegate(ELUtils.getDelegatePR(associate, true)); associate.setLegacyPropertyResolver(resolver); propertyResolver = new PropertyResolverImpl(); if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("set PropertyResolver Instance to ''{0}''", resolver.getClass().getName())); } } @SuppressWarnings("deprecation") public MethodBinding createMethodBinding(String ref, Class params[]) { MethodExpression result; if (ref == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "ref"); throw new NullPointerException(message); } if (!(ref.startsWith("#{") && ref.endsWith("}"))) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Expression ''{0}'' does not follow the syntax #{...}", ref)); } throw new ReferenceSyntaxException(ref); } FacesContext context = FacesContext.getCurrentInstance(); try { // return a MethodBinding that wraps a MethodExpression. if (null == params) { params = RIConstants.EMPTY_CLASS_ARGS; } result = getExpressionFactory(). createMethodExpression(context.getELContext(), ref, null, params); } catch (ELException elex) { throw new ReferenceSyntaxException(elex); } return (new MethodBindingMethodExpressionAdapter(result)); } @SuppressWarnings("deprecation") public ValueBinding createValueBinding(String ref) throws ReferenceSyntaxException { if (ref == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "ref"); throw new NullPointerException(message); } ValueExpression result; FacesContext context = FacesContext.getCurrentInstance(); // return a ValueBinding that wraps a ValueExpression. try { result= getExpressionFactory(). createValueExpression(context.getELContext(),ref, Object.class); } catch (ELException elex) { throw new ReferenceSyntaxException(elex); } return (new ValueBindingValueExpressionAdapter(result)); } @SuppressWarnings("deprecation") public VariableResolver getVariableResolver() { if (compositeELResolver == null) { performOneTimeELInitialization(); } return variableResolver; } @SuppressWarnings("deprecation") public void setVariableResolver(VariableResolver resolver) { if (associate.hasRequestBeenServiced()) { throw new IllegalStateException( MessageUtils.getExceptionMessageString( MessageUtils.APPLICATION_INIT_COMPLETE_ERROR_ID)); } if (resolver == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "resolver"); throw new NullPointerException(message); } variableResolver.setDelegate(ELUtils.getDelegateVR(associate, true)); associate.setLegacyVariableResolver(resolver); if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("set VariableResolver Instance to ''{0}''", variableResolver.getClass().getName())); } } public void addComponent(String componentType, String componentClass) { if (componentType == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentType"); throw new NullPointerException(message); } if (componentClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentClass"); throw new NullPointerException(message); } componentMap.put(componentType, componentClass); if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("added component of type ''{0}'' and class ''{1}''", componentType, componentClass)); } } public UIComponent createComponent(String componentType) throws FacesException { if (componentType == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentType"); throw new NullPointerException(message); } UIComponent returnVal; try { returnVal = (UIComponent) newThing(componentType, componentMap); } catch (Exception ex) { if (logger.isLoggable(Level.WARNING)) { logger.log(Level.WARNING, "jsf.cannot_instantiate_component_error", componentType); } throw new FacesException(ex); } if (returnVal == null) { Object[] params = {componentType}; if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "jsf.cannot_instantiate_component_error", params); } throw new FacesException(MessageUtils.getExceptionMessageString( MessageUtils.NAMED_OBJECT_NOT_FOUND_ERROR_MESSAGE_ID, params)); } if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, MessageFormat.format("Created component with component type of ''{0}''", componentType)); } return returnVal; } @SuppressWarnings("deprecation") public UIComponent createComponent(ValueBinding componentBinding, FacesContext context, String componentType) throws FacesException { if (null == componentBinding) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentBinding"); throw new NullPointerException(message); } if (null == context) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "context"); throw new NullPointerException(message); } if (null == componentType) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "componentType"); throw new NullPointerException(message); } Object result; boolean createOne = false; try { if (null != (result = componentBinding.getValue(context))) { // if the result is not an instance of UIComponent createOne = (!(result instanceof UIComponent)); // we have to create one. } if (null == result || createOne) { result = this.createComponent(componentType); componentBinding.setValue(context, result); } } catch (Exception ex) { throw new FacesException(ex); } return (UIComponent) result; } public Iterator getComponentTypes() { return componentMap.keySet().iterator(); } public void addConverter(String converterId, String converterClass) { if (converterId == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "converterId"); throw new NullPointerException(message); } if (converterClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "converterClass"); throw new NullPointerException(message); } converterIdMap.put(converterId, converterClass); Class[] types = STANDARD_CONV_ID_TO_TYPE_MAP.get(converterId); if (types != null) { for (Class clazz : types) { // go directly against map to prevent cyclic method calls converterTypeMap.put(clazz, converterClass); addPropertyEditorIfNecessary(clazz); } } if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("added converter of type ''{0}'' and class ''{1}''", converterId, converterClass)); } } public void addConverter(Class targetClass, String converterClass) { if (targetClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "targetClass"); throw new NullPointerException(message); } if (converterClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "converterClass"); throw new NullPointerException(message); } String converterId = STANDARD_TYPE_TO_CONV_ID_MAP.get(targetClass); if (converterId != null) { addConverter(converterId, converterClass); } else { converterTypeMap.put(targetClass, converterClass); addPropertyEditorIfNecessary(targetClass); } if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("added converter of class type ''{0}''", converterClass)); } } private final String [] STANDARD_BY_TYPE_CONVERTER_CLASSES = { "java.math.BigDecimal", "java.lang.Boolean", "java.lang.Byte", "java.lang.Character", "java.lang.Double", "java.lang.Float", "java.lang.Integer", "java.lang.Long", "java.lang.Short", "java.lang.Enum" }; /** *

To enable EL Coercion to use JSF Custom converters, this * method will call PropertyEditorManager.registerEditor(), * passing the ConverterPropertyEditor class for the * targetClass if the target class is not one of the standard * by-type converter target classes. * @param targetClass the target class for which a PropertyEditory may or * may not be created */ private void addPropertyEditorIfNecessary(Class targetClass) { if (!registerPropertyEditors) { return; } PropertyEditor editor = PropertyEditorManager.findEditor(targetClass); if (null != editor) { return; } String className = targetClass.getName(); // Don't add a PropertyEditor for the standard by-type converters. if (targetClass.isPrimitive()) { return; } for (String standardClass : STANDARD_BY_TYPE_CONVERTER_CLASSES) { if (-1 != standardClass.indexOf(className)) { return; } } Class editorClass = ConverterPropertyEditorFactory.getDefaultInstance().definePropertyEditorClassFor(targetClass); if (editorClass != null) { PropertyEditorManager.registerEditor(targetClass, editorClass); } else { if (logger.isLoggable(Level.WARNING)) { logger.warning(MessageFormat.format("definePropertyEditorClassFor({0}) returned null.", targetClass.getName())); } } } private void performOneTimeELInitialization() { if (null != compositeELResolver) { throw new IllegalStateException("Class invariant invalidated: " + "The Application instance's ELResolver is not null " + "and it should be."); } associate.initializeELResolverChains(); } public Converter createConverter(String converterId) { if (converterId == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "convertedId"); throw new NullPointerException(message); } Converter returnVal = (Converter) newThing(converterId, converterIdMap); if (returnVal == null) { Object[] params = {converterId}; if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "jsf.cannot_instantiate_converter_error", converterId); } throw new FacesException(MessageUtils.getExceptionMessageString( MessageUtils.NAMED_OBJECT_NOT_FOUND_ERROR_MESSAGE_ID, params)); } if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("created converter of type ''{0}''", converterId)); } return returnVal; } public Converter createConverter(Class targetClass) { if (targetClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "targetClass"); throw new NullPointerException(message); } Converter returnVal = (Converter) newConverter(targetClass, converterTypeMap,targetClass); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } //Search for converters registered to interfaces implemented by //targetClass Class[] interfaces = targetClass.getInterfaces(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { returnVal = createConverterBasedOnClass(interfaces[i], targetClass); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } } } //Search for converters registered to superclasses of targetClass Class superclass = targetClass.getSuperclass(); if (superclass != null) { returnVal = createConverterBasedOnClass(superclass, targetClass); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } } return returnVal; } protected Converter createConverterBasedOnClass(Class targetClass, Class baseClass) { Converter returnVal = (Converter) newConverter(targetClass, converterTypeMap, baseClass); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } //Search for converters registered to interfaces implemented by //targetClass Class[] interfaces = targetClass.getInterfaces(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { returnVal = createConverterBasedOnClass(interfaces[i], null); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } } } //Search for converters registered to superclasses of targetClass Class superclass = targetClass.getSuperclass(); if (superclass != null) { returnVal = createConverterBasedOnClass(superclass, null); if (returnVal != null) { if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("Created converter of type ''{0}''", returnVal.getClass().getName())); } return returnVal; } } return returnVal; } public Iterator getConverterIds() { return converterIdMap.keySet().iterator(); } public Iterator getConverterTypes() { return converterTypeMap.keySet().iterator(); } public Iterator getSupportedLocales() { if (null != supportedLocales) { return supportedLocales.iterator(); } else { return Collections.emptyList().iterator(); } } public synchronized void setSupportedLocales(Collection newLocales) { if (null == newLocales) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "newLocales"); throw new NullPointerException(message); } supportedLocales = new ArrayList(newLocales); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, MessageFormat.format("set Supported Locales ''{0}''", supportedLocales.toString())); } } public Locale getDefaultLocale() { return defaultLocale; } public synchronized void setDefaultLocale(Locale locale) { if (locale == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "locale"); throw new NullPointerException(message); } defaultLocale = locale; if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, (MessageFormat.format("set defaultLocale ''{0}''", defaultLocale.getClass().getName()))); } } protected String defaultRenderKitId = null; public String getDefaultRenderKitId() { return defaultRenderKitId; } public void setDefaultRenderKitId(String renderKitId) { defaultRenderKitId = renderKitId; } public void addValidator(String validatorId, String validatorClass) { if (validatorId == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "validatorId"); throw new NullPointerException(message); } if (validatorClass == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "validatorClass"); throw new NullPointerException(message); } validatorMap.put(validatorId, validatorClass); if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("added validator of type ''{0}'' class ''{1}''", validatorId, validatorClass)); } } public Validator createValidator(String validatorId) throws FacesException { if (validatorId == null) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "validatorId"); throw new NullPointerException(message); } Validator returnVal = (Validator) newThing(validatorId, validatorMap); if (returnVal == null) { Object[] params = {validatorId}; if (logger.isLoggable(Level.SEVERE)) { logger.log(Level.SEVERE, "jsf.cannot_instantiate_validator_error", params); } throw new FacesException(MessageUtils.getExceptionMessageString( MessageUtils.NAMED_OBJECT_NOT_FOUND_ERROR_MESSAGE_ID, params)); } if (logger.isLoggable(Level.FINE)) { logger.fine(MessageFormat.format("created validator of type ''{0}''", validatorId)); } return returnVal; } public Iterator getValidatorIds() { return validatorMap.keySet().iterator(); } public synchronized void setMessageBundle(String messageBundle) { if (null == messageBundle) { String message = MessageUtils.getExceptionMessageString (MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "messageBundle"); throw new NullPointerException(message); } this.messageBundle = messageBundle; if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, MessageFormat.format("set messageBundle ''{0}''", messageBundle)); } } public String getMessageBundle() { return messageBundle; } /** *

PRECONDITIONS: the values in the Map are either Strings * representing fully qualified java class names, or java.lang.Class * instances.

*

ALGORITHM: Look in the argument map for a value for the argument * key. If found, if the value is instanceof String, assume the String * specifies a fully qualified java class name and obtain the * java.lang.Class instance for that String using Util.loadClass(). * Replace the String instance in the argument map with the Class * instance. If the value is instanceof Class, proceed. Assert that the * value is either instanceof java.lang.Class or java.lang.String.

*

Now that you have a java.lang.class, call its newInstance and * return it as the result of this method.

* * @param key Used to look up the value in the Map. * @param map The Map that will be searched. * @return The new object instance. */ protected Object newThing(String key, Map map) { assert (key != null && map != null); Object result; Class clazz = null; Object value; value = map.get(key); if (value == null) { return null; } assert (value instanceof String || value instanceof Class); if (value instanceof String) { String cValue = (String) value; try { clazz = Util.loadClass(cValue, value); if (!associate.isDevModeEnabled()) { map.put(key, clazz); } assert (clazz != null); } catch (Throwable t) { throw new FacesException(t.getMessage(), t); } } else { clazz = (Class) value; } try { result = clazz.newInstance(); } catch (Throwable t) { throw new FacesException((MessageUtils.getExceptionMessageString( MessageUtils.CANT_INSTANTIATE_CLASS_ERROR_MESSAGE_ID, clazz.getName())), t); } return result; } /** *

The same as newThing except that a single argument constructor * that accepts a Class is looked for before calling the no-arg version.

* *

PRECONDITIONS: the values in the Map are either Strings * representing fully qualified java class names, or java.lang.Class * instances.

*

ALGORITHM: Look in the argument map for a value for the argument * key. If found, if the value is instanceof String, assume the String * specifies a fully qualified java class name and obtain the * java.lang.Class instance for that String using Util.loadClass(). * Replace the String instance in the argument map with the Class * instance. If the value is instanceof Class, proceed. Assert that the * value is either instanceof java.lang.Class or java.lang.String.

*

Now that you have a java.lang.class, call its newInstance and * return it as the result of this method.

* * @param key Used to look up the value in the Map. * @param map The Map that will be searched. * @param targetClass the target class for the single argument ctor * @return The new object instance. */ protected Object newConverter(Class key, Map map, Class targetClass) { assert (key != null && map != null); Object result = null; Class clazz = null; Object value; value = map.get(key); if (value == null) { return null; } assert (value instanceof String || value instanceof Class); if (value instanceof String) { String cValue = (String) value; try { clazz = Util.loadClass(cValue, value); if (!associate.isDevModeEnabled()) { map.put(key, clazz); } assert (clazz != null); } catch (Throwable t) { throw new FacesException(t.getMessage(), t); } } else { clazz = (Class) value; } Constructor ctor = ReflectionUtils.lookupConstructor(clazz, Class.class); Throwable cause = null; if (ctor != null) { try { result = ctor.newInstance(targetClass); } catch (Exception e) { cause = e; } } else { try { result = clazz.newInstance(); } catch (Exception e) { cause = e; } } if (null != cause) { throw new FacesException((MessageUtils.getExceptionMessageString( MessageUtils.CANT_INSTANTIATE_CLASS_ERROR_MESSAGE_ID, clazz.getName())), cause); } return result; } // The testcase for this class is // com.sun.faces.application.TestApplicationImpl.java // The testcase for this class is // com.sun.faces.application.TestApplicationImpl_Config.java }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy