App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / BackendsYamlReader.java
blob8b133f185bbc24cd29b0e9fe1048ee7c433b6266
1 // Copyright 2011 Google. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import com.google.common.base.Joiner;
7 import net.sourceforge.yamlbeans.YamlException;
8 import net.sourceforge.yamlbeans.YamlReader;
10 import java.beans.IntrospectionException;
11 import java.beans.PropertyDescriptor;
12 import java.beans.SimpleBeanInfo;
13 import java.io.File;
14 import java.io.FileNotFoundException;
15 import java.io.FileReader;
16 import java.io.Reader;
17 import java.io.StringReader;
18 import java.util.EnumSet;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.ArrayList;
23 /**
24 * JavaBean representation of the Java backends.yaml file.
27 public class BackendsYamlReader {
29 public static class BackendsYaml {
31 private List<Entry> backends;
33 public List<Entry> getBackends() {
34 return backends;
37 public void setBackends(List<Entry> backends) {
38 this.backends = backends;
41 public BackendsXml toXml() {
42 BackendsXml xml = new BackendsXml();
43 for (Entry backend : backends) {
44 xml.addBackend(backend.toXml());
46 return xml;
49 public static class Entry {
50 private String name;
51 private Integer instances;
52 private String instanceClass;
53 private BackendsXml.State state;
54 private Integer maxConcurrentRequests;
55 private Set<BackendsXml.Option> options = EnumSet.noneOf(BackendsXml.Option.class);
57 public String getName() {
58 return name;
61 public void setName(String name) {
62 this.name = name;
65 public Integer getInstances() {
66 return instances;
69 public void setInstances(Integer instances) {
70 this.instances = instances;
73 public String getInstanceClass() {
74 return instanceClass;
77 public void setInstanceClass(String instanceClass) {
78 this.instanceClass = instanceClass;
81 public Integer getMax_concurrent_requests() {
82 return maxConcurrentRequests;
85 public void setMax_concurrent_requests(Integer maxConcurrentRequests) {
86 this.maxConcurrentRequests = maxConcurrentRequests;
89 public String getOptions() {
90 List<String> optionNames = new ArrayList<String>();
91 for (BackendsXml.Option option : options) {
92 optionNames.add(option.getYamlValue());
94 return Joiner.on(", ").useForNull("null").join(optionNames);
97 public void setOptions(String optionString) {
98 options.clear();
99 for (String optionName : optionString.split(" *, *")) {
100 options.add(BackendsXml.Option.fromYamlValue(optionName));
104 public String getState() {
105 return (state != null) ? state.getYamlValue() : null;
108 public void setState(String state) {
109 this.state = (state != null) ? BackendsXml.State.fromYamlValue(state) : null;
112 public BackendsXml.Entry toXml() {
113 return new BackendsXml.Entry(
114 name,
115 instances,
116 instanceClass,
117 maxConcurrentRequests,
118 options,
119 state);
123 public static class EntryBeanInfo extends SimpleBeanInfo {
124 @Override
125 public PropertyDescriptor[] getPropertyDescriptors() {
126 try {
127 return new PropertyDescriptor[] {
128 new PropertyDescriptor("name", Entry.class),
129 new PropertyDescriptor("instances", Entry.class),
130 new PropertyDescriptor("class", Entry.class, "getInstanceClass", "setInstanceClass"),
131 new PropertyDescriptor("state", Entry.class),
132 new PropertyDescriptor("max_concurrent_requests", Entry.class),
133 new PropertyDescriptor("options", Entry.class),
135 } catch (IntrospectionException ex) {
136 throw new RuntimeException(ex);
142 private static final String FILENAME = "backends.yaml";
143 private String appDir;
145 public BackendsYamlReader(String appDir) {
146 if (appDir.length() > 0 && appDir.charAt(appDir.length() - 1) != File.separatorChar) {
147 appDir += File.separatorChar;
149 this.appDir = appDir;
152 public String getFilename() {
153 return appDir + FILENAME;
156 public BackendsXml parse() {
157 if (new File(getFilename()).exists()) {
158 try {
159 return parse(new FileReader(getFilename()));
160 } catch (FileNotFoundException ex) {
161 throw new AppEngineConfigException("Cannot find file " + getFilename(), ex);
164 return null;
167 public static BackendsXml parse(Reader yaml) {
168 YamlReader reader = new YamlReader(yaml);
169 reader.getConfig().setPropertyElementType(BackendsYaml.class,
170 "backends",
171 BackendsYaml.Entry.class);
173 try {
174 BackendsYaml backendsYaml = reader.read(BackendsYaml.class);
175 if (backendsYaml == null) {
176 throw new AppEngineConfigException("Empty backends configuration.");
178 return backendsYaml.toXml();
179 } catch (YamlException ex) {
180 throw new AppEngineConfigException(ex.getMessage(), ex);
184 public static BackendsXml parse(String yaml) {
185 return parse(new StringReader(yaml));