App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / CronXml.java
blob2cc11c2de128ca830bdc9d28cae19df732756893
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 String url;
28 String desc;
29 String tz;
30 String schedule;
31 String target;
33 /** Create an empty cron entry. */
34 public Entry() {
35 desc = "";
36 tz = TZ_GMT;
37 url = null;
38 schedule = null;
39 target = null;
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', ' ');
52 /**
53 * Records the schedule of this cron entry. May throw
54 * {@link AppEngineConfigException} if the schedule does not parse
55 * correctly.
57 * @param schedule the schedule to save
59 public void setSchedule(String schedule) {
60 schedule = schedule.replace('\n', ' ');
61 this.schedule = schedule;
64 /**
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) {
73 this.target = target;
76 public String getUrl() {
77 return url;
80 public String getDescription() {
81 return desc;
84 public String getSchedule() {
85 return schedule;
88 public String getTimezone() {
89 return tz;
92 public String getTarget() {
93 return target;
98 private List<Entry> entries;
100 /** Create an empty configuration object. */
101 public CronXml() {
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
109 * incomplete.
110 * @return the new entry
112 public Entry addNewEntry() {
113 validateLastEntry();
114 Entry entry = new Entry();
115 entries.add(entry);
116 return 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) {
125 validateLastEntry();
126 entries.add(entry);
127 validateLastEntry();
131 * Get the entries. Used for testing.
133 public List<Entry> getEntries() {
134 return entries;
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) {
143 return;
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());
152 try {
153 GrocTimeSpecification parsedSchedule =
154 GrocTimeSpecification.create(last.schedule);
155 } catch (IllegalArgumentException iae) {
156 throw new AppEngineConfigException("schedule " + last.schedule + " failed to parse",
157 iae.getCause());
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();