01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.framework.util;
16:
17: import java.io.Serializable;
18: import java.util.Random;
19: import org.araneaframework.framework.TransactionContext;
20:
21: /**
22: * Helper class for determining if transaction id is consistent. Transaction
23: * id is considered consistent when it equals {@link TransactionContext#OVERRIDE_KEY}
24: * or current transaction id. If current transaction id is not yet set,
25: * any transaction id is considered consistent.
26: *
27: * @author "Toomas Römer" <toomas@webmedia.ee>
28: */
29: public class TransactionHelper implements Serializable {
30: //*******************************************************************
31: // FIELDS
32: //*******************************************************************
33: private Long currentTransactionId;
34: private Long nextTransactionId;
35: private Random random = new Random(System.currentTimeMillis());
36:
37: {
38: resetTransactionId();
39: }
40:
41: //*******************************************************************
42: // PUBLIC METHODS
43: //*******************************************************************
44: /**
45: * Generates a new current transaction id.
46: */
47: public void resetTransactionId() {
48: currentTransactionId = nextTransactionId;
49: nextTransactionId = new Long(random.nextLong());
50: }
51:
52: /**
53: * Returns the current transaction id.
54: */
55: public Object getCurrentTransactionId() {
56: return currentTransactionId;
57: }
58:
59: public Long getNextTransactionId() {
60: return nextTransactionId;
61: }
62:
63: /**
64: * Returns true if current transaction id is null or transactionId
65: * equals the current transaction id or transactionId has been
66: * overriden.
67: */
68: public boolean isConsistent(Object transactionId) {
69: if (currentTransactionId == null)
70: return true;
71:
72: if (isOverride(transactionId))
73: return true;
74:
75: return currentTransactionId.toString().equals(transactionId);
76: }
77:
78: /**
79: * Returns true if current transaction id is null or transactionId does not
80: * equal the current transaction id.
81: */
82: public boolean isOverride(Object transactionId) {
83: return TransactionContext.OVERRIDE_KEY.equals(transactionId);
84: }
85: }
|