KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > registries > locks > JahiaLocksRegistry


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.registries.locks;
14
15 import java.util.Hashtable JavaDoc;
16
17 import org.jahia.utils.JahiaConsole;
18
19
20 /**
21  * .... add comments here ....
22  *
23  * @auhtor Fulco Houkes
24  * @version 1.0
25  */

26 public class JahiaLocksRegistry
27 {
28     private static JahiaLocksRegistry mObject;
29
30     /**
31      * @associates JahiaLock
32      */

33     private Hashtable JavaDoc mRegistry;
34
35
36     //-------------------------------------------------------------------------
37
/** Default constructor
38      */

39     protected JahiaLocksRegistry () {
40         mRegistry = new Hashtable JavaDoc ();
41         JahiaConsole.println ("JahiaLocksRegistry.constructor",
42                 "=---= Lock registry has been instanciated =---=");
43     }
44
45
46     //-------------------------------------------------------------------------
47
/** Return the unique registry instance. If the instance does not exist,
48      * a new instance is created.
49      *
50      * #return
51      * Return the unique registry instance. Return null is the registry
52      * could not be instanciated.
53      */

54     public static JahiaLocksRegistry getInstance() {
55         if (mObject == null) {
56             mObject = new JahiaLocksRegistry();
57         }
58         return mObject;
59     }
60
61     //-------------------------------------------------------------------------
62
/** Set the lock data. The lock is created if the specified lock name is not
63      * used. If the lock already exits, the lock's data are updated.
64      *
65      * @param lockName
66      * The lock name. This name must be unique among the registry.
67      * @param lockData
68      * The additional (user-defined) lock data.
69      *
70      * @return
71      * Return true if the lock could be added to the registry. False if the
72      * lock name is already used.
73      */

74     public synchronized boolean setLock (String JavaDoc lockName, Hashtable JavaDoc lockData,
75             int timeout)
76     {
77         if (lockName == null) {
78             return false;
79         }
80
81         JahiaLock lock = (JahiaLock)mRegistry.get (lockName);
82         if (lock == null) {
83             lock = new JahiaLock (lockName, lockData, timeout);
84             mRegistry.put (lockName, lock);
85
86         } else {
87             lock.setLockData (lockData);
88             lock.resetTimeout();
89         }
90         return true;
91     }
92
93
94
95     //-------------------------------------------------------------------------
96
/** Get the specified lock.
97      *
98      * @param lockName
99      * The lock name.
100      *
101      * @return
102      * Return the lock's data hashtable. If the lock could not befound,
103      * null will be returned.
104      */

105     public synchronized Hashtable JavaDoc getLock (String JavaDoc lockName)
106     {
107         if (lockName != null) {
108             JahiaLock lock = (JahiaLock)mRegistry.get (lockName);
109             if (lock != null) {
110                 return lock.getLockData();
111             }
112         }
113         return null;
114     }
115
116
117     //-------------------------------------------------------------------------
118
/** Remove the specified lock. Does not consider as an error if the lock
119      * is not present in the registry.
120      *
121      * @param lockName
122      * The lock name.
123      */

124     public synchronized void removeLock (String JavaDoc lockName)
125     {
126         if (lockName != null) {
127             mRegistry.remove (lockName);
128         }
129     }
130
131
132     //-------------------------------------------------------------------------
133
/** check if the specified lock name is already in use.
134      *
135      * @param lockName
136      * The lock name.
137      *
138      * @return
139      * Return true if the lock name is already in use, otherwise return
140      * false.
141      */

142     public final boolean doesLockExist (String JavaDoc lockName)
143     {
144         return (getLock (lockName) != null);
145     }
146
147     //-------------------------------------------------------------------------
148
/**
149      *
150      */

151     public synchronized boolean isLockValid (String JavaDoc lockName)
152     {
153         if (lockName == null) {
154             return false;
155         }
156
157         JahiaLock lock = (JahiaLock)mRegistry.get (lockName);
158         return lock.isValid();
159     }
160
161     //-------------------------------------------------------------------------
162
/**
163      *
164      */

165     public synchronized boolean resetLockTimeout (String JavaDoc lockName)
166     {
167         if (lockName == null) {
168             return false;
169         }
170
171         JahiaLock lock = (JahiaLock)mRegistry.get (lockName);
172         lock.resetTimeout();
173         return true;
174     }
175
176 }
177
Popular Tags