KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > utils > LockableHashtable


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.utils;
56
57 import java.util.Hashtable JavaDoc;
58 import java.util.Vector JavaDoc;
59
60 /**
61  * This subclass of the java Hashtable allows individual
62  * entries to be "locked" so that their values cannot be
63  * overwritten or removed.
64  * <p/>
65  * Note, only the put() and remove() methods have been
66  * overridden. The clear() method still removes all
67  * entries whether they've been locked or not.
68  *
69  * @author James Snell (jasnell@us.ibm.com)
70  */

71 public class LockableHashtable extends Hashtable JavaDoc
72 {
73
74    /**
75     * Stores the keys of the locked entries
76     */

77    Vector JavaDoc lockedEntries = new Vector JavaDoc();
78
79    /**
80     * Place to look for properties which we don't find locally.
81     */

82    private Hashtable JavaDoc parent = null;
83
84    public LockableHashtable()
85    {
86       super();
87    }
88
89    public LockableHashtable(int p1, float p2)
90    {
91       super(p1, p2);
92    }
93
94    public LockableHashtable(java.util.Map JavaDoc p1)
95    {
96       super(p1);
97    }
98
99    public LockableHashtable(int p1)
100    {
101       super(p1);
102    }
103
104    /**
105     * Set the parent Hashtable for this object
106     */

107    public synchronized void setParent(Hashtable JavaDoc parent)
108    {
109       this.parent = parent;
110    }
111
112    /**
113     * Get an entry from this hashtable, and if we don't find anything,
114     * defer to our parent, if any.
115     */

116    public synchronized Object JavaDoc get(Object JavaDoc key)
117    {
118       Object JavaDoc ret = super.get(key);
119       if ((ret == null) && (parent != null))
120       {
121          ret = parent.get(key);
122       }
123       return ret;
124    }
125
126    /**
127     * New version of the put() method that allows for explicitly marking
128     * items added to the hashtable as locked.
129     */

130    public synchronized Object JavaDoc put(Object JavaDoc p1, Object JavaDoc p2, boolean locked)
131    {
132       if (this.containsKey(p1) && lockedEntries.contains(p1))
133       {
134          return null;
135       }
136       if (locked) lockedEntries.add(p1);
137       return super.put(p1, p2);
138    }
139
140    /**
141     * Overrides the Hashtable.put() method to mark items as not being locked.
142     */

143    public synchronized Object JavaDoc put(Object JavaDoc p1, Object JavaDoc p2)
144    {
145       return put(p1, p2, false);
146    }
147
148    /**
149     * Checks to see if an item is locked before it is removed.
150     */

151    public synchronized Object JavaDoc remove(Object JavaDoc p1)
152    {
153       if (lockedEntries.contains(p1))
154       {
155          return null;
156       }
157       if (lockedEntries.contains(p1)) lockedEntries.remove(p1);
158       return super.remove(p1);
159    }
160
161    /**
162     * Returns true if a given key is in our locked list
163     */

164    public boolean isKeyLocked(Object JavaDoc key)
165    {
166       return lockedEntries.contains(key);
167    }
168 }
169
Popular Tags