KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > acting > modular > TestAction


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
17 package org.apache.cocoon.acting.modular;
18
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.service.ServiceSelector;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25 import org.apache.cocoon.acting.ServiceableAction;
26 import org.apache.cocoon.components.modules.input.InputModule;
27 import org.apache.cocoon.components.modules.output.OutputModule;
28 import org.apache.cocoon.environment.Redirector;
29 import org.apache.cocoon.environment.SourceResolver;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 /** Demo action that uses componentized input / output layer. In order
35  * to stop combinatorial explosion of actions, matchers, and selectors
36  * they should instead use components to access their inputs and
37  * outputs. Available components include request parameters,
38  * attributes, headers, and session attributes. Which component to use
39  * can be specified upon setup via "input-module" and
40  * "output-module" tags through the name attribute.
41  *
42  * This particular action copies all available parameters obtained
43  * from the input component to the output component or, if a specific
44  * parameter is specified through parameter-name, just one parameter.
45  *
46  * @version CVS $Id: TestAction.java 55135 2004-10-20 06:47:08Z cziegeler $
47  */

48 public class TestAction extends ServiceableAction
49     implements Configurable, ThreadSafe {
50
51     String JavaDoc INPUT_MODULE_ROLE = InputModule.ROLE;
52     String JavaDoc OUTPUT_MODULE_ROLE = OutputModule.ROLE;
53     String JavaDoc INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector";
54     String JavaDoc OUTPUT_MODULE_SELECTOR = OUTPUT_MODULE_ROLE+"Selector";
55
56     Configuration inputConf = null;
57     Configuration outputConf = null;
58     String JavaDoc inputName = null;
59     String JavaDoc outputName = null;
60     String JavaDoc defaultParameterName = null;
61     boolean useGetValues = false;
62
63     String JavaDoc inputHint = "request-param"; // default to request parameters
64
String JavaDoc outputHint = "request-attr"; // default to request attributes
65

66
67     /* (non-Javadoc)
68      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
69      */

70     public void configure(Configuration config) throws ConfigurationException {
71
72         this.inputConf = config.getChild("input-module");
73         this.inputName = this.inputConf.getAttribute("name", this.inputHint);
74         this.outputConf = config.getChild("output-module");
75         this.outputName = this.outputConf.getAttribute("name", this.outputHint);
76         this.defaultParameterName = config.getChild("parameter-name").getValue(null);
77         this.useGetValues = config.getChild("use-getValues").getValueAsBoolean(this.useGetValues);
78     }
79
80
81
82     /* (non-Javadoc)
83      * @see org.apache.cocoon.acting.Action#act(org.apache.cocoon.environment.Redirector, org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters)
84      */

85     public Map JavaDoc act( Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel,
86                     String JavaDoc source, Parameters param ) throws Exception JavaDoc {
87
88         // general setup
89
String JavaDoc parameterName = param.getParameter("parameter-name",this.defaultParameterName);
90         boolean useGetValues = param.getParameterAsBoolean("use-getValues",this.useGetValues);
91         InputModule input = null;
92         OutputModule output = null;
93         ServiceSelector inputSelector = null;
94         ServiceSelector outputSelector = null;
95
96         try {
97             if (getLogger().isDebugEnabled()) getLogger().debug("start...");
98             // obtain input and output components
99
inputSelector=(ServiceSelector) this.manager.lookup(INPUT_MODULE_SELECTOR);
100             if (inputName != null && inputSelector != null && inputSelector.isSelectable(inputName)){
101                 input = (InputModule) inputSelector.select(inputName);
102             }
103             outputSelector=(ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR);
104             if (outputName != null && outputSelector != null && outputSelector.isSelectable(outputName)){
105                 output = (OutputModule) outputSelector.select(outputName);
106             }
107
108
109             if (input != null && output != null) {
110                 if (getLogger().isDebugEnabled()) getLogger().debug("got input and output modules");
111                 // do something
112

113                 if (parameterName == null) {
114                     if (getLogger().isDebugEnabled()) getLogger().debug("reading all parameter values");
115                     // for a test, read all parameters from input and write them to outout
116
// get names first, then (one) value per name
117
Iterator JavaDoc iter = input.getAttributeNames(this.inputConf,objectModel);
118                     while (iter.hasNext()) {
119                         parameterName = (String JavaDoc) iter.next();
120                         Object JavaDoc value = input.getAttribute(parameterName, this.inputConf, objectModel);
121                         output.setAttribute(this.outputConf, objectModel, parameterName, value);
122                         
123                         if (getLogger().isDebugEnabled())
124                             getLogger().debug("["+parameterName+"] = ["+value+"]");
125                     }
126                         // ------------------------------------------------------------------------
127
} else {
128
129                     if (useGetValues) {
130                         // get all existing values
131
Object JavaDoc[] value = input.getAttributeValues(parameterName, this.inputConf, objectModel);
132                         output.setAttribute(this.outputConf, objectModel, parameterName, value);
133                         
134                         if (getLogger().isDebugEnabled())
135                             for (int i=0; i<value.length; i++)
136                                 getLogger().debug("["+parameterName+"["+i+"]] = ["+value[i]+"]");
137                         // ------------------------------------------------------------------------
138

139                     } else {
140                         // get just one parameter value
141
if (getLogger().isDebugEnabled())
142                             getLogger().debug("reading parameter values for "+parameterName);
143                         
144                         Object JavaDoc value = input.getAttribute(parameterName, this.inputConf, objectModel);
145                         output.setAttribute(this.outputConf, objectModel, parameterName, value);
146                         
147                         if (getLogger().isDebugEnabled()) getLogger().debug("["+parameterName+"] = ["+value+"]");
148                         // ------------------------------------------------------------------------
149
}
150                 }
151                 output.commit(this.outputConf,objectModel);
152                 if (getLogger().isDebugEnabled()) getLogger().debug("done commit");
153                 // done
154
}
155
156
157         } catch (Exception JavaDoc e) {
158             throw e;
159         } finally {
160             // release components
161
if (getLogger().isDebugEnabled()) getLogger().debug("releasing components");
162             if (outputSelector != null) {
163                 if (output != null)
164                     outputSelector.release(output);
165                 this.manager.release(outputSelector);
166             }
167             if (inputSelector != null) {
168                 if (input != null)
169                     inputSelector.release(input);
170                 this.manager.release(inputSelector);
171             }
172             if (getLogger().isDebugEnabled()) getLogger().debug("... end");
173         }
174         return EMPTY_MAP;
175     }
176
177 }
178
Popular Tags