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";
30 private String schedule
;
31 private String target
;
32 private RetryParametersXml retryParameters
;
34 /** Create an empty cron entry. */
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', ' ');
55 * Records the schedule of this cron entry. May throw
56 * {@link AppEngineConfigException} if the schedule does not parse
59 * @param schedule the schedule to save
61 public void setSchedule(String schedule
) {
62 schedule
= schedule
.replace('\n', ' ');
63 this.schedule
= schedule
;
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
) {
78 public void setRetryParameters(RetryParametersXml retryParameters
) {
79 this.retryParameters
= retryParameters
;
82 public String
getUrl() {
86 public String
getDescription() {
90 public String
getSchedule() {
94 public String
getTimezone() {
98 public String
getTarget() {
102 public RetryParametersXml
getRetryParameters() {
103 return retryParameters
;
108 private List
<Entry
> entries
;
110 /** Create an empty configuration object. */
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
120 * @return the new entry
122 public Entry
addNewEntry() {
124 Entry entry
= new 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
) {
141 * Get the entries. Used for testing.
143 public List
<Entry
> getEntries() {
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) {
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());
163 GrocTimeSpecification parsedSchedule
=
164 GrocTimeSpecification
.create(last
.schedule
);
165 } catch (IllegalArgumentException iae
) {
166 throw new AppEngineConfigException("schedule " + last
.schedule
+ " failed to parse",
172 * Get the YAML equivalent of this cron.xml file.
174 * @return contents of an equivalent {@code cron.yaml} file.
176 public String
toYaml() {
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();