App Engine Java SDK version 1.9.25
[gae.git] / java / src / main / com / google / apphosting / utils / config / CronXml.java
blob000ca5afdedd0b83f406bd308bdf067c4a92224d
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;
8 import java.util.List;
10 /**
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 {
19 /**
20 * Describes a single cron entry.
23 public static class Entry {
25 private static final String TZ_GMT = "UTC";
27 private String url;
28 private String desc;
29 private String tz;
30 private String schedule;
31 private String target;
32 private RetryParametersXml retryParameters;
34 /** Create an empty cron entry. */
35 public Entry() {
36 desc = "";
37 tz = TZ_GMT;
38 url = null;
39 schedule = null;
40 target = null;
41 retryParameters = null;
44 /** Records the human-readable description of this cron entry. */
45 public void setDescription(String description) {
46 this.desc = description.replace('\n', ' ');
49 /** Records the URL of this cron entry */
50 public void setUrl(String url) {
51 this.url = url.replace('\n', ' ');
54 /**
55 * Records the schedule of this cron entry. May throw
56 * {@link AppEngineConfigException} if the schedule does not parse
57 * correctly.
59 * @param schedule the schedule to save
61 public void setSchedule(String schedule) {
62 schedule = schedule.replace('\n', ' ');
63 this.schedule = schedule;
66 /**
67 * Sets the timezone for this cron entry's schedule. Defaults to "GMT"
68 * @param timezone timezone for the cron entry's {@code schedule}.
70 public void setTimezone(String timezone) {
71 this.tz = timezone.replace('\n', ' ');
74 public void setTarget(String target) {
75 this.target = target;
78 public void setRetryParameters(RetryParametersXml retryParameters) {
79 this.retryParameters = retryParameters;
82 public String getUrl() {
83 return url;
86 public String getDescription() {
87 return desc;
90 public String getSchedule() {
91 return schedule;
94 public String getTimezone() {
95 return tz;
98 public String getTarget() {
99 return target;
102 public RetryParametersXml getRetryParameters() {
103 return retryParameters;
108 private List<Entry> entries;
110 /** Create an empty configuration object. */
111 public CronXml() {
112 entries = new ArrayList<Entry>();
116 * Puts a new entry into the list defined by the config file.
118 * @throws AppEngineConfigException if the previously-last entry is still
119 * incomplete.
120 * @return the new entry
122 public Entry addNewEntry() {
123 validateLastEntry();
124 Entry entry = new Entry();
125 entries.add(entry);
126 return entry;
130 * Puts an entry into the list defined by the config file.
132 * @throws AppEngineConfigException if the entry is still incomplete.
134 public void addEntry(Entry entry) {
135 validateLastEntry();
136 entries.add(entry);
137 validateLastEntry();
141 * Get the entries. Used for testing.
143 public List<Entry> getEntries() {
144 return entries;
148 * Check that the last entry defined is complete.
149 * @throws AppEngineConfigException if it is not.
151 public void validateLastEntry() {
152 if (entries.size() == 0) {
153 return;
155 Entry last = entries.get(entries.size() - 1);
156 if (last.getUrl() == null) {
157 throw new AppEngineConfigException("no URL for cronentry");
159 if (last.getSchedule() == null) {
160 throw new AppEngineConfigException("no schedule for cronentry " + last.getUrl());
162 try {
163 GrocTimeSpecification parsedSchedule =
164 GrocTimeSpecification.create(last.schedule);
165 } catch (IllegalArgumentException iae) {
166 throw new AppEngineConfigException("schedule " + last.schedule + " failed to parse",
167 iae.getCause());
172 * Get the YAML equivalent of this cron.xml file.
174 * @return contents of an equivalent {@code cron.yaml} file.
176 public String toYaml() {
177 validateLastEntry();
178 StringBuilder builder = new StringBuilder("cron:\n");
179 for (Entry ent : entries) {
180 builder.append("- description: '" + ent.getDescription().replace("'", "''") + "'\n");
181 builder.append(" url: " + ent.getUrl() + "\n");
182 builder.append(" schedule: " + ent.getSchedule() + "\n");
183 builder.append(" timezone: " + ent.getTimezone() + "\n");
184 String target = ent.getTarget();
185 if (target != null) {
186 builder.append(" target: " + target + "\n");
188 RetryParametersXml retryParameters = ent.getRetryParameters();
189 if (retryParameters != null) {
190 builder.append(retryParameters.toYaml("job"));
193 return builder.toString();