View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.danube.webdav.lock;
14  
15  import org.abstracthorizon.danube.webdav.util.Timeout;
16  
17  /**
18   * This clas represents a lock
19   *
20   * @author Daniel Sendula
21   */
22  public class Lock {
23  
24      /**
25       * Lock type
26       * @see LockingMechanism#TYPE_WRITE
27       */
28      protected int type;
29  
30      /**
31       * Lock scope
32       * @see LockingMechanism#SCOPE_SHARED
33       * @see LockingMechanism#SCOPE_EXCLUSIVE
34       */
35      protected int scope;
36  
37      /** Locks owner. May be <code>null</code> */
38      protected Object owner;
39  
40      /** Locks depth. May be <code>null</code> */
41      protected int depth;
42  
43      /** Locks timeout */
44      protected Timeout timeout;
45  
46      /** Lock's token */
47      protected String token;
48  
49      /** Timestamp until where this lock is valid */
50      protected long validUntil = Long.MAX_VALUE;
51  
52      /** Static token element counter */
53      protected static long counter = 1;
54  
55      /**
56       * Constructor
57       * @param type lock type
58       * @param scope lock scope
59       * @param owner lock owner
60       * @param timeout lock timeout
61       * @param depth lock depth
62       */
63      public Lock(int type, int scope, Object owner, Timeout timeout, int depth) {
64          this.type = type;
65          this.scope = scope;
66          this.owner = owner;
67          this.depth = depth;
68          this.timeout = timeout;
69          createToken();
70      }
71  
72      /**
73       * Creates new lock's token
74       */
75      protected synchronized void createToken() {
76          String t = "opaquelocktoken:" + Long.toHexString(System.currentTimeMillis()) + "-" + Long.toHexString(counter);
77          token = t;
78          counter = counter + 1;
79      }
80  
81      /**
82       * Returns lock depth
83       * @return lock depth
84       */
85      public int getDepth() {
86          return depth;
87      }
88  
89      /**
90       * Sets lock depth
91       * @param depth lock depth
92       */
93      public void setDepth(int depth) {
94          this.depth = depth;
95      }
96  
97      /**
98       * Returns lock owner
99       * @return lock owner
100      */
101     public Object getOwner() {
102         return owner;
103     }
104 
105     /**
106      * Sets lock owner
107      * @param owner lock owner
108      */
109     public void setOwner(Object owner) {
110         this.owner = owner;
111     }
112 
113     /**
114      * Returns lock scope
115      * @return lock scope
116      */
117     public int getScope() {
118         return scope;
119     }
120 
121     /**
122      * Sets lock scope
123      * @param scope lock scope
124      */
125     public void setScope(int scope) {
126         this.scope = scope;
127     }
128 
129     /**
130      * Returns lock timeout
131      * @return lock timeout
132      */
133     public Timeout getTimeout() {
134         return timeout;
135     }
136 
137     /**
138      * Sets lock timeout
139      * @param timeout lock timeout
140      */
141     public void setTimeout(Timeout timeout) {
142         this.timeout = timeout;
143         validUntil = timeout.calculateValidity();
144     }
145 
146     /**
147      * Return's lock token
148      * @return lock token
149      */
150     public String getToken() {
151         return token;
152     }
153 
154     /**
155      * Sets lock token
156      * @param token lock token
157      */
158     // TODO check if this is needed at all. Remove it!
159     public void setToken(String token) {
160         this.token = token;
161     }
162 
163     /**
164      * Returns lock type
165      * @return lock type
166      */
167     public int getType() {
168         return type;
169     }
170 
171     /**
172      * Sets lock type
173      * @param type lock type
174      */
175     public void setType(int type) {
176         this.type = type;
177     }
178 
179     /**
180      * Returns timestamp until lock is valid
181      * @return timestamp until lock is valid
182      */
183     public long getValidUntil() {
184         return validUntil;
185     }
186 
187     /**
188      * Refreshes locks validity by reusing given timeout
189      * @param timeout timeout
190      * @return <code>true</code>
191      */
192     public boolean refreshTimeout(Timeout timeout) {
193         this.timeout = timeout;
194         validUntil = timeout.calculateValidity();
195         return true;
196     }
197 
198     /**
199      * Frees lock. This implementation does nothing - it is defined
200      * hook point for extensions
201      *
202      */
203     public void freeLock() {
204     }
205 
206     /**
207      * Retunrs <code>true</code> if locks are same (token wise)
208      * @return <code>true</code> if locks are same (token wise)
209      */
210     public boolean equals(Object o) {
211         if (o instanceof Lock) {
212             Lock l = (Lock)o;
213             return token.equals(l.token);
214         }
215         return false;
216     }
217 }