Author: vgritsenko Date: Mon Jan 24 07:20:00 2005 New Revision: 126293 URL: http://svn.apache.org/viewcvs?view=rev&rev=126293 Log: Make constants static. Fix attribute name for prefixed rollback error message. Code cleanup.
Modified: cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java Modified: cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java?view=diff&rev=126293&p1=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java&r1=126292&p2=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java&r2=126293 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/AbstractOutputModule.java Mon Jan 24 07:20:00 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:[EMAIL PROTECTED]">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 [EMAIL PROTECTED] #rollback(Map, String)} or [EMAIL PROTECTED] #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. - * [EMAIL PROTECTED] #transientSetAttribute(Map, String, String, Object)} since the last call to + * [EMAIL PROTECTED] #transientSetAttribute(Map, String, String, Object)} since the last call to * [EMAIL PROTECTED] #rollback(Map, String)} or [EMAIL PROTECTED] #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 - * [EMAIL PROTECTED] #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 + * [EMAIL PROTECTED] #transientSetAttribute(Map, String, String, Object)} since the last call to * [EMAIL PROTECTED] #rollback(Map, String)} or [EMAIL PROTECTED] #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/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java?view=diff&rev=126293&p1=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java&r1=126292&p2=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java&r2=126293 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/OutputModule.java Mon Jan 24 07:20:00 2005 @@ -1,25 +1,24 @@ /* * 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.configuration.Configuration; +import java.util.Map; + /** * Communicate results to other components. This could be done via * request attributes, session attribute etc. Implementors should obey @@ -67,6 +66,4 @@ * successfully. See notes on [EMAIL PROTECTED] #rollback(Configuration, Map, Exception)}. * */ void commit( Configuration modeConf, Map objectModel ); - - } Modified: cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java?view=diff&rev=126293&p1=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java&r1=126292&p2=cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java&r2=126293 ============================================================================== --- cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java (original) +++ cocoon/trunk/src/java/org/apache/cocoon/components/modules/output/RequestAttributeOutputModule.java Mon Jan 24 07:20:00 2005 @@ -1,188 +1,169 @@ /* * 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 <key-prefix> 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:[EMAIL PROTECTED]">Christian Haul</a> - * @version CVS $Id: RequestAttributeOutputModule.java,v 1.3 2004/03/05 13:02:49 bdelacretaz Exp $ + * @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); - } else { + */ + public void rollback(Configuration modeConf, Map objectModel, Exception e) { + getLogger().debug("Rollback"); + final Request request = ObjectModelHelper.getRequest(objectModel); - if (getLogger().isDebugEnabled()) - getLogger().debug("start rolling back"); - - Request request = ObjectModelHelper.getRequest(objectModel); - Object tmp = this.prepareCommit(objectModel,ROLLBACK_LIST); - if (tmp != null) { - Map rollbackList = (Map) tmp; - Iterator iter = rollbackList.keySet().iterator(); - while(iter.hasNext()) { - String key = (String) iter.next(); - Object val = rollbackList.get(key); + if (this.settings.get("isolation-level", "0").equals("1")) { + rollback(objectModel, TRANS_PREFIX); + } else { + Map rollbackList = prepareCommit(objectModel, ROLLBACK_LIST); + if (rollbackList != null) { + 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+"']"); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Rolling back ['" + key + "'] to ['" + val + "']"); + } request.setAttribute(key, val); } else { - if (getLogger().isDebugEnabled()) - getLogger().debug("rolling back ['"+key+"']"); + if (getLogger().isDebugEnabled()) { + getLogger().debug("Rolling back ['" + key + "']"); + } request.removeAttribute(key); } } } } - 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) { - return; - } - - Iterator iter = aMap.keySet().iterator(); - if (!iter.hasNext()){ + */ + 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 != "") { - prefix = prefix+":"; - } else { + + String prefix = (String) this.settings.get("key-prefix", PREFIX); + if (prefix.length() == 0) { prefix = null; } + Request request = ObjectModelHelper.getRequest(objectModel); - while (iter.hasNext()) { - String key = (String) iter.next(); - Object value = aMap.get(key); - if (prefix != null) { key = prefix + key; } - if (logger.isDebugEnabled()) - logger.debug("committing ['"+key+"'] to ['"+value+"']"); + 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; + } + 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 == "" ? name : prefix+":"+name); + protected String getName(String name) { + String prefix = (String) this.settings.get("key-prefix", PREFIX); + return prefix.equals("") ? name : prefix + ':' + name; } - }