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 | */ |
19 | package org.jclouds.http; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.util.concurrent.ExecutionException; |
24 | |
25 | import javax.annotation.Resource; |
26 | import javax.inject.Inject; |
27 | |
28 | import org.jclouds.logging.Logger; |
29 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
30 | |
31 | import com.google.common.base.Function; |
32 | import com.google.common.util.concurrent.ListenableFuture; |
33 | |
34 | /** |
35 | * Executor which will invoke and transform the response of an {@code EndpointCommand} into generic |
36 | * type <T>. |
37 | * |
38 | * @see TransformingHttpCommand |
39 | * |
40 | * @author Adrian Cole |
41 | */ |
42 | public class TransformingHttpCommandImpl<T> implements TransformingHttpCommand<T> { |
43 | |
44 | protected final TransformingHttpCommandExecutorService executorService; |
45 | protected final Function<HttpResponse, T> transformer; |
46 | |
47 | protected volatile HttpRequest request; |
48 | protected volatile int failureCount; |
49 | protected volatile int redirectCount; |
50 | protected volatile Exception exception; |
51 | |
52 | @Resource |
53 | protected Logger logger = Logger.NULL; |
54 | |
55 | @Inject |
56 | public TransformingHttpCommandImpl(TransformingHttpCommandExecutorService executorService, HttpRequest request, |
57 | Function<HttpResponse, T> transformer) { |
58 | this.request = checkNotNull(request, "request"); |
59 | this.executorService = checkNotNull(executorService, "executorService"); |
60 | this.transformer = checkNotNull(transformer, "transformer"); |
61 | this.failureCount = 0; |
62 | this.redirectCount = 0; |
63 | } |
64 | |
65 | /** |
66 | * {@inheritDoc} |
67 | */ |
68 | @Override |
69 | public ListenableFuture<T> execute() throws ExecutionException { |
70 | if (exception != null) |
71 | throw new ExecutionException(exception); |
72 | return executorService.submit(this, transformer); |
73 | } |
74 | |
75 | /** |
76 | * {@inheritDoc} |
77 | */ |
78 | @Override |
79 | public int getFailureCount() { |
80 | return failureCount; |
81 | } |
82 | |
83 | /** |
84 | * {@inheritDoc} |
85 | */ |
86 | @Override |
87 | public int incrementFailureCount() { |
88 | return ++failureCount; |
89 | } |
90 | |
91 | /** |
92 | * {@inheritDoc} |
93 | */ |
94 | @Override |
95 | public void setException(Exception exception) { |
96 | this.exception = exception; |
97 | } |
98 | |
99 | /** |
100 | * {@inheritDoc} |
101 | */ |
102 | @Override |
103 | public Exception getException() { |
104 | return exception; |
105 | } |
106 | |
107 | /** |
108 | * {@inheritDoc} |
109 | */ |
110 | @Override |
111 | public int incrementRedirectCount() { |
112 | return ++redirectCount; |
113 | } |
114 | |
115 | /** |
116 | * {@inheritDoc} |
117 | */ |
118 | @Override |
119 | public int getRedirectCount() { |
120 | return redirectCount; |
121 | } |
122 | |
123 | /** |
124 | * {@inheritDoc} |
125 | */ |
126 | @Override |
127 | public boolean isReplayable() { |
128 | return (request.getPayload() == null) ? true : request.getPayload().isRepeatable(); |
129 | } |
130 | |
131 | /** |
132 | * {@inheritDoc} |
133 | */ |
134 | @Override |
135 | public HttpRequest getCurrentRequest() { |
136 | return request; |
137 | } |
138 | |
139 | /** |
140 | * {@inheritDoc} |
141 | */ |
142 | @Override |
143 | public void setCurrentRequest(HttpRequest request) { |
144 | this.request = request; |
145 | } |
146 | |
147 | @Override |
148 | public String toString() { |
149 | if (request instanceof GeneratedHttpRequest<?>) |
150 | return String.format("[method=%s.%s, request=%s]", GeneratedHttpRequest.class.cast(request).getDeclaring() |
151 | .getSimpleName(), GeneratedHttpRequest.class.cast(request).getJavaMethod().getName(), request |
152 | .getRequestLine()); |
153 | else |
154 | return "[request=" + request.getRequestLine() + "]"; |
155 | } |
156 | |
157 | } |