KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > LockMap


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.lock;
8
9 import java.util.Collections JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.concurrent.CopyOnWriteArraySet JavaDoc;
12
13 /**
14  * Provide lock ownership mapping.
15  *
16  * @author Ben Wang
17  * @version $Id: LockMap.java,v 1.11 2006/12/08 18:50:49 genman Exp $
18  */

19 public class LockMap
20 {
21    public static final int OWNER_ANY = 0;
22    public static final int OWNER_READ = 1;
23    public static final int OWNER_WRITE = 2;
24
25    private Object JavaDoc writeOwner_=null;
26
27    /**
28     * Set of owners. As this set is small (not like 100+) this structure is
29     * sufficient.
30     */

31    private final Set JavaDoc readOwnerList_ = new CopyOnWriteArraySet JavaDoc();
32
33    public LockMap()
34    {
35    }
36
37
38    /**
39     * Check whether this owner has reader or writer ownership.
40     *
41     * @param caller the potential owner. Cannot be <code>null</code>.
42     * @param ownership Either <code>OWNER_ANY</code>, <code>OWNER_READ</code>,
43     * or <code>OWNER_WRITE</code>.
44     * @return
45     *
46     * @throws NullPointerException if <code>caller</code> is <code>null</code>.
47     */

48    public boolean isOwner(Object JavaDoc caller, int ownership)
49    {
50       /* This method doesn't need to be synchronized; the thread is doing a simple read access (writer, readers)
51          and only the current thread can *change* the writer or readers, so this cannot happen while we read.
52       */

53
54       switch (ownership) {
55          case OWNER_ANY:
56             return (writeOwner_ != null && caller.equals(writeOwner_) || readOwnerList_.contains(caller));
57          case OWNER_READ:
58             return (readOwnerList_.contains(caller));
59          case OWNER_WRITE:
60             return (writeOwner_ != null && caller.equals(writeOwner_));
61          default:
62       return false;
63    }
64    }
65
66
67
68    /**
69     * Adding a reader owner.
70     *
71     * @param owner
72     */

73    public void addReader(Object JavaDoc owner)
74    {
75       readOwnerList_.add(owner);
76    }
77
78    /**
79     * Adding a writer owner.
80     *
81     * @param owner
82     */

83    public void setWriterIfNotNull(Object JavaDoc owner)
84    {
85       synchronized(this) {
86          if(writeOwner_ != null)
87             throw new IllegalStateException JavaDoc("there is already a writer holding the lock: " + writeOwner_);
88          writeOwner_=owner;
89       }
90    }
91
92    private Object JavaDoc setWriter(Object JavaDoc owner) {
93       Object JavaDoc old;
94       synchronized(this) {
95          old=writeOwner_;
96          writeOwner_=owner;
97       }
98       return old;
99    }
100
101
102    /**
103     * Upgrading current reader ownership to writer one.
104     *
105     * @param owner
106     * @return True if successful.
107     */

108    public boolean upgrade(Object JavaDoc owner) throws OwnerNotExistedException
109    {
110       boolean old_value = readOwnerList_.remove(owner);
111       if(!old_value) // didn't exist in the list
112
throw new OwnerNotExistedException("Can't upgrade lock. Read lock owner did not exist");
113       setWriter(owner);
114       return true;
115    }
116
117    /**
118     * Returns an unmodifiable set of reader owner objects.
119     */

120    public Set JavaDoc readerOwners()
121    {
122       return Collections.unmodifiableSet(readOwnerList_);
123    }
124
125    public void releaseReaderOwners(LockStrategy lock)
126    {
127       int size = readOwnerList_.size();
128       for (int i = 0; i < size; i++)
129          lock.readLock().unlock();
130    }
131
132    /**
133     * @return Writer owner object. Null if none.
134     */

135    public Object JavaDoc writerOwner()
136    {
137       return writeOwner_;
138    }
139
140    /**
141     * Remove reader ownership.
142     */

143    public void removeReader(Object JavaDoc owner)
144    {
145       readOwnerList_.remove(owner);
146    }
147
148    /**
149     * Remove writer ownership.
150     */

151    public void removeWriter()
152    {
153       synchronized(this) {
154          writeOwner_=null;
155       }
156    }
157
158    /**
159     * Remove all ownership.
160     */

161    public void removeAll()
162    {
163       removeWriter();
164       readOwnerList_.clear();
165    }
166
167    /**
168     * Debugging information.
169     *
170     * @return
171     */

172    public String JavaDoc printInfo()
173    {
174       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(64);
175       buf.append("Read lock owners: ").append(readOwnerList_).append('\n');
176       buf.append("Write lock owner: ").append(writeOwner_).append('\n');
177       return buf.toString();
178    }
179
180    public boolean isReadLocked() {
181       return !readOwnerList_.isEmpty();
182    }
183 }
184
Popular Tags