001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.pojos;
020:
021: import java.io.Serializable;
022: import java.util.Calendar;
023: import java.util.Date;
024:
025: /**
026: * Represents locking information about a specific RollerTask.
027: *
028: * @ejb:bean name="TaskLockData"
029: * @hibernate.class lazy="false" table="roller_tasklock"
030: * @hibernate.cache usage="read-write"
031: */
032: public class TaskLockData extends PersistentObject implements
033: Serializable {
034:
035: private String id = null;
036: private String name = null;
037: private boolean locked = false;
038: private Date timeAquired = null;
039: private int timeLeased = 0;
040: private Date lastRun = null;
041:
042: public TaskLockData() {
043: }
044:
045: /**
046: * Calculate the next allowed time the task managed by this lock would
047: * be allowed to run. i.e. lastRun + interval
048: */
049: public Date getNextRun(int interval) {
050:
051: Date lastRun = this .getLastRun();
052: if (lastRun == null) {
053: return null;
054: }
055:
056: // calculate next run time
057: Calendar cal = Calendar.getInstance();
058: cal.setTime(lastRun);
059: cal.add(Calendar.MINUTE, interval);
060:
061: return cal.getTime();
062: }
063:
064: /**
065: * Get the time the lease for this lock will expire, or null if this task
066: * lock is not currently locked.
067: */
068: public Date getLeaseExpires() {
069:
070: if (!locked || timeAquired == null) {
071: return null;
072: }
073:
074: // calculate lease expiration time
075: Calendar cal = Calendar.getInstance();
076: cal.setTime(timeAquired);
077: cal.add(Calendar.MINUTE, timeLeased);
078:
079: return cal.getTime();
080: }
081:
082: public void setData(PersistentObject otherData) {
083: TaskLockData other = (TaskLockData) otherData;
084: this .id = other.getId();
085: this .name = other.getName();
086: this .locked = other.isLocked();
087: this .timeAquired = other.getTimeAquired();
088: this .timeLeased = other.getTimeLeased();
089: this .lastRun = other.getLastRun();
090: }
091:
092: public boolean equals(Object other) {
093:
094: if (this == other)
095: return true;
096: if (!(other instanceof TaskLockData))
097: return false;
098:
099: // our natural key, or business key, is our name
100: final TaskLockData that = (TaskLockData) other;
101: return this .name.equals(that.getName());
102: }
103:
104: public int hashCode() {
105: // our natrual key, or business key, is our name
106: return this .name.hashCode();
107: }
108:
109: /**
110: * @ejb:persistent-field
111: * @hibernate.id column="id" generator-class="uuid.hex" unsaved-value="null"
112: */
113: public String getId() {
114: return id;
115: }
116:
117: public void setId(String id) {
118: this .id = id;
119: }
120:
121: /**
122: * @ejb:persistent-field
123: * @hibernate.property column="name" non-null="true" unique="true"
124: */
125: public String getName() {
126: return name;
127: }
128:
129: public void setName(String name) {
130: this .name = name;
131: }
132:
133: /**
134: * @ejb:persistent-field
135: * @hibernate.property column="timeacquired" non-null="false" unique="false"
136: */
137: public Date getTimeAquired() {
138: return timeAquired;
139: }
140:
141: public void setTimeAquired(Date timeAquired) {
142: this .timeAquired = timeAquired;
143: }
144:
145: /**
146: * @ejb:persistent-field
147: * @hibernate.property column="lastrun" non-null="false" unique="false"
148: */
149: public Date getLastRun() {
150: return lastRun;
151: }
152:
153: public void setLastRun(Date lastRun) {
154: this .lastRun = lastRun;
155: }
156:
157: /**
158: * @ejb:persistent-field
159: * @hibernate.property column="islocked" non-null="false" unique="false"
160: */
161: public boolean isLocked() {
162:
163: // this method requires a little extra logic because we return false
164: // even if a task is locked when it's lease has expired
165: if (!locked) {
166: return false;
167: }
168:
169: Date now = new Date();
170: Date leaseExpiration = this .getLeaseExpires();
171:
172: return now.before(leaseExpiration);
173: }
174:
175: public void setLocked(boolean locked) {
176: this .locked = locked;
177: }
178:
179: /**
180: * @ejb:persistent-field
181: * @hibernate.property column="timeleased" non-null="false" unique="false"
182: */
183: public int getTimeLeased() {
184: return timeLeased;
185: }
186:
187: public void setTimeLeased(int timeLeased) {
188: this.timeLeased = timeLeased;
189: }
190:
191: }
|