1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com
.google
.appengine
.api
.log
;
5 import com
.google
.appengine
.api
.utils
.FutureWrapper
;
6 import com
.google
.apphosting
.api
.ApiProxy
;
7 import com
.google
.apphosting
.api
.logservice
.LogServicePb
.LogModuleVersion
;
8 import com
.google
.apphosting
.api
.logservice
.LogServicePb
.LogReadRequest
;
9 import com
.google
.apphosting
.api
.logservice
.LogServicePb
.LogReadResponse
;
10 import com
.google
.apphosting
.api
.logservice
.LogServicePb
.LogServiceError
;
11 import com
.google
.apphosting
.api
.logservice
.LogServicePb
.LogServiceError
.ErrorCode
;
12 import com
.google
.common
.collect
.Sets
;
15 import java
.util
.concurrent
.ExecutionException
;
16 import java
.util
.concurrent
.Future
;
19 * {@code LogServiceImpl} is an implementation of {@link LogService}
20 * that makes API calls to {@link ApiProxy}.
23 final class LogServiceImpl
implements LogService
{
24 static final String PACKAGE
= "logservice";
25 static final String READ_RPC_NAME
= "Read";
28 public LogQueryResult
fetch(LogQuery query
) {
30 return fetchAsync(query
).get();
31 } catch (ExecutionException e
) {
32 if (e
.getCause() instanceof LogServiceException
) {
33 throw (LogServiceException
) e
.getCause();
34 } else if (e
.getCause() instanceof InvalidRequestException
) {
35 throw (InvalidRequestException
) e
.getCause();
37 throw new LogServiceException(e
.getMessage());
39 } catch (InterruptedException e
) {
40 throw new LogServiceException(e
.getMessage());
44 Future
<LogQueryResult
> fetchAsync(LogQuery query
) {
45 LogReadRequest request
= new LogReadRequest();
47 request
.setAppId(ApiProxy
.getCurrentEnvironment().getAppId());
49 Long startTimeUs
= query
.getStartTimeUsec();
50 if (startTimeUs
!= null) {
51 request
.setStartTime(startTimeUs
);
54 Long endTimeUs
= query
.getEndTimeUsec();
55 if (endTimeUs
!= null) {
56 request
.setEndTime(endTimeUs
);
59 request
.setCount(query
.getBatchSize());
61 if (query
.getMinLogLevel() != null) {
62 request
.setMinimumLogLevel(query
.getMinLogLevel().ordinal());
65 request
.setIncludeIncomplete(query
.getIncludeIncomplete());
66 request
.setIncludeAppLogs(query
.getIncludeAppLogs());
68 Set
<LogQuery
.Version
> convertedModuleInfos
=
69 Sets
.newTreeSet(LogQuery
.VERSION_COMPARATOR
);
71 if (!query
.getMajorVersionIds().isEmpty()) {
72 for (String versionId
: query
.getMajorVersionIds()) {
73 convertedModuleInfos
.add(new LogQuery
.Version("default", versionId
));
75 } else if (!query
.getVersions().isEmpty()) {
76 convertedModuleInfos
.addAll(query
.getVersions());
78 String currentVersionId
= ApiProxy
.getCurrentEnvironment().getVersionId();
79 String versionId
= currentVersionId
.split("\\.")[0];
80 convertedModuleInfos
.add(new LogQuery
.Version(
81 ApiProxy
.getCurrentEnvironment().getModuleId(), versionId
));
84 for (LogQuery
.Version moduleInfo
: convertedModuleInfos
) {
85 LogModuleVersion requestModuleVersion
= request
.addModuleVersion();
86 if (!moduleInfo
.getModuleId().equals("default")) {
87 requestModuleVersion
.setModuleId(moduleInfo
.getModuleId());
89 requestModuleVersion
.setVersionId(moduleInfo
.getVersionId());
92 for (String requestId
: query
.getRequestIds()) {
93 request
.addRequestId(requestId
);
96 String offset
= query
.getOffset();
98 request
.setOffset(LogQueryResult
.parseOffset(offset
));
101 final LogQuery finalizedQuery
= query
;
102 ApiProxy
.ApiConfig apiConfig
= new ApiProxy
.ApiConfig();
104 Future
<byte[]> responseBytes
= ApiProxy
.makeAsyncCall(PACKAGE
,
105 READ_RPC_NAME
, request
.toByteArray(), apiConfig
);
106 return new FutureWrapper
<byte[], LogQueryResult
>(responseBytes
) {
108 protected LogQueryResult
wrap(byte[] responseBytes
) {
109 LogReadResponse response
= new LogReadResponse();
110 response
.mergeFrom(responseBytes
);
111 return new LogQueryResult(response
, finalizedQuery
);
115 protected Throwable
convertException(Throwable cause
) {
116 if (cause
instanceof ApiProxy
.ApplicationException
) {
117 ApiProxy
.ApplicationException e
= (ApiProxy
.ApplicationException
) cause
;
118 ErrorCode errorCode
= LogServiceError
.ErrorCode
.valueOf(e
.getApplicationError());
119 if (errorCode
== LogServiceError
.ErrorCode
.INVALID_REQUEST
) {
120 return new InvalidRequestException(e
.getErrorDetail());
122 return new LogServiceException(e
.getErrorDetail());