KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > lock > BeanLockSupport


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.ejb.plugins.lock;
23
24 import javax.transaction.Transaction JavaDoc;
25
26 import org.jboss.ejb.BeanLock;
27 import org.jboss.ejb.Container;
28 import org.jboss.ejb.BeanLockExt;
29 import org.jboss.invocation.Invocation;
30 import org.jboss.logging.Logger;
31 import org.jboss.util.deadlock.Resource;
32
33
34 /**
35  * Support for the BeanLock
36  *
37  * @author <a HREF="bill@burkecentral.com">Bill Burke</a>
38  * @author <a HREF="marc.fleury@jboss.org">Marc Fleury</a>
39  * @version $Revision: 37459 $
40  */

41 public abstract class BeanLockSupport implements Resource, BeanLockExt
42 {
43    protected Container container = null;
44    
45    /**
46     * Number of threads that retrieved this lock from the manager
47     * (0 means removing)
48     */

49    protected int refs = 0;
50    
51    /** The Cachekey corresponding to this Bean */
52    protected Object JavaDoc id = null;
53  
54    /** Logger instance */
55    static Logger log = Logger.getLogger(BeanLock.class);
56  
57    /** Transaction holding lock on bean */
58    protected Transaction JavaDoc tx = null;
59  
60    protected Thread JavaDoc synched = null;
61    protected int synchedDepth = 0;
62
63    protected int txTimeout;
64
65     
66    public void setId(Object JavaDoc id) { this.id = id;}
67    public Object JavaDoc getId() { return id;}
68    public void setTimeout(int timeout) {txTimeout = timeout;}
69    public void setContainer(Container container) { this.container = container; }
70    public Object JavaDoc getResourceHolder() { return tx; }
71
72    /**
73     * A non-blocking method that checks if the calling thread will be able to acquire
74     * the sync lock based on the calling thread.
75     *
76     * @return true if the calling thread can obtain the sync lock in which
77     * case it will, false if another thread already has the lock.
78     */

79    public boolean attemptSync()
80    {
81       boolean didSync = false;
82       synchronized(this)
83       {
84          Thread JavaDoc thread = Thread.currentThread();
85          if(synched == null || synched.equals(thread) == true)
86          {
87             synched = thread;
88             ++ synchedDepth;
89             didSync = true;
90          }
91       }
92       return didSync;
93    }
94
95    /**
96     * A method that checks if the calling thread has the lock, and if it
97     * does not blocks until the lock is available. If there is no current owner
98     * of the lock, or the calling thread already owns the lock then the
99     * calling thread will immeadiately acquire the lock.
100     */

101    public void sync()
102    {
103       synchronized(this)
104       {
105          Thread JavaDoc thread = Thread.currentThread();
106          while(synched != null && synched.equals(thread) == false)
107          {
108             try
109             {
110                this.wait();
111             }
112             catch (InterruptedException JavaDoc ex) { /* ignore */ }
113          }
114          synched = thread;
115          ++synchedDepth;
116       }
117    }
118  
119    public void releaseSync()
120    {
121       synchronized(this)
122       {
123          if (--synchedDepth == 0)
124             synched = null;
125          this.notify();
126       }
127    }
128  
129    public abstract void schedule(Invocation mi) throws Exception JavaDoc;
130     
131    /**
132     * The setTransaction associates a transaction with the lock.
133     * The current transaction is associated by the schedule call.
134     */

135    public void setTransaction(Transaction JavaDoc tx){this.tx = tx;}
136    public Transaction JavaDoc getTransaction(){return tx;}
137    
138    public abstract void endTransaction(Transaction JavaDoc tx);
139    public abstract void wontSynchronize(Transaction JavaDoc tx);
140     
141    public abstract void endInvocation(Invocation mi);
142    
143    public void addRef() { refs++;}
144    public void removeRef() { refs--;}
145    public int getRefs() { return refs;}
146    
147    // Private --------------------------------------------------------
148

149 }
150
Popular Tags