App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / DispatchXml.java
blob4f71aa48cbd5f9eee7d8ad9dec4dd16e797071e2
1 package com.google.apphosting.utils.config;
3 import com.google.common.collect.ImmutableList;
5 import java.util.List;
7 /**
8 * Parsed configuration from a dispatch.xml file.
9 */
10 public class DispatchXml {
12 private final List<DispatchEntry> dispatchEntries;
14 public static Builder builder() {
15 return new Builder();
18 private DispatchXml(List<DispatchEntry> dispatchEntries) {
19 this.dispatchEntries = dispatchEntries;
22 /**
23 * Returns the YAML equivalent of this dispatch.xml file.
25 * @return contents of an equivalent {@code dos.yaml} file.
27 public String toYaml() {
28 StringBuilder builder = new StringBuilder("dispatch:\n");
29 for (DispatchEntry entry : dispatchEntries) {
30 builder.append("- url: " + yamlQuote(entry.getUrl()) + "\n");
31 builder.append(" module: " + entry.getModule() + "\n");
33 return builder.toString();
36 /**
37 * Surrounds the provided string with single quotes, escaping any single
38 * quotes in the string by replacing them with ''.
40 private String yamlQuote(String str) {
41 return "'" + str.replace("'", "''") + "'";
44 @Override
45 public int hashCode() {
46 final int prime = 31;
47 int result = 1;
48 result = prime * result + ((dispatchEntries == null) ? 0 : dispatchEntries.hashCode());
49 return result;
52 @Override
53 public boolean equals(Object obj) {
54 if (this == obj) {
55 return true;
57 if (!(obj instanceof DispatchXml)) {
58 return false;
60 DispatchXml other = (DispatchXml) obj;
61 if (dispatchEntries == null) {
62 if (other.dispatchEntries != null) {
63 return false;
65 } else if (!dispatchEntries.equals(other.dispatchEntries)) {
66 return false;
68 return true;
71 /**
72 * Builder for a {@link DispatchXml}.
74 public static class Builder {
75 private final ImmutableList.Builder<DispatchEntry> dispatchEntriesBuilder =
76 ImmutableList.builder();
78 private Builder() {
81 public Builder addDispatchEntry(DispatchEntry entry) {
82 entry.validate();
83 dispatchEntriesBuilder.add(entry);
84 return this;
87 public DispatchXml build() {
88 return new DispatchXml(dispatchEntriesBuilder.build());
92 /**
93 * Describes a single dispatch entry.
95 public static class DispatchEntry {
97 private final String url;
98 private final String module;
100 /** Create an empty blacklist entry. */
101 public DispatchEntry(String url, String module) {
102 this.url = url;
103 this.module = module;
106 public String getUrl() {
107 return url;
110 public String getModule() {
111 return module;
114 @Override
115 public int hashCode() {
116 final int prime = 31;
117 int result = 1;
118 result = prime * result + ((module == null) ? 0 : module.hashCode());
119 result = prime * result + ((url == null) ? 0 : url.hashCode());
120 return result;
123 @Override
124 public boolean equals(Object obj) {
125 if (this == obj) {
126 return true;
128 if (!(obj instanceof DispatchEntry)) {
129 return false;
131 DispatchEntry other = (DispatchEntry) obj;
132 if (module == null) {
133 if (other.module != null) {
134 return false;
136 } else if (!module.equals(other.module)) {
137 return false;
139 if (url == null) {
140 if (other.url != null) {
141 return false;
143 } else if (!url.equals(other.url)) {
144 return false;
146 return true;
149 private void validate() {
150 if (url == null) {
151 throw new AppEngineConfigException("Invalid url in dispatch.xml - " + url);
153 if (module == null) {
154 throw new AppEngineConfigException("Invalid module in dispatch.xml - " + module);