File Source: actionfactory.java

     1  /*
     2   * Copyright (c) 2003-2006, Simon Brown
     3   * All rights reserved.
     4   *
     5   * Redistribution and use in source and binary forms, with or without
     6   * modification, are permitted provided that the following conditions are met:
     7   *
     8   *   - Redistributions of source code must retain the above copyright
     9   *     notice, this list of conditions and the following disclaimer.
    10   *
    11   *   - Redistributions in binary form must reproduce the above copyright
    12   *     notice, this list of conditions and the following disclaimer in
    13   *     the documentation and/or other materials provided with the
    14   *     distribution.
    15   *
    16   *   - Neither the name of Pebble nor the names of its contributors may
    17   *     be used to endorse or promote products derived from this software
    18   *     without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    30   * POSSIBILITY OF SUCH DAMAGE.
    31   */
    32  package net.sourceforge.pebble.web.action;
    33  
    34  import org.apache.commons.logging.Log;
    35  import org.apache.commons.logging.LogFactory;
    36  
    37  import java.io.InputStream;
    38  import java.util.Enumeration;
    39  import java.util.HashMap;
    40  import java.util.Map;
    41  import java.util.Properties;
    42  
    43  /**
    44   * A factory class from which to look up and retrieve an instance
    45   * of an Action class to process a specific request.
    46   *
    47   * @author    Simon Brown
    48   */
    49  public class ActionFactory {
    50  
    51    /** the log used by this class */
           /* 
    P/P     *  Method: net.sourceforge.pebble.web.action.ActionFactory__static_init
            * 
            *  Postconditions:
            *    init'ed(log)
            */
    52    private static Log log = LogFactory.getLog(ActionFactory.class);
    53  
    54    /** the collection of actions that we know about */
    55    private Map actions = new HashMap();
    56  
    57    /** the name of the action mapping file */
    58    private String actionMappingFileName;
    59  
    60    /**
    61     * Creates a new instance, using the given configuration file.
    62     */
           /* 
    P/P     *  Method: void net.sourceforge.pebble.web.action.ActionFactory(String)
            * 
            *  Preconditions:
            *    (soft) log != null
            * 
            *  Postconditions:
            *    this.actionMappingFileName == actionMappingFileName
            *    init'ed(this.actionMappingFileName)
            *    this.actions == &new HashMap(ActionFactory#1)
            *    new HashMap(ActionFactory#1) num objects == 1
            */
    63    public ActionFactory(String actionMappingFileName) {
    64      this.actionMappingFileName = actionMappingFileName;
    65      init();
    66    }
    67  
    68    /**
    69     * Initialises this component, reading in and creating the map
    70     * of action names to action classes.
    71     */
    72    private void init() {
    73      try {
    74        // load the properties file containing the name -> class name mapping
               /* 
    P/P         *  Method: void init()
                * 
                *  Preconditions:
                *    (soft) log != null
                *    (soft) init'ed(this.actionMappingFileName)
                *    (soft) this.actions != null
                * 
                *  Presumptions:
                *    java.lang.Class:getClassLoader(...)@75 != null
                *    java.lang.Object:getClass(...)@75 != null
                *    java.util.Properties:propertyNames(...)@80 != null
                */
    75        InputStream in = getClass().getClassLoader().getResourceAsStream(actionMappingFileName);
    76        Properties props = new Properties();
    77        props.load(in);
    78  
    79        // and store them in a HashMap
    80        Enumeration e = props.propertyNames();
    81        String actionName;
    82        String className;
    83  
    84        while (e.hasMoreElements()) {
    85          actionName = (String)e.nextElement();
    86          className = props.getProperty(actionName);
    87  
    88          actions.put(actionName, className);
    89        }
    90  
    91      } catch (Exception e) {
    92        log.error(e.getMessage(), e);
    93        e.printStackTrace();
    94      }
    95    }
    96  
    97    /**
    98     * Given the name/type of request, this method returns the Action
    99     * instance appropriate to process the request.
   100     *
   101     * @param name    the name (type) of the request
   102     * @return  an instance of Action (could be null if no mapping has been defined)
   103     */
   104    public Action getAction(String name) throws ActionNotFoundException {
   105      try {
   106        // instantiate the appropriate class to handle the request
               /* 
    P/P         *  Method: Action getAction(String)
                * 
                *  Preconditions:
                *    this.actions != null
                * 
                *  Presumptions:
                *    java.lang.Class:getClassLoader(...)@108 != null
                *    java.lang.ClassLoader:loadClass(...)@108 != null
                *    java.lang.Object:getClass(...)@108 != null
                *    java.util.Map:containsKey(...)@107 == 1
                * 
                *  Postconditions:
                *    return_value != null
                */
   107        if (actions.containsKey(name)) {
   108          Class c = getClass().getClassLoader().loadClass((String)actions.get(name));
   109          Action action = (Action)c.newInstance();
   110  
   111          return action;
   112        } else {
   113          throw new ActionNotFoundException("An action called " + name + " could not be found");
   114        }
   115      } catch (ClassNotFoundException cnfe) {
   116        log.error(cnfe.getMessage(), cnfe);
   117        throw new ActionNotFoundException("An action called " + name + " could not be loaded : " + cnfe.getMessage());
   118      } catch (IllegalAccessException iae) {
   119        log.error(iae.getMessage(), iae);
   120        throw new ActionNotFoundException("An action called " + name + " could not be created : " + iae.getMessage());
   121      } catch (InstantiationException ie) {
   122        log.error(ie.getMessage(), ie);
   123        throw new ActionNotFoundException("An action called " + name + " could not be created : " + ie.getMessage());
   124      }
   125    }
   126  
   127  }








SofCheck Inspector Build Version : 2.22510
actionfactory.java 2010-Jun-25 19:40:34
actionfactory.class 2010-Jul-19 20:23:38