ValidationFactoryConfig.java :  » Development » iScreen » org » iscreen » Java Open Source

Java Open Source » Development » iScreen 
iScreen » org » iscreen » ValidationFactoryConfig.java
/*
 * Copyright 2006-2007 Dan Shellman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.iscreen;

import java.util.Map;
import java.util.HashMap;
import java.util.Locale;

/**
 * This JavaBean is purely for configuration.  It's used to retrieve an
 * instance of the ValidationFactory via a getter, as opposed to getting/building
 * one directly by calling the ValidationFactory.buildFactory() method.  Either
 * use the appropriate constructor, or call the appropriate setters, and then
 * retrieve the factory by calling the getFactory() method.  Note that the
 * default factory is the default XML one (so, you don't need to set that if
 * you plan to use the XML factory).  The location of the configuration file
 * is required, so if it's not set, an exception will be thrown.  Since services
 * are optional, they do not need to be set.
 *
 * @author Shellman, Dan
 */
public class ValidationFactoryConfig
{
  protected String                factoryId = ValidationFactory.FACTORY_OGNL_XML;
  protected String                configLocation;
  protected Locale                defaultLocale = Locale.getDefault();
  protected Map                   services = new HashMap();
  protected ValidationFactory     factory;


  /**
   * Default constructor.
   */
  public ValidationFactoryConfig()
  {
  } //end ValidationFactoryConfig()

  /**
   * Constructor that defines all available config properties.
   *
   * @param   theFactoryId The id of the factory to use (it must have been previously
   *                       registered.
   * @param   theConfigurationLocation The location of the configuration file.
   * @param   locale The default locale.
   * @param   theServices The set of services the factory requires.
   */
  public ValidationFactoryConfig( String theFactoryId,
                                  String theConfigurationLocation,
                                  Locale locale,
                                  Map theServices )
  {
    factoryId = theFactoryId;
    configLocation = theConfigurationLocation;
    defaultLocale = locale;

    if ( theServices != null )
    {
      services.putAll( theServices );
    }
  } //end ValidationFactoryConfig()

  /**
   * Constructor that defines all available config properties except services.
   *
   * @param   theFactoryId The id of the factory to use (it must have been previously
   *                       registered.
   * @param   theConfigurationLocation The location of the configuration file.
   */
  public ValidationFactoryConfig( String theFactoryId,
                                  String theConfigurationLocation )
  {
    factoryId = theFactoryId;
    configLocation = theConfigurationLocation;
  } //end ValidationFactoryConfig()

  /**
   * Constructor that defines only the configuration file.  The factory
   * will be the default XML one, and no services will be defined.
   *
   * @param   theConfigurationLocation The location of the configuration file.
   */
  public ValidationFactoryConfig( String theConfigurationLocation )
  {
    configLocation = theConfigurationLocation;
  } //end ValidationFactoryConfig()

  /**
   * Constructor that defines only the configuration file.  The factory
   * will be the default XML one, and no services will be defined.
   *
   * @param   theConfigurationLocation The location of the configuration file.
   * @param   locale The default locale.
   */
  public ValidationFactoryConfig( String theConfigurationLocation,
                                  Locale locale )
  {
    configLocation = theConfigurationLocation;
    defaultLocale = locale;
  } //end ValidationFactoryConfig()

  /**
   * Constructor that defines only the configuration file.  The factory
   * will be the default XML one, and no services will be defined.
   *
   * @param   theConfigurationLocation The location of the configuration file.
   * @param   theServices The set of services the factory requires.
   */
  public ValidationFactoryConfig( String theConfigurationLocation,
                                  Map theServices )
  {
    configLocation = theConfigurationLocation;
    services.putAll( theServices );
  } //end ValidationFactoryConfig()

  /**
   * Defines the location of the configuration file.  This will override the
   * configuration location defined in the constructor (if it was).
   *
   * @param   location The location of the configuration file.
   */
  public void setConfigurationLocation( String location )
  {
    configLocation = location;
  } //end setConfigurationLocation()

  /**
   * Sets the factory id.  This will override the factory id defined in the
   * constructor (if it was).
   *
   * @param   theFactoryId The id of the factory to use.
   */
  public void setFactoryId( String theFactoryId )
  {
    factoryId = theFactoryId;
  } //end setFactoryId()

  /**
   * Sets the default locale for this factory.
   *
   * @param   locale The default locale.
   */
  public void setDefaultLocale( Locale locale )
  {
    defaultLocale = locale;
  } //end setDefaultLocale()

  /**
   * Sets the services the factory will use.  This will remove any prior
   * defined services and replace them with those passed in.
   *
   * @param   theServices The services the factory requires.
   */
  public void setServices( Map theServices )
  {
    services.clear();
    services.putAll( theServices );
  } //end setServices()

  /**
   * Adds a service to those already defined for the factory.
   *
   * @param   serviceId The id of the individual service.
   * @param   value The service object, itself.
   */
  public void addService( String serviceId, Object value )
  {
    services.put( serviceId, value );
  } //end addService()

  /**
   * Builds and returns the factory based upon the configuration defined
   * within this configuration.  If some of the configuration is invalid
   * or missing (such as no configuration location), no exception will be
   * thrown, so ensure that all configuration has been set properly.<br />
   * <br />
   * If this method is called multiple times, a new factory is NOT constructed.
   * The same factory returned on the first call will be returned on
   * subsequent calls.
   *
   * @return Returns a built factory.
   */
  public ValidationFactory getFactory()
  {
    if ( factory == null )
    {
      factory = ValidationFactory.buildFactory( factoryId,
                                                configLocation,
                                                defaultLocale,
                                                services );
    }

    return factory;
  } //end getFactory()
} //end ValidationFactoryConfig
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.