cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r126301 - /cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java
Date Mon, 24 Jan 2005 16:24:28 GMT
Author: vgritsenko
Date: Mon Jan 24 08:24:28 2005
New Revision: 126301

URL: http://svn.apache.org/viewcvs?view=rev&rev=126301
Log:
code cleanup

Modified:
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java
Url: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java?view=diff&rev=126301&p1=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java&r1=126300&p2=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java&r2=126301
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java	(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/acting/PropagatorAction.java	Mon
Jan 24 08:24:28 2005
@@ -1,19 +1,18 @@
 /*
  * Copyright 1999-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.cocoon.acting;
 
 import org.apache.avalon.framework.configuration.Configurable;
@@ -22,10 +21,10 @@
 import org.apache.avalon.framework.parameters.Parameters;
 import org.apache.avalon.framework.service.ServiceSelector;
 import org.apache.avalon.framework.thread.ThreadSafe;
+
 import org.apache.cocoon.components.modules.output.OutputModule;
 import org.apache.cocoon.environment.Redirector;
 import org.apache.cocoon.environment.SourceResolver;
-import org.apache.commons.lang.BooleanUtils;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -34,12 +33,12 @@
 import java.util.List;
 import java.util.Map;
 
-/** 
- * This is the action used to propagate parameters into a store using an 
+/**
+ * This is the action used to propagate parameters into a store using an
  * {@link org.apache.cocoon.components.modules.output.OutputModule}. It
  * simply propagates given expression. Additionaly it will make all propagated values
  * available via returned Map.
- * 
+ *
  * <p>Example configuration:</p>
  * <pre>
  * &lt;map:action type="...." name="...." logger="..."&gt;
@@ -63,7 +62,7 @@
  *      &lt;parameter name="PropagatorAction:output-module" value="session-attr"/&gt;
  * &lt;/map:act&gt;
  * </pre>
- * 
+ *
  * <h3>Configuration</h3>
  * <table><tbody>
  * <tr>
@@ -114,192 +113,153 @@
  * @author <a href="mailto:Martin.Man@seznam.cz">Martin Man</a>
  * @version CVS $Id$
  */
-public class PropagatorAction
-	extends  ServiceableAction
-	implements Configurable, ThreadSafe {
-
-    /** Role name for output modules. */
-    private static final String OUTPUT_MODULE_ROLE = OutputModule.ROLE;
-    
-    /** Selector name for output modules. */
-    private static final String OUTPUT_MODULE_SELECTOR = OUTPUT_MODULE_ROLE + "Selector";
+public class PropagatorAction extends ServiceableAction
+                              implements Configurable, ThreadSafe {
 
     /** Prefix for sitemap parameters targeted at this action. */
-	private static final String ACTION_PREFIX = "PropagatorAction:";
+    private static final String ACTION_PREFIX = "PropagatorAction:";
 
     /** Configuration parameter name. */
-	private static final String CONFIG_STORE_EMPTY = "store-empty-parameters";
+    private static final String CONFIG_STORE_EMPTY = "store-empty-parameters";
 
     /** Configuration parameter name. */
-	private static final String CONFIG_OUTPUT_MODULE = "output-module";
+    private static final String CONFIG_OUTPUT_MODULE = "output-module";
+
+    /** Default output module name. */
+    private static final String OUTPUT_HINT = "request-attr"; // defaults to request attributes
+
 
     /** Should empty parameter values be propagated? */
-	private boolean storeEmpty = true;
+    private boolean storeEmpty = true;
 
     /** Configuration object for output module. */
-	private Configuration outputConf = null;
-    
+    private Configuration outputConf;
+
     /** Name of output module to use. */
-	private String outputName = null;
+    private String outputName;
 
-    /** Default output module name. */
-	private static final String outputHint = "request-attr"; // default to request attributes
-    
     /** List of {@link Entry}s holding default values. */
-	private List defaults = null;
+    private List defaults;
 
     /**
      * A private helper holding default parameter entries.
-     * 
+     *
+     */
+    private static class Entry {
+        public String key;
+        public String value;
+
+        public Entry(String key, String value) {
+            this.key = key;
+            this.value = value;
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+     */
+    public void configure(Configuration config) throws ConfigurationException {
+        this.outputConf = config.getChild(CONFIG_OUTPUT_MODULE);
+        this.outputName = this.outputConf.getAttribute("name", OUTPUT_HINT);
+        this.storeEmpty =
+            config.getChild(CONFIG_STORE_EMPTY).getValueAsBoolean(this.storeEmpty);
+
+        Configuration[] dflts = config.getChild("defaults").getChildren("default");
+        if (dflts != null) {
+            this.defaults = new ArrayList(dflts.length);
+            for (int i = 0; i < dflts.length; i++) {
+                this.defaults.add(
+                        new Entry(dflts[i].getAttribute("name"),
+                                  dflts[i].getAttribute("value")));
+            }
+        } else {
+            this.defaults = new ArrayList(0);
+        }
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.cocoon.acting.Action#act(Redirector, SourceResolver, Map, String,
Parameters)
      */
-	private static class Entry {
-		public String key = null;
-		public String value = null;
-
-		public Entry(String key, String value) {
-			this.key = key;
-			this.value = value;
-		}
-	}
-
-	/*
-	 *  (non-Javadoc)
-	 * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
-	 */
-	public void configure(Configuration config) throws ConfigurationException {
-
-		this.outputConf = config.getChild(CONFIG_OUTPUT_MODULE);
-		this.outputName = this.outputConf.getAttribute("name", outputHint);
-		this.storeEmpty =
-			config.getChild(CONFIG_STORE_EMPTY).getValueAsBoolean(this.storeEmpty);
-		Configuration[] dflts = config.getChild("defaults").getChildren("default");
-		if (dflts != null) {
-			this.defaults = new ArrayList(dflts.length);
-			for (int i = 0; i < dflts.length; i++) {
-				this.defaults.add(
-					new Entry(
-						dflts[i].getAttribute("name"),
-						dflts[i].getAttribute("value")));
-			}
-		} else {
-			this.defaults = new ArrayList(0);
-		}
-	}
-
-	/**
-	 * Read parameters and remove configuration for this action.
-	 * 
-	 * @param param
-	 * @return
-	 */
-	private Object[] readParameters(Parameters param) {
-		String outputName =
-			param.getParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE, null);
-		Boolean storeEmpty =
-			BooleanUtils.toBooleanObject(
-				param.getParameterAsBoolean(
-					ACTION_PREFIX + CONFIG_STORE_EMPTY, this.storeEmpty));
-		param.removeParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE);
-		param.removeParameter(ACTION_PREFIX + CONFIG_STORE_EMPTY);
-		return new Object[] { outputName, storeEmpty };
-	}
-
-	/*
-	 *  (non-Javadoc)
-	 * @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)
-	 */
-	public Map act(
-		Redirector redirector,
-		SourceResolver resolver,
-		Map objectModel,
-		String source,
-		Parameters parameters)
-		throws Exception {
-
-		// general setup
-		OutputModule output = null;
-		ServiceSelector outputSelector = null;
-
-		// I don't like this. Have a better idea to return two values.		
-		Object[] obj = this.readParameters(parameters);
-		String outputName = (String) obj[0];
-		boolean storeEmpty = ((Boolean) obj[1]).booleanValue();
-
-		Configuration outputConf = null;
-		if (outputName == null) {
-			outputName = this.outputName;
-			outputConf = this.outputConf;
-		}
-
-		Map actionMap = new HashMap();
-
-		try {
-			outputSelector =
-				(ServiceSelector) this.manager.lookup(OUTPUT_MODULE_SELECTOR);
-			if (outputName != null
-				&& outputSelector != null
-				&& outputSelector.isSelectable(outputName)) {
-
-				output = (OutputModule) outputSelector.select(outputName);
-
-				String[] names = parameters.getNames();
-
-				// parameters
-				for (int i = 0; i < names.length; i++) {
-					String sessionParamName = names[i];
-					String value = parameters.getParameter(sessionParamName);
-					if (storeEmpty || (value != null && !value.equals(""))) {
-						if (getLogger().isDebugEnabled()) {
-							getLogger().debug(
-								"Propagating value "
-									+ value
-									+ " to output module"
-									+ sessionParamName);
-						}
-						output.setAttribute(
-							outputConf,
-							objectModel,
-							sessionParamName,
-							value);
-						actionMap.put(sessionParamName, value);
-					}
-				}
-
-				// defaults, that are not overridden
-				for (Iterator i = defaults.iterator(); i.hasNext();) {
-					Entry entry = (Entry) i.next();
-					if (!actionMap.containsKey(entry.key)) {
-						if (getLogger().isDebugEnabled()) {
-							getLogger().debug(
-								"Propagating default value "
-									+ entry.value
-									+ " to session attribute "
-									+ entry.key);
-						}
-						output.setAttribute(
-							outputConf,
-							objectModel,
-							entry.key,
-							entry.value);
-						actionMap.put(entry.key, entry.value);
-					}
-				}
-
-				output.commit(outputConf, objectModel);
-			}
-		} catch (Exception e) {
-			if (output != null) {
-				output.rollback(outputConf, objectModel, e);
-			}
-			throw e;
-		} finally {
-			if (outputSelector != null) {
-				if (output != null)
-					outputSelector.release(output);
-				this.manager.release(outputSelector);
-			}
-		}
-		return Collections.unmodifiableMap(actionMap);
-	}
+    public Map act(Redirector redirector,
+                   SourceResolver resolver,
+                   Map objectModel,
+                   String source,
+                   Parameters parameters)
+    throws Exception {
+        // Read action parameters
+        String outputName = parameters.getParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE,
+                                                    null);
+        boolean storeEmpty = parameters.getParameterAsBoolean(ACTION_PREFIX + CONFIG_STORE_EMPTY,
+                                                              this.storeEmpty);
+        parameters.removeParameter(ACTION_PREFIX + CONFIG_OUTPUT_MODULE);
+        parameters.removeParameter(ACTION_PREFIX + CONFIG_STORE_EMPTY);
+
+        Configuration outputConf = null;
+        if (outputName == null) {
+            outputName = this.outputName;
+            outputConf = this.outputConf;
+        }
+
+        // Action results map
+        final Map results = new HashMap();
+
+        OutputModule output = null;
+        ServiceSelector selector = null;
+        try {
+            selector = (ServiceSelector) this.manager.lookup(OutputModule.ROLE + "Selector");
+            if (outputName != null
+                && selector != null
+                && selector.isSelectable(outputName)) {
+
+                output = (OutputModule) selector.select(outputName);
+
+                String[] names = parameters.getNames();
+                for (int i = 0; i < names.length; i++) {
+                    String name = names[i];
+                    String value = parameters.getParameter(name);
+                    if (storeEmpty || (value != null && !value.equals(""))) {
+                        if (getLogger().isDebugEnabled()) {
+                            getLogger().debug("Propagating <" + name + "> value <"
+ value + ">");
+                        }
+                        output.setAttribute(outputConf,
+                                            objectModel,
+                                            name,
+                                            value);
+                        results.put(name, value);
+                    }
+                }
+
+                // Defaults, that are not overridden
+                for (Iterator i = defaults.iterator(); i.hasNext();) {
+                    Entry entry = (Entry) i.next();
+                    if (!results.containsKey(entry.key)) {
+                        if (getLogger().isDebugEnabled()) {
+                            getLogger().debug("Propagating default <" + entry.key + ">
value <" + entry.value + ">");
+                        }
+                        output.setAttribute(outputConf,
+                                            objectModel,
+                                            entry.key,
+                                            entry.value);
+                        results.put(entry.key, entry.value);
+                    }
+                }
+
+                output.commit(outputConf, objectModel);
+            }
+        } catch (Exception e) {
+            if (output != null) {
+                output.rollback(outputConf, objectModel, e);
+            }
+            throw e;
+        } finally {
+            if (selector != null) {
+                if (output != null) {
+                    selector.release(output);
+                }
+                this.manager.release(selector);
+            }
+        }
 
+        return Collections.unmodifiableMap(results);
+    }
 }

Mime
View raw message