home *** CD-ROM | disk | FTP | other *** search
- package org.apache.cocoon.acting.modular;
-
- import java.util.Iterator;
- import java.util.Map;
- import org.apache.avalon.framework.component.ComponentSelector;
- import org.apache.avalon.framework.configuration.Configurable;
- import org.apache.avalon.framework.configuration.Configuration;
- import org.apache.avalon.framework.configuration.ConfigurationException;
- import org.apache.avalon.framework.parameters.Parameters;
- import org.apache.avalon.framework.thread.ThreadSafe;
- import org.apache.cocoon.acting.ComposerAction;
- import org.apache.cocoon.components.modules.input.InputModule;
- import org.apache.cocoon.components.modules.output.OutputModule;
- import org.apache.cocoon.environment.Redirector;
- import org.apache.cocoon.environment.SourceResolver;
-
- public class TestAction extends ComposerAction implements Configurable, ThreadSafe {
- String INPUT_MODULE_ROLE;
- String OUTPUT_MODULE_ROLE;
- String INPUT_MODULE_SELECTOR;
- String OUTPUT_MODULE_SELECTOR;
- Configuration inputConf;
- Configuration outputConf;
- String inputName;
- String outputName;
- String defaultParameterName;
- boolean useGetValues;
- String inputHint;
- String outputHint;
-
- public TestAction() {
- this.INPUT_MODULE_ROLE = InputModule.ROLE;
- this.OUTPUT_MODULE_ROLE = OutputModule.ROLE;
- this.INPUT_MODULE_SELECTOR = this.INPUT_MODULE_ROLE + "Selector";
- this.OUTPUT_MODULE_SELECTOR = this.OUTPUT_MODULE_ROLE + "Selector";
- this.inputConf = null;
- this.outputConf = null;
- this.inputName = null;
- this.outputName = null;
- this.defaultParameterName = null;
- this.useGetValues = false;
- this.inputHint = "request-param";
- this.outputHint = "request-attr";
- }
-
- public void configure(Configuration config) throws ConfigurationException {
- this.inputConf = config.getChild("input-module");
- this.inputName = this.inputConf.getAttribute("name", this.inputHint);
- this.outputConf = config.getChild("output-module");
- this.outputName = this.outputConf.getAttribute("name", this.outputHint);
- this.defaultParameterName = config.getChild("parameter-name").getValue((String)null);
- this.useGetValues = config.getChild("use-getValues").getValueAsBoolean(this.useGetValues);
- }
-
- public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception {
- String parameterName = param.getParameter("parameter-name", this.defaultParameterName);
- boolean useGetValues = param.getParameterAsBoolean("use-getValues", this.useGetValues);
- InputModule input = null;
- OutputModule output = null;
- ComponentSelector inputSelector = null;
- ComponentSelector outputSelector = null;
-
- try {
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("start...");
- }
-
- inputSelector = (ComponentSelector)this.manager.lookup(this.INPUT_MODULE_SELECTOR);
- if (this.inputName != null && inputSelector != null && inputSelector.hasComponent(this.inputName)) {
- input = (InputModule)inputSelector.select(this.inputName);
- }
-
- outputSelector = (ComponentSelector)this.manager.lookup(this.OUTPUT_MODULE_SELECTOR);
- if (this.outputName != null && outputSelector != null && outputSelector.hasComponent(this.outputName)) {
- output = (OutputModule)outputSelector.select(this.outputName);
- }
-
- if (input != null && output != null) {
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("got input and output modules");
- }
-
- if (parameterName == null) {
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("reading all parameter values");
- }
-
- Iterator iter = input.getAttributeNames(this.inputConf, objectModel);
-
- while(iter.hasNext()) {
- parameterName = (String)iter.next();
- Object value = input.getAttribute(parameterName, this.inputConf, objectModel);
- output.setAttribute(this.outputConf, objectModel, parameterName, value);
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("[" + parameterName + "] = [" + value + "]");
- }
- }
- } else if (useGetValues) {
- Object[] value = input.getAttributeValues(parameterName, this.inputConf, objectModel);
- output.setAttribute(this.outputConf, objectModel, parameterName, value);
- if (this.getLogger().isDebugEnabled()) {
- for(int i = 0; i < value.length; ++i) {
- this.getLogger().debug("[" + parameterName + "[" + i + "]] = [" + value[i] + "]");
- }
- }
- } else {
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("reading parameter values for " + parameterName);
- }
-
- Object value = input.getAttribute(parameterName, this.inputConf, objectModel);
- output.setAttribute(this.outputConf, objectModel, parameterName, value);
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("[" + parameterName + "] = [" + value + "]");
- }
- }
-
- output.commit(this.outputConf, objectModel);
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("done commit");
- }
- }
- } catch (Exception e) {
- throw e;
- } finally {
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("releasing components");
- }
-
- if (outputSelector != null) {
- if (output != null) {
- outputSelector.release(output);
- }
-
- this.manager.release(outputSelector);
- }
-
- if (inputSelector != null) {
- if (input != null) {
- inputSelector.release(input);
- }
-
- this.manager.release(inputSelector);
- }
-
- if (this.getLogger().isDebugEnabled()) {
- this.getLogger().debug("... end");
- }
-
- }
-
- return EMPTY_MAP;
- }
- }
-