lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 32 | package io.grpc; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 33 | |
Kun Zhang | d2929cd | 2015-08-04 16:32:32 -0700 | [diff] [blame] | 34 | import io.grpc.internal.ClientTransportFactory; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 35 | |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 36 | import java.util.ArrayList; |
| 37 | import java.util.Arrays; |
| 38 | import java.util.List; |
Eric Anderson | c3e8dae | 2015-01-30 14:58:07 -0800 | [diff] [blame] | 39 | import java.util.concurrent.ExecutorService; |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 40 | |
| 41 | import javax.annotation.Nullable; |
| 42 | |
| 43 | /** |
| 44 | * The base class for channel builders. |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame] | 45 | * |
| 46 | * @param <BuilderT> The concrete type of this builder. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 47 | */ |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 48 | public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder<BuilderT>> { |
Eric Anderson | aeeebb7 | 2014-12-19 16:41:03 -0800 | [diff] [blame] | 49 | |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 50 | @Nullable |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame^] | 51 | private ExecutorService executor; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 52 | private final List<ClientInterceptor> interceptors = new ArrayList<ClientInterceptor>(); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 53 | |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 54 | @Nullable |
| 55 | private String userAgent; |
| 56 | |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 57 | /** |
| 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 | */ |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 66 | public final BuilderT executor(ExecutorService executor) { |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame^] | 67 | this.executor = executor; |
Eric Anderson | 0df3d5e | 2015-06-25 18:00:00 -0700 | [diff] [blame] | 68 | 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; |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
nmittler | 8c1d38a | 2015-06-01 08:31:00 -0700 | [diff] [blame] | 97 | * 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 | /** |
ejona | 7235a39 | 2015-01-13 13:38:54 -0800 | [diff] [blame] | 109 | * Builds a channel using the given parameters. |
| 110 | */ |
| 111 | public ChannelImpl build() { |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame^] | 112 | ClientTransportFactory transportFactory = buildTransportFactory(); |
| 113 | return new ChannelImpl(transportFactory, executor, userAgent, interceptors); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame^] | 117 | * 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. |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 120 | */ |
Kun Zhang | 0eb9862 | 2015-08-11 11:06:50 -0700 | [diff] [blame] | 121 | @Internal |
nmittler | 777e928 | 2015-08-19 10:01:52 -0700 | [diff] [blame^] | 122 | protected abstract ClientTransportFactory buildTransportFactory(); |
zhangkun | 8d6d12e | 2014-10-15 13:04:19 -0700 | [diff] [blame] | 123 | } |