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

📄 requesthandlerfactory.java

📁 JAVA Servlet2.3外文书籍源码
💻 JAVA
字号:
package firewall.server;

import firewall.common.*;

import java.util.*;

/**
 * Title:        RequestHandlerFactory
 * Description:  Singleton class to map requests to request handlers
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Andrew Harbourne-Thomas
 * @version 1.0
 */
public class RequestHandlerFactory {

  /** maps request types to request handlers */
  private Map requestHandlers;

  /** the application's singleton instance of the RequestHandlerFactory class */
  private static RequestHandlerFactory singleton = null;

  /**
   * Private constructor prevents illegal instantiation.  This initialises the
   * map of request types to handler classes.
   */
  private RequestHandlerFactory() {
    requestHandlers = new HashMap(5);
    //set up the map
    requestHandlers.put(BaseRequest.DELETE, DeleteHandler.class);
    requestHandlers.put(BaseRequest.REFRESH, RefreshHandler.class);
    requestHandlers.put(BaseRequest.DOWNLOAD, DownloadHandler.class);
    requestHandlers.put(BaseRequest.UPLOAD, UploadHandler.class);
  }

  /**
   * Static method to get a reference to the singleton instance of the
   * RequestHandlerFactory.  If the singleton instance has not been created yet
   * it will create it and return it
   *
   * @return the singleton instance of the RequestHandlerFactory class
   */
  public static RequestHandlerFactory getInstance() {
    if (singleton == null) {
      synchronized (RequestHandlerFactory.class) {
        if (singleton == null) {
          singleton = new RequestHandlerFactory();
        }
      }
    }
    return singleton;
  }

  /**
   * retrieves and instantiates the appropriate handler class to process
   * the request
   *
   * @param name the request type
   *
   * @return the appropriate handler for the request
   */
  public RequestHandler getHandler(String name) {
    //look up the handler class in the map and create a new instance
    Class handlerClass = (Class) requestHandlers.get(name);
    RequestHandler handler = instantiateHandler(handlerClass);
    return handler;
  }

  /**
   * instantiates the handler class passed to the method
   *
   * @param handlerClass the handler class to be instantiated
   *
   * @return the instantiated handler
   */
  private RequestHandler instantiateHandler(Class handlerClass) {
    RequestHandler handler = null;
    if (handlerClass != null) {
      try {
        handler = (RequestHandler) handlerClass.newInstance();
      }
      catch (InstantiationException e) {
        e.printStackTrace();
      }
      catch (IllegalAccessException e) {
        e.printStackTrace();
      }
    }

    //in case of unknown/unrecognised request
    if (handler == null) {
      //Code here could handle instantiation problems - perhaps with
      //default handler just in case.
    }

    return handler;
  }
}

⌨️ 快捷键说明

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