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
;
16 * Utility for making client deploy yaml.
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
;
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");
43 public void setRuntime(String runtime
) {
44 this.runtime
= checkNotNull(runtime
, "runtime may not be null");
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
));
64 * Returns the runtime.
66 public String
getRuntime() {
71 * Returns the sdk version.
73 public String
getSdkVersion() {
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
,
98 yamlWriter
.write(clientDeploy
);
100 return stringWriter
.toString();
104 * A holder class for representing a http request within a client deployment for use with
105 * {@link YamlWriter}.
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
{
112 private int responseCode
;
113 private long startTimeUsec
;
114 private long endTimeUsec
;
115 private long requestSizeBytes
;
120 public Request(String path
, int responseCode
, long startTimeUsec
,
121 long endTimeUsec
, long requestSizeBytes
) {
123 this.responseCode
= responseCode
;
124 this.startTimeUsec
= startTimeUsec
;
125 this.endTimeUsec
= endTimeUsec
;
126 this.requestSizeBytes
= requestSizeBytes
;
129 public String
getPath() {
133 public void setPath(@SuppressWarnings("unused") String path
) {
134 throw new UnsupportedOperationException();
137 public int getResponseCode() {
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() {
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
{
176 public PropertyDescriptor
[] getPropertyDescriptors() {
178 return new PropertyDescriptor
[] {
179 new PropertyDescriptor("path", Request
.class),
180 new PropertyDescriptor("response_code", Request
.class, "getResponseCode",
182 new PropertyDescriptor("start_time_usec", Request
.class, "getStartTimeUsec",
184 new PropertyDescriptor("end_time_usec", Request
.class, "getEndTimeUsec",
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}.
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() {
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() {
242 public void setEndTimeUsec(@SuppressWarnings("unused") long endTimeUsec
) {
243 throw new UnsupportedOperationException();
246 public ArrayList
<Request
> getRequests() {
250 public void setRequests(@SuppressWarnings("unused") ArrayList
<Request
> requests
) {
251 throw new UnsupportedOperationException();
254 public boolean isSuccess() {
258 public void setSuccess(@SuppressWarnings("unused") boolean success
) {
259 throw new UnsupportedOperationException();
262 public String
getSdkVersion() {
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
{
277 public PropertyDescriptor
[] getPropertyDescriptors() {
279 return new PropertyDescriptor
[] {
280 new PropertyDescriptor("runtime", ClientDeploy
.class),
281 new PropertyDescriptor("start_time_usec", ClientDeploy
.class, "getStartTimeUsec",
283 new PropertyDescriptor("end_time_usec", ClientDeploy
.class, "getEndTimeUsec",
285 new PropertyDescriptor("requests", ClientDeploy
.class),
286 new PropertyDescriptor("success", ClientDeploy
.class),
287 new PropertyDescriptor("sdk_version", ClientDeploy
.class, "getSdkVersion",
289 } catch (IntrospectionException ex
) {
290 throw new RuntimeException(ex
);