cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r126292 - cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output
Date Mon, 24 Jan 2005 15:13:31 GMT
Author: vgritsenko
Date: Mon Jan 24 07:13:30 2005
New Revision: 126292

URL: http://svn.apache.org/viewcvs?view=rev&rev=126292
Log:
Make constants static.
Fix attribute name for prefixed rollback error message.
Code cleanup.

Modified:
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java
   cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java
Url: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java?view=diff&rev=126292&p1=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java&r1=126291&p2=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java&r2=126292
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java
Mon Jan 24 07:13:30 2005
@@ -1,40 +1,40 @@
 /*
  * 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.components.modules.output;
 
-import java.util.Map;
-
 import org.apache.avalon.framework.activity.Disposable;
 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.logger.AbstractLogEnabled;
+
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
 import org.apache.cocoon.util.HashMap;
-//import java.util.HashMap;
+
+import java.util.Map;
 
 /**
  * AbstractOutputModule gives you the infrastructure for easily
- * deploying more output modules.  In order to get at the
- * Logger, use getLogger().
+ * deploying more output modules.
+ *
+ * <p>In order to get at the logger, use <code>getLogger()</code>.</p>
  *
  * @author <a href="mailto:haul@apache.org">Christian Haul</a>
- * @version CVS $Id: AbstractOutputModule.java,v 1.4 2004/03/05 13:02:49 bdelacretaz Exp
$
+ * @version CVS $Id$
  */
 public abstract class AbstractOutputModule extends AbstractLogEnabled
     implements OutputModule, Configurable, Disposable {
@@ -43,21 +43,22 @@
      * Stores (global) configuration parameters as <code>key</code> /
      * <code>value</code> pairs.
      */
-    protected HashMap settings = null;
+    protected HashMap settings;
 
     /**
      * Configures the module.
      *
-     * Takes all elements nested in component declaration and stores
+     * <p>Takes all elements nested in component declaration and stores
      * them as key-value pairs in <code>settings</code>. Nested
      * configuration option are not catered for. This way global
-     * configuration options can be used.
+     * configuration options can be used.</p>
      *
-     * For nested configurations override this function.
-     * */
+     * <p>For nested configurations override this function.</p>
+     */
     public void configure(Configuration conf) throws ConfigurationException {
         Configuration[] parameters = conf.getChildren();
-        this.settings = new HashMap(parameters.length);
+        // Ideally here should be length * 1.333(3) but simple +1 will do for lengths up
to 3
+        this.settings = new HashMap(parameters.length + 1);
         for (int i = 0; i < parameters.length; i++) {
             String key = parameters[i].getName();
             String val = parameters[i].getValue("");
@@ -66,84 +67,77 @@
     }
 
     /**
-     *  dispose
+     * Dispose
      */
     public void dispose() {
-        // Purposely empty so that we don't need to implement it in every
-        // class.
+        // Implemeted so that we don't need to implement it in every subclass
+        this.settings = null;
     }
 
     /**
-     * Utility method to store parameters in a map as request attribute until 
+     * Utility method to store parameters in a map as request attribute until
      * either {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
      * is called.
      * @param objectModel - the objectModel
      * @param trans_place - request attribute name used for the transient data
      * @param name - name of the attribute to set
      * @param value - attribute value
-     */    
-    protected void transientSetAttribute( Map objectModel, String trans_place, String name,
Object value ) {
+     */
+    protected void transientSetAttribute(Map objectModel, String trans_place, String name,
Object value) {
+        final Request request = ObjectModelHelper.getRequest(objectModel);
 
-        Request request = ObjectModelHelper.getRequest(objectModel);
-        Object temp = request.getAttribute(trans_place);
-        Map aMap = null;
-
-        if (temp == null) {           
-            aMap = new java.util.HashMap();
-            // need java.util.HashMap here since JXPath does not like the extended version...
-        } else {
-            aMap = (Map) temp;
+        Map map = (Map) request.getAttribute(trans_place);
+        if (map == null) {
+            // Need java.util.HashMap here since JXPath does not like the extended version...
+            map = new java.util.HashMap();
         }
 
-        aMap.put(name,value);
-
-        request.setAttribute(trans_place, aMap);
+        map.put(name, value);
+        request.setAttribute(trans_place, map);
     }
 
     /**
      * Clears all uncommitted transient attributes.
+     *
      * @param objectModel - the objectModel
      * @param trans_place - request attribute name used for the transient data
-     */    
-    protected void rollback( Map objectModel, String trans_place) {
-        ObjectModelHelper.getRequest(objectModel).setAttribute(trans_place, null);
+     */
+    protected void rollback(Map objectModel, String trans_place) {
+        ObjectModelHelper.getRequest(objectModel).removeAttribute(trans_place);
     }
 
     /**
      * Returns a whether an transient attribute already exists.
-     * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to

+     * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to
      * {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
+     *
      * @param objectModel - the objectModel
      * @param trans_place - request attribute name used for the transient data
-     */    
-    protected boolean attributeExists( Map objectModel, String trans_place, String name )
-    {
-        Request request = ObjectModelHelper.getRequest(objectModel);
-        Object temp = request.getAttribute(trans_place);
-        if (temp == null) {
+     */
+    protected boolean attributeExists(Map objectModel, String trans_place, String name) {
+        final Request request = ObjectModelHelper.getRequest(objectModel);
+
+        Map map = (Map) request.getAttribute(trans_place);
+        if (map == null) {
             return false;
-        } else {
-            return ((Map) temp).containsKey(name);
         }
+
+        return map.containsKey(name);
     }
 
     /**
-     * Returns a map containing all transient attributes and remove them i.e. attributes
set with 
-     * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to

+     * Returns a map containing all transient attributes and remove them i.e. attributes
set with
+     * {@link #transientSetAttribute(Map, String, String, Object)} since the last call to
      * {@link #rollback(Map, String)} or {@link #prepareCommit(Map, String)}
+     *
      * @param objectModel - the objectModel
      * @param trans_place - request attribute name used for the transient data
-     */    
-    protected Map prepareCommit( Map objectModel, String trans_place )
-    {
-        Request request = ObjectModelHelper.getRequest(objectModel);
-        Object temp = request.getAttribute(trans_place);
-        request.setAttribute(trans_place, null);
-        if (temp == null) {
-            return null;
-        } else {
-            return (Map) temp;
-        }
-    }
+     */
+    protected Map prepareCommit(Map objectModel, String trans_place) {
+        final Request request = ObjectModelHelper.getRequest(objectModel);
 
+        Map data = (Map) request.getAttribute(trans_place);
+        request.removeAttribute(trans_place);
+        return data;
+    }
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java
Url: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java?view=diff&rev=126292&p1=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java&r1=126291&p2=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java&r2=126292
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/OutputModule.java
Mon Jan 24 07:13:30 2005
@@ -1,26 +1,25 @@
 /*
  * 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.components.modules.output;
 
-import java.util.Map;
-
 import org.apache.avalon.framework.component.Component;
 import org.apache.avalon.framework.configuration.Configuration;
 
+import java.util.Map;
+
 /**
  * Communicate results to other components. This could be done via
  * request attributes, session attribute etc. Implementors should obey
@@ -29,7 +28,7 @@
  * the transaction completes successfully.
  *
  * @author <a href="mailto:haul@apache.org">Christian Haul</a>
- * @version CVS $Id: OutputModule.java,v 1.3 2004/03/05 13:02:49 bdelacretaz Exp $
+ * @version CVS $Id$
  */
 public interface OutputModule extends Component {
 
@@ -68,6 +67,4 @@
      * successfully. See notes on {@link #rollback(Configuration, Map, Exception)}.
      * */
     void commit( Configuration modeConf, Map objectModel );
-
-
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java
Url: http://svn.apache.org/viewcvs/cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java?view=diff&rev=126292&p1=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java&r1=126291&p2=cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java&r2=126292
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java
Mon Jan 24 07:13:30 2005
@@ -1,118 +1,115 @@
 /*
  * 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.components.modules.output;
 
-import java.util.Iterator;
-import java.util.Map;
-
 import org.apache.avalon.framework.configuration.Configuration;
-import org.apache.avalon.framework.logger.Logger;
+
 import org.apache.cocoon.environment.ObjectModelHelper;
 import org.apache.cocoon.environment.Request;
 
+import java.util.Iterator;
+import java.util.Map;
+
 /**
  * Abstraction layer to encapsulate different output
  * destinations. Configuration option &lt;key-prefix&gt; defaults to
- * "org.apache.cocoon.components.modules.output.OutputModule"+":"
+ * <code>"org.apache.cocoon.components.modules.output.OutputModule" + ":"</code>
  *
- * Can be used with different isolation-level: default is "0" being
+ * <p>Can be used with different isolation-level: default is "0" being
  * no isolation at all, values are immediately visible but are removed
- * on a rollback; "1" keeps the values at a save place until either
+ * on a rollback; "1" keeps the values at a safe place until either
  * rollback or commit is called. Then values are either discarded or
- * copied to the final destination.
+ * copied to the final destination.</p>
  *
  * @author <a href="mailto:haul@apache.org">Christian Haul</a>
  * @version CVS $Id$
  */
 public class RequestAttributeOutputModule extends AbstractOutputModule implements OutputModule
{
-    
-    public final String PREFIX = "org.apache.cocoon.components.modules.output.OutputModule";
-    public final String TRANS_PREFIX = "org.apache.cocoon.components.modules.output.OutputModule.RequestAttributeOutputModule.transient";
-    public final String ROLLBACK_LIST = "org.apache.cocoon.components.modules.output.OutputModule.RequestAttributeOutputModule.rollback";
-    
+
+    public static final String PREFIX = OutputModule.ROLE;
+    public static final String TRANS_PREFIX = PREFIX + ".RequestAttributeOutputModule.transient";
+    public static final String ROLLBACK_LIST = PREFIX + ".RequestAttributeOutputModule.rollback";
+
     /**
      * communicate an attribute value to further processing logic.
      * @param modeConf column's mode configuration from resource
-     * description. This argument is optional.
+     *                 description. This argument is optional.
      * @param objectModel The objectModel
      * @param name The attribute's label, consisting of "table.column"
-     * or "table.column[index]" in case of multiple attributes of the
-     * same spec.
+     *             or "table.column[index]" in case of multiple attributes
+     *             of the same spec.
      * @param value The attriute's value.
-     * */
-    public void setAttribute( Configuration modeConf, Map objectModel, String name, Object
value ) {
-        if (this.settings.get("isolation-level","0").equals("1")) {
-            if (getLogger().isDebugEnabled())
-                getLogger().debug("setting transient ['"+name+"'] to ['"+value+"']");
-            this.transientSetAttribute(objectModel, TRANS_PREFIX, name, value);
+     */
+    public void setAttribute(Configuration modeConf, Map objectModel, String name, Object
value) {
+        if (this.settings.get("isolation-level", "0").equals("1")) {
+            // Read committed isolation level
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Setting transient ['" + name + "'] to ['" + value + "']");
+            }
+            transientSetAttribute(objectModel, TRANS_PREFIX, name, value);
         } else {
-            // use read uncommitted isolation level
-
-            Request request = ObjectModelHelper.getRequest(objectModel);
+            // Read uncommitted isolation level
+            final Request request = ObjectModelHelper.getRequest(objectModel);
 
             name = getName(name);
 
-            if (!this.attributeExists(objectModel, ROLLBACK_LIST, name)) { 
+            if (!attributeExists(objectModel, ROLLBACK_LIST, name)) {
                 Object tmp = request.getAttribute(name);
-                this.transientSetAttribute(objectModel, ROLLBACK_LIST, name, tmp);
+                transientSetAttribute(objectModel, ROLLBACK_LIST, name, tmp);
             }
 
-            if (getLogger().isDebugEnabled())
-                getLogger().debug("setting ['"+name+"'] to ['"+value+"']");
+            if (getLogger().isDebugEnabled()) {
+                getLogger().debug("Setting ['" + name + "'] to ['" + value + "']");
+            }
             request.setAttribute(name, value);
         }
     }
-    
-    
-    
+
     /**
      * If a database transaction needs to rollback, this is called to
      * inform the further processing logic about this fact. All
-     * already set attribute values are invalidated. <em>This is difficult
+     * already set attribute values are invalidated.
+     *
+     * <em>This is difficult
      * because only the request object can be used to synchronize this
-     * and build some kind of transaction object. Beaware that sending
+     * and build some kind of transaction object. Beware that sending
      * your data straight to some beans or other entities could result
      * in data corruption!</em>
-     * */
-    public void rollback( Configuration modeConf, Map objectModel, Exception e ) {
-        if (this.settings.get("isolation-level","0").equals("1")) {
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug("rolling back");
-            }
-            this.rollback(objectModel, TRANS_PREFIX);
+     */
+    public void rollback(Configuration modeConf, Map objectModel, Exception e) {
+        getLogger().debug("Rollback");
+        final Request request = ObjectModelHelper.getRequest(objectModel);
+
+        if (this.settings.get("isolation-level", "0").equals("1")) {
+            rollback(objectModel, TRANS_PREFIX);
         } else {
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug("start rolling back");
-            }
-            Request request = ObjectModelHelper.getRequest(objectModel);
-            Map rollbackList = this.prepareCommit(objectModel,ROLLBACK_LIST);
+            Map rollbackList = prepareCommit(objectModel, ROLLBACK_LIST);
             if (rollbackList != null) {
-                for (Iterator i = rollbackList.entrySet().iterator(); i.hasNext(); ){
-                    Map.Entry me = (Map.Entry)i.next();
-                    String key = (String)me.getKey();
+                for (Iterator i = rollbackList.entrySet().iterator(); i.hasNext();) {
+                    final Map.Entry me = (Map.Entry) i.next();
+                    String key = (String) me.getKey();
                     Object val = me.getValue();
                     if (val != null) {
                         if (getLogger().isDebugEnabled()) {
-                            getLogger().debug("rolling back ['" + key + "'] to ['" + val
+ "']");
+                            getLogger().debug("Rolling back ['" + key + "'] to ['" + val
+ "']");
                         }
                         request.setAttribute(key, val);
                     } else {
                         if (getLogger().isDebugEnabled()) {
-                            getLogger().debug("rolling back ['" + key + "']");
+                            getLogger().debug("Rolling back ['" + key + "']");
                         }
                         request.removeAttribute(key);
                     }
@@ -120,67 +117,51 @@
             }
         }
 
-        if (getLogger().isDebugEnabled())
-            getLogger().debug("done rolling back");
-
-        String prefix = (String) this.settings.get("key-prefix", PREFIX );
-        if (prefix!="") {
-            ObjectModelHelper.getRequest(objectModel).setAttribute(prefix+":",e.getMessage());
+        String prefix = (String) this.settings.get("key-prefix", PREFIX);
+        if (prefix.equals("")) {
+            request.setAttribute("errorMessage", e.getMessage());
         } else {
-            ObjectModelHelper.getRequest(objectModel).setAttribute("errorMessage",e.getMessage());
+            request.setAttribute(prefix + ':' + "errorMessage", e.getMessage());
         }
     }
-    
-    
+
     /**
      * Signal that the database transaction completed
      * successfully. See notes on @link{rollback}.
-     * */
-    public void commit( Configuration modeConf, Map objectModel ) {
-        if (this.settings.get("isolation-level","0").equals("1")) {
-
-            Logger logger = getLogger();
-            if (logger.isDebugEnabled()) {
-                logger.debug("prepare commit");
-            }
-
-            Map aMap = this.prepareCommit(objectModel, TRANS_PREFIX);
-            if (aMap == null || aMap.isEmpty()) {
+     */
+    public void commit(Configuration modeConf, Map objectModel) {
+        getLogger().debug("Commit");
+        if (this.settings.get("isolation-level", "0").equals("1")) {
+            Map data = prepareCommit(objectModel, TRANS_PREFIX);
+            if (data == null || data.isEmpty()) {
                 return;
             }
-            String prefix = (String)this.settings.get("key-prefix", PREFIX );
-            if (prefix.length() > 0) {
-                prefix = prefix + ":";
-            } else {
+
+            String prefix = (String) this.settings.get("key-prefix", PREFIX);
+            if (prefix.length() == 0) {
                 prefix = null;
             }
+
             Request request = ObjectModelHelper.getRequest(objectModel);
-            for (Iterator i = aMap.entrySet().iterator(); i.hasNext(); ) {
-                Map.Entry me = (Map.Entry)i.next();
-                String key = (String)me.getKey();
+            for (Iterator i = data.entrySet().iterator(); i.hasNext();) {
+                final Map.Entry me = (Map.Entry) i.next();
+                String key = (String) me.getKey();
                 Object value = me.getValue();
                 if (prefix != null) {
-                    key = prefix + key;
+                    key = prefix + ':' + key;
                 }
-                if (logger.isDebugEnabled()) {
-                    logger.debug("committing ['" + key + "'] to ['" + value + "']");
+                if (getLogger().isDebugEnabled()) {
+                    getLogger().debug("Committing ['" + key + "'] to ['" + value + "']");
                 }
                 request.setAttribute(key, value);
             }
-            if (logger.isDebugEnabled()) {
-                logger.debug("done commit");
-            }
         } else {
-            if (getLogger().isDebugEnabled()) {
-                getLogger().debug("commit");
-            }
-            this.prepareCommit(objectModel, ROLLBACK_LIST);
+            prepareCommit(objectModel, ROLLBACK_LIST);
         }
     }
 
-    protected String getName( String name ) {
-        String prefix = (String) this.settings.get("key-prefix", PREFIX );
-        return (prefix.equals("") ? name : prefix + ":" + name);
+    protected String getName(String name) {
+        String prefix = (String) this.settings.get("key-prefix", PREFIX);
+        return prefix.equals("") ? name : prefix + ':' + name;
     }
-    
 }

Mime
View raw message