1 package com.fc.taglibs.castor;
2
3 import java.io.*;
4 import java.util.ArrayList;
5 import javax.naming.*;
6 import javax.servlet.jsp.*;
7 import javax.servlet.jsp.tagext.*;
8
9
10
11 import javax.naming.InitialContext;
12 import org.exolab.castor.jdo.DataObjects;
13 import org.exolab.castor.jdo.Database;
14 import org.exolab.castor.jdo.PersistenceException;
15 import org.exolab.castor.jdo.DatabaseNotFoundException;
16 import org.exolab.castor.jdo.TransactionNotInProgressException;
17
18
19 public class CastorTransactionTag extends BodyTagSupport implements TryCatchFinally,CastorTransactionInitiator
20 {
21 /***
22 * The name of this tag
23 */
24 private static String _tagName = "CastorTransactionTag";
25
26 /***
27 * The name of the page scoped JDO DataObjects object
28 */
29 private String jdoName = null;
30
31 /***
32 * The castor objects we will need.
33 */
34 private Database db = null;
35
36
37 /***
38 * The castor objects we will need.
39 */
40 private DataObjects jdo = null;
41
42 /***
43 * Set the JDO name
44 */
45 public void setJdoName(String _jdoName)
46 {
47 this.jdoName = _jdoName;
48 }
49
50
51 /***
52 * For the CastorTransactionInitiator interface
53 * Must be able to return the database to any
54 * nested tags
55 */
56 public Database getDatabase()
57 {
58 return this.db;
59 }
60
61 /***
62 * Do the start tag bit
63 */
64 public int doStartTag() throws JspException
65 {
66
67 try
68 {
69 /***
70 * Check to see if this transaction is nested within
71 * either another transaction tag or any other tag
72 * cabable of spawning a transaction. If so, throw
73 * an exception. (Can't nest transaction tags)
74 */
75 CastorTransactionInitiator t = (CastorTransactionInitiator)findAncestorWithClass(this, CastorTransactionInitiator.class);
76 if (t!=null)
77 {
78 String msg = "Cannot nest a CastorTransactionTag within another CastorTransactionInitiator.";
79 log(msg);
80 throw new JspTagException(msg);
81 }
82 /*
83 * Get the JDO object
84 */
85 jdo = (DataObjects)pageContext.getAttribute(this.jdoName,PageContext.PAGE_SCOPE);
86 db = jdo.getDatabase();
87
88
89 /***
90 * Begin a transaction
91 */
92 db.begin();
93
94 return EVAL_BODY_INCLUDE;
95
96 }
97
98 catch(DatabaseNotFoundException e)
99 {
100 String msg = "DatabaseNotFoundException thrown in CastorTransactionTag.doStartTag(): " + e.getMessage();
101 log(msg);
102 throw new JspException(msg);
103 }
104 catch(PersistenceException e)
105 {
106 String msg = "PersistenceException thrown in CastorTransactionTag.doStartTag(): " + e.getMessage();
107 log(msg);
108 throw new JspException(msg);
109 }
110
111 }
112
113
114
115
116 public int doEndTag() throws JspException
117 {
118 /*
119 * End the transaction
120 */
121 try
122 {
123 db.commit();
124 }
125 catch(PersistenceException e)
126 {
127 log("Could not commit the transaction: " + e.getMessage());
128 throw new JspException(e.getMessage());
129 }
130
131 /*
132 * Close the connection to the database.
133 */
134 try
135 {
136 if(db!=null)
137 {
138 db.close();
139 }
140 }
141 catch(PersistenceException e)
142 {
143 log("Could not close a database connection: " + e.getMessage());
144 }
145
146 return EVAL_PAGE;
147 }
148
149 public void doCatch(java.lang.Throwable q) throws Throwable
150 {
151 /*
152 * If an exception is thrown during the execution of the
153 * body of the tag, roll back the transaction
154 */
155 String msg = null;
156 try
157 {
158 log("Rolling back transaction due to exception: " + q.toString());
159 db.rollback();
160 log("Transaction rolled back");
161 }
162 catch(TransactionNotInProgressException e)
163 {
164 log("Could not rollback transaction:" + e.getMessage());
165 }
166 throw q;
167 }
168
169 public void doFinally()
170 {
171 /*
172 * Close the connection to the database.
173 */
174 String msg = null;
175 try
176 {
177 if(db!=null)
178 {
179 msg = "Closing database connection...";
180 db.close();
181 msg = msg + "Successfully closed database";
182 log(msg);
183 }
184 }
185 catch(PersistenceException e)
186 {
187 msg = msg + "Could not close a database connection: " + e.getMessage();
188 log(msg);
189 }
190 }
191
192 private void log(String msg)
193 {
194 System.out.println(_tagName + ": " + msg);
195 }
196
197
198 }
This page was automatically generated by Maven