/**
* Copyright (C) 2007 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.jbpm.jobexecutor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.jbpm.env.session.Job;
import org.jbpm.env.session.Timer;
import org.jbpm.pvm.Execution;
/**
* @author Pascal Verdage
*/
public class MemoryJobSession implements JobSession {
protected long idCounter = 1;
protected Set<Job> jobs = new TreeSet<Job>();
// comparator to order jobs by descending dueDate
protected static Comparator<Job> dueDateJobComparatorDesc =
new Comparator<Job>() {
public int compare(Job job1, Job job2) {
Long diff = job2.getDueDate().getTime() -
job1.getDueDate().getTime();
return diff.intValue();
}
};
public void save(Job job) {
if (! jobs.contains(job)) {
// add the job id
job.setDbid(idCounter++);
jobs.add(job);
}
}
public void delete(Job job) {
jobs.remove(job);
}
public List<Job> findExclusiveJobs(Execution processInstance) {
List<Job> result = new ArrayList<Job>();
for (Job job : jobs) {
if (job.getLockOwner() == null &&
job.getProcessInstance().equals(processInstance) &&
job.isExclusive() &&
( job.getEligibleDate() == null ||
job.getEligibleDate().getTime() < System.currentTimeMillis() ))
result.add(job);
}
Collections.sort(result, dueDateJobComparatorDesc);
return result;
}
public Job get(long jobId) {
Job result = null;
for (Job job : jobs) {
if (job.getDbid() == jobId) {
result = job;
break;
}
}
return result;
}
public Job getFirstAcquirableJob() {
Job result = null;
long firstAcquirableDate = Long.MAX_VALUE;
long currentTime = System.currentTimeMillis();
for (Job job : jobs) {
if ( (job.getLockExpirationTime() == null ||
job.getLockExpirationTime().getTime() < currentTime) &&
(job.getEligibleDate() == null ||
job.getEligibleDate().getTime() < currentTime) ) {
if (job.getDueDate().getTime() < firstAcquirableDate) {
firstAcquirableDate = job.getDueDate().getTime();
result = job;
}
}
}
return result;
}
public Job getFirstDueJob() {
Job result = null;
long firstDueDate = Long.MAX_VALUE;
long currentTime = System.currentTimeMillis();
for (Job job : jobs) {
if (job.getLockOwner() == null &&
(job.getEligibleDate() == null ||
job.getEligibleDate().getTime() < currentTime) ) {
if (job.getDueDate().getTime() < firstDueDate) {
firstDueDate = job.getDueDate().getTime();
result = job;
}
}
}
return result;
}
public List<Timer> findAllTimers() {
List<Timer> result = new ArrayList<Timer>();
for (Job job : jobs) {
if (job instanceof Timer) {
result.add((Timer)job);
}
}
Collections.sort(result, dueDateJobComparatorDesc);
return result;
}
public Timer findNextTimer() {
Timer result = null;
long nextTimerExpirationDate = Long.MAX_VALUE;
for (Job job : jobs) {
if (job.getEligibleDate().getTime()<nextTimerExpirationDate
&& job instanceof Timer) {
result = (Timer)job;
nextTimerExpirationDate =
result.getEligibleDate().getTime();
}
}
return result;
}
}