App Engine Java SDK version 1.9.25
[gae.git] / java / src / main / com / google / apphosting / utils / config / ClientDeployYamlMaker.java
blob171b941fecbb9d88f89e6964e615b0f0c13ea507
1 package com.google.apphosting.utils.config;
3 import static com.google.common.base.Preconditions.checkNotNull;
5 import net.sourceforge.yamlbeans.YamlConfig;
6 import net.sourceforge.yamlbeans.YamlException;
7 import net.sourceforge.yamlbeans.YamlWriter;
9 import java.beans.IntrospectionException;
10 import java.beans.PropertyDescriptor;
11 import java.beans.SimpleBeanInfo;
12 import java.io.StringWriter;
13 import java.util.ArrayList;
15 /**
16 * Utility for making client deploy yaml.
17 * <p>
18 * Keep in sync with apphosting/api/client_deployinfo.py.
20 public class ClientDeployYamlMaker {
21 public static final String UNKNOWN_RUNTIME = "unknown";
23 private String runtime = UNKNOWN_RUNTIME;
24 private final long startTimeUsec;
25 private final ArrayList<Request> requests = new ArrayList<Request>();
26 private final String sdkVersion;
28 /**
29 * Constructs a {@link ClientDeployYamlMaker} for logging an attempted deployment of
30 * a module version to appengine.
32 * @param startTimeUsec The start time of the deployment in micro seconds.
33 * @param sdkVersion The version of the client SDK.
35 public ClientDeployYamlMaker(long startTimeUsec, String sdkVersion) {
36 this.startTimeUsec = startTimeUsec;
37 this.sdkVersion = checkNotNull(sdkVersion, "sdkVersion may not be null");
40 /**
41 * Sets the runtime.
43 public void setRuntime(String runtime) {
44 this.runtime = checkNotNull(runtime, "runtime may not be null");
47 /**
48 * Adds a record of an HTTP request to include in the yaml returned by
49 * {@link #make(long, boolean)}
51 * @param path The path for the request.
52 * @param responseCode The HTTP response code for the request.
53 * @param startTimeUsec The start time for the request in micro seconds.
54 * @param endTimeUsec The end time for the request in micro seconds.
55 * @param requestSizeBytes The size of the payload for the request.
57 public void addRequest(String path, int responseCode, long startTimeUsec, long endTimeUsec,
58 long requestSizeBytes) {
59 requests.add(new Request(checkNotNull(path, "path may not be null"), responseCode,
60 startTimeUsec, endTimeUsec, requestSizeBytes));
63 /**
64 * Returns the runtime.
66 public String getRuntime() {
67 return runtime;
70 /**
71 * Returns the sdk version.
73 public String getSdkVersion() {
74 return sdkVersion;
77 /**
78 * Returns client deploy yaml suitable for logging a deployment attempt for
79 * a single module version.
81 * @param endTimeUsec The time in micro seconds when the deployment completed.
82 * @param success True iff the deployment succeeds
83 * @return A {@link String} with the requested yaml.
84 * @throws YamlException If yaml formatting fails.
86 public String make(long endTimeUsec, boolean success) throws YamlException {
87 StringWriter stringWriter = new StringWriter();
88 YamlConfig yamlConfig = new YamlConfig();
89 yamlConfig.writeConfig.setIndentSize(2);
90 yamlConfig.writeConfig.setWriteRootTags(false);
91 yamlConfig.writeConfig.setWriteRootElementTags(false);
92 yamlConfig.writeConfig.setWriteDefaultValues(true);
93 yamlConfig.setPropertyElementType(ClientDeploy.class, "requests", Request.class);
94 YamlWriter yamlWriter = new YamlWriter(stringWriter, yamlConfig);
95 ClientDeploy clientDeploy = new ClientDeploy(runtime, startTimeUsec, endTimeUsec, requests,
96 success, sdkVersion);
98 yamlWriter.write(clientDeploy);
99 yamlWriter.close();
100 return stringWriter.toString();
104 * A holder class for representing a http request within a client deployment for use with
105 * {@link YamlWriter}.
106 * <p>
107 * Please do not reference {@link ClientDeploy} objects directly. This class is public
108 * to meet the needs of {@link YamlWriter} but should not be used outside its containing class.
110 public static class Request {
111 private String path;
112 private int responseCode;
113 private long startTimeUsec;
114 private long endTimeUsec;
115 private long requestSizeBytes;
117 public Request() {
120 public Request(String path, int responseCode, long startTimeUsec,
121 long endTimeUsec, long requestSizeBytes) {
122 this.path = path;
123 this.responseCode = responseCode;
124 this.startTimeUsec = startTimeUsec;
125 this.endTimeUsec = endTimeUsec;
126 this.requestSizeBytes = requestSizeBytes;
129 public String getPath() {
130 return path;
133 public void setPath(@SuppressWarnings("unused") String path) {
134 throw new UnsupportedOperationException();
137 public int getResponseCode() {
138 return responseCode;
141 public void setResponseCode(@SuppressWarnings("unused") int responseCode) {
142 throw new UnsupportedOperationException();
145 public long getStartTimeUsec() {
146 return startTimeUsec;
149 public void setStartTimeUsec(@SuppressWarnings("unused") long startTimeUsec) {
150 throw new UnsupportedOperationException();
153 public long getEndTimeUsec() {
154 return endTimeUsec;
157 public void setEndTimeUsec(@SuppressWarnings("unused") long endTimeUsec) {
158 throw new UnsupportedOperationException();
161 public long getRequestSizeBytes() {
162 return requestSizeBytes;
165 public void setRequestSizeBytes(@SuppressWarnings("unused") long requestSizeBytes) {
166 throw new UnsupportedOperationException();
171 * BeanInfo to map Request field names to client deploy yaml names which happen to not be
172 * conforming Google java names.
174 public static class RequestBeanInfo extends SimpleBeanInfo {
175 @Override
176 public PropertyDescriptor[] getPropertyDescriptors() {
177 try {
178 return new PropertyDescriptor[] {
179 new PropertyDescriptor("path", Request.class),
180 new PropertyDescriptor("response_code", Request.class, "getResponseCode",
181 "setResponseCode"),
182 new PropertyDescriptor("start_time_usec", Request.class, "getStartTimeUsec",
183 "setStartTimeUsec"),
184 new PropertyDescriptor("end_time_usec", Request.class, "getEndTimeUsec",
185 "setEndTimeUsec"),
186 new PropertyDescriptor("request_size_bytes", Request.class, "getRequestSizeBytes",
187 "setRequestSizeBytes")};
188 } catch (IntrospectionException ex) {
189 throw new RuntimeException(ex);
195 * A holder class for representing a client deployment for use with {@link YamlWriter}.
196 * <p>
197 * Please do not reference {@link ClientDeploy} objects directly. This class is public
198 * to meet the needs of {@link YamlWriter} but should not be used its containing class.
200 public static class ClientDeploy {
202 private String runtime;
203 private long startTimeUsec;
204 private long endTimeUsec;
205 private ArrayList<Request> requests;
206 private boolean success;
207 private String sdkVersion;
209 public ClientDeploy(String runtime, long startTimeUsec, long endTimeUsec,
210 ArrayList<Request> requests, boolean success, String sdkVersion) {
211 this.runtime = runtime;
212 this.startTimeUsec = startTimeUsec;
213 this.endTimeUsec = endTimeUsec;
214 this.requests = requests;
215 this.success = success;
216 this.sdkVersion = sdkVersion;
219 public ClientDeploy() {
222 public String getRuntime() {
223 return runtime;
226 public void setRuntime(@SuppressWarnings("unused") String runtime) {
227 throw new UnsupportedOperationException();
230 public long getStartTimeUsec() {
231 return startTimeUsec;
234 public void setStartTimeUsec(@SuppressWarnings("unused") long startTimeUsec) {
235 throw new UnsupportedOperationException();
238 public long getEndTimeUsec() {
239 return endTimeUsec;
242 public void setEndTimeUsec(@SuppressWarnings("unused") long endTimeUsec) {
243 throw new UnsupportedOperationException();
246 public ArrayList<Request> getRequests() {
247 return requests;
250 public void setRequests(@SuppressWarnings("unused") ArrayList<Request> requests) {
251 throw new UnsupportedOperationException();
254 public boolean isSuccess() {
255 return success;
258 public void setSuccess(@SuppressWarnings("unused") boolean success) {
259 throw new UnsupportedOperationException();
262 public String getSdkVersion() {
263 return sdkVersion;
266 public void setSdkVersion(@SuppressWarnings("unused") String sdkVersion) {
267 throw new UnsupportedOperationException();
272 * BeanInfo to map ClientDeploy field names to client deploy yaml names which happen to not be
273 * conforming Google java names.
275 public static class ClientDeployBeanInfo extends SimpleBeanInfo {
276 @Override
277 public PropertyDescriptor[] getPropertyDescriptors() {
278 try {
279 return new PropertyDescriptor[] {
280 new PropertyDescriptor("runtime", ClientDeploy.class),
281 new PropertyDescriptor("start_time_usec", ClientDeploy.class, "getStartTimeUsec",
282 "setStartTimeUsec"),
283 new PropertyDescriptor("end_time_usec", ClientDeploy.class, "getEndTimeUsec",
284 "setEndTimeUsec"),
285 new PropertyDescriptor("requests", ClientDeploy.class),
286 new PropertyDescriptor("success", ClientDeploy.class),
287 new PropertyDescriptor("sdk_version", ClientDeploy.class, "getSdkVersion",
288 "setSdkVersion")};
289 } catch (IntrospectionException ex) {
290 throw new RuntimeException(ex);