KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > support > DefaultTransactionDefinition


1 /*
2  * Copyright 2002-2006 the original author or authors.
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.springframework.transaction.support;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.springframework.core.Constants;
22 import org.springframework.transaction.TransactionDefinition;
23
24 /**
25  * Default implementation of the TransactionDefinition interface,
26  * offering bean-style configuration and sensible default values
27  * (PROPAGATION_REQUIRED, ISOLATION_DEFAULT, TIMEOUT_DEFAULT, readOnly=false).
28  *
29  * <p>Base class for both TransactionTemplate and DefaultTransactionAttribute.
30  *
31  * @author Juergen Hoeller
32  * @since 08.05.2003
33  * @see org.springframework.transaction.support.TransactionTemplate
34  * @see org.springframework.transaction.interceptor.DefaultTransactionAttribute
35  */

36 public class DefaultTransactionDefinition implements TransactionDefinition, Serializable JavaDoc {
37
38     /** Prefix for the propagation constants defined in TransactionDefinition */
39     public static final String JavaDoc PREFIX_PROPAGATION = "PROPAGATION_";
40
41     /** Prefix for the isolation constants defined in TransactionDefinition */
42     public static final String JavaDoc PREFIX_ISOLATION = "ISOLATION_";
43
44     /** Prefix for transaction timeout values in description strings */
45     public static final String JavaDoc PREFIX_TIMEOUT = "timeout_";
46
47     /** Marker for read-only transactions in description strings */
48     public static final String JavaDoc READ_ONLY_MARKER = "readOnly";
49
50
51     /** Constants instance for TransactionDefinition */
52     private static final Constants constants = new Constants(TransactionDefinition.class);
53
54     private int propagationBehavior = PROPAGATION_REQUIRED;
55
56     private int isolationLevel = ISOLATION_DEFAULT;
57
58     private int timeout = TIMEOUT_DEFAULT;
59
60     private boolean readOnly = false;
61
62     private String JavaDoc name;
63
64
65     /**
66      * Create a new DefaultTransactionDefinition, with default settings.
67      * Can be modified through bean property setters.
68      * @see #setPropagationBehavior
69      * @see #setIsolationLevel
70      * @see #setTimeout
71      * @see #setReadOnly
72      * @see #setName
73      */

74     public DefaultTransactionDefinition() {
75     }
76
77     /**
78      * Copy constructor. Definition can be modified through bean property setters.
79      * @see #setPropagationBehavior
80      * @see #setIsolationLevel
81      * @see #setTimeout
82      * @see #setReadOnly
83      * @see #setName
84      */

85     public DefaultTransactionDefinition(TransactionDefinition other) {
86         this.propagationBehavior = other.getPropagationBehavior();
87         this.isolationLevel = other.getIsolationLevel();
88         this.timeout = other.getTimeout();
89         this.readOnly = other.isReadOnly();
90         this.name = other.getName();
91     }
92
93     /**
94      * Create a new DefaultTransactionDefinition with the the given
95      * propagation behavior. Can be modified through bean property setters.
96      * @param propagationBehavior one of the propagation constants in the
97      * TransactionDefinition interface
98      * @see #setIsolationLevel
99      * @see #setTimeout
100      * @see #setReadOnly
101      */

102     public DefaultTransactionDefinition(int propagationBehavior) {
103         this.propagationBehavior = propagationBehavior;
104     }
105
106
107     /**
108      * Set the propagation behavior by the name of the corresponding constant in
109      * TransactionDefinition, e.g. "PROPAGATION_REQUIRED".
110      * @param constantName name of the constant
111      * @exception IllegalArgumentException if the supplied value is not resolvable
112      * to one of the <code>PROPAGATION_</code> constants or is <code>null</code>
113      * @see #setPropagationBehavior
114      * @see #PROPAGATION_REQUIRED
115      */

116     public final void setPropagationBehaviorName(String JavaDoc constantName) throws IllegalArgumentException JavaDoc {
117         if (constantName == null || !constantName.startsWith(PREFIX_PROPAGATION)) {
118             throw new IllegalArgumentException JavaDoc("Only propagation constants allowed");
119         }
120         setPropagationBehavior(constants.asNumber(constantName).intValue());
121     }
122
123     /**
124      * Set the propagation behavior. Must be one of the propagation constants
125      * in the TransactionDefinition interface. Default is PROPAGATION_REQUIRED.
126      * @exception IllegalArgumentException if the supplied value is not
127      * one of the <code>PROPAGATION_</code> constants
128      * @see #PROPAGATION_REQUIRED
129      */

130     public final void setPropagationBehavior(int propagationBehavior) {
131         if (!constants.getValues(PREFIX_PROPAGATION).contains(new Integer JavaDoc(propagationBehavior))) {
132             throw new IllegalArgumentException JavaDoc("Only values of propagation constants allowed");
133         }
134         this.propagationBehavior = propagationBehavior;
135     }
136
137     public final int getPropagationBehavior() {
138         return this.propagationBehavior;
139     }
140
141     /**
142      * Set the isolation level by the name of the corresponding constant in
143      * TransactionDefinition, e.g. "ISOLATION_DEFAULT".
144      * @param constantName name of the constant
145      * @exception IllegalArgumentException if the supplied value is not resolvable
146      * to one of the <code>ISOLATION_</code> constants or is <code>null</code>
147      * @see #setIsolationLevel
148      * @see #ISOLATION_DEFAULT
149      */

150     public final void setIsolationLevelName(String JavaDoc constantName) throws IllegalArgumentException JavaDoc {
151         if (constantName == null || !constantName.startsWith(PREFIX_ISOLATION)) {
152             throw new IllegalArgumentException JavaDoc("Only isolation constants allowed");
153         }
154         setIsolationLevel(constants.asNumber(constantName).intValue());
155     }
156
157     /**
158      * Set the isolation level. Must be one of the isolation constants
159      * in the TransactionDefinition interface. Default is ISOLATION_DEFAULT.
160      * @exception IllegalArgumentException if the supplied value is not
161      * one of the <code>ISOLATION_</code> constants
162      * @see #ISOLATION_DEFAULT
163      */

164     public final void setIsolationLevel(int isolationLevel) {
165         if (!constants.getValues(PREFIX_ISOLATION).contains(new Integer JavaDoc(isolationLevel))) {
166             throw new IllegalArgumentException JavaDoc("Only values of isolation constants allowed");
167         }
168         this.isolationLevel = isolationLevel;
169     }
170
171     public final int getIsolationLevel() {
172         return this.isolationLevel;
173     }
174
175     /**
176      * Set the timeout to apply, as number of seconds.
177      * Default is TIMEOUT_DEFAULT (-1).
178      * @see #TIMEOUT_DEFAULT
179      */

180     public final void setTimeout(int timeout) {
181         if (timeout < TIMEOUT_DEFAULT) {
182             throw new IllegalArgumentException JavaDoc("Timeout must be a positive integer or TIMEOUT_DEFAULT");
183         }
184         this.timeout = timeout;
185     }
186
187     public final int getTimeout() {
188         return this.timeout;
189     }
190
191     /**
192      * Set whether to optimize as read-only transaction.
193      * Default is "false".
194      */

195     public final void setReadOnly(boolean readOnly) {
196         this.readOnly = readOnly;
197     }
198
199     public final boolean isReadOnly() {
200         return this.readOnly;
201     }
202
203     /**
204      * Set the name of this transaction. Default is none.
205      * <p>This will be used as transaction name to be shown in a
206      * transaction monitor, if applicable (for example, WebLogic's).
207      */

208     public final void setName(String JavaDoc name) {
209         this.name = name;
210     }
211
212     public final String JavaDoc getName() {
213         return this.name;
214     }
215
216
217     /**
218      * This implementation compares the <code>toString()</code> results.
219      * @see #toString()
220      */

221     public boolean equals(Object JavaDoc other) {
222         return (other instanceof TransactionDefinition && toString().equals(other.toString()));
223     }
224
225     /**
226      * This implementation returns <code>toString()</code>'s hash code.
227      * @see #toString()
228      */

229     public int hashCode() {
230         return toString().hashCode();
231     }
232
233     /**
234      * Return an identifying description for this transaction definition.
235      * <p>The format matches the one used by
236      * {@link org.springframework.transaction.interceptor.TransactionAttributeEditor},
237      * to be able to feed <code>toString</code> results into bean properties of type
238      * {@link org.springframework.transaction.interceptor.TransactionAttribute}.
239      * <p>Has to be overridden in subclasses for correct <code>equals</code>
240      * and <code>hashCode</code> behavior. Alternatively, {@link #equals}
241      * and {@link #hashCode} can be overridden themselves.
242      * @see #getDefinitionDescription()
243      * @see org.springframework.transaction.interceptor.TransactionAttributeEditor
244      */

245     public String JavaDoc toString() {
246         return getDefinitionDescription().toString();
247     }
248
249     /**
250      * Return an identifying description for this transaction definition.
251      * <p>Available to subclasses, for inclusion in their <code>toString()</code> result.
252      */

253     protected final StringBuffer JavaDoc getDefinitionDescription() {
254         StringBuffer JavaDoc desc = new StringBuffer JavaDoc();
255         desc.append(constants.toCode(new Integer JavaDoc(this.propagationBehavior), PREFIX_PROPAGATION));
256         desc.append(',');
257         desc.append(constants.toCode(new Integer JavaDoc(this.isolationLevel), PREFIX_ISOLATION));
258         if (this.timeout != TIMEOUT_DEFAULT) {
259             desc.append(',');
260             desc.append(PREFIX_TIMEOUT + this.timeout);
261         }
262         if (this.readOnly) {
263             desc.append(',');
264             desc.append(READ_ONLY_MARKER);
265         }
266         return desc;
267     }
268
269 }
270
Popular Tags