Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / appengine / api / log / LogServiceImpl.java
blobfd93012eedac8ef11d020e36097ea1926221366d
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.LogReadRequest;
8 import com.google.apphosting.api.logservice.LogServicePb.LogReadResponse;
9 import com.google.apphosting.api.logservice.LogServicePb.LogServerVersion;
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;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
19 /**
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";
28 @Override
29 public LogQueryResult fetch(LogQuery query) {
30 try {
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();
37 } else {
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<Pair<String, String>> convertedServerVersions = Sets.newHashSet();
71 if (query.getMajorVersionIds().isEmpty() && query.getServerVersions().isEmpty()) {
72 String currentVersionId = ApiProxy.getCurrentEnvironment().getVersionId();
73 String versionId = currentVersionId.split("\\.")[0];
74 convertedServerVersions.add(Pair.of("default", versionId));
77 if (query.getMajorVersionIds().size() > 0) {
78 for (String versionId : query.getMajorVersionIds()) {
79 convertedServerVersions.add(Pair.of("default", versionId));
81 } else if (query.getServerVersions().size() > 0) {
82 convertedServerVersions.addAll(query.getServerVersions());
85 for (Pair<String, String> serverVersion : convertedServerVersions) {
86 LogServerVersion requestServerVersion = request.addServerVersion();
87 if (!serverVersion.first.equals("default")) {
88 requestServerVersion.setServerId(serverVersion.first);
90 requestServerVersion.setVersionId(serverVersion.second);
93 for (String requestId : query.getRequestIds()) {
94 request.addRequestId(requestId);
97 String offset = query.getOffset();
98 if (offset != null) {
99 request.setOffset(LogQueryResult.parseOffset(offset));
102 final LogQuery finalizedQuery = query;
103 ApiProxy.ApiConfig apiConfig = new ApiProxy.ApiConfig();
105 Future<byte[]> responseBytes = ApiProxy.makeAsyncCall(PACKAGE,
106 READ_RPC_NAME, request.toByteArray(), apiConfig);
107 return new FutureWrapper<byte[], LogQueryResult>(responseBytes) {
108 @Override
109 protected LogQueryResult wrap(byte[] responseBytes) {
110 LogReadResponse response = new LogReadResponse();
111 response.mergeFrom(responseBytes);
112 return new LogQueryResult(response, finalizedQuery);
115 @Override
116 protected Throwable convertException(Throwable cause) {
117 if (cause instanceof ApiProxy.ApplicationException) {
118 ApiProxy.ApplicationException e = (ApiProxy.ApplicationException) cause;
119 ErrorCode errorCode = LogServiceError.ErrorCode.valueOf(e.getApplicationError());
120 if (errorCode == LogServiceError.ErrorCode.INVALID_REQUEST) {
121 return new InvalidRequestException(e.getErrorDetail());
123 return new LogServiceException(e.getErrorDetail());
125 return cause;