EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.gae]

COVERAGE SUMMARY FOR SOURCE FILE [AsyncGaeHttpCommandExecutorService.java]

nameclass, %method, %block, %line, %
AsyncGaeHttpCommandExecutorService.java0%   (0/2)0%   (0/9)0%   (0/361)0%   (0/51)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class AsyncGaeHttpCommandExecutorService0%   (0/1)0%   (0/6)0%   (0/131)0%   (0/25)
AsyncGaeHttpCommandExecutorService (ExecutorService, URLFetchService, Convert... 0%   (0/1)0%   (0/36)0%   (0/13)
access$000 (AsyncGaeHttpCommandExecutorService): IOExceptionRetryHandler 0%   (0/1)0%   (0/3)0%   (0/1)
access$100 (AsyncGaeHttpCommandExecutorService): DelegatingRetryHandler 0%   (0/1)0%   (0/3)0%   (0/1)
access$200 (AsyncGaeHttpCommandExecutorService): DelegatingErrorHandler 0%   (0/1)0%   (0/3)0%   (0/1)
filterLogAndConvertRe (HttpRequest): HTTPRequest 0%   (0/1)0%   (0/61)0%   (0/8)
submit (HttpCommand): ListenableFuture 0%   (0/1)0%   (0/25)0%   (0/3)
     
class AsyncGaeHttpCommandExecutorService$10%   (0/1)0%   (0/3)0%   (0/230)0%   (0/27)
AsyncGaeHttpCommandExecutorService$1 (AsyncGaeHttpCommandExecutorService, Htt... 0%   (0/1)0%   (0/9)0%   (0/1)
apply (HttpResponse): HttpResponse 0%   (0/1)0%   (0/199)0%   (0/21)
shouldContinue (HttpResponse): boolean 0%   (0/1)0%   (0/22)0%   (0/5)

1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.gae;
20 
21import static com.google.common.base.Throwables.propagate;
22import static org.jclouds.http.HttpUtils.checkRequestHasContentLengthOrChunkedEncoding;
23import static org.jclouds.http.HttpUtils.wirePayloadIfEnabled;
24 
25import java.io.IOException;
26import java.util.concurrent.ExecutorService;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Named;
31import javax.inject.Singleton;
32 
33import org.jclouds.Constants;
34import org.jclouds.concurrent.Futures;
35import org.jclouds.concurrent.SingleThreaded;
36import org.jclouds.http.HttpCommand;
37import org.jclouds.http.HttpCommandExecutorService;
38import org.jclouds.http.HttpRequest;
39import org.jclouds.http.HttpRequestFilter;
40import org.jclouds.http.HttpResponse;
41import org.jclouds.http.HttpResponseException;
42import org.jclouds.http.HttpUtils;
43import org.jclouds.http.IOExceptionRetryHandler;
44import org.jclouds.http.handlers.DelegatingErrorHandler;
45import org.jclouds.http.handlers.DelegatingRetryHandler;
46import org.jclouds.http.internal.HttpWire;
47import org.jclouds.logging.Logger;
48import org.jclouds.util.Throwables2;
49 
50import com.google.appengine.api.urlfetch.HTTPRequest;
51import com.google.appengine.api.urlfetch.URLFetchService;
52import com.google.common.base.Function;
53import com.google.common.util.concurrent.ListenableFuture;
54 
55/**
56 * Google App Engine version of {@link HttpCommandExecutorService} using their fetchAsync call
57 * 
58 * @author Adrian Cole
59 */
60@SingleThreaded
61@Singleton
62public class AsyncGaeHttpCommandExecutorService implements HttpCommandExecutorService {
63   private final ExecutorService service;
64   private final URLFetchService urlFetchService;
65   private final ConvertToGaeRequest convertToGaeRequest;
66   private final ConvertToJcloudsResponse convertToJcloudsResponse;
67   private final DelegatingRetryHandler retryHandler;
68   private final IOExceptionRetryHandler ioRetryHandler;
69   private final DelegatingErrorHandler errorHandler;
70 
71   @Resource
72   protected Logger logger = Logger.NULL;
73   @Resource
74   @Named(Constants.LOGGER_HTTP_HEADERS)
75   protected Logger headerLog = Logger.NULL;
76   protected final HttpWire wire;
77   protected final HttpUtils utils;
78 
79   @Inject
80   public AsyncGaeHttpCommandExecutorService(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService service,
81         URLFetchService urlFetchService, ConvertToGaeRequest convertToGaeRequest,
82         ConvertToJcloudsResponse convertToJcloudsResponse, DelegatingRetryHandler retryHandler,
83         IOExceptionRetryHandler ioRetryHandler, DelegatingErrorHandler errorHandler, HttpUtils utils, HttpWire wire) {
84      this.service = service;
85      this.urlFetchService = urlFetchService;
86      this.convertToGaeRequest = convertToGaeRequest;
87      this.convertToJcloudsResponse = convertToJcloudsResponse;
88      this.retryHandler = retryHandler;
89      this.ioRetryHandler = ioRetryHandler;
90      this.errorHandler = errorHandler;
91      this.utils = utils;
92      this.wire = wire;
93   }
94 
95   public HTTPRequest filterLogAndConvertRe(HttpRequest request) {
96 
97      for (HttpRequestFilter filter : request.getFilters()) {
98         request = filter.filter(request);
99      }
100      checkRequestHasContentLengthOrChunkedEncoding(request,
101            "After filtering, the request has niether chunked encoding nor content length: " + request);
102      logger.debug("Sending request %s: %s", request.hashCode(), request.getRequestLine());
103      wirePayloadIfEnabled(wire, request);
104      HTTPRequest nativeRequest = convertToGaeRequest.apply(request);
105      utils.logRequest(headerLog, request, ">>");
106      return nativeRequest;
107   }
108 
109   @Override
110   public ListenableFuture<HttpResponse> submit(final HttpCommand command) {
111 
112      HTTPRequest nativeRequest = filterLogAndConvertRe(command.getCurrentRequest());
113 
114      ListenableFuture<HttpResponse> response = Futures.compose(urlFetchService.fetchAsync(nativeRequest),
115            convertToJcloudsResponse, service);
116 
117      return Futures.compose(response, new Function<HttpResponse, HttpResponse>() {
118 
119         @Override
120         public HttpResponse apply(HttpResponse response) {
121            try {
122               logger.debug("Receiving response %s: %s", command.getCurrentRequest().hashCode(),
123                     response.getStatusLine());
124               utils.logResponse(headerLog, response, "<<");
125               if (response.getPayload() != null && wire.enabled())
126                  wire.input(response);
127               int statusCode = response.getStatusCode();
128               if (statusCode >= 300) {
129                  if (shouldContinue(response))
130                     return submit(command).get();
131                  else
132                     return response;
133               }
134               return response;
135            } catch (Exception e) {
136               IOException ioe = Throwables2.getFirstThrowableOfType(e, IOException.class);
137               if (ioe != null && ioRetryHandler.shouldRetryRequest(command, ioe)) {
138                  try {
139                     return submit(command).get();
140                  } catch (Exception e1) {
141                     command.setException(e1);
142                     return response;
143                  }
144               } else {
145                  command.setException(new HttpResponseException(e.getMessage() + " connecting to "
146                        + command.getCurrentRequest().getRequestLine(), command, null, e));
147                  return response;
148               }
149            } finally {
150               if (command.getException() != null)
151                  propagate(command.getException());
152            }
153         }
154 
155         private boolean shouldContinue(HttpResponse response) {
156            boolean shouldContinue = false;
157            if (retryHandler.shouldRetryRequest(command, response)) {
158               shouldContinue = true;
159            } else {
160               errorHandler.handleError(command, response);
161            }
162            return shouldContinue;
163         }
164      }, service);
165 
166   }
167 
168}

[all classes][org.jclouds.gae]
EMMA 2.0.5312 (C) Vladimir Roubtsov