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
.base
.Pair
;
13 import com
.google
.common
.collect
.Sets
;
16 import java
.util
.concurrent
.ExecutionException
;
17 import java
.util
.concurrent
.Future
;
20 * {@code LogServiceImpl} is an implementation of {@link LogService}
21 * that makes API calls to {@link ApiProxy}.
24 final class LogServiceImpl
implements LogService
{
25 static final String PACKAGE
= "logservice";
26 static final String READ_RPC_NAME
= "Read";
29 public LogQueryResult
fetch(LogQuery query
) {
31 return fetchAsync(query
).get();
32 } catch (ExecutionException e
) {
33 if (e
.getCause() instanceof LogServiceException
) {
34 throw (LogServiceException
) e
.getCause();
35 } else if (e
.getCause() instanceof InvalidRequestException
) {
36 throw (InvalidRequestException
) e
.getCause();
38 throw new LogServiceException(e
.getMessage());
40 } catch (InterruptedException e
) {
41 throw new LogServiceException(e
.getMessage());
45 Future
<LogQueryResult
> fetchAsync(LogQuery query
) {
46 LogReadRequest request
= new LogReadRequest();
48 request
.setAppId(ApiProxy
.getCurrentEnvironment().getAppId());
50 Long startTimeUs
= query
.getStartTimeUsec();
51 if (startTimeUs
!= null) {
52 request
.setStartTime(startTimeUs
);
55 Long endTimeUs
= query
.getEndTimeUsec();
56 if (endTimeUs
!= null) {
57 request
.setEndTime(endTimeUs
);
60 request
.setCount(query
.getBatchSize());
62 if (query
.getMinLogLevel() != null) {
63 request
.setMinimumLogLevel(query
.getMinLogLevel().ordinal());
66 request
.setIncludeIncomplete(query
.getIncludeIncomplete());
67 request
.setIncludeAppLogs(query
.getIncludeAppLogs());
69 Set
<LogQuery
.Version
> convertedModuleInfos
=
70 Sets
.newTreeSet(LogQuery
.VERSION_COMPARATOR
);
72 if (!query
.getMajorVersionIds().isEmpty()) {
73 for (String versionId
: query
.getMajorVersionIds()) {
74 convertedModuleInfos
.add(new LogQuery
.Version("default", versionId
));
76 } else if (!query
.getModuleVersions().isEmpty()) {
77 for (Pair
<String
, String
> moduleAndVersionPair
: query
.getModuleVersions()) {
78 convertedModuleInfos
.add(new LogQuery
.Version(
79 moduleAndVersionPair
.getFirst(),
80 moduleAndVersionPair
.getSecond()));
82 } else if (!query
.getVersions().isEmpty()) {
83 convertedModuleInfos
.addAll(query
.getVersions());
85 String currentVersionId
= ApiProxy
.getCurrentEnvironment().getVersionId();
86 String versionId
= currentVersionId
.split("\\.")[0];
87 convertedModuleInfos
.add(new LogQuery
.Version(
88 ApiProxy
.getCurrentEnvironment().getModuleId(), versionId
));
91 for (LogQuery
.Version moduleInfo
: convertedModuleInfos
) {
92 LogModuleVersion requestModuleVersion
= request
.addModuleVersion();
93 if (!moduleInfo
.getModuleId().equals("default")) {
94 requestModuleVersion
.setModuleId(moduleInfo
.getModuleId());
96 requestModuleVersion
.setVersionId(moduleInfo
.getVersionId());
99 for (String requestId
: query
.getRequestIds()) {
100 request
.addRequestId(requestId
);
103 String offset
= query
.getOffset();
104 if (offset
!= null) {
105 request
.setOffset(LogQueryResult
.parseOffset(offset
));
108 final LogQuery finalizedQuery
= query
;
109 ApiProxy
.ApiConfig apiConfig
= new ApiProxy
.ApiConfig();
111 Future
<byte[]> responseBytes
= ApiProxy
.makeAsyncCall(PACKAGE
,
112 READ_RPC_NAME
, request
.toByteArray(), apiConfig
);
113 return new FutureWrapper
<byte[], LogQueryResult
>(responseBytes
) {
115 protected LogQueryResult
wrap(byte[] responseBytes
) {
116 LogReadResponse response
= new LogReadResponse();
117 response
.mergeFrom(responseBytes
);
118 return new LogQueryResult(response
, finalizedQuery
);
122 protected Throwable
convertException(Throwable cause
) {
123 if (cause
instanceof ApiProxy
.ApplicationException
) {
124 ApiProxy
.ApplicationException e
= (ApiProxy
.ApplicationException
) cause
;
125 ErrorCode errorCode
= LogServiceError
.ErrorCode
.valueOf(e
.getApplicationError());
126 if (errorCode
== LogServiceError
.ErrorCode
.INVALID_REQUEST
) {
127 return new InvalidRequestException(e
.getErrorDetail());
129 return new LogServiceException(e
.getErrorDetail());