blob: 2d286a303b2589542d24442b735e76940c74b7cb [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
nmittlerf8314582015-01-27 10:25:39 -080032package io.grpc;
zhangkun8d6d12e2014-10-15 13:04:19 -070033
Kun Zhangd2929cd2015-08-04 16:32:32 -070034import io.grpc.internal.ClientTransportFactory;
zhangkun8d6d12e2014-10-15 13:04:19 -070035
Eric Anderson0df3d5e2015-06-25 18:00:00 -070036import java.util.ArrayList;
37import java.util.Arrays;
38import java.util.List;
Eric Andersonc3e8dae2015-01-30 14:58:07 -080039import java.util.concurrent.ExecutorService;
zhangkun8d6d12e2014-10-15 13:04:19 -070040
41import javax.annotation.Nullable;
42
43/**
44 * The base class for channel builders.
nathanmittler0304b3d2014-10-24 13:39:13 -070045 *
46 * @param <BuilderT> The concrete type of this builder.
zhangkun8d6d12e2014-10-15 13:04:19 -070047 */
ejona7235a392015-01-13 13:38:54 -080048public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder<BuilderT>> {
Eric Andersonaeeebb72014-12-19 16:41:03 -080049
ejona7235a392015-01-13 13:38:54 -080050 @Nullable
nmittler777e9282015-08-19 10:01:52 -070051 private ExecutorService executor;
Eric Anderson0df3d5e2015-06-25 18:00:00 -070052 private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>();
zhangkun8d6d12e2014-10-15 13:04:19 -070053
nmittler8c1d38a2015-06-01 08:31:00 -070054 @Nullable
55 private String userAgent;
56
ejona7235a392015-01-13 13:38:54 -080057 /**
58 * Provides a custom executor.
59 *
60 * <p>It's an optional parameter. If the user has not provided an executor when the channel is
61 * built, the builder will use a static cached thread pool.
62 *
63 * <p>The channel won't take ownership of the given executor. It's caller's responsibility to
64 * shut down the executor when it's desired.
65 */
ejona7235a392015-01-13 13:38:54 -080066 public final BuilderT executor(ExecutorService executor) {
nmittler777e9282015-08-19 10:01:52 -070067 this.executor = executor;
Eric Anderson0df3d5e2015-06-25 18:00:00 -070068 return thisT();
69 }
70
71 /**
72 * Adds interceptors that will be called before the channel performs its real work. This is
73 * functionally equivalent to using {@link ClientInterceptors#intercept(Channel, List)}, but while
74 * still having access to the original {@code ChannelImpl}.
75 */
76 public final BuilderT intercept(List<ClientInterceptor> interceptors) {
77 this.interceptors.addAll(interceptors);
78 return thisT();
79 }
80
81 /**
82 * Adds interceptors that will be called before the channel performs its real work. This is
83 * functionally equivalent to using {@link ClientInterceptors#intercept(Channel,
84 * ClientInterceptor...)}, but while still having access to the original {@code ChannelImpl}.
85 */
86 public final BuilderT intercept(ClientInterceptor... interceptors) {
87 return intercept(Arrays.asList(interceptors));
88 }
89
90 private BuilderT thisT() {
91 @SuppressWarnings("unchecked")
92 BuilderT thisT = (BuilderT) this;
93 return thisT;
ejona7235a392015-01-13 13:38:54 -080094 }
95
96 /**
nmittler8c1d38a2015-06-01 08:31:00 -070097 * Provides a custom {@code User-Agent} for the application.
98 *
99 * <p>It's an optional parameter. If provided, the given agent will be prepended by the
100 * grpc {@code User-Agent}.
101 */
102 @SuppressWarnings("unchecked")
103 public final BuilderT userAgent(String userAgent) {
104 this.userAgent = userAgent;
105 return (BuilderT) this;
106 }
107
108 /**
ejona7235a392015-01-13 13:38:54 -0800109 * Builds a channel using the given parameters.
110 */
111 public ChannelImpl build() {
nmittler777e9282015-08-19 10:01:52 -0700112 ClientTransportFactory transportFactory = buildTransportFactory();
113 return new ChannelImpl(transportFactory, executor, userAgent, interceptors);
zhangkun8d6d12e2014-10-15 13:04:19 -0700114 }
115
116 /**
nmittler777e9282015-08-19 10:01:52 -0700117 * Children of AbstractChannelBuilder should override this method to provide the
118 * {@link ClientTransportFactory} appropriate for this channel. This method is meant for
119 * Transport implementors and should not be used by normal users.
zhangkun8d6d12e2014-10-15 13:04:19 -0700120 */
Kun Zhang0eb98622015-08-11 11:06:50 -0700121 @Internal
nmittler777e9282015-08-19 10:01:52 -0700122 protected abstract ClientTransportFactory buildTransportFactory();
zhangkun8d6d12e2014-10-15 13:04:19 -0700123}