KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > lock > CmsLock


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/lock/CmsLock.java,v $
3  * Date : $Date: 2005/06/27 23:22:25 $
4  * Version: $Revision: 1.28 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.lock;
33
34 import org.opencms.db.CmsDbUtil;
35 import org.opencms.util.CmsUUID;
36
37 /**
38  * Represents the lock state of a VFS resource.<p>
39  *
40  * The lock state is combination of how, by whom and in which project
41  * a resource is currently locked.<p>
42  *
43  * Using old-style methods on CmsResource objects to prove the lock
44  * state of a resource may result to incorrect lock states. Use
45  * {@link org.opencms.file.CmsObject#getLock(String)} to obtain a
46  * CmsLock object that represents the current lock state of a resource.
47  *
48  * @author Thomas Weckert
49  * @author Andreas Zahner
50  *
51  * @version $Revision: 1.28 $
52  *
53  * @since 6.0.0
54  *
55  * @see org.opencms.file.CmsObject#getLock(org.opencms.file.CmsResource)
56  * @see org.opencms.lock.CmsLockManager
57  */

58 public class CmsLock implements Cloneable JavaDoc {
59
60     /** Indicates that the lock is a common lock and doesn't expire. */
61     public static final int COMMON = 0;
62
63     /** Indicates that the lock is a temporary lock that expires is the user was logged out. */
64     public static final int TEMPORARY = 1;
65
66     /**
67      * A lock that allows the user to edit the resource’s structure record,
68      * it’s resource record, and its content record.<p>
69      *
70      * This lock is assigned to files that are locked via the context menu.
71      */

72     public static final int TYPE_EXCLUSIVE = 4;
73
74     /**
75      * A lock that is inherited from a locked parent folder.
76      */

77     public static final int TYPE_INHERITED = 3;
78
79     /**
80      * A lock that allows the user to edit the resource’s structure record only,
81      * but not it’s resource record nor content record.<p>
82      *
83      * This lock is assigned to files if a sibling of the resource record has
84      * already an exclusive lock.
85      */

86     public static final int TYPE_SHARED_EXCLUSIVE = 2;
87
88     /**
89      * A lock that allows the user to edit the resource’s structure record only,
90      * but not it’s resource record nor content record.<p>
91      *
92      * This lock is assigned to resources that already have a shared exclusive lock,
93      * and then inherit a lock because one if it's parent folders gets locked.
94      */

95     public static final int TYPE_SHARED_INHERITED = 1;
96
97     /**
98      * Reserved for the Null CmsLock.
99      */

100     public static final int TYPE_UNLOCKED = 0;
101
102     /** The shared null lock object. */
103     private static final CmsLock NULL_LOCK = new CmsLock(
104         "",
105         CmsUUID.getNullUUID(),
106         CmsDbUtil.UNKNOWN_ID,
107         CmsLock.TYPE_UNLOCKED);
108
109     /** Flag to indicate if the lock is a temporary lock. */
110     private int m_mode;
111
112     /** The ID of the project where the resource is locked. */
113     private int m_projectId;
114
115     /** The name of the locked resource. */
116     private String JavaDoc m_resourceName;
117
118     /** Saves how the resource is locked. */
119     private int m_type;
120
121     /** The ID of the user who locked the resource. */
122     private CmsUUID m_userId;
123
124     /**
125      * Constructor for a new Cms lock.<p>
126      *
127      * @param resourceName the full resource name including the site root
128      * @param userId the ID of the user who locked the resource
129      * @param projectId the ID of the project where the resource is locked
130      * @param type flag indicating how the resource is locked
131      */

132     public CmsLock(String JavaDoc resourceName, CmsUUID userId, int projectId, int type) {
133
134         m_resourceName = resourceName;
135         m_userId = userId;
136         m_projectId = projectId;
137         m_type = type;
138         m_mode = COMMON;
139     }
140
141     /**
142      * Constructor for a new Cms lock.<p>
143      *
144      * @param resourceName the full resource name including the site root
145      * @param userId the ID of the user who locked the resource
146      * @param projectId the ID of the project where the resource is locked
147      * @param type flag indicating how the resource is locked
148      * @param mode flag indicating the mode (temporary or common) of a lock
149      */

150     public CmsLock(String JavaDoc resourceName, CmsUUID userId, int projectId, int type, int mode) {
151
152         m_resourceName = resourceName;
153         m_userId = userId;
154         m_projectId = projectId;
155         m_type = type;
156         m_mode = mode;
157     }
158
159     /**
160      * Returns the shared Null CmsLock.<p>
161      *
162      * @return the shared Null CmsLock
163      */

164     public static CmsLock getNullLock() {
165
166         return CmsLock.NULL_LOCK;
167     }
168
169     /**
170      * Compares this lock to the specified object.<p>
171      *
172      * @param obj the object to compare to
173      * @return true if and only if member values of this CmsLock are the same with the compared CmsLock
174      */

175     public boolean equals(Object JavaDoc obj) {
176
177         if (obj == this) {
178             return true;
179         }
180         if (obj instanceof CmsLock) {
181             CmsLock other = (CmsLock)obj;
182             return other.m_resourceName.equals(m_resourceName)
183                 && other.m_userId.equals(m_userId)
184                 && (other.m_projectId == m_projectId);
185         }
186         return false;
187     }
188
189     /**
190      * Returns the mode of the lock to indicate if the lock is a temporary lock.<p>
191      *
192      * @return the temporary mode of the lock
193      */

194     public int getMode() {
195
196         return m_mode;
197     }
198
199     /**
200      * Returns the ID of the project where the resource is currently locked.<p>
201      *
202      * @return the ID of the project
203      */

204     public int getProjectId() {
205
206         return m_projectId;
207     }
208
209     /**
210      * Returns the name of the locked resource.<p>
211      *
212      * @return the name of the locked resource
213      */

214     public String JavaDoc getResourceName() {
215
216         return m_resourceName;
217     }
218
219     /**
220      * Returns the type about how the resource is locked.<p>
221      *
222      * @return the type of the lock
223      */

224     public int getType() {
225
226         return m_type;
227     }
228
229     /**
230      * Returns the ID of the user who currently locked the resource.<p>
231      *
232      * @return the ID of the user
233      */

234     public CmsUUID getUserId() {
235
236         return m_userId;
237     }
238
239     /**
240      * @see java.lang.Object#hashCode()
241      */

242     public int hashCode() {
243
244         return getResourceName().hashCode();
245     }
246
247     /**
248      * Proves if this CmsLock is the Null CmsLock.<p>
249      *
250      * @return true if and only if this CmsLock is the Null CmsLock
251      */

252     public boolean isNullLock() {
253
254         return this.equals(CmsLock.NULL_LOCK);
255     }
256
257     /**
258      * Builds a string representation of the current state.<p>
259      *
260      * @see java.lang.Object#toString()
261      */

262     public String JavaDoc toString() {
263
264         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
265
266         buf.append("resource: ");
267         buf.append(this.getResourceName());
268         buf.append(" type: ");
269         switch (this.getType()) {
270             case CmsLock.TYPE_EXCLUSIVE:
271                 buf.append("exclusive");
272                 break;
273             case CmsLock.TYPE_SHARED_EXCLUSIVE:
274                 buf.append("shared exclusive");
275                 break;
276             case CmsLock.TYPE_INHERITED:
277                 buf.append("inherited");
278                 break;
279             case CmsLock.TYPE_SHARED_INHERITED:
280                 buf.append("shared inherited");
281                 break;
282             default:
283                 buf.append("unlocked");
284                 break;
285         }
286         buf.append(" project: ");
287         buf.append(this.getProjectId());
288         buf.append(" user: ");
289         buf.append(this.getUserId());
290
291         return buf.toString();
292     }
293 }
294
Popular Tags