blob: dd81e130df46aa08c4e970a973bd8e258db702b4 [file] [log] [blame]
lryan56e307f2014-12-05 13:25:08 -08001/*
2 * Copyright 2014, Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
nmittlerb687bdc2015-08-31 16:13:39 -070032package io.grpc.internal;
zhangkun8d6d12e2014-10-15 13:04:19 -070033
nmittlerb687bdc2015-08-31 16:13:39 -070034import io.grpc.ClientInterceptor;
35import io.grpc.Internal;
36import io.grpc.ManagedChannelBuilder;
zhangkun8d6d12e2014-10-15 13:04:19 -070037
Eric Anderson0df3d5e2015-06-25 18:00:00 -070038import java.util.ArrayList;
39import java.util.Arrays;
40import java.util.List;
Eric Andersonc3e8dae2015-01-30 14:58:07 -080041import java.util.concurrent.ExecutorService;
zhangkun8d6d12e2014-10-15 13:04:19 -070042
43import javax.annotation.Nullable;
44
45/**
46 * The base class for channel builders.
nathanmittler0304b3d2014-10-24 13:39:13 -070047 *
nmittlerb687bdc2015-08-31 16:13:39 -070048 * @param <T> The concrete type of this builder.
zhangkun8d6d12e2014-10-15 13:04:19 -070049 */
nmittlerb687bdc2015-08-31 16:13:39 -070050public abstract class AbstractManagedChannelImplBuilder
51 <T extends AbstractManagedChannelImplBuilder<T>> extends ManagedChannelBuilder<T> {
Eric Andersonaeeebb72014-12-19 16:41:03 -080052
ejona7235a392015-01-13 13:38:54 -080053 @Nullable
nmittler777e9282015-08-19 10:01:52 -070054 private ExecutorService executor;
Eric Anderson0df3d5e2015-06-25 18:00:00 -070055 private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>();
zhangkun8d6d12e2014-10-15 13:04:19 -070056
nmittler8c1d38a2015-06-01 08:31:00 -070057 @Nullable
58 private String userAgent;
59
Eric Anderson6122daf2015-09-03 12:14:30 -070060 @Override
nmittlerb687bdc2015-08-31 16:13:39 -070061 public final T executor(ExecutorService executor) {
nmittler777e9282015-08-19 10:01:52 -070062 this.executor = executor;
Eric Anderson0df3d5e2015-06-25 18:00:00 -070063 return thisT();
64 }
65
Eric Anderson6122daf2015-09-03 12:14:30 -070066 @Override
nmittlerb687bdc2015-08-31 16:13:39 -070067 public final T intercept(List<ClientInterceptor> interceptors) {
Eric Anderson0df3d5e2015-06-25 18:00:00 -070068 this.interceptors.addAll(interceptors);
69 return thisT();
70 }
71
Eric Anderson6122daf2015-09-03 12:14:30 -070072 @Override
nmittlerb687bdc2015-08-31 16:13:39 -070073 public final T intercept(ClientInterceptor... interceptors) {
Eric Anderson0df3d5e2015-06-25 18:00:00 -070074 return intercept(Arrays.asList(interceptors));
75 }
76
nmittlerb687bdc2015-08-31 16:13:39 -070077 private T thisT() {
Eric Anderson0df3d5e2015-06-25 18:00:00 -070078 @SuppressWarnings("unchecked")
nmittlerb687bdc2015-08-31 16:13:39 -070079 T thisT = (T) this;
Eric Anderson0df3d5e2015-06-25 18:00:00 -070080 return thisT;
ejona7235a392015-01-13 13:38:54 -080081 }
82
Eric Anderson6122daf2015-09-03 12:14:30 -070083 @Override
nmittlerb687bdc2015-08-31 16:13:39 -070084 public final T userAgent(String userAgent) {
nmittler8c1d38a2015-06-01 08:31:00 -070085 this.userAgent = userAgent;
nmittlerb687bdc2015-08-31 16:13:39 -070086 return thisT();
nmittler8c1d38a2015-06-01 08:31:00 -070087 }
88
Eric Anderson6122daf2015-09-03 12:14:30 -070089 @Override
nmittlerb687bdc2015-08-31 16:13:39 -070090 public ManagedChannelImpl build() {
nmittler777e9282015-08-19 10:01:52 -070091 ClientTransportFactory transportFactory = buildTransportFactory();
nmittlerb687bdc2015-08-31 16:13:39 -070092 return new ManagedChannelImpl(transportFactory, executor, userAgent, interceptors);
zhangkun8d6d12e2014-10-15 13:04:19 -070093 }
94
95 /**
nmittler777e9282015-08-19 10:01:52 -070096 * Children of AbstractChannelBuilder should override this method to provide the
97 * {@link ClientTransportFactory} appropriate for this channel. This method is meant for
98 * Transport implementors and should not be used by normal users.
zhangkun8d6d12e2014-10-15 13:04:19 -070099 */
Kun Zhang0eb98622015-08-11 11:06:50 -0700100 @Internal
nmittler777e9282015-08-19 10:01:52 -0700101 protected abstract ClientTransportFactory buildTransportFactory();
zhangkun8d6d12e2014-10-15 13:04:19 -0700102}