Skip to content

Commit be78f9d

Browse files
committed
add sasl connection support
Support Binary Protocol SASL Connection add three properties org.mybatis.caches.memcached.sasl=[true|false] org.mybatis.caches.memcached.username=[username] org.mybatis.caches.memcached.password=[password]
1 parent 4457efc commit be78f9d

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import java.util.Set;
2222
import java.util.concurrent.Future;
2323

24+
import net.spy.memcached.ConnectionFactoryBuilder;
2425
import net.spy.memcached.MemcachedClient;
2526

27+
import net.spy.memcached.auth.AuthDescriptor;
28+
import net.spy.memcached.auth.PlainCallbackHandler;
2629
import org.apache.ibatis.cache.CacheException;
2730
import org.apache.ibatis.logging.Log;
2831
import org.apache.ibatis.logging.LogFactory;
@@ -44,7 +47,14 @@ final class MemcachedClientWrapper {
4447
public MemcachedClientWrapper() {
4548
configuration = MemcachedConfigurationBuilder.getInstance().parseConfiguration();
4649
try {
47-
client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
50+
if (configuration.isUsingSASL()) {
51+
AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"}, new PlainCallbackHandler(configuration.getUsername(), configuration.getPassword()));
52+
client = new MemcachedClient(new ConnectionFactoryBuilder()
53+
.setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
54+
.setAuthDescriptor(ad).build(), configuration.getAddresses());
55+
} else {
56+
client = new MemcachedClient(configuration.getConnectionFactory(), configuration.getAddresses());
57+
}
4858
} catch (IOException e) {
4959
String message = "Impossible to instantiate a new memecached client instance, see nested exceptions";
5060
log.error(message, e);

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ final class MemcachedConfiguration {
7171
*/
7272
private TimeUnit timeUnit;
7373

74+
/**
75+
* The flag to enable SASL Connection
76+
*/
77+
private boolean usingSASL;
78+
79+
/**
80+
* The Memcached SASL username
81+
*/
82+
private String username;
83+
84+
/**
85+
* The Memcached SASL password
86+
*/
87+
private String password;
88+
7489
/**
7590
* @return the keyPrefix
7691
*/
@@ -183,12 +198,42 @@ public void setTimeUnit(TimeUnit timeUnit) {
183198
this.timeUnit = timeUnit;
184199
}
185200

201+
/**
202+
* @return the usingSASL
203+
*/
204+
public boolean isUsingSASL() { return usingSASL; }
205+
206+
/**
207+
* @param usingSASL the usingSASL to set
208+
*/
209+
public void setUsingSASL(boolean usingSASL) { this.usingSASL = usingSASL; }
210+
211+
/**
212+
* @return the username
213+
*/
214+
public String getUsername() { return username; }
215+
216+
/**
217+
* @param username the username to set
218+
*/
219+
public void setUsername(String username) { this.username = username; }
220+
221+
/**
222+
* @return the password
223+
*/
224+
public String getPassword() { return password; }
225+
226+
/**
227+
* @param password the password to set
228+
*/
229+
public void setPassword(String password) { this.password = password; }
230+
186231
/**
187232
* {@inheritDoc}
188233
*/
189234
@Override
190235
public int hashCode() {
191-
return hash(1, 31, addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet);
236+
return hash(1, 31, addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet, usingSASL, username, password);
192237
}
193238

194239
/**
@@ -227,7 +272,10 @@ && eq(expiration, other.expiration)
227272
&& eq(keyPrefix, other.keyPrefix)
228273
&& eq(timeUnit, other.timeUnit)
229274
&& eq(timeout, other.timeout)
230-
&& eq(usingAsyncGet, other.usingAsyncGet);
275+
&& eq(usingAsyncGet, other.usingAsyncGet)
276+
&& eq(usingSASL, other.usingSASL)
277+
&& eq(username, other.username)
278+
&& eq(password, other.password);
231279
}
232280

233281
/**
@@ -246,8 +294,8 @@ private static <O> boolean eq( O o1, O o2 ) {
246294
*/
247295
@Override
248296
public String toString() {
249-
return format( "MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s]",
250-
addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet );
297+
return format( "MemcachedConfiguration [addresses=%s, compressionEnabled=%s, connectionFactory=%s, , expiration=%s, keyPrefix=%s, timeUnit=%s, timeout=%s, usingAsyncGet=%s, usingSASL=%s, username=%s, password=%s]",
298+
addresses, compressionEnabled, connectionFactory, expiration, keyPrefix, timeUnit, timeout, usingAsyncGet, usingSASL, username, password );
251299
}
252300

253301
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ private MemcachedConfigurationBuilder() {
6363
memcachedPropertiesFilename = System.getProperty(SYSTEM_PROPERTY_MEMCACHED_PROPERTIES_FILENAME, MEMCACHED_RESOURCE);
6464

6565
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.keyprefix", "keyPrefix", "_mybatis_"));
66+
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.username", "username", ""));
67+
settersRegistry.add(new StringPropertySetter("org.mybatis.caches.memcached.password", "password", ""));
6668

6769
settersRegistry.add(new IntegerPropertySetter("org.mybatis.caches.memcached.expiration", "expiration", 60 * 60 * 24 * 30));
6870
settersRegistry.add(new IntegerPropertySetter("org.mybatis.caches.memcached.timeout", "timeout", 5));
6971
settersRegistry.add(new TimeUnitSetter());
7072

7173
settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.asyncget", "usingAsyncGet", false));
7274
settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.compression", "compressionEnabled", false));
75+
settersRegistry.add(new BooleanPropertySetter("org.mybatis.caches.memcached.sasl", "usingSASL", true));
7376

7477
settersRegistry.add(new InetSocketAddressListPropertySetter());
7578
settersRegistry.add(new ConnectionFactorySetter());

0 commit comments

Comments
 (0)