1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.parameters.Parameters; 21 import org.apache.cocoon.environment.Redirector; 22 import org.apache.cocoon.environment.SourceResolver; 23 import org.apache.cocoon.environment.Request; 24 import org.apache.cocoon.environment.ObjectModelHelper; 25 26 import java.util.HashMap ; 27 import java.util.Map ; 28 import java.util.Enumeration ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 32 46 public abstract class AbstractMultiAction extends ConfigurableServiceableAction { 47 48 private static final String ACTION_METHOD_PREFIX = "do"; 49 private static final String ACTION_METHOD_PARAMETER = "method"; 50 51 private HashMap methodIndex; 52 53 private static final String removePrefix( String name ) { 54 int prefixLen = ACTION_METHOD_PREFIX.length(); 55 return name.substring(prefixLen, prefixLen + 1).toLowerCase() + name.substring(prefixLen + 1); 56 } 57 58 public void configure(Configuration conf) throws ConfigurationException { 59 super.configure(conf); 60 61 try { 62 Method [] methods = this.getClass().getMethods(); 63 methodIndex = new HashMap (); 64 65 for (int i = 0; i < methods.length; i++) { 66 String methodName = methods[i].getName(); 67 if (methodName.startsWith(ACTION_METHOD_PREFIX)) { 68 String actionName = removePrefix(methodName); 69 methodIndex.put(actionName, methods[i]); 70 if (getLogger().isDebugEnabled()) { 71 getLogger().debug("registered method \"" + methodName + "\" as action \"" + actionName + "\""); 72 } 73 } 74 } 75 } catch (Exception e) { 76 throw new ConfigurationException("cannot get methods by reflection", e); 77 } 78 } 79 80 81 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception { 82 String actionMethod = parameters.getParameter(ACTION_METHOD_PARAMETER, null); 83 84 if (actionMethod == null) { 85 Request req = ObjectModelHelper.getRequest(objectModel); 86 if (req != null) { 87 String name; 89 for (Enumeration e = req.getParameterNames(); e.hasMoreElements();) { 90 name = (String ) e.nextElement(); 91 if (name.startsWith(ACTION_METHOD_PREFIX)) { 92 if (name.endsWith(".x") || name.endsWith(".y")) { 93 name = name.substring(ACTION_METHOD_PREFIX.length(), name.length() - 2); 94 } 95 actionMethod = removePrefix(name); 96 break; 97 } 98 } 99 } 100 } 101 102 if((actionMethod != null) && (actionMethod.length() > 0)) { 103 Method method = (Method ) methodIndex.get(actionMethod); 104 if (method != null) { 105 try { 106 return ((Map ) method.invoke(this, new Object []{redirector, resolver, objectModel, source, parameters})); 107 } catch (InvocationTargetException ite) { 108 if ((ite.getTargetException() != null) && (ite.getTargetException() instanceof Exception )) { 109 throw (Exception )ite.getTargetException(); 110 } else { 111 throw ite; 112 } 113 } 114 } else { 115 throw new Exception ("action has no method \"" + actionMethod + "\""); 116 } 117 } 118 119 if (getLogger().isDebugEnabled()) { 120 getLogger().debug("you need to specify the method with parameter \"" + ACTION_METHOD_PARAMETER + "\" or have a request parameter starting with \"" + ACTION_METHOD_PREFIX + "\""); 121 } 122 return null; 123 } 124 } 125 | Popular Tags |