KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > edit > TransactionTag


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.bridge.jsp.taglib.edit;
11
12 import javax.servlet.jsp.JspTagException JavaDoc;
13
14 import java.io.IOException JavaDoc;
15
16 import org.mmbase.bridge.Cloud;
17 import org.mmbase.bridge.Transaction;
18
19 import org.mmbase.bridge.jsp.taglib.CloudReferrerTag;
20 import org.mmbase.bridge.jsp.taglib.CloudProvider;
21 import org.mmbase.bridge.jsp.taglib.util.Attribute;
22
23 import org.mmbase.util.logging.Logger;
24 import org.mmbase.util.logging.Logging;
25
26 /**
27  *
28  * Creates a new Transaction.
29  *
30  * @author Michiel Meeuwissen
31  * @version $Id: TransactionTag.java,v 1.23 2006/08/30 18:00:15 michiel Exp $
32  */

33
34 public class TransactionTag extends CloudReferrerTag implements CloudProvider {
35
36     private static final Logger log = Logging.getLoggerInstance(TransactionTag.class);
37     protected Transaction transaction;
38     protected Attribute commit = Attribute.NULL;
39     protected Attribute name = Attribute.NULL;
40     protected String JavaDoc jspvar = null;
41
42     public void setCommitonclose(String JavaDoc c) throws JspTagException JavaDoc {
43         commit = getAttribute(c);
44     }
45
46     /**
47      * @since 1.7.1 this method shadows/implements the methods below.
48      *
49      * @see org.mmbase.bridge.jsp.taglib.CloudReferrerTag#getCloudVar()
50      * @see org.mmbase.bridge.jsp.taglib.CloudProvider#getCloudVar()
51      */

52     public Cloud getCloudVar() throws JspTagException JavaDoc {
53         return transaction;
54     }
55
56     public void setName(String JavaDoc s) throws JspTagException JavaDoc {
57         name = getAttribute(s);
58     }
59
60     public void setJspvar(String JavaDoc jv) {
61         jspvar = jv;
62     }
63
64     protected String JavaDoc getName() throws JspTagException JavaDoc {
65         return (String JavaDoc) name.getValue(this);
66     }
67
68     /**
69      * Creates the transaction.
70      */

71     public int doStartTag() throws JspTagException JavaDoc{
72         if (log.isDebugEnabled()) {
73             log.debug("value of commit: " + commit);
74         }
75         transaction = null;
76         if (getId() != null) { // look it up from session
77
log.debug("looking up transaction in context");
78             try {
79                 Object JavaDoc o = getObject(getId());
80                 if (o instanceof Transaction) {
81                     transaction = (Transaction) getObject(getId());
82                 } else {
83                     throw new JspTagException JavaDoc("The object with id " + getId() + " is not a transaction, but " + (o == null ? "NULL" : "a " + o.getClass()));
84                 }
85                 if (log.isDebugEnabled()) {
86                     log.debug("found " + transaction);
87                 }
88             } catch (JspTagException JavaDoc e) { }
89         }
90         if (transaction == null) { // not found in context
91
String JavaDoc n = getName();
92             if (name == null) {
93                 throw new JspTagException JavaDoc("Did not find transaction in context, and no name for transaction supplied");
94             }
95             transaction = findCloudProvider().getCloudVar().getTransaction(n);
96             if (getId() != null) { // put it in context
97
log.debug("putting transaction in context");
98                 getContextProvider().getContextContainer().register(getId(), transaction);
99             }
100         }
101         if (jspvar != null) {
102             pageContext.setAttribute(jspvar, transaction);
103         }
104         return EVAL_BODY;
105     }
106
107     protected boolean getDefaultCommit() {
108         return true;
109     }
110
111     public int doEndTag() throws JspTagException JavaDoc {
112         if (commit.getBoolean(this, getDefaultCommit())) {
113             transaction.commit();
114             if (getId() != null) {
115                 getContextProvider().getContextContainer().unRegister(getId());
116             }
117         }
118         transaction = null;
119         return super.doEndTag();
120     }
121     public int doAfterBody() throws JspTagException JavaDoc {
122         if (bodyContent != null) {
123             try {
124                 bodyContent.writeOut(bodyContent.getEnclosingWriter());
125             } catch (IOException JavaDoc ioe){
126                 throw new JspTagException JavaDoc(ioe.toString());
127             }
128         }
129         return SKIP_BODY;
130     }
131
132 }
133
134
Popular Tags