001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.components.modules.output;
019:
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.cocoon.environment.ObjectModelHelper;
024: import org.apache.cocoon.environment.Request;
025:
026: /**
027: * Abstraction layer to encapsulate different output
028: * destinations. This module outputs to a request attribute
029: * java.util.Map object that contains all the attributes that were
030: * set. Configuration option <key-prefix> defaults to
031: * "org.apache.cocoon.components.modules.output.OutputModule"
032: *
033: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
034: * @version CVS $Id: RequestAttributeMap.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class RequestAttributeMap extends AbstractOutputModule {
037:
038: public final String PREFIX = "org.apache.cocoon.components.modules.output.OutputModule";
039: public final String TRANS_PREFIX = "org.apache.cocoon.components.modules.output.OutputModule.RequestAttributeMap.transient";
040:
041: /**
042: * communicate an attribute value to further processing logic.
043: * @param modeConf column's mode configuration from resource
044: * description. This argument is optional.
045: * @param objectModel The objectModel
046: * @param name The attribute's label, consisting of "table.column"
047: * or "table.column[index]" in case of multiple attributes of the
048: * same spec.
049: * @param value The attriute's value.
050: * */
051: public void setAttribute(Configuration modeConf, Map objectModel,
052: String name, Object value) {
053: if (getLogger().isDebugEnabled())
054: getLogger().debug(
055: "setting transient ['" + name + "'] to ['" + value
056: + "']");
057: super .transientSetAttribute(objectModel, TRANS_PREFIX, name,
058: value);
059: }
060:
061: /**
062: * If a database transaction needs to rollback, this is called to
063: * inform the further processing logic about this fact. All
064: * already set attribute values are invalidated. <em>This is difficult
065: * because only the request object can be used to synchronize this
066: * and build some kind of transaction object. Beaware that sending
067: * your data straight to some beans or other entities could result
068: * in data corruption!</em>
069: * */
070: public void rollback(Configuration modeConf, Map objectModel,
071: Exception e) {
072: if (getLogger().isDebugEnabled())
073: getLogger().debug("rolling back");
074: super .rollback(objectModel, TRANS_PREFIX);
075: }
076:
077: /**
078: * Signal that the database transaction completed
079: * successfully. See notes on @link{rollback}.
080: * */
081: public void commit(Configuration modeConf, Map objectModel) {
082: if (getLogger().isDebugEnabled())
083: getLogger().debug("prepare commit");
084: Map aMap = super .prepareCommit(objectModel, TRANS_PREFIX);
085: if (aMap == null) {
086: // nothing to do
087: return;
088: }
089:
090: String prefix = (String) this .settings
091: .get("key-prefix", PREFIX);
092: Request request = ObjectModelHelper.getRequest(objectModel);
093: Object temp = request.getAttribute(prefix);
094: Map old = null;
095: if (temp == null) {
096: old = aMap;
097: } else {
098: old = (Map) temp;
099: old.putAll(aMap);
100: }
101: request.setAttribute(prefix, old);
102: if (getLogger().isDebugEnabled())
103: getLogger().debug("done commit to ['" + prefix + "']");
104: }
105:
106: }
|