| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 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 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | 8 | public class DefaultActionFactory implements ActionFactory, ApplicationContextAware { |
| 58 | |
|
| 59 | |
|
| 60 | 4 | private static Log log = LogFactory.getLog(DefaultActionFactory.class); |
| 61 | |
|
| 62 | |
|
| 63 | 8 | private final Map<String, String> actions = new HashMap<String, String>(); |
| 64 | |
|
| 65 | |
|
| 66 | |
private String actionMappingFileName; |
| 67 | |
|
| 68 | |
|
| 69 | |
private AutowireCapableBeanFactory beanFactory; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
@PostConstruct |
| 78 | |
public void init() throws IOException { |
| 79 | |
|
| 80 | 8 | for (Enumeration<URL> e = getClass().getClassLoader().getResources(actionMappingFileName); e.hasMoreElements();) { |
| 81 | 8 | URL url = e.nextElement(); |
| 82 | |
|
| 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 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
public Action getAction(String name) throws ActionNotFoundException { |
| 105 | |
try { |
| 106 | |
|
| 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 | |
} |