|
21 | 21 | */
|
22 | 22 | public class ConnectionFactory implements PooledObjectFactory<Connection> {
|
23 | 23 |
|
| 24 | + public static class Builder { |
| 25 | + private JedisClientConfig clientConfig; |
| 26 | + private Connection.Builder connectionBuilder; |
| 27 | + private JedisSocketFactory jedisSocketFactory; |
| 28 | + private Cache cache; |
| 29 | + private HostAndPort hostAndPort; |
| 30 | + |
| 31 | + // Fluent API methods (preferred) |
| 32 | + public Builder clientConfig(JedisClientConfig clientConfig) { |
| 33 | + this.clientConfig = clientConfig; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public Builder connectionBuilder(Connection.Builder connectionBuilder) { |
| 38 | + this.connectionBuilder = connectionBuilder; |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + public Builder socketFactory(JedisSocketFactory jedisSocketFactory) { |
| 43 | + this.jedisSocketFactory = jedisSocketFactory; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + public Builder cache(Cache cache) { |
| 48 | + this.cache = cache; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + public Builder hostAndPort(HostAndPort hostAndPort) { |
| 53 | + this.hostAndPort = hostAndPort; |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public Connection.Builder getConnectionBuilder() { |
| 58 | + return connectionBuilder; |
| 59 | + } |
| 60 | + |
| 61 | + public JedisSocketFactory getJedisSocketFactory() { |
| 62 | + return jedisSocketFactory; |
| 63 | + } |
| 64 | + |
| 65 | + public JedisClientConfig getClientConfig() { |
| 66 | + return clientConfig; |
| 67 | + } |
| 68 | + |
| 69 | + public Cache getCache() { |
| 70 | + return cache; |
| 71 | + } |
| 72 | + |
| 73 | + public ConnectionFactory build() { |
| 74 | + withDefaults(); |
| 75 | + return new ConnectionFactory(this); |
| 76 | + } |
| 77 | + |
| 78 | + private Builder withDefaults() { |
| 79 | + if (jedisSocketFactory == null) { |
| 80 | + this.jedisSocketFactory = createDefaultSocketFactory(); |
| 81 | + } |
| 82 | + if (connectionBuilder == null) { |
| 83 | + this.connectionBuilder = createDefaultConnectionBuilder(); |
| 84 | + } |
| 85 | + return this; |
| 86 | + } |
| 87 | + |
| 88 | + private JedisSocketFactory createDefaultSocketFactory() { |
| 89 | + if (clientConfig == null) { |
| 90 | + clientConfig = DefaultJedisClientConfig.builder().build(); |
| 91 | + } |
| 92 | + if (hostAndPort == null) { |
| 93 | + throw new IllegalStateException("HostAndPort is required when no socketFactory is provided"); |
| 94 | + } |
| 95 | + return new DefaultJedisSocketFactory(hostAndPort, clientConfig); |
| 96 | + } |
| 97 | + |
| 98 | + private Connection.Builder createDefaultConnectionBuilder() { |
| 99 | + Connection.Builder connBuilder = cache == null ? Connection.builder() : CacheConnection.builder(cache); |
| 100 | + connBuilder.socketFactory(jedisSocketFactory).clientConfig(clientConfig); |
| 101 | + return connBuilder; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public static Builder builder() { |
| 106 | + return new Builder(); |
| 107 | + } |
| 108 | + |
24 | 109 | private static final Logger logger = LoggerFactory.getLogger(ConnectionFactory.class);
|
25 | 110 |
|
26 |
| - private final JedisSocketFactory jedisSocketFactory; |
27 | 111 | private final JedisClientConfig clientConfig;
|
28 |
| - private final Cache clientSideCache; |
29 |
| - private final Supplier<Connection> objectMaker; |
| 112 | + private Supplier<Connection> objectMaker; |
| 113 | + private Connection.Builder connectionBuilder; |
30 | 114 |
|
31 |
| - private final AuthXEventListener authXEventListener; |
| 115 | + private AuthXEventListener authXEventListener; |
32 | 116 |
|
33 | 117 | public ConnectionFactory(final HostAndPort hostAndPort) {
|
34 |
| - this(hostAndPort, DefaultJedisClientConfig.builder().build(), null); |
| 118 | + this(builder().hostAndPort(hostAndPort).withDefaults()); |
35 | 119 | }
|
36 | 120 |
|
37 | 121 | public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
|
38 |
| - this(hostAndPort, clientConfig, null); |
| 122 | + this(builder().hostAndPort(hostAndPort).clientConfig(clientConfig).withDefaults()); |
39 | 123 | }
|
40 | 124 |
|
41 | 125 | @Experimental
|
42 |
| - public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, |
43 |
| - Cache csCache) { |
44 |
| - this(new DefaultJedisSocketFactory(hostAndPort, clientConfig), clientConfig, csCache); |
| 126 | + public ConnectionFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig, Cache csCache) { |
| 127 | + this(builder().hostAndPort(hostAndPort).clientConfig(clientConfig).cache(csCache).withDefaults()); |
45 | 128 | }
|
46 | 129 |
|
47 |
| - public ConnectionFactory(final JedisSocketFactory jedisSocketFactory, |
48 |
| - final JedisClientConfig clientConfig) { |
49 |
| - this(jedisSocketFactory, clientConfig, null); |
| 130 | + public ConnectionFactory(final JedisSocketFactory jedisSocketFactory, final JedisClientConfig clientConfig) { |
| 131 | + this(builder().socketFactory(jedisSocketFactory).clientConfig(clientConfig).withDefaults()); |
50 | 132 | }
|
51 | 133 |
|
52 |
| - private ConnectionFactory(final JedisSocketFactory jedisSocketFactory, |
53 |
| - final JedisClientConfig clientConfig, Cache csCache) { |
| 134 | + public ConnectionFactory(Builder builder) { |
| 135 | + this.clientConfig = builder.getClientConfig(); |
| 136 | + this.connectionBuilder = builder.getConnectionBuilder(); |
54 | 137 |
|
55 |
| - this.jedisSocketFactory = jedisSocketFactory; |
56 |
| - this.clientSideCache = csCache; |
57 |
| - this.clientConfig = clientConfig; |
| 138 | + initAuthXManager(); |
| 139 | + } |
58 | 140 |
|
| 141 | + private void initAuthXManager() { |
59 | 142 | AuthXManager authXManager = clientConfig.getAuthXManager();
|
60 | 143 | if (authXManager == null) {
|
61 |
| - this.objectMaker = connectionSupplier(); |
| 144 | + this.objectMaker = () -> build(); |
62 | 145 | this.authXEventListener = AuthXEventListener.NOOP_LISTENER;
|
63 | 146 | } else {
|
64 |
| - Supplier<Connection> supplier = connectionSupplier(); |
65 |
| - this.objectMaker = () -> (Connection) authXManager.addConnection(supplier.get()); |
| 147 | + this.objectMaker = () -> (Connection) authXManager.addConnection(build()); |
66 | 148 | this.authXEventListener = authXManager.getListener();
|
67 | 149 | authXManager.start();
|
68 | 150 | }
|
69 | 151 | }
|
70 | 152 |
|
71 |
| - private Supplier<Connection> connectionSupplier() { |
72 |
| - return clientSideCache == null ? () -> new Connection(jedisSocketFactory, clientConfig) |
73 |
| - : () -> new CacheConnection(jedisSocketFactory, clientConfig, clientSideCache); |
| 153 | + private Connection build() { |
| 154 | + return connectionBuilder.build(); |
74 | 155 | }
|
75 | 156 |
|
76 | 157 | @Override
|
|
0 commit comments