001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.acting.modular;
019:
020: import org.apache.avalon.framework.configuration.Configurable;
021: import org.apache.avalon.framework.configuration.Configuration;
022: import org.apache.avalon.framework.configuration.ConfigurationException;
023: import org.apache.avalon.framework.parameters.Parameters;
024: import org.apache.avalon.framework.service.ServiceSelector;
025: import org.apache.avalon.framework.thread.ThreadSafe;
026: import org.apache.cocoon.acting.ServiceableAction;
027: import org.apache.cocoon.components.modules.input.InputModule;
028: import org.apache.cocoon.components.modules.output.OutputModule;
029: import org.apache.cocoon.environment.Redirector;
030: import org.apache.cocoon.environment.SourceResolver;
031:
032: import java.util.Iterator;
033: import java.util.Map;
034:
035: /** Demo action that uses componentized input / output layer. In order
036: * to stop combinatorial explosion of actions, matchers, and selectors
037: * they should instead use components to access their inputs and
038: * outputs. Available components include request parameters,
039: * attributes, headers, and session attributes. Which component to use
040: * can be specified upon setup via "input-module" and
041: * "output-module" tags through the name attribute.
042: *
043: * This particular action copies all available parameters obtained
044: * from the input component to the output component or, if a specific
045: * parameter is specified through parameter-name, just one parameter.
046: *
047: * @version CVS $Id: TestAction.java 433543 2006-08-22 06:22:54Z crossley $
048: */
049: public class TestAction extends ServiceableAction implements
050: Configurable, ThreadSafe {
051:
052: String INPUT_MODULE_ROLE = InputModule.ROLE;
053: String OUTPUT_MODULE_ROLE = OutputModule.ROLE;
054: String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE + "Selector";
055: String OUTPUT_MODULE_SELECTOR = OUTPUT_MODULE_ROLE + "Selector";
056:
057: Configuration inputConf = null;
058: Configuration outputConf = null;
059: String inputName = null;
060: String outputName = null;
061: String defaultParameterName = null;
062: boolean useGetValues = false;
063:
064: String inputHint = "request-param"; // default to request parameters
065: String outputHint = "request-attr"; // default to request attributes
066:
067: /* (non-Javadoc)
068: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
069: */
070: public void configure(Configuration config)
071: throws ConfigurationException {
072:
073: this .inputConf = config.getChild("input-module");
074: this .inputName = this .inputConf.getAttribute("name",
075: this .inputHint);
076: this .outputConf = config.getChild("output-module");
077: this .outputName = this .outputConf.getAttribute("name",
078: this .outputHint);
079: this .defaultParameterName = config.getChild("parameter-name")
080: .getValue(null);
081: this .useGetValues = config.getChild("use-getValues")
082: .getValueAsBoolean(this .useGetValues);
083: }
084:
085: /* (non-Javadoc)
086: * @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)
087: */
088: public Map act(Redirector redirector, SourceResolver resolver,
089: Map objectModel, String source, Parameters param)
090: throws Exception {
091:
092: // general setup
093: String parameterName = param.getParameter("parameter-name",
094: this .defaultParameterName);
095: boolean useGetValues = param.getParameterAsBoolean(
096: "use-getValues", this .useGetValues);
097: InputModule input = null;
098: OutputModule output = null;
099: ServiceSelector inputSelector = null;
100: ServiceSelector outputSelector = null;
101:
102: try {
103: if (getLogger().isDebugEnabled())
104: getLogger().debug("start...");
105: // obtain input and output components
106: inputSelector = (ServiceSelector) this .manager
107: .lookup(INPUT_MODULE_SELECTOR);
108: if (inputName != null && inputSelector != null
109: && inputSelector.isSelectable(inputName)) {
110: input = (InputModule) inputSelector.select(inputName);
111: }
112: outputSelector = (ServiceSelector) this .manager
113: .lookup(OUTPUT_MODULE_SELECTOR);
114: if (outputName != null && outputSelector != null
115: && outputSelector.isSelectable(outputName)) {
116: output = (OutputModule) outputSelector
117: .select(outputName);
118: }
119:
120: if (input != null && output != null) {
121: if (getLogger().isDebugEnabled())
122: getLogger().debug("got input and output modules");
123: // do something
124:
125: if (parameterName == null) {
126: if (getLogger().isDebugEnabled())
127: getLogger().debug(
128: "reading all parameter values");
129: // for a test, read all parameters from input and write them to outout
130: // get names first, then (one) value per name
131: Iterator iter = input.getAttributeNames(
132: this .inputConf, objectModel);
133: while (iter.hasNext()) {
134: parameterName = (String) iter.next();
135: Object value = input.getAttribute(
136: parameterName, this .inputConf,
137: objectModel);
138: output.setAttribute(this .outputConf,
139: objectModel, parameterName, value);
140:
141: if (getLogger().isDebugEnabled())
142: getLogger().debug(
143: "[" + parameterName + "] = ["
144: + value + "]");
145: }
146: // ------------------------------------------------------------------------
147: } else {
148:
149: if (useGetValues) {
150: // get all existing values
151: Object[] value = input.getAttributeValues(
152: parameterName, this .inputConf,
153: objectModel);
154: output.setAttribute(this .outputConf,
155: objectModel, parameterName, value);
156:
157: if (getLogger().isDebugEnabled())
158: for (int i = 0; i < value.length; i++)
159: getLogger().debug(
160: "[" + parameterName + "[" + i
161: + "]] = [" + value[i]
162: + "]");
163: // ------------------------------------------------------------------------
164:
165: } else {
166: // get just one parameter value
167: if (getLogger().isDebugEnabled())
168: getLogger().debug(
169: "reading parameter values for "
170: + parameterName);
171:
172: Object value = input.getAttribute(
173: parameterName, this .inputConf,
174: objectModel);
175: output.setAttribute(this .outputConf,
176: objectModel, parameterName, value);
177:
178: if (getLogger().isDebugEnabled())
179: getLogger().debug(
180: "[" + parameterName + "] = ["
181: + value + "]");
182: // ------------------------------------------------------------------------
183: }
184: }
185: output.commit(this .outputConf, objectModel);
186: if (getLogger().isDebugEnabled())
187: getLogger().debug("done commit");
188: // done
189: }
190:
191: } catch (Exception e) {
192: throw e;
193: } finally {
194: // release components
195: if (getLogger().isDebugEnabled())
196: getLogger().debug("releasing components");
197: if (outputSelector != null) {
198: if (output != null)
199: outputSelector.release(output);
200: this .manager.release(outputSelector);
201: }
202: if (inputSelector != null) {
203: if (input != null)
204: inputSelector.release(input);
205: this .manager.release(inputSelector);
206: }
207: if (getLogger().isDebugEnabled())
208: getLogger().debug("... end");
209: }
210: return EMPTY_MAP;
211: }
212:
213: }
|