|
| 1 | +// Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | + |
| 15 | + |
| 16 | +package com.rabbitmq.client; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.channels.SocketChannel; |
| 20 | + |
| 21 | +/** |
| 22 | + * Ready-to-use instances and builder for {@link SocketChannelConfigurator}. |
| 23 | + * <p> |
| 24 | + * Note {@link SocketChannelConfigurator}s can be combined with |
| 25 | + * {@link AbstractSocketChannelConfigurator#andThen(SocketChannelConfigurator)}. |
| 26 | + * |
| 27 | + * @since 4.8.0 |
| 28 | + */ |
| 29 | +public abstract class SocketChannelConfigurators { |
| 30 | + |
| 31 | + /** |
| 32 | + * Disable Nagle's algorithm. |
| 33 | + */ |
| 34 | + public static final AbstractSocketChannelConfigurator DISABLE_NAGLE_ALGORITHM = |
| 35 | + new AbstractSocketChannelConfigurator() { |
| 36 | + |
| 37 | + @Override |
| 38 | + public void configure(SocketChannel socketChannel) throws IOException { |
| 39 | + SocketConfigurators.DISABLE_NAGLE_ALGORITHM.configure(socketChannel.socket()); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + /** |
| 44 | + * Default {@link SocketChannelConfigurator} that disables Nagle's algorithm. |
| 45 | + */ |
| 46 | + public static final AbstractSocketChannelConfigurator DEFAULT = DISABLE_NAGLE_ALGORITHM; |
| 47 | + |
| 48 | + /** |
| 49 | + * The default {@link SocketChannelConfigurator} that disables Nagle's algorithm. |
| 50 | + * |
| 51 | + * @return |
| 52 | + */ |
| 53 | + public static AbstractSocketChannelConfigurator defaultConfigurator() { |
| 54 | + return DEFAULT; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * {@link SocketChannelConfigurator} that disables Nagle's algorithm. |
| 59 | + * |
| 60 | + * @return |
| 61 | + */ |
| 62 | + public static AbstractSocketChannelConfigurator disableNagleAlgorithm() { |
| 63 | + return DISABLE_NAGLE_ALGORITHM; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Builder to configure and creates a {@link SocketChannelConfigurator} instance. |
| 68 | + * |
| 69 | + * @return |
| 70 | + */ |
| 71 | + public static SocketChannelConfigurators.Builder builder() { |
| 72 | + return new SocketChannelConfigurators.Builder(); |
| 73 | + } |
| 74 | + |
| 75 | + public static abstract class AbstractSocketChannelConfigurator implements SocketChannelConfigurator { |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns a composed configurator that performs, in sequence, this |
| 79 | + * operation followed by the {@code after} operation. |
| 80 | + * |
| 81 | + * @param after the operation to perform after this operation |
| 82 | + * @return a composed configurator that performs in sequence this |
| 83 | + * operation followed by the {@code after} operation |
| 84 | + * @throws NullPointerException if {@code after} is null |
| 85 | + */ |
| 86 | + public AbstractSocketChannelConfigurator andThen(final SocketChannelConfigurator after) { |
| 87 | + if (after == null) { |
| 88 | + throw new NullPointerException(); |
| 89 | + } |
| 90 | + return new AbstractSocketChannelConfigurator() { |
| 91 | + |
| 92 | + @Override |
| 93 | + public void configure(SocketChannel socketChannel) throws IOException { |
| 94 | + AbstractSocketChannelConfigurator.this.configure(socketChannel); |
| 95 | + after.configure(socketChannel); |
| 96 | + } |
| 97 | + }; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static class Builder { |
| 102 | + |
| 103 | + private AbstractSocketChannelConfigurator configurator = new AbstractSocketChannelConfigurator() { |
| 104 | + |
| 105 | + @Override |
| 106 | + public void configure(SocketChannel channel) { |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + /** |
| 111 | + * Set default configuration. |
| 112 | + * |
| 113 | + * @return |
| 114 | + */ |
| 115 | + public Builder defaultConfigurator() { |
| 116 | + configurator = configurator.andThen(DEFAULT); |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Disable Nagle's Algorithm. |
| 122 | + * |
| 123 | + * @return |
| 124 | + */ |
| 125 | + public Builder disableNagleAlgorithm() { |
| 126 | + configurator = configurator.andThen(DISABLE_NAGLE_ALGORITHM); |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Add an extra configuration step. |
| 132 | + * |
| 133 | + * @param extraConfiguration |
| 134 | + * @return |
| 135 | + */ |
| 136 | + public Builder add(SocketChannelConfigurator extraConfiguration) { |
| 137 | + configurator = configurator.andThen(extraConfiguration); |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Return the configured {@link SocketConfigurator}. |
| 143 | + * |
| 144 | + * @return |
| 145 | + */ |
| 146 | + public SocketChannelConfigurator build() { |
| 147 | + return configurator; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments