1 17 package org.alfresco.repo.search.transaction; 18 19 import java.io.UnsupportedEncodingException ; 20 21 import javax.transaction.HeuristicMixedException ; 22 import javax.transaction.HeuristicRollbackException ; 23 import javax.transaction.NotSupportedException ; 24 import javax.transaction.RollbackException ; 25 import javax.transaction.Synchronization ; 26 import javax.transaction.SystemException ; 27 import javax.transaction.xa.XAResource ; 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 CHAR_SET = "UTF-8"; 42 43 private byte[] globalTransactionId; 44 45 private byte[] branchQualifier; 46 47 private String guid; 49 50 private static ThreadLocal <SimpleTransaction> transaction = new ThreadLocal <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 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 , HeuristicMixedException , HeuristicRollbackException , 79 SecurityException , SystemException 80 { 81 try 82 { 83 if (isRollBackOnly) 84 { 85 throw new RollbackException ("Commit failed: Transaction marked for rollback"); 86 } 87 88 } 89 finally 90 { 91 transaction.set(null); 92 } 93 } 94 95 public boolean delistResource(XAResource arg0, int arg1) throws IllegalStateException , SystemException 96 { 97 throw new UnsupportedOperationException (); 99 } 100 101 public boolean enlistResource(XAResource arg0) throws RollbackException , IllegalStateException , SystemException 102 { 103 throw new UnsupportedOperationException (); 105 } 106 107 public int getStatus() throws SystemException 108 { 109 throw new UnsupportedOperationException (); 111 } 112 113 public void registerSynchronization(Synchronization arg0) throws RollbackException , IllegalStateException , 114 SystemException 115 { 116 throw new UnsupportedOperationException (); 118 } 119 120 public void rollback() throws IllegalStateException , SystemException 121 { 122 throw new UnsupportedOperationException (); 124 } 125 126 public void setRollbackOnly() throws IllegalStateException , SystemException 127 { 128 isRollBackOnly = true; 129 } 130 131 134 135 static SimpleTransaction suspend() 136 { 137 SimpleTransaction tx = getTransaction(); 138 transaction.set(null); 139 return tx; 140 } 141 142 static void begin() throws NotSupportedException 143 { 144 if (getTransaction() != null) 145 { 146 throw new NotSupportedException ("Nested transactions are not supported"); 147 } 148 transaction.set(new SimpleTransaction()); 149 } 150 151 static void resume(SimpleTransaction tx) 152 { 153 if (getTransaction() != null) 154 { 155 throw new IllegalStateException ("A transaction is already associated with the thread"); 156 } 157 transaction.set((SimpleTransaction) tx); 158 } 159 160 public String getGUID() 161 { 162 return guid; 163 } 164 165 public boolean equals(Object 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 toString() 185 { 186 StringBuffer buffer = new StringBuffer (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 |