KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > transaction > SimpleTransaction


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.transaction;
18
19 import java.io.UnsupportedEncodingException JavaDoc;
20
21 import javax.transaction.HeuristicMixedException JavaDoc;
22 import javax.transaction.HeuristicRollbackException JavaDoc;
23 import javax.transaction.NotSupportedException JavaDoc;
24 import javax.transaction.RollbackException JavaDoc;
25 import javax.transaction.Synchronization JavaDoc;
26 import javax.transaction.SystemException JavaDoc;
27 import javax.transaction.xa.XAResource JavaDoc;
28
29 import org.alfresco.util.GUID;
30
31 public class SimpleTransaction implements XidTransaction
32 {
33     private static final int DEFAULT_TIMEOUT = 600;
34
35     private boolean isRollBackOnly;
36
37     private int timeout;
38
39     public static final int FORMAT_ID = 12321;
40
41     private static final String JavaDoc CHAR_SET = "UTF-8";
42
43     private byte[] globalTransactionId;
44
45     private byte[] branchQualifier;
46
47     // This is the transactoin id
48
private String JavaDoc guid;
49
50     private static ThreadLocal JavaDoc<SimpleTransaction> transaction = new ThreadLocal JavaDoc<SimpleTransaction>();
51
52     private SimpleTransaction(int timeout)
53     {
54         super();
55         this.timeout = timeout;
56         guid = GUID.generate();
57         try
58         {
59             globalTransactionId = guid.getBytes(CHAR_SET);
60         }
61         catch (UnsupportedEncodingException JavaDoc e)
62         {
63             throw new XidException(e);
64         }
65         branchQualifier = new byte[0];
66     }
67
68     private SimpleTransaction()
69     {
70         this(DEFAULT_TIMEOUT);
71     }
72
73     public static SimpleTransaction getTransaction()
74     {
75         return (SimpleTransaction) transaction.get();
76     }
77
78     public void commit() throws RollbackException JavaDoc, HeuristicMixedException JavaDoc, HeuristicRollbackException JavaDoc,
79             SecurityException JavaDoc, SystemException JavaDoc
80     {
81         try
82         {
83             if (isRollBackOnly)
84             {
85                 throw new RollbackException JavaDoc("Commit failed: Transaction marked for rollback");
86             }
87
88         }
89         finally
90         {
91             transaction.set(null);
92         }
93     }
94
95     public boolean delistResource(XAResource JavaDoc arg0, int arg1) throws IllegalStateException JavaDoc, SystemException JavaDoc
96     {
97         // TODO Auto-generated method stub
98
throw new UnsupportedOperationException JavaDoc();
99     }
100
101     public boolean enlistResource(XAResource JavaDoc arg0) throws RollbackException JavaDoc, IllegalStateException JavaDoc, SystemException JavaDoc
102     {
103         // TODO Auto-generated method stub
104
throw new UnsupportedOperationException JavaDoc();
105     }
106
107     public int getStatus() throws SystemException JavaDoc
108     {
109         // TODO Auto-generated method stub
110
throw new UnsupportedOperationException JavaDoc();
111     }
112
113     public void registerSynchronization(Synchronization JavaDoc arg0) throws RollbackException JavaDoc, IllegalStateException JavaDoc,
114             SystemException JavaDoc
115     {
116         // TODO Auto-generated method stub
117
throw new UnsupportedOperationException JavaDoc();
118     }
119
120     public void rollback() throws IllegalStateException JavaDoc, SystemException JavaDoc
121     {
122         // TODO Auto-generated method stub
123
throw new UnsupportedOperationException JavaDoc();
124     }
125
126     public void setRollbackOnly() throws IllegalStateException JavaDoc, SystemException JavaDoc
127     {
128         isRollBackOnly = true;
129     }
130
131     /*
132      * Support for suspend, resume and begin.
133      */

134
135     /* package */static SimpleTransaction suspend()
136     {
137         SimpleTransaction tx = getTransaction();
138         transaction.set(null);
139         return tx;
140     }
141
142     /* package */static void begin() throws NotSupportedException JavaDoc
143     {
144         if (getTransaction() != null)
145         {
146             throw new NotSupportedException JavaDoc("Nested transactions are not supported");
147         }
148         transaction.set(new SimpleTransaction());
149     }
150
151     /* package */static void resume(SimpleTransaction tx)
152     {
153         if (getTransaction() != null)
154         {
155             throw new IllegalStateException JavaDoc("A transaction is already associated with the thread");
156         }
157         transaction.set((SimpleTransaction) tx);
158     }
159
160     public String JavaDoc getGUID()
161     {
162         return guid;
163     }
164
165     public boolean equals(Object JavaDoc o)
166     {
167         if (this == o)
168         {
169             return true;
170         }
171         if (!(o instanceof SimpleTransaction))
172         {
173             return false;
174         }
175         SimpleTransaction other = (SimpleTransaction) o;
176         return this.getGUID().equals(other.getGUID());
177     }
178
179     public int hashCode()
180     {
181         return getGUID().hashCode();
182     }
183
184     public String JavaDoc toString()
185     {
186         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(128);
187         buffer.append("Simple Transaction GUID = " + getGUID());
188         return buffer.toString();
189     }
190
191     public int getFormatId()
192     {
193         return FORMAT_ID;
194     }
195
196     public byte[] getGlobalTransactionId()
197     {
198         return globalTransactionId;
199     }
200
201     public byte[] getBranchQualifier()
202     {
203         return branchQualifier;
204     }
205 }
206
Popular Tags