KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > output > RequestAttributeOutputModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.modules.output;
17
18 import org.apache.avalon.framework.configuration.Configuration;
19
20 import org.apache.cocoon.environment.ObjectModelHelper;
21 import org.apache.cocoon.environment.Request;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 /**
27  * Abstraction layer to encapsulate different output
28  * destinations. Configuration option <key-prefix> defaults to
29  * <code>"org.apache.cocoon.components.modules.output.OutputModule" + ":"</code>
30  *
31  * <p>Can be used with different isolation-level: default is "0" being
32  * no isolation at all, values are immediately visible but are removed
33  * on a rollback; "1" keeps the values at a safe place until either
34  * rollback or commit is called. Then values are either discarded or
35  * copied to the final destination.</p>
36  *
37  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
38  * @version CVS $Id: RequestAttributeOutputModule.java 126292 2005-01-24 15:13:30Z vgritsenko $
39  */

40 public class RequestAttributeOutputModule extends AbstractOutputModule implements OutputModule {
41
42     public static final String JavaDoc PREFIX = OutputModule.ROLE;
43     public static final String JavaDoc TRANS_PREFIX = PREFIX + ".RequestAttributeOutputModule.transient";
44     public static final String JavaDoc ROLLBACK_LIST = PREFIX + ".RequestAttributeOutputModule.rollback";
45
46     /**
47      * communicate an attribute value to further processing logic.
48      * @param modeConf column's mode configuration from resource
49      * description. This argument is optional.
50      * @param objectModel The objectModel
51      * @param name The attribute's label, consisting of "table.column"
52      * or "table.column[index]" in case of multiple attributes
53      * of the same spec.
54      * @param value The attriute's value.
55      */

56     public void setAttribute(Configuration modeConf, Map JavaDoc objectModel, String JavaDoc name, Object JavaDoc value) {
57         if (this.settings.get("isolation-level", "0").equals("1")) {
58             // Read committed isolation level
59
if (getLogger().isDebugEnabled()) {
60                 getLogger().debug("Setting transient ['" + name + "'] to ['" + value + "']");
61             }
62             transientSetAttribute(objectModel, TRANS_PREFIX, name, value);
63         } else {
64             // Read uncommitted isolation level
65
final Request request = ObjectModelHelper.getRequest(objectModel);
66
67             name = getName(name);
68
69             if (!attributeExists(objectModel, ROLLBACK_LIST, name)) {
70                 Object JavaDoc tmp = request.getAttribute(name);
71                 transientSetAttribute(objectModel, ROLLBACK_LIST, name, tmp);
72             }
73
74             if (getLogger().isDebugEnabled()) {
75                 getLogger().debug("Setting ['" + name + "'] to ['" + value + "']");
76             }
77             request.setAttribute(name, value);
78         }
79     }
80
81     /**
82      * If a database transaction needs to rollback, this is called to
83      * inform the further processing logic about this fact. All
84      * already set attribute values are invalidated.
85      *
86      * <em>This is difficult
87      * because only the request object can be used to synchronize this
88      * and build some kind of transaction object. Beware that sending
89      * your data straight to some beans or other entities could result
90      * in data corruption!</em>
91      */

92     public void rollback(Configuration modeConf, Map JavaDoc objectModel, Exception JavaDoc e) {
93         getLogger().debug("Rollback");
94         final Request request = ObjectModelHelper.getRequest(objectModel);
95
96         if (this.settings.get("isolation-level", "0").equals("1")) {
97             rollback(objectModel, TRANS_PREFIX);
98         } else {
99             Map JavaDoc rollbackList = prepareCommit(objectModel, ROLLBACK_LIST);
100             if (rollbackList != null) {
101                 for (Iterator JavaDoc i = rollbackList.entrySet().iterator(); i.hasNext();) {
102                     final Map.Entry JavaDoc me = (Map.Entry JavaDoc) i.next();
103                     String JavaDoc key = (String JavaDoc) me.getKey();
104                     Object JavaDoc val = me.getValue();
105                     if (val != null) {
106                         if (getLogger().isDebugEnabled()) {
107                             getLogger().debug("Rolling back ['" + key + "'] to ['" + val + "']");
108                         }
109                         request.setAttribute(key, val);
110                     } else {
111                         if (getLogger().isDebugEnabled()) {
112                             getLogger().debug("Rolling back ['" + key + "']");
113                         }
114                         request.removeAttribute(key);
115                     }
116                 }
117             }
118         }
119
120         String JavaDoc prefix = (String JavaDoc) this.settings.get("key-prefix", PREFIX);
121         if (prefix.equals("")) {
122             request.setAttribute("errorMessage", e.getMessage());
123         } else {
124             request.setAttribute(prefix + ':' + "errorMessage", e.getMessage());
125         }
126     }
127
128     /**
129      * Signal that the database transaction completed
130      * successfully. See notes on @link{rollback}.
131      */

132     public void commit(Configuration modeConf, Map JavaDoc objectModel) {
133         getLogger().debug("Commit");
134         if (this.settings.get("isolation-level", "0").equals("1")) {
135             Map JavaDoc data = prepareCommit(objectModel, TRANS_PREFIX);
136             if (data == null || data.isEmpty()) {
137                 return;
138             }
139
140             String JavaDoc prefix = (String JavaDoc) this.settings.get("key-prefix", PREFIX);
141             if (prefix.length() == 0) {
142                 prefix = null;
143             }
144
145             Request request = ObjectModelHelper.getRequest(objectModel);
146             for (Iterator JavaDoc i = data.entrySet().iterator(); i.hasNext();) {
147                 final Map.Entry JavaDoc me = (Map.Entry JavaDoc) i.next();
148                 String JavaDoc key = (String JavaDoc) me.getKey();
149                 Object JavaDoc value = me.getValue();
150                 if (prefix != null) {
151                     key = prefix + ':' + key;
152                 }
153                 if (getLogger().isDebugEnabled()) {
154                     getLogger().debug("Committing ['" + key + "'] to ['" + value + "']");
155                 }
156                 request.setAttribute(key, value);
157             }
158         } else {
159             prepareCommit(objectModel, ROLLBACK_LIST);
160         }
161     }
162
163     protected String JavaDoc getName(String JavaDoc name) {
164         String JavaDoc prefix = (String JavaDoc) this.settings.get("key-prefix", PREFIX);
165         return prefix.equals("") ? name : prefix + ':' + name;
166     }
167 }
168
Popular Tags