⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userclassfactory.java

📁 这是一个mvc模式
💻 JAVA
字号:
package jsp.tags.dapact.conf;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
import jsp.tags.dapact.*;
import jsp.tags.dapact.conf.TablibConfig;
import jsp.tags.dapact.lookup.*;
import jsp.tags.dapact.util.*;

/**
 * Title:        Data Aware Processing And Control Tags
 * Description:  Tag library for the processing and controlling the input and output of data.
 * Copyright:    LGPL (http://www.gnu.org/copyleft/lesser.html)
 * Compile Date: @compile_date@
 * @author Allen M Servedio
 * @amp_sign@version @VERSION@
 */

/**
 * Factory for generating classes that users create to implement different algorithms.
 * It uses a configuration file to determine what classes to create. Some of
 * these classes should be thread safe so that only one instance has to be created
 * (that will be specified in the factory function below).
 */
public class UserClassFactory
{
  /**
   * Interface that holds the name of the node that contains the fully qualified
   * class path for the class to create.
   */
  public interface ConfigNodeName
  {
    /**
     * Name of the filter value class to instantiate.
     */
    public static final String FILTER_VALUE_NODE_NAME = "filterValueClass";
    /**
     * Name of the lookup value class to instantiate.
     */
    public static final String LOOKUP_VALUE_NODE_NAME = "lookupValueClass";
    /**
     * Name of the logger class to instantiate.
     */
    public static final String LOGGER_NODE_NAME = "loggerClass";
  }

  /**
   * Default constructor... Since this is a utility class it is not really used.
   */
  private UserClassFactory()
  {
  }

  /**
   * This function should return an instance of your object that overrides the BaseFilterValue class.
   * To save on object instanciation, this class will only create one instance of the
   * object, so it has to be threadsafe.
   *
   * <p>Any class that calls this is the client class from the Strategy pattern.</p>
   *
   * @param name the name of node that contains the fully qualified class name of the filter
   *   value object to use.
   *
   * @return an object that overrides the BaseLookupValue.
   */
  public static BaseLookupValue getFilterValueInstance(String name)
  {
    // If no name is sent in, assign it the default name.
    if ((name == null) || (name.trim().equals("")))
    {
      name = ConfigNodeName.FILTER_VALUE_NODE_NAME;
    }

    // Look for the filter value in the has table by the name.
    BaseLookupValue baseLookupValue = (BaseLookupValue)filterValueTable.get(name);
    if (baseLookupValue == null)
    {
      synchronized(filterValueTable)
      {
        baseLookupValue = (BaseLookupValue)filterValueTable.get(name);
        if (baseLookupValue == null)
        {
          try
          {
            // Since none exist, create a new instance of the filter value.
            baseLookupValue = (BaseLookupValue)createInstance(name);
            if (baseLookupValue == null)
            {
              getLogger().log("Lookup not found, using default: " + name);
              baseLookupValue = new DefaultLookupValue();
            }
          }
          catch (Exception e)
          {
            getLogger().log("Unable to create instance of filter value for name (creating default filter value object): " + name, e);
            baseLookupValue = new DefaultLookupValue();
          }
          // Add the new base filter value to the hash table.
          getLogger().log("Adding base filter value: " + baseLookupValue.getClass().getName() + " with name " + name);
          filterValueTable.put(name, baseLookupValue);
        }
      }
    }
//    getLogger().log("Returning base filter value: " + baseLookupValue.getClass().getName());
    return baseLookupValue;
  }

  /**
   * This function should return an instance of your object that overrides the BaseLookupValue class.
   * To save on object instanciation, this class will only create one instance of the
   * object, so it has to be threadsafe.
   *
   * <p>Any class that calls this is the client class from the Strategy pattern.</p>
   *
   * @param name the name of node that contains the fully qualified class name of the lookup
   *   value object to use.
   *
   * @return an object that overrides the BaseLookupValue.
   */
  public static BaseLookupValue getLookupValueInstance(String name)
  {
    // If no name is sent in, assign it the default name.
    if ((name == null) || (name.trim().equals("")))
    {
      name = ConfigNodeName.LOOKUP_VALUE_NODE_NAME;
    }

    // Look for the lookup value in the has table by the name.
    BaseLookupValue baseLookupValue = (BaseLookupValue)lookupValueTable.get(name);
    if (baseLookupValue == null)
    {
      synchronized(lookupValueTable)
      {
        baseLookupValue = (BaseLookupValue)lookupValueTable.get(name);
        if (baseLookupValue == null)
        {
          try
          {
            // Since none exist, create a new instance of the lookup value.
            baseLookupValue = (BaseLookupValue)createInstance(name);
            if (baseLookupValue == null)
            {
              getLogger().log("Lookup not found, using default: " + name);
              baseLookupValue = new DefaultLookupValue();
            }
          }
          catch (Exception e)
          {
            getLogger().log("Unable to create instance of lookup value for name (creating default lookup value object): " + name, e);
            baseLookupValue = new DefaultLookupValue();
          }
          // Add the new base lookup value to the hash table.
          lookupValueTable.put(name, baseLookupValue);
          getLogger().log("Adding base lookup value: " + baseLookupValue.getClass().getName() + " with name " + name);
        }
      }
    }
//    getLogger().log("Returning base lookup value: " + baseLookupValue.getClass().getName());
    return baseLookupValue;
  }

  /**
   * This function should return an instance of your object that implements the LoggerInf interface.
   * To save on object instanciation, this class will only create one instance of the
   * object, so it has to be threadsafe.
   *
   * @return an object that implements the LoggerInf interface.
   */
  public static LoggerInf getLogger()
  {
    return logger;
  }

  /**
   * Called by the factory methods to instantiate an instance of a class as defined
   * in the config file.
   *
   * @param nameOfConfigNode the name of the configuration node that specifies the
   *   fully qualified class name to create an instance of.
   *
   * @return an instance of the class as specified in the config file.
   *
   * @exception ClassNotFoundException - not able to find the class defined in the configuration file.
   * @exception IllegalAccessException - if the class or initializer is not accessible
   * @exception InstantiationException - if this Class represents an abstract class,
   *   an interface, an array class, a primitive type, or void; or if the instantiation fails for some other reason.
   * @exception IOException - if there is a problem reading the system configuration.
   */
  protected static Object createInstance(String nameOfConfigNode) throws ClassNotFoundException,
    IllegalAccessException, InstantiationException, IOException
  {
    Properties fullyQualifiedClassTable = TablibConfig.getFullyQualifiedClassTable();

    // Load the class and create it.
    return Class.forName(fullyQualifiedClassTable.getProperty(nameOfConfigNode)).newInstance();
  }

  private static final Hashtable filterValueTable = new Hashtable();
  private static final Hashtable lookupValueTable = new Hashtable();

  /**
   * Holds the logger for the system.
   */
  private static LoggerInf logger;
  /**
   * Try to create an instance in a thread safe way (during class loading).
   */
  static
  {
    try
    {
      // Since none exist, create a new instance of the logger.
      logger = (LoggerInf)createInstance(ConfigNodeName.LOGGER_NODE_NAME);
      if (logger == null)
      {
        logger = new DefaultLogger();
        logger.log("Unable to create instance of configured default logger, using default logger class.");
      }
    }
    catch (Exception e)
    {
      logger = new DefaultLogger();
      logger.log("Unable to create user defined logger class - using default logger class.", e);
    }
  }

  /**
   * For testing...
   */
  public static void main(String[] args)
  {
    try
    {
      LoggerInf logger = getLogger();
      logger.log(LoggerInf.LogTypes.ERROR, "ERROR: This is a test message...");
      logger.log(LoggerInf.LogTypes.INFO, "INFO: This is a test message...");

      BaseLookupValue lookup = getLookupValueInstance(null);
      if (lookup != null)
      {
        logger.log("Lookup Class Name: " + lookup.getClass().getName());
      }
      else
      {
        logger.log("Can not get default lookup value.");
      }

      Throwable ex = new Exception("Test Exception...");
      logger.log(ex);
      logger.log("Extra message...", ex);
      logger.log(LoggerInf.LogTypes.WARNING, ex);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -