KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > lock > Lock


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.services.lock;
14
15 import java.util.Date JavaDoc;
16
17 import org.jahia.services.usermanager.JahiaUser;
18 import java.io.Serializable JavaDoc;
19
20 /**
21  * <p>Title: Jahia locking system implementation.</p>
22  * <p>Description:
23  * This class implements the lock object with an expiration time. A lock belongs
24  * always to a context meaning that a owner (JahiaUser) and a identifier (session
25  * ID for example) should be provided.
26  * </p>
27  * <p>Copyright: MAP (Jahia Solutions Sàrl 2003)</p>
28  * <p>Company: Jahia Solutions Sàrl</p>
29  * @author MAP
30  * @version 1.0
31  */

32 class Lock implements LockDefinitions, Serializable JavaDoc {
33
34     /**
35      * Default constructor.
36      *
37      * @param owner The lock owner defined by a JahiaUser object.
38      * @param id The lock id defined by a contextual string such as http session id.
39      * @param timeout The period in second during which the lock is valid. For a
40      * non expiration time lock use the constant NO_EXPIRATION_TIME.
41      */

42     public Lock(JahiaUser owner, String JavaDoc id, int timeout) {
43         this(owner, id, timeout, false);
44     }
45
46     /**
47      * Persistence constructor
48      *
49      * @param owner The lock owner defined by a JahiaUser object.
50      * @param id The lock id defined by a contextual string such as http session id.
51      * @param timeout The period in second during which the lock is valid. For a
52      * non expiration time lock use the constant NO_EXPIRATION_TIME.
53      * @param stolen initial value to indicate if this lock has been stolen.
54      */

55     protected Lock(JahiaUser owner, String JavaDoc id, int timeout, boolean stolen) {
56         if (timeout > NO_EXPIRATION_TIME) {
57             this.timeout = timeout * 1000;
58             Date JavaDoc date = new Date JavaDoc();
59             this.expirationDate = date.getTime() + this.timeout;
60         }
61         this.owner = owner;
62         this.id = id;
63         this.stealed = stolen;
64     }
65
66     /**
67      * Get the system time and return if lock is already valid or not.
68      *
69      * @return True if lock did not expired, false otherwise.
70      */

71     public boolean hasExpired() {
72         if (this.timeout <= NO_EXPIRATION_TIME) {
73             return false;
74         }
75         Date JavaDoc date = new Date JavaDoc();
76         return date.getTime() > this.expirationDate;
77     }
78
79     /**
80      * Get the lock time remaining.
81      *
82      * @return The lock time remaining.
83      */

84     public long getTimeRemaining() {
85         if (this.timeout <= NO_EXPIRATION_TIME) {
86             return NO_EXPIRATION_TIME;
87         }
88         Date JavaDoc date = new Date JavaDoc();
89         return this.expirationDate - date.getTime();
90     }
91
92     /**
93      * Reset the lock time out to the new timeout parameter.
94      *
95      * @param timeout The timeout in second.
96      */

97     public void resetTimeout(int timeout) {
98         if (this.timeout <= NO_EXPIRATION_TIME) {
99             return;
100         }
101         Date JavaDoc date = new Date JavaDoc();
102         this.timeout = timeout * 1000;
103         this.expirationDate = date.getTime() + this.timeout;
104     }
105
106     /**
107      * Time out getter.
108      *
109      * @return The time out in second.
110      */

111     public long getTimeout() {
112         return this.timeout;
113     }
114
115     /**
116      * Lock owner (JahiaUser) getter.
117      *
118      * @return The lock owner.
119      */

120     public JahiaUser getOwner() {
121         return this.owner;
122     }
123
124     /**
125      * Owner (JahiaUser) getter. Change the lock owner.
126      *
127      * @param owner The new lock owner.
128      */

129     public void setOwner(JahiaUser owner) {
130         this.owner = owner;
131     }
132
133     /**
134      * Lock identifier getter.
135      *
136      * @return The lock ID.
137      */

138     public String JavaDoc getID() {
139         return this.id;
140     }
141
142     /**
143      * Lock ID setter. Change the lock ID.
144      *
145      * @param id The lock ID.
146      */

147     public void setID(String JavaDoc id) {
148         this.id = id;
149     }
150
151     /**
152      * If a Jahia user has sufficiant rights (admin for example) he can steal a
153      * lock. In this case this getter returned if the lock was stolen or not.
154      *
155      * @return True if the lock was stolen, false otherwise.
156      */

157     public boolean isStealed() {
158         return stealed;
159     }
160
161     /**
162      * If a Jahia user has sufficiant rights (admin for example) he can steal a
163      * lock. In this case this setter can be used to mark the lock as stolen.
164      *
165      * @param stealed Should be true for stolen lock.
166      */

167     public void setStealed(boolean stealed) {
168         this.stealed = stealed;
169     }
170
171     private long expirationDate;
172     private long timeout;
173     private JahiaUser owner;
174     private String JavaDoc id;
175     private boolean stealed;
176 }
177
Popular Tags