KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > AbstractMultiAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 /**
33  * The <code>AbstractMultiAction</code> provides a way
34  * to call methods of an action specified by
35  * the <code>method</code> parameter or request parameter.
36  * This can be extremly useful for action-sets or as
37  * action-sets replacement.
38  *
39  * Example:
40  * <input type="submit" name="doSave" value="Save it"/>
41  * will call the method "doSave" of the MultiAction
42  *
43  * @author <a HREF="mailto:tcurdt@dff.st">Torsten Curdt</a>
44  * @version CVS $Id: AbstractMultiAction.java 30932 2004-07-29 17:35:38Z vgritsenko $
45  */

46 public abstract class AbstractMultiAction extends ConfigurableServiceableAction {
47
48     private static final String JavaDoc ACTION_METHOD_PREFIX = "do";
49     private static final String JavaDoc ACTION_METHOD_PARAMETER = "method";
50
51     private HashMap JavaDoc methodIndex;
52
53     private static final String JavaDoc removePrefix( String JavaDoc 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 JavaDoc[] methods = this.getClass().getMethods();
63             methodIndex = new HashMap JavaDoc();
64
65             for (int i = 0; i < methods.length; i++) {
66                 String JavaDoc methodName = methods[i].getName();
67                 if (methodName.startsWith(ACTION_METHOD_PREFIX)) {
68                     String JavaDoc 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 JavaDoc e) {
76             throw new ConfigurationException("cannot get methods by reflection", e);
77         }
78     }
79
80
81     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source, Parameters parameters) throws Exception JavaDoc {
82         String JavaDoc actionMethod = parameters.getParameter(ACTION_METHOD_PARAMETER, null);
83
84         if (actionMethod == null) {
85             Request req = ObjectModelHelper.getRequest(objectModel);
86             if (req != null) {
87                 // checking request for action method parameters
88
String JavaDoc name;
89                 for (Enumeration JavaDoc e = req.getParameterNames(); e.hasMoreElements();) {
90                     name = (String JavaDoc) 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 JavaDoc method = (Method JavaDoc) methodIndex.get(actionMethod);
104             if (method != null) {
105                 try {
106                     return ((Map JavaDoc) method.invoke(this, new Object JavaDoc[]{redirector, resolver, objectModel, source, parameters}));
107                 } catch (InvocationTargetException JavaDoc ite) {
108                     if ((ite.getTargetException() != null) && (ite.getTargetException() instanceof Exception JavaDoc)) {
109                         throw (Exception JavaDoc)ite.getTargetException();
110                     } else {
111                         throw ite;
112                     }
113                 }
114             } else {
115                 throw new Exception JavaDoc("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