Coverage Report - net.sourceforge.pebble.web.action.DefaultActionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultActionFactory
74%
23/31
100%
4/4
3.25
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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.io.IOUtils;
 35  
 import org.apache.commons.logging.Log;
 36  
 import org.apache.commons.logging.LogFactory;
 37  
 import org.springframework.beans.BeansException;
 38  
 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
 39  
 import org.springframework.context.ApplicationContext;
 40  
 import org.springframework.context.ApplicationContextAware;
 41  
 
 42  
 import javax.annotation.PostConstruct;
 43  
 import java.io.IOException;
 44  
 import java.io.InputStream;
 45  
 import java.net.URL;
 46  
 import java.util.Enumeration;
 47  
 import java.util.HashMap;
 48  
 import java.util.Map;
 49  
 import java.util.Properties;
 50  
 
 51  
 /**
 52  
  * A factory class from which to look up and retrieve an instance
 53  
  * of an Action class to process a specific request.
 54  
  *
 55  
  * @author    Simon Brown
 56  
  */
 57  8
 public class DefaultActionFactory implements ActionFactory, ApplicationContextAware {
 58  
 
 59  
   /** the log used by this class */
 60  4
   private static Log log = LogFactory.getLog(DefaultActionFactory.class);
 61  
 
 62  
   /** the collection of actions that we know about */
 63  8
   private final Map<String, String> actions = new HashMap<String, String>();
 64  
 
 65  
   /** the name of the action mapping file */
 66  
   private String actionMappingFileName;
 67  
 
 68  
   /** the ApplicationContext to instantiate actions with */
 69  
   private AutowireCapableBeanFactory beanFactory;
 70  
 
 71  
   /**
 72  
    * Initialises this component, reading in and creating the map
 73  
    * of action names to action classes.
 74  
    *
 75  
    * @throws IOException If an error occurs looking up all the actions
 76  
    */
 77  
   @PostConstruct
 78  
   public void init() throws IOException {
 79  
     // Load all resources by the given name, allows for plugins to provide their own actions
 80  8
     for (Enumeration<URL> e = getClass().getClassLoader().getResources(actionMappingFileName); e.hasMoreElements();) {
 81  8
       URL url = e.nextElement();
 82  
       // load the properties file containing the name -> class name mapping
 83  8
       InputStream in = null;
 84  
       try {
 85  8
         in = url.openStream();
 86  8
         Properties props = new Properties();
 87  8
         props.load(in);
 88  8
         actions.putAll((Map) props);
 89  0
       } catch (IOException ioe) {
 90  0
         log.error("Error reading actions for class: " + url, ioe);
 91  
       } finally {
 92  8
         IOUtils.closeQuietly(in);
 93  8
       }
 94  8
     }
 95  8
   }
 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
 107  12
       if (actions.containsKey(name)) {
 108  8
         Class<?> c = getClass().getClassLoader().loadClass((String)actions.get(name));
 109  8
         Class<? extends Action> actionClass = c.asSubclass(Action.class);
 110  8
         return (Action) beanFactory.createBean(actionClass, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
 111  
       } else {
 112  4
         throw new ActionNotFoundException("An action called " + name + " could not be found");
 113  
       }
 114  0
     } catch (ClassNotFoundException cnfe) {
 115  0
       log.error(cnfe.getMessage(), cnfe);
 116  0
       throw new ActionNotFoundException("An action called " + name + " could not be loaded", cnfe);
 117  0
     } catch (BeansException be) {
 118  0
       log.error(be.getMessage(), be);
 119  0
       throw new ActionNotFoundException("An action called " + name + " could not be instantiated", be);
 120  
     }
 121  
   }
 122  
 
 123  
   public void setActionMappingFileName(String actionMappingFileName) {
 124  8
     this.actionMappingFileName = actionMappingFileName;
 125  8
   }
 126  
 
 127  
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 128  8
     this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
 129  8
   }
 130  
 }