home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / TestAction.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  6.1 KB  |  155 lines

  1. package org.apache.cocoon.acting.modular;
  2.  
  3. import java.util.Iterator;
  4. import java.util.Map;
  5. import org.apache.avalon.framework.component.ComponentSelector;
  6. import org.apache.avalon.framework.configuration.Configurable;
  7. import org.apache.avalon.framework.configuration.Configuration;
  8. import org.apache.avalon.framework.configuration.ConfigurationException;
  9. import org.apache.avalon.framework.parameters.Parameters;
  10. import org.apache.avalon.framework.thread.ThreadSafe;
  11. import org.apache.cocoon.acting.ComposerAction;
  12. import org.apache.cocoon.components.modules.input.InputModule;
  13. import org.apache.cocoon.components.modules.output.OutputModule;
  14. import org.apache.cocoon.environment.Redirector;
  15. import org.apache.cocoon.environment.SourceResolver;
  16.  
  17. public class TestAction extends ComposerAction implements Configurable, ThreadSafe {
  18.    String INPUT_MODULE_ROLE;
  19.    String OUTPUT_MODULE_ROLE;
  20.    String INPUT_MODULE_SELECTOR;
  21.    String OUTPUT_MODULE_SELECTOR;
  22.    Configuration inputConf;
  23.    Configuration outputConf;
  24.    String inputName;
  25.    String outputName;
  26.    String defaultParameterName;
  27.    boolean useGetValues;
  28.    String inputHint;
  29.    String outputHint;
  30.  
  31.    public TestAction() {
  32.       this.INPUT_MODULE_ROLE = InputModule.ROLE;
  33.       this.OUTPUT_MODULE_ROLE = OutputModule.ROLE;
  34.       this.INPUT_MODULE_SELECTOR = this.INPUT_MODULE_ROLE + "Selector";
  35.       this.OUTPUT_MODULE_SELECTOR = this.OUTPUT_MODULE_ROLE + "Selector";
  36.       this.inputConf = null;
  37.       this.outputConf = null;
  38.       this.inputName = null;
  39.       this.outputName = null;
  40.       this.defaultParameterName = null;
  41.       this.useGetValues = false;
  42.       this.inputHint = "request-param";
  43.       this.outputHint = "request-attr";
  44.    }
  45.  
  46.    public void configure(Configuration config) throws ConfigurationException {
  47.       this.inputConf = config.getChild("input-module");
  48.       this.inputName = this.inputConf.getAttribute("name", this.inputHint);
  49.       this.outputConf = config.getChild("output-module");
  50.       this.outputName = this.outputConf.getAttribute("name", this.outputHint);
  51.       this.defaultParameterName = config.getChild("parameter-name").getValue((String)null);
  52.       this.useGetValues = config.getChild("use-getValues").getValueAsBoolean(this.useGetValues);
  53.    }
  54.  
  55.    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters param) throws Exception {
  56.       String parameterName = param.getParameter("parameter-name", this.defaultParameterName);
  57.       boolean useGetValues = param.getParameterAsBoolean("use-getValues", this.useGetValues);
  58.       InputModule input = null;
  59.       OutputModule output = null;
  60.       ComponentSelector inputSelector = null;
  61.       ComponentSelector outputSelector = null;
  62.  
  63.       try {
  64.          if (this.getLogger().isDebugEnabled()) {
  65.             this.getLogger().debug("start...");
  66.          }
  67.  
  68.          inputSelector = (ComponentSelector)this.manager.lookup(this.INPUT_MODULE_SELECTOR);
  69.          if (this.inputName != null && inputSelector != null && inputSelector.hasComponent(this.inputName)) {
  70.             input = (InputModule)inputSelector.select(this.inputName);
  71.          }
  72.  
  73.          outputSelector = (ComponentSelector)this.manager.lookup(this.OUTPUT_MODULE_SELECTOR);
  74.          if (this.outputName != null && outputSelector != null && outputSelector.hasComponent(this.outputName)) {
  75.             output = (OutputModule)outputSelector.select(this.outputName);
  76.          }
  77.  
  78.          if (input != null && output != null) {
  79.             if (this.getLogger().isDebugEnabled()) {
  80.                this.getLogger().debug("got input and output modules");
  81.             }
  82.  
  83.             if (parameterName == null) {
  84.                if (this.getLogger().isDebugEnabled()) {
  85.                   this.getLogger().debug("reading all parameter values");
  86.                }
  87.  
  88.                Iterator iter = input.getAttributeNames(this.inputConf, objectModel);
  89.  
  90.                while(iter.hasNext()) {
  91.                   parameterName = (String)iter.next();
  92.                   Object value = input.getAttribute(parameterName, this.inputConf, objectModel);
  93.                   output.setAttribute(this.outputConf, objectModel, parameterName, value);
  94.                   if (this.getLogger().isDebugEnabled()) {
  95.                      this.getLogger().debug("[" + parameterName + "] = [" + value + "]");
  96.                   }
  97.                }
  98.             } else if (useGetValues) {
  99.                Object[] value = input.getAttributeValues(parameterName, this.inputConf, objectModel);
  100.                output.setAttribute(this.outputConf, objectModel, parameterName, value);
  101.                if (this.getLogger().isDebugEnabled()) {
  102.                   for(int i = 0; i < value.length; ++i) {
  103.                      this.getLogger().debug("[" + parameterName + "[" + i + "]] = [" + value[i] + "]");
  104.                   }
  105.                }
  106.             } else {
  107.                if (this.getLogger().isDebugEnabled()) {
  108.                   this.getLogger().debug("reading parameter values for " + parameterName);
  109.                }
  110.  
  111.                Object value = input.getAttribute(parameterName, this.inputConf, objectModel);
  112.                output.setAttribute(this.outputConf, objectModel, parameterName, value);
  113.                if (this.getLogger().isDebugEnabled()) {
  114.                   this.getLogger().debug("[" + parameterName + "] = [" + value + "]");
  115.                }
  116.             }
  117.  
  118.             output.commit(this.outputConf, objectModel);
  119.             if (this.getLogger().isDebugEnabled()) {
  120.                this.getLogger().debug("done commit");
  121.             }
  122.          }
  123.       } catch (Exception e) {
  124.          throw e;
  125.       } finally {
  126.          if (this.getLogger().isDebugEnabled()) {
  127.             this.getLogger().debug("releasing components");
  128.          }
  129.  
  130.          if (outputSelector != null) {
  131.             if (output != null) {
  132.                outputSelector.release(output);
  133.             }
  134.  
  135.             this.manager.release(outputSelector);
  136.          }
  137.  
  138.          if (inputSelector != null) {
  139.             if (input != null) {
  140.                inputSelector.release(input);
  141.             }
  142.  
  143.             this.manager.release(inputSelector);
  144.          }
  145.  
  146.          if (this.getLogger().isDebugEnabled()) {
  147.             this.getLogger().debug("... end");
  148.          }
  149.  
  150.       }
  151.  
  152.       return EMPTY_MAP;
  153.    }
  154. }
  155.