KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

57     public void setAttribute( Configuration modeConf, Map JavaDoc objectModel, String JavaDoc name, Object JavaDoc value ) {
58         if (this.settings.get("isolation-level","0").equals("1")) {
59             if (getLogger().isDebugEnabled())
60                 getLogger().debug("setting transient ['"+name+"'] to ['"+value+"']");
61             this.transientSetAttribute(objectModel, TRANS_PREFIX, name, value);
62         } else {
63             // use read uncommitted isolation level
64

65             Session session = ObjectModelHelper.getRequest(objectModel).getSession();
66
67             name = getName(name);
68
69             if (!this.attributeExists(objectModel, ROLLBACK_LIST, name)) {
70                 Object JavaDoc tmp = session.getAttribute(name);
71                 this.transientSetAttribute(objectModel, ROLLBACK_LIST, name, tmp);
72             }
73
74             if (getLogger().isDebugEnabled())
75                 getLogger().debug("setting ['"+name+"'] to ['"+value+"']");
76             session.setAttribute(name, value);
77         }
78
79     }
80     
81     
82     
83     /**
84      * If a database transaction needs to rollback, this is called to
85      * inform the further processing logic about this fact. All
86      * already set attribute values are invalidated. <em>This is difficult
87      * because only the request object can be used to synchronize this
88      * and build some kind of transaction object. Beaware 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         if (this.settings.get("isolation-level","0").equals("1")) {
94             if (getLogger().isDebugEnabled()) {
95                 getLogger().debug("rolling back");
96             }
97             this.rollback(objectModel, TRANS_PREFIX);
98         } else {
99             if (getLogger().isDebugEnabled()) {
100                 getLogger().debug("start rolling back");
101             }
102             Session session = ObjectModelHelper.getRequest(objectModel).getSession();
103             Map JavaDoc rollbackList = this.prepareCommit(objectModel,ROLLBACK_LIST);
104             if (rollbackList != null) {
105                 for (Iterator JavaDoc i = rollbackList.entrySet().iterator(); i.hasNext(); ) {
106                     Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
107                     String JavaDoc key = (String JavaDoc)me.getKey();
108                     Object JavaDoc val = me.getValue();
109                     if (val != null) {
110                         if (getLogger().isDebugEnabled()) {
111                             getLogger().debug("rolling back ['" + key + "'] to ['" + val + "']");
112                         }
113                         session.setAttribute(key, val);
114                     } else {
115                         if (getLogger().isDebugEnabled()) {
116                             getLogger().debug("rolling back ['" + key + "']");
117                         }
118                         session.removeAttribute(key);
119                     }
120                 }
121             }
122         }
123         if (getLogger().isDebugEnabled()) {
124             getLogger().debug("done rolling back");
125         }
126         String JavaDoc prefix = (String JavaDoc) this.settings.get("key-prefix", PREFIX );
127         if (prefix!="") {
128             ObjectModelHelper.getRequest(objectModel).getSession().setAttribute(prefix+":",e.getMessage());
129         } else {
130             ObjectModelHelper.getRequest(objectModel).getSession().setAttribute("errorMessage",e.getMessage());
131         }
132     }
133     
134     
135     /**
136      * Signal that the database transaction completed
137      * successfully. See notes on @link{rollback}.
138      * */

139     public void commit( Configuration modeConf, Map JavaDoc objectModel ) {
140         if (this.settings.get("isolation-level","0").equals("1")) {
141
142             Logger logger = getLogger();
143             if (logger.isDebugEnabled()) {
144                 logger.debug("prepare commit");
145             }
146             Map JavaDoc aMap = this.prepareCommit(objectModel, TRANS_PREFIX);
147             if (aMap == null || aMap.isEmpty()) {
148                 return;
149             }
150             String JavaDoc prefix = (String JavaDoc)this.settings.get("key-prefix", PREFIX );
151             if (prefix.length() > 0) {
152                 prefix = prefix + ":";
153             } else {
154                 prefix = null;
155             }
156             Session session = ObjectModelHelper.getRequest(objectModel).getSession();
157             for (Iterator JavaDoc i = aMap.entrySet().iterator(); i.hasNext(); ) {
158                 Map.Entry JavaDoc me = (Map.Entry JavaDoc)i.next();
159                 String JavaDoc key = (String JavaDoc)me.getKey();
160                 Object JavaDoc value = me.getValue();
161                 if (prefix != null) {
162                     key = prefix + key;
163                 }
164                 if (logger.isDebugEnabled()) {
165                     logger.debug("committing ['" + key + "'] to ['" + value + "']");
166                 }
167                 session.setAttribute(key, value);
168             }
169             if (logger.isDebugEnabled()) {
170                 logger.debug("done commit");
171             }
172         } else {
173             if (getLogger().isDebugEnabled())
174                 getLogger().debug("commit");
175             this.prepareCommit(objectModel, ROLLBACK_LIST);
176         }
177     }
178     
179     protected String JavaDoc getName( String JavaDoc name ) {
180         String JavaDoc prefix = (String JavaDoc) this.settings.get("key-prefix", PREFIX );
181         return (prefix.equals("") ? name : prefix+":"+name);
182     }
183
184 }
185
Popular Tags