|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| This report was generated with an evaluation server license. Purchase Clover or configure your license. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ActionFactory | Line # 49 | 27 | 0% | 9 | 8 | 76,5% |
0.7647059
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (4) | |||
| Result | |||
|
0.7058824
|
net.sourceforge.pebble.web.action.ActionFactoryTest.testGetAction
net.sourceforge.pebble.web.action.ActionFactoryTest.testGetAction
|
1 PASS | |
|
0.7058824
|
net.sourceforge.pebble.web.action.ActionFactoryTest.testGetAction
net.sourceforge.pebble.web.action.ActionFactoryTest.testGetAction
|
1 PASS | |
|
0.64705884
|
net.sourceforge.pebble.web.action.ActionFactoryTest.testActionNotFound
net.sourceforge.pebble.web.action.ActionFactoryTest.testActionNotFound
|
1 PASS | |
|
0.64705884
|
net.sourceforge.pebble.web.action.ActionFactoryTest.testActionNotFound
net.sourceforge.pebble.web.action.ActionFactoryTest.testActionNotFound
|
1 PASS | |
| 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 */ | |
| 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 | */ | |
| 63 | 4 |
public ActionFactory(String actionMappingFileName) { |
| 64 | 4 | this.actionMappingFileName = actionMappingFileName; |
| 65 | 4 | init(); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Initialises this component, reading in and creating the map | |
| 70 | * of action names to action classes. | |
| 71 | */ | |
| 72 | 4 |
private void init() { |
| 73 | 4 | try { |
| 74 | // load the properties file containing the name -> class name mapping | |
| 75 | 4 | InputStream in = getClass().getClassLoader().getResourceAsStream(actionMappingFileName); |
| 76 | 4 | Properties props = new Properties(); |
| 77 | 4 | props.load(in); |
| 78 | ||
| 79 | // and store them in a HashMap | |
| 80 | 4 | Enumeration e = props.propertyNames(); |
| 81 | 4 | String actionName; |
| 82 | 4 | String className; |
| 83 | ||
| 84 | 144 | while (e.hasMoreElements()) { |
| 85 | 140 | actionName = (String)e.nextElement(); |
| 86 | 140 | className = props.getProperty(actionName); |
| 87 | ||
| 88 | 140 | actions.put(actionName, className); |
| 89 | } | |
| 90 | ||
| 91 | } catch (Exception e) { | |
| 92 | 0 | log.error(e.getMessage(), e); |
| 93 | 0 | 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 | 6 |
public Action getAction(String name) throws ActionNotFoundException { |
| 105 | 6 | try { |
| 106 | // instantiate the appropriate class to handle the request | |
| 107 | 6 | if (actions.containsKey(name)) { |
| 108 | 4 | Class c = getClass().getClassLoader().loadClass((String)actions.get(name)); |
| 109 | 4 | Action action = (Action)c.newInstance(); |
| 110 | ||
| 111 | 4 | return action; |
| 112 | } else { | |
| 113 | 2 | throw new ActionNotFoundException("An action called " + name + " could not be found"); |
| 114 | } | |
| 115 | } catch (ClassNotFoundException cnfe) { | |
| 116 | 0 | log.error(cnfe.getMessage(), cnfe); |
| 117 | 0 | throw new ActionNotFoundException("An action called " + name + " could not be loaded : " + cnfe.getMessage()); |
| 118 | } catch (IllegalAccessException iae) { | |
| 119 | 0 | log.error(iae.getMessage(), iae); |
| 120 | 0 | throw new ActionNotFoundException("An action called " + name + " could not be created : " + iae.getMessage()); |
| 121 | } catch (InstantiationException ie) { | |
| 122 | 0 | log.error(ie.getMessage(), ie); |
| 123 | 0 | throw new ActionNotFoundException("An action called " + name + " could not be created : " + ie.getMessage()); |
| 124 | } | |
| 125 | } | |
| 126 | ||
| 127 | } | |
|
||||||||||||