1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.avalon.framework.service.ServiceSelector; 23 import org.apache.avalon.framework.thread.ThreadSafe; 24 25 import org.apache.cocoon.components.modules.output.OutputModule; 26 import org.apache.cocoon.environment.Redirector; 27 import org.apache.cocoon.environment.SourceResolver; 28 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 36 116 public class PropagatorAction extends ServiceableAction 117 implements Configurable, ThreadSafe { 118 119 120 private static final String ACTION_PREFIX = "PropagatorAction:"; 121 122 123 private static final String CONFIG_STORE_EMPTY = "store-empty-parameters"; 124 125 126 private static final String CONFIG_OUTPUT_MODULE = "output-module"; 127 128 129 private static final String OUTPUT_HINT = "request-attr"; 131 132 133 private boolean storeEmpty = true; 134 135 136 private Configuration outputConf; 137 138 139 private String outputName; 140 141 142 private List defaults; 143 144 148 private static class Entry { 149 public String key; 150 public String value; 151 152 public Entry(String key, String value) { 153 this.key = key; 154 this.value = value; 155 } 156 } 157 158 161 public void configure(Configuration config) throws ConfigurationException { 162 this.outputConf = config.getChild(CONFIG_OUTPUT_MODULE); 163 this.outputName = this.outputConf.getAttribute("name", OUTPUT_HINT); 164 this.storeEmpty = 165 config.getChild(CONFIG_STORE_EMPTY).getValueAsBoolean(this.storeEmpty); 166 167 Configuration[] dflts = config.getChild("defaults").getChildren("default"); 168 if (dflts != null) { 169 this.defaults = new ArrayList (dflts.length); 170 for (int i = 0; i < dflts.length; i++) { 171 this.defaults.add( 172 new Entry(dflts[i].getAttribute("name"), 173 dflts[i].getAttribute("value"))); 174 } 175 } else { 176 this.defaults = new ArrayList (0); 177 } 178 } 179 180 183 public Map act(Redirector redirector, 184 SourceResolver resolver, 185 Map objectModel, 186 String source, 187 Parameters parameters) 188 throws Exception { 189 String outputName = parameters.getParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE, 191 null); 192 boolean storeEmpty = parameters.getParameterAsBoolean(ACTION_PREFIX + CONFIG_STORE_EMPTY, 193 this.storeEmpty); 194 parameters.removeParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE); 195 parameters.removeParameter(ACTION_PREFIX + CONFIG_STORE_EMPTY); 196 197 Configuration outputConf = null; 198 if (outputName == null) { 199 outputName = this.outputName; 200 outputConf = this.outputConf; 201 } 202 203 final Map results = new HashMap (); 205 206 OutputModule output = null; 207 ServiceSelector selector = null; 208 try { 209 selector = (ServiceSelector) this.manager.lookup(OutputModule.ROLE + "Selector"); 210 if (outputName != null 211 && selector != null 212 && selector.isSelectable(outputName)) { 213 214 output = (OutputModule) selector.select(outputName); 215 216 String [] names = parameters.getNames(); 217 for (int i = 0; i < names.length; i++) { 218 String name = names[i]; 219 String value = parameters.getParameter(name); 220 if (storeEmpty || (value != null && !value.equals(""))) { 221 if (getLogger().isDebugEnabled()) { 222 getLogger().debug("Propagating <" + name + "> value <" + value + ">"); 223 } 224 output.setAttribute(outputConf, 225 objectModel, 226 name, 227 value); 228 results.put(name, value); 229 } 230 } 231 232 for (Iterator i = defaults.iterator(); i.hasNext();) { 234 Entry entry = (Entry) i.next(); 235 if (!results.containsKey(entry.key)) { 236 if (getLogger().isDebugEnabled()) { 237 getLogger().debug("Propagating default <" + entry.key + "> value <" + entry.value + ">"); 238 } 239 output.setAttribute(outputConf, 240 objectModel, 241 entry.key, 242 entry.value); 243 results.put(entry.key, entry.value); 244 } 245 } 246 247 output.commit(outputConf, objectModel); 248 } 249 } catch (Exception e) { 250 if (output != null) { 251 output.rollback(outputConf, objectModel, e); 252 } 253 throw e; 254 } finally { 255 if (selector != null) { 256 if (output != null) { 257 selector.release(output); 258 } 259 this.manager.release(selector); 260 } 261 } 262 263 return Collections.unmodifiableMap(results); 264 } 265 } 266 | Popular Tags |