1 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 ; 32 import java.util.Map ; 33 34 48 public class TestAction extends ServiceableAction 49 implements Configurable, ThreadSafe { 50 51 String INPUT_MODULE_ROLE = InputModule.ROLE; 52 String OUTPUT_MODULE_ROLE = OutputModule.ROLE; 53 String INPUT_MODULE_SELECTOR = INPUT_MODULE_ROLE+"Selector"; 54 String OUTPUT_MODULE_SELECTOR = OUTPUT_MODULE_ROLE+"Selector"; 55 56 Configuration inputConf = null; 57 Configuration outputConf = null; 58 String inputName = null; 59 String outputName = null; 60 String defaultParameterName = null; 61 boolean useGetValues = false; 62 63 String inputHint = "request-param"; String outputHint = "request-attr"; 66 67 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 85 public Map act( Redirector redirector, SourceResolver resolver, Map objectModel, 86 String source, Parameters param ) throws Exception { 87 88 String 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 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 113 if (parameterName == null) { 114 if (getLogger().isDebugEnabled()) getLogger().debug("reading all parameter values"); 115 Iterator iter = input.getAttributeNames(this.inputConf,objectModel); 118 while (iter.hasNext()) { 119 parameterName = (String ) iter.next(); 120 Object 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 } else { 128 129 if (useGetValues) { 130 Object [] 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 139 } else { 140 if (getLogger().isDebugEnabled()) 142 getLogger().debug("reading parameter values for "+parameterName); 143 144 Object 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 } 150 } 151 output.commit(this.outputConf,objectModel); 152 if (getLogger().isDebugEnabled()) getLogger().debug("done commit"); 153 } 155 156 157 } catch (Exception e) { 158 throw e; 159 } finally { 160 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 |