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
;
10 import java
.io
.FileNotFoundException
;
11 import java
.io
.FileReader
;
12 import java
.io
.Reader
;
13 import java
.util
.List
;
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
;
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;
42 return parseImpl(new FileReader(getFilename()));
43 } catch (FileNotFoundException ex
) {
49 static DispatchXml
parseImpl(Reader yaml
) {
50 YamlReader reader
= new YamlReader(yaml
);
51 reader
.getConfig().setPropertyElementType(DispatchYaml
.class, "dispatch",
52 DispatchYamlEntry
.class);
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
);
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();
91 * Bean for a parsed single uri to module mapping entry in a
92 * dispatch.yaml file that meets the requirements of
95 public static class DispatchYamlEntry
{
97 private String module
;
99 public String
getUrl() {
103 public void setUrl(String url
) {
107 public String
getModule() {
111 public void setModule(String module
) {
112 this.module
= module
;
115 DispatchEntry
asDispatchEntry() {
116 return new DispatchEntry(url
, module
);