App Engine Python SDK version 1.8.4
[gae.git] / java / src / main / com / google / apphosting / utils / config / CronYamlReader.java
blobe588b69270e66eea9c04e36a84a6aa1a80bf7e2f
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileNotFoundException;
8 import java.io.Reader;
9 import java.io.StringReader;
10 import java.util.List;
11 import net.sourceforge.yamlbeans.YamlException;
12 import net.sourceforge.yamlbeans.YamlReader;
14 /**
15 * Class to parse queue.yaml into a QueueXml object.
18 public class CronYamlReader {
20 /**
21 * Wrapper around CronXml to make the JavaBeans properties match the YAML file syntax.
23 public static class CronYaml {
24 private List<CronXml.Entry> entries;
26 public List<CronXml.Entry> getCron() {
27 return entries;
30 public void setCron(List<CronXml.Entry> entries) {
31 this.entries = entries;
34 public CronXml toXml() {
35 CronXml xml = new CronXml();
36 if (entries != null) {
37 for (CronXml.Entry entry : entries) {
38 xml.addEntry(entry);
41 return xml;
45 private static final String FILENAME = "cron.yaml";
46 private String appDir;
48 public CronYamlReader(String appDir) {
49 if (appDir.length() > 0 && appDir.charAt(appDir.length() - 1) != File.separatorChar) {
50 appDir += File.separatorChar;
52 this.appDir = appDir;
55 public String getFilename() {
56 return appDir + CronYamlReader.FILENAME;
59 public CronXml parse() {
60 if (new File(getFilename()).exists()) {
61 try {
62 return parse(new FileReader(getFilename()));
63 } catch (FileNotFoundException ex) {
64 throw new AppEngineConfigException("Cannot find file " + getFilename(), ex);
67 return null;
70 public static CronXml parse(Reader yaml) {
71 YamlReader reader = new YamlReader(yaml);
72 reader.getConfig().setPropertyElementType(CronYaml.class,
73 "cron",
74 CronXml.Entry.class);
75 try {
76 CronYaml cronYaml = reader.read(CronYaml.class);
77 if (cronYaml == null) {
78 throw new AppEngineConfigException("Empty cron configuration.");
80 return cronYaml.toXml();
81 } catch (YamlException ex) {
82 throw new AppEngineConfigException(ex.getMessage(), ex);
86 public static CronXml parse(String yaml) {
87 return parse(new StringReader(yaml));