Skip to content

Commit 61ea6d6

Browse files
authored
Merge pull request #47 from hazendaz/guodong000-sasl
Guodong000 sasl merging #7
2 parents 299fa40 + 6c4fb25 commit 61ea6d6

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2012-2018 the original author or authors.
4+
Copyright 2012-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

src/main/java/org/mybatis/caches/memcached/MemcachedClientWrapper.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,9 +24,12 @@
2424

2525
import net.spy.memcached.CASResponse;
2626
import net.spy.memcached.CASValue;
27+
import net.spy.memcached.ConnectionFactoryBuilder;
2728
import net.spy.memcached.MemcachedClient;
2829
import net.spy.memcached.internal.OperationFuture;
2930

31+
import net.spy.memcached.auth.AuthDescriptor;
32+
import net.spy.memcached.auth.PlainCallbackHandler;
3033
import org.apache.ibatis.cache.CacheException;
3134
import org.apache.ibatis.logging.Log;
3235
import org.apache.ibatis.logging.LogFactory;
@@ -81,7 +84,15 @@ public void setCas(long cas) {
8184
public MemcachedClientWrapper() {
8285
configuration = MemcachedConfigurationBuilder.getInstance().parseConfiguration();
8386
try {
84-
client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
87+
if (configuration.isUsingSASL()) {
88+
AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" },
89+
new PlainCallbackHandler(configuration.getUsername(), configuration.getPassword()));
90+
client = new MemcachedClient(new ConnectionFactoryBuilder()
91+
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(),
92+
configuration.getAddresses());
93+
} else {
94+
client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
95+
}
8596
} catch (IOException e) {
8697
String message = "Impossible to instantiate a new memecached client instance, see nested exceptions";
8798
LOG.error(message, e);

src/main/java/org/mybatis/caches/memcached/MemcachedConfiguration.java

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,6 +70,21 @@ final class MemcachedConfiguration {
7070
*/
7171
private TimeUnit timeUnit;
7272

73+
/**
74+
* The flag to enable SASL Connection
75+
*/
76+
private boolean usingSASL;
77+
78+
/**
79+
* The Memcached SASL username
80+
*/
81+
private String username;
82+
83+
/**
84+
* The Memcached SASL password
85+
*/
86+
private String password;
87+
7388
/**
7489
* @return the keyPrefix
7590
*/
@@ -190,13 +205,58 @@ public void setTimeUnit(TimeUnit timeUnit) {
190205
this.timeUnit = timeUnit;
191206
}
192207

208+
/**
209+
* @return the usingSASL
210+
*/
211+
public boolean isUsingSASL() {
212+
return usingSASL;
213+
}
214+
215+
/**
216+
* @param usingSASL
217+
* the usingSASL to set
218+
*/
219+
public void setUsingSASL(boolean usingSASL) {
220+
this.usingSASL = usingSASL;
221+
}
222+
223+
/**
224+
* @return the username
225+
*/
226+
public String getUsername() {
227+
return username;
228+
}
229+
230+
/**
231+
* @param username
232+
* the username to set
233+
*/
234+
public void setUsername(String username) {
235+
this.username = username;
236+
}
237+
238+
/**
239+
* @return the password
240+
*/
241+
public String getPassword() {
242+
return password;
243+
}
244+
245+
/**
246+
* @param password
247+
* the password to set
248+
*/
249+
public void setPassword(String password) {
250+
this.password = password;
251+
}
252+
193253
/**
194254
* {@inheritDoc}
195255
*/
196256
@Override
197257
public int hashCode() {
198258
return hash(1, 31, addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout,
199-
usingAsyncGet);
259+
usingAsyncGet, usingSASL, username, password);
200260
}
201261

202262
/**
@@ -234,7 +294,8 @@ public boolean equals(Object obj) {
234294
return eq(addresses, other.addresses) && eq(compressionEnabled, other.compressionEnabled)
235295
&& eq(connectionFactory, other.connectionFactory) && eq(expiration, other.expiration)
236296
&& eq(keyPrefix, other.keyPrefix) && eq(timeUnit, other.timeUnit) && eq(timeout, other.timeout)
237-
&& eq(usingAsyncGet, other.usingAsyncGet);
297+
&& eq(usingAsyncGet, other.usingAsyncGet) && eq(usingSASL, other.usingSASL) && eq(username, other.username)
298+
&& eq(password, other.password);
238299
}
239300

240301
/**
@@ -256,8 +317,9 @@ private static <O> boolean eq(O o1, O o2) {
256317
@Override
257318
public String toString() {
258319
return format(
259-
"MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s]",
260-
addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet);
320+
"MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s, usingSASL=%s, username=%s, password=%s]",
321+
addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet,
322+
usingSASL, username, password);
261323
}
262324

263325
}

src/main/java/org/mybatis/caches/memcached/MemcachedConfigurationBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,8 @@ private MemcachedConfigurationBuilder() {
5454
memcachedPropertiesFilename = System.getProperty(SYSTEM_PROPERTY_MEMCACHED_PROPERTIES_FILENAME, MEMCACHED_RESOURCE);
5555

5656
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.keyprefix", "keyPrefix", "_mybatis_"));
57+
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.username", "username", ""));
58+
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.password", "password", ""));
5759

5860
settersRegistry
5961
.add(new IntegerPropertySetter("org.mybatis.caches.memcached.expiration", "expiration", 60 * 60 * 24 * 30));
@@ -63,6 +65,7 @@ private MemcachedConfigurationBuilder() {
6365
settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.asyncget", "usingAsyncGet", false));
6466
settersRegistry
6567
.add(new BooleanPropertySetter("org.mybatis.caches.memcached.compression", "compressionEnabled", false));
68+
settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.sasl", "usingSASL", false));
6669

6770
settersRegistry.add(new InetSocketAddressListPropertySetter());
6871
settersRegistry.add(new ConnectionFactorySetter());

src/site/site.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2012-2016 the original author or authors.
4+
Copyright 2012-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)