App Engine Python SDK version 1.8.4
[gae.git] / java / src / main / com / google / apphosting / utils / config / DispatchYamlReader.java
blob7a696827698f63f8b1af7ab4627bb31c83b09fd4
1 package com.google.apphosting.utils.config;
3 import com.google.apphosting.utils.config.DispatchXml.DispatchEntry;
4 import com.google.common.annotations.VisibleForTesting;
6 import net.sourceforge.yamlbeans.YamlException;
7 import net.sourceforge.yamlbeans.YamlReader;
9 import java.io.File;
10 import java.io.FileNotFoundException;
11 import java.io.FileReader;
12 import java.io.Reader;
13 import java.util.List;
15 /**
16 * Class to parse dispatch.yaml into a {@link DispatchXml}.
19 public class DispatchYamlReader {
20 private static final String DISPATCH_FILENAME = "dispatch.yaml";
21 private final String parentDirectory;
23 /**
24 * Constructs a {@link DispatchYamlReader}.
25 * @param parentDirectory the directory containing the dispatch.yaml file.
27 public DispatchYamlReader(String parentDirectory) {
28 if (parentDirectory.length() > 0
29 && parentDirectory.charAt(parentDirectory.length() - 1) != File.separatorChar) {
30 parentDirectory += File.separatorChar;
32 this.parentDirectory = parentDirectory;
35 public String getFilename() {
36 return parentDirectory + DISPATCH_FILENAME;
39 public DispatchXml parse() {
40 DispatchXml result = null;
41 try {
42 return parseImpl(new FileReader(getFilename()));
43 } catch (FileNotFoundException ex) {
45 return null;
48 @VisibleForTesting
49 static DispatchXml parseImpl(Reader yaml) {
50 YamlReader reader = new YamlReader(yaml);
51 reader.getConfig().setPropertyElementType(DispatchYaml.class, "dispatch",
52 DispatchYamlEntry.class);
53 try {
54 DispatchYaml dispatchYaml = reader.read(DispatchYaml.class);
55 if (dispatchYaml == null) {
56 throw new AppEngineConfigException("Empty dispatch.yaml configuration.");
58 return dispatchYaml.toXml();
59 } catch (YamlException ex) {
60 throw new AppEngineConfigException(ex.getMessage(), ex);
64 /**
65 * Top level bean for a parsed dispatch.yaml file that meets the
66 * requirements of {@link YamlReader}.
68 public static class DispatchYaml {
69 private List<DispatchYamlEntry> dispatchEntries;
71 public List<DispatchYamlEntry> getDispatch() {
72 return dispatchEntries;
75 public void setDispatch(List<DispatchYamlEntry> entries) {
76 this.dispatchEntries = entries;
79 public DispatchXml toXml() {
80 DispatchXml.Builder builder = DispatchXml.builder();
81 if (dispatchEntries != null) {
82 for (DispatchYamlEntry entry : dispatchEntries) {
83 builder.addDispatchEntry(entry.asDispatchEntry());
86 return builder.build();
90 /**
91 * Bean for a parsed single uri to module mapping entry in a
92 * dispatch.yaml file that meets the requirements of
93 * {@link YamlReader}.
95 public static class DispatchYamlEntry {
96 private String url;
97 private String module;
99 public String getUrl() {
100 return url;
103 public void setUrl(String url) {
104 this.url = url;
107 public String getModule() {
108 return module;
111 public void setModule(String module) {
112 this.module = module;
115 DispatchEntry asDispatchEntry() {
116 return new DispatchEntry(url, module);