KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tm > TransactionLocalDelegateImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.tm;
23
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.transaction.RollbackException JavaDoc;
29 import javax.transaction.Synchronization JavaDoc;
30 import javax.transaction.SystemException JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33
34 import org.jboss.util.NestedRuntimeException;
35
36 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
37 import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
38
39 /**
40  * An implementation of the transaction local implementation
41  * using Transaction synchronizations.
42  *
43  * There is one of these per transaction local
44  *
45  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
46  * @version $Revision: 37459 $
47  */

48 public class TransactionLocalDelegateImpl
49    implements TransactionLocalDelegate
50 {
51    // Attributes ----------------------------------------------------
52

53    /** The transaction manager */
54    protected TransactionManager JavaDoc manager;
55
56    // Static --------------------------------------------------------
57

58    /** The synchronizations for each transaction */
59    protected static ConcurrentHashMap synchronizationsByTransaction = new ConcurrentHashMap();
60
61    /**
62     * Retrieve a synchronization for the transaction
63     *
64     * @param tx the transaction
65     * @param create whether to create a synchronization if one doesn't exist
66     */

67    protected static TransactionLocalSynchronization getSynchronization(Transaction JavaDoc tx, boolean create)
68    {
69       synchronized (tx)
70       {
71          TransactionLocalSynchronization result = (TransactionLocalSynchronization) synchronizationsByTransaction.get(tx);
72          if (result == null && create == true)
73          {
74             result = new TransactionLocalSynchronization(tx);
75             try
76             {
77                tx.registerSynchronization(result);
78             }
79             catch (RollbackException JavaDoc e)
80             {
81                throw new IllegalStateException JavaDoc("Transaction already rolled back or marked for rollback");
82             }
83             catch (SystemException JavaDoc e)
84             {
85                throw new NestedRuntimeException(e);
86             }
87             synchronizationsByTransaction.put(tx, result);
88          }
89          return result;
90       }
91    }
92
93    /**
94     * Remove a synchronization
95     *
96     * @param tx the transaction to remove
97     */

98    protected static void removeSynchronization(Transaction JavaDoc tx)
99    {
100       synchronizationsByTransaction.remove(tx);
101    }
102
103    // Constructor ---------------------------------------------------
104

105    /**
106     * Construct a new delegate for the given transaction manager
107     *
108     * @param manager the transaction manager
109     */

110    public TransactionLocalDelegateImpl(TransactionManager JavaDoc manager)
111    {
112       this.manager = manager;
113    }
114
115    public void lock(TransactionLocal local, Transaction JavaDoc tx) throws InterruptedException JavaDoc
116    {
117       TransactionLocalSynchronization sync = getSynchronization(tx, true);
118       sync.lock(local);
119    }
120
121    public void unlock(TransactionLocal local, Transaction JavaDoc tx)
122    {
123       TransactionLocalSynchronization sync = getSynchronization(tx, false);
124       if (sync != null)
125         sync.unlock(local);
126       else
127          throw new IllegalStateException JavaDoc("No synchronization found tx=" + tx + " local=" + local);
128    }
129
130    public Object JavaDoc getValue(TransactionLocal local, Transaction JavaDoc tx)
131    {
132       TransactionLocalSynchronization sync = getSynchronization(tx, false);
133       if (sync == null)
134          return null;
135       return sync.getValue(local);
136    }
137
138    public void storeValue(TransactionLocal local, Transaction JavaDoc tx, Object JavaDoc value)
139    {
140       TransactionLocalSynchronization sync = getSynchronization(tx, true);
141       sync.setValue(local, value);
142    }
143
144    public boolean containsValue(TransactionLocal local, Transaction JavaDoc tx)
145    {
146       TransactionLocalSynchronization sync = getSynchronization(tx, false);
147       if (sync == null)
148          return false;
149       return sync.containsValue(local);
150    }
151
152    // InnerClasses ---------------------------------------------------
153

154    protected static class TransactionLocalSynchronization
155       implements Synchronization JavaDoc
156    {
157       protected Transaction JavaDoc tx;
158
159       private Map JavaDoc valuesByLocal = Collections.synchronizedMap(new HashMap JavaDoc());
160
161       protected ReentrantLock reentrantLock = new ReentrantLock();
162
163       public TransactionLocalSynchronization(Transaction JavaDoc tx)
164       {
165          this.tx = tx;
166       }
167
168       public void beforeCompletion()
169       {
170       }
171
172       public void afterCompletion(int status)
173       {
174          removeSynchronization(tx);
175          valuesByLocal.clear(); // Help the GC
176
}
177
178       public void lock(Object JavaDoc local) throws InterruptedException JavaDoc
179       {
180          boolean locked = reentrantLock.attempt(60000);
181          if (locked == false)
182             throw new IllegalStateException JavaDoc("Failed to acquire lock within 60 seconds.");
183       }
184       
185       public void unlock(Object JavaDoc local)
186       {
187          reentrantLock.release();
188       }
189
190       public Object JavaDoc getValue(Object JavaDoc local)
191       {
192          return valuesByLocal.get(local);
193       }
194
195       public void setValue(Object JavaDoc local, Object JavaDoc value)
196       {
197          valuesByLocal.put(local, value);
198       }
199
200       public boolean containsValue(Object JavaDoc local)
201       {
202          return valuesByLocal.containsKey(local);
203       }
204    }
205 }
206
Popular Tags