App Engine Java SDK version 1.7.0
[gae.git] / java / src / main / com / google / appengine / tools / admin / CronEntryImpl.java
blob7d60c178e66c7400e3c277c70cab6d2fb28775ce
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.appengine.tools.admin;
5 import com.google.cron.GrocTimeSpecification;
7 import java.text.DateFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import java.util.Iterator;
11 import java.util.TimeZone;
13 /**
14 * Implementation of the CronEntry interface used to describe a single cron
15 * task.
18 public class CronEntryImpl implements CronEntry {
20 private final DateFormat formatter;
22 private final String url;
23 private final String description;
24 private final String schedule;
25 private final String tz;
27 public CronEntryImpl(String url, String description, String schedule, String tz) {
28 this.url = url;
29 this.description = description;
30 this.schedule = schedule;
31 this.tz = (tz == null) ? "UTC" : tz;
32 formatter = new SimpleDateFormat("EEE MMM dd, yyyy HH:mm z (Z)");
33 formatter.setTimeZone(TimeZone.getTimeZone(this.tz));
36 @Override
37 public String getDescription() {
38 return description;
41 @Override
42 public Iterator<String> getNextTimesIterator() {
43 return new TimeIterator(new Date(),
44 GrocTimeSpecification.create(schedule, TimeZone.getTimeZone(tz)));
47 /**
48 * Test variant of {@link #getNextTimesIterator()}, with a specified start.
50 Iterator<String> getNextTimesIterator(Date start) {
51 return new TimeIterator(start,
52 GrocTimeSpecification.create(schedule, TimeZone.getTimeZone(tz)));
55 @Override
56 public String getSchedule() {
57 return schedule;
60 public String getTimezone() {
61 return tz;
64 @Override
65 public String getUrl() {
66 return url;
69 @Override
70 public String toXml() {
71 String result =
72 "<cron>\n" +
73 " <url>" + url + "</url>\n" +
74 " <schedule>" + schedule + "</schedule>\n" +
75 " <timezone>" + tz + "</timezone>\n";
76 if (description != null) {
77 result = result + " <description>" + description + "</description>\n";
79 return result + "</cron>";
82 /**
83 * An iterator tracking the last execution time, and the groc time
84 * specification object to compute future execution times.
86 public class TimeIterator implements Iterator<String> {
88 private Date lastRun;
89 private GrocTimeSpecification groc;
91 public TimeIterator(Date seedTime, GrocTimeSpecification groc) {
92 lastRun = seedTime;
93 this.groc = groc;
96 @Override
97 public boolean hasNext() {
98 return true;
101 @Override
102 public String next() {
103 lastRun = groc.getMatch(lastRun);
104 String result = formatter.format(lastRun);
105 return result;
108 @Override
109 public void remove() {
110 throw new UnsupportedOperationException("cron execution times cannot be removed");