001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.concurrency.locking;
007:
008: import java.util.Date;
009:
010: import org.jasig.portal.concurrency.IEntityLock;
011: import org.jasig.portal.concurrency.IEntityLockService;
012: import org.jasig.portal.concurrency.LockingException;
013:
014: /**
015: * An implementation of IEntityLock. A lock is granted to a
016: * <code>lockOwner</code> for an <code>entityType</code> and
017: * <code>entityKey</code>. It guarantees some degree of exclusive
018: * access to the entity, depending on <code>lockType</code> and
019: * <code>expirationTime</code>.
020: *
021: * @author Dan Ellentuck
022: * @version $Revision: 34757 $
023: * @see org.jasig.portal.concurrency.IEntityLock
024: */
025: public class EntityLockImpl implements IEntityLock {
026: private Class entityType;
027: private String entityKey;
028: private String lockOwner;
029: private Date expirationTime;
030: private int lockType;
031: private IEntityLockService service;
032:
033: /**
034: *
035: */
036: protected EntityLockImpl(Class newEntityType, String newEntityKey,
037: int newLockType, Date newExpirationTime, String newLockOwner) {
038: super ();
039: setEntityType(newEntityType);
040: setEntityKey(newEntityKey);
041: setLockType(newLockType);
042: setExpirationTime(newExpirationTime);
043: setLockOwner(newLockOwner);
044: }
045:
046: /**
047: *
048: */
049: protected EntityLockImpl(Class newEntityType, String newEntityKey,
050: int newLockType, Date newExpirationTime,
051: String newLockOwner, IEntityLockService newService) {
052: super ();
053: setEntityType(newEntityType);
054: setEntityKey(newEntityKey);
055: setLockType(newLockType);
056: setExpirationTime(newExpirationTime);
057: setLockOwner(newLockOwner);
058: setService(newService);
059: }
060:
061: /**
062: * Delegate to the service.
063: * @param newType int
064: * @exception org.jasig.portal.concurrency.LockingException - if the conversion fails.
065: */
066: public void convert(int newType) throws LockingException {
067: getService().convert(this , newType);
068: }
069:
070: /**
071: * Delegate to the service.
072: * @param newType int
073: * @param duration int
074: * @exception org.jasig.portal.concurrency.LockingException - if the conversion fails.
075: */
076: public void convert(int newType, int duration)
077: throws LockingException {
078: getService().convert(this , newType);
079: }
080:
081: /**
082: * @param obj the Object to compare with
083: * @return true if these Objects are equal; false otherwise.
084: * @see java.util.Hashtable
085: */
086: public boolean equals(Object obj) {
087: if (obj == null)
088: return false;
089: if (obj == this )
090: return true;
091: if (!(obj instanceof EntityLockImpl))
092: return false;
093:
094: IEntityLock candidate = (IEntityLock) obj;
095: return this .getEntityType().equals(candidate.getEntityType())
096: && this .getEntityKey().equals(candidate.getEntityKey())
097: && this .getLockOwner().equals(candidate.getLockOwner())
098: && this .getLockType() == candidate.getLockType()
099: && this .getExpirationTime().equals(
100: candidate.getExpirationTime());
101: }
102:
103: /**
104: */
105: protected void expire() {
106: setExpirationTime(new Date(0));
107: }
108:
109: /**
110: * @throws Throwable
111: */
112: protected void finalize() throws Throwable {
113: super .finalize();
114: }
115:
116: /**
117: * @return java.lang.String
118: */
119: public String getEntityKey() {
120: return entityKey;
121: }
122:
123: /**
124: * @see org.jasig.portal.EntityTypes for known types.
125: * @return java.lang.Class
126: */
127: public Class getEntityType() {
128: return entityType;
129: }
130:
131: /**
132: * @return java.util.Date
133: */
134: public Date getExpirationTime() {
135: return expirationTime;
136: }
137:
138: /**
139: * Could be the portal user or the framework or ...?
140: * @return java.lang.String
141: */
142: public String getLockOwner() {
143: return lockOwner;
144: }
145:
146: /**
147: * See IEntityLockingService for a description of lock types.
148: * @return int
149: */
150: public int getLockType() {
151: return lockType;
152: }
153:
154: /**
155: * @return org.jasig.portal.concurrency.locking.IEntityLockService
156: */
157: protected IEntityLockService getService() {
158: return service;
159: }
160:
161: /**
162: * This method is supported primarily for hash tables, such as those provided in java.util.
163: * @return an integer hash code for the receiver
164: * @see java.util.Hashtable
165: */
166: public int hashCode() {
167: return getEntityType().hashCode() + getEntityKey().hashCode()
168: + getLockOwner().hashCode();
169: }
170:
171: /**
172: * Answer if the lock is unexpired.
173: * @return boolean
174: */
175: public boolean isLocked() {
176: return getExpirationTime().after(new Date());
177: }
178:
179: /**
180: * Delegate to the service.
181: * @return boolean
182: */
183: public boolean isValid() throws LockingException {
184: return getService().isValid(this );
185: }
186:
187: /**
188: * Delegate to the service.
189: */
190: public void release() throws LockingException {
191: getService().release(this );
192: }
193:
194: /**
195: * Delegate to the service.
196: */
197: public void renew() throws LockingException {
198: getService().renew(this );
199: }
200:
201: /**
202: * Delegate to the service.
203: */
204: public void renew(int duration) throws LockingException {
205: getService().renew(this , duration);
206: }
207:
208: /**
209: * @param newEntityKey
210: */
211: private void setEntityKey(String newEntityKey) {
212: entityKey = newEntityKey;
213: }
214:
215: /**
216: * @param newEntityType
217: */
218: private void setEntityType(Class newEntityType) {
219: entityType = newEntityType;
220: }
221:
222: /**
223: * @param newExpirationTime
224: */
225: void setExpirationTime(Date newExpirationTime) {
226: expirationTime = newExpirationTime;
227: }
228:
229: /**
230: * @param newLockOwner
231: */
232: private void setLockOwner(String newLockOwner) {
233: lockOwner = newLockOwner;
234: }
235:
236: /**
237: * @param newLockType
238: */
239: void setLockType(int newLockType) {
240: lockType = newLockType;
241: }
242:
243: /**
244: * @param newService org.jasig.portal.concurrency.locking.IEntityLockService
245: */
246: private void setService(IEntityLockService newService) {
247: service = newService;
248: }
249:
250: /**
251: * Returns a String that represents the value of this object.
252: * @return a string representation of the receiver
253: */
254: public String toString() {
255: return "EntityLockImpl for " + getEntityType().getName() + "("
256: + getEntityKey() + ")" + " type " + getLockType()
257: + " owner " + getLockOwner() + " "
258: + getExpirationTime();
259: }
260: }
|