1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
5 import com
.google
.cron
.GrocTimeSpecification
;
7 import java
.util
.ArrayList
;
11 * Parsed cron.xml file.
13 * Any additions to this class should also be made to the YAML
14 * version in CronYamlReader.java.
17 public class CronXml
{
20 * Describes a single cron entry.
23 public static class Entry
{
25 private static final String TZ_GMT
= "UTC";
33 /** Create an empty cron entry. */
42 /** Records the human-readable description of this cron entry. */
43 public void setDescription(String description
) {
44 this.desc
= description
.replace('\n', ' ');
47 /** Records the URL of this cron entry */
48 public void setUrl(String url
) {
49 this.url
= url
.replace('\n', ' ');
53 * Records the schedule of this cron entry. May throw
54 * {@link AppEngineConfigException} if the schedule does not parse
57 * @param schedule the schedule to save
59 public void setSchedule(String schedule
) {
60 schedule
= schedule
.replace('\n', ' ');
61 this.schedule
= schedule
;
65 * Sets the timezone for this cron entry's schedule. Defaults to "GMT"
66 * @param timezone timezone for the cron entry's {@code schedule}.
68 public void setTimezone(String timezone
) {
69 this.tz
= timezone
.replace('\n', ' ');
72 public void setTarget(String target
) {
76 public String
getUrl() {
80 public String
getDescription() {
84 public String
getSchedule() {
88 public String
getTimezone() {
92 public String
getTarget() {
98 private List
<Entry
> entries
;
100 /** Create an empty configuration object. */
102 entries
= new ArrayList
<Entry
>();
106 * Puts a new entry into the list defined by the config file.
108 * @throws AppEngineConfigException if the previously-last entry is still
110 * @return the new entry
112 public Entry
addNewEntry() {
114 Entry entry
= new Entry();
120 * Puts an entry into the list defined by the config file.
122 * @throws AppEngineConfigException if the entry is still incomplete.
124 public void addEntry(Entry entry
) {
131 * Get the entries. Used for testing.
133 public List
<Entry
> getEntries() {
138 * Check that the last entry defined is complete.
139 * @throws AppEngineConfigException if it is not.
141 public void validateLastEntry() {
142 if (entries
.size() == 0) {
145 Entry last
= entries
.get(entries
.size() - 1);
146 if (last
.getUrl() == null) {
147 throw new AppEngineConfigException("no URL for cronentry");
149 if (last
.getSchedule() == null) {
150 throw new AppEngineConfigException("no schedule for cronentry " + last
.getUrl());
153 GrocTimeSpecification parsedSchedule
=
154 GrocTimeSpecification
.create(last
.schedule
);
155 } catch (IllegalArgumentException iae
) {
156 throw new AppEngineConfigException("schedule " + last
.schedule
+ " failed to parse",
162 * Get the YAML equivalent of this cron.xml file.
164 * @return contents of an equivalent {@code cron.yaml} file.
166 public String
toYaml() {
167 StringBuilder builder
= new StringBuilder("cron:\n");
168 for (Entry ent
: entries
) {
169 builder
.append("- description: '" + ent
.getDescription().replace("'", "''") + "'\n");
170 builder
.append(" url: " + ent
.getUrl() + "\n");
171 builder
.append(" schedule: " + ent
.getSchedule() + "\n");
172 builder
.append(" timezone: " + ent
.getTimezone() + "\n");
173 String target
= ent
.getTarget();
174 if (target
!= null) {
175 builder
.append(" target: " + target
+ "\n");
178 return builder
.toString();