Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/net/spy/memcached/DefaultConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.transcoders.SerializingTranscoder;
import net.spy.memcached.transcoders.Transcoder;
import net.spy.memcached.util.PropertyUtils;

/**
* Default implementation of ConnectionFactory.
Expand Down Expand Up @@ -435,7 +436,7 @@ public int getTimeoutExceptionThreshold() {

@Override
public MetricType enableMetrics() {
String metricType = System.getProperty("net.spy.metrics.type");
String metricType = PropertyUtils.getSystemProperty("net.spy.metrics.type");
return metricType == null
? DEFAULT_METRIC_TYPE : MetricType.valueOf(metricType.toUpperCase());
}
Expand All @@ -446,7 +447,8 @@ public MetricCollector getMetricCollector() {
return metrics;
}

String enableMetrics = System.getProperty("net.spy.metrics.enable");
String enableMetrics =
PropertyUtils.getSystemProperty("net.spy.metrics.enable");
if (enableMetrics().equals(MetricType.OFF) || enableMetrics == "false") {
getLogger().debug("Metric collection disabled.");
metrics = new NoopMetricCollector();
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import net.spy.memcached.protocol.binary.BinaryOperationFactory;
import net.spy.memcached.protocol.binary.MultiGetOperationImpl;
import net.spy.memcached.protocol.binary.TapAckOperationImpl;
import net.spy.memcached.util.PropertyUtils;
import net.spy.memcached.util.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -280,17 +281,20 @@ public MemcachedConnection(final int bufSize, final ConnectionFactory f,
this.bufSize = bufSize;
this.connectionFactory = f;

String verifyAlive = System.getProperty("net.spy.verifyAliveOnConnect");
String verifyAlive =
PropertyUtils.getSystemProperty("net.spy.verifyAliveOnConnect");
if(verifyAlive != null && verifyAlive.equals("true")) {
verifyAliveOnConnect = true;
} else {
verifyAliveOnConnect = false;
}

wakeupDelay = Integer.parseInt( System.getProperty("net.spy.wakeupDelay",
Integer.toString(DEFAULT_WAKEUP_DELAY)));
wakeupDelay = Integer.parseInt(
PropertyUtils.getSystemProperty("net.spy.wakeupDelay",
Integer.toString(DEFAULT_WAKEUP_DELAY)));

retryQueueSize = Integer.parseInt(System.getProperty("net.spy.retryQueueSize",
retryQueueSize = Integer.parseInt(
PropertyUtils.getSystemProperty("net.spy.retryQueueSize",
Integer.toString(DEFAULT_RETRY_QUEUE_SIZE)));
getLogger().info("Setting retryQueueSize to " + retryQueueSize);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/spy/memcached/auth/AuthDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import javax.security.auth.callback.CallbackHandler;

import net.spy.memcached.util.PropertyUtils;

/**
* Information required to specify authentication mechanisms and callbacks.
*/
Expand Down Expand Up @@ -57,7 +59,7 @@ public AuthDescriptor(String[] m, CallbackHandler h) {
cbh = h;
authAttempts = 0;
String authThreshhold =
System.getProperty("net.spy.memcached.auth.AuthThreshold");
PropertyUtils.getSystemProperty("net.spy.memcached.auth.AuthThreshold");
if (authThreshhold != null) {
allowedAuthAttempts = Integer.parseInt(authThreshhold);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.spy.memcached.util.PropertyUtils;

/**
* Factory to get logger instances.
*
Expand Down Expand Up @@ -124,7 +126,8 @@ private Logger getNewInstance(String name) throws InstantiationException,
@SuppressWarnings("unchecked")
private void getConstructor() {
Class<? extends Logger> c = DefaultLogger.class;
String className = System.getProperty("net.spy.log.LoggerImpl");
String className =
PropertyUtils.getSystemProperty("net.spy.log.LoggerImpl");

if (className != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
package net.spy.memcached.metrics;

import com.codahale.metrics.*;

import net.spy.memcached.util.PropertyUtils;

import org.slf4j.LoggerFactory;

import java.io.File;
Expand Down Expand Up @@ -96,12 +99,15 @@ public DefaultMetricCollector() {
* Initialize the proper metrics Reporter.
*/
private void initReporter() {
String reporterType =
System.getProperty("net.spy.metrics.reporter.type", DEFAULT_REPORTER_TYPE);
String reporterInterval =
System.getProperty("net.spy.metrics.reporter.interval", DEFAULT_REPORTER_INTERVAL);
String reporterDir =
System.getProperty("net.spy.metrics.reporter.outdir", DEFAULT_REPORTER_OUTDIR);
String reporterType = PropertyUtils.getSystemProperty(
"net.spy.metrics.reporter.type",
DEFAULT_REPORTER_TYPE);
String reporterInterval = PropertyUtils.getSystemProperty(
"net.spy.metrics.reporter.interval",
DEFAULT_REPORTER_INTERVAL);
String reporterDir = PropertyUtils.getSystemProperty(
"net.spy.metrics.reporter.outdir",
DEFAULT_REPORTER_OUTDIR);

if(reporterType.equals("console")) {
final ConsoleReporter reporter = ConsoleReporter.forRegistry(registry)
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/net/spy/memcached/util/PropertyUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (C) 2017 David Eagen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/

package net.spy.memcached.util;

/** Utility methods to read system properties and handle SecurityExceptions. */
public final class PropertyUtils {
/** Private constructor, since this is a purely static class. */
private PropertyUtils() {
throw new UnsupportedOperationException();
}

/**
* Get a system property in a manner cognizant of the SecurityManager.
*
* @param key the name of the system property.
* @return the string value of the system property, or null if there is no
* property with that key or if the property read was denied.
* @exception NullPointerException if <code>key</code> is <code>null</code>.
* @exception IllegalArgumentException if <code>key</code> is empty.
*/
public static String getSystemProperty(String key) {
String value = null;
try {
value = System.getProperty(key);
} catch (SecurityException e) {
// Do nothing, leaving the value equal to null.
}
return value;
}

/**
* Get a system property in a manner cognizant of the SecurityManager.
*
* @param key the name of the system property.
* @param defaultValue the default value to return
* @return the string value of the system property. If there is no property
* with that key or if access to the property is denied by the
* SecurityManager, the default value is returned.
* @exception NullPointerException if <code>key</code> is <code>null</code>.
* @exception IllegalArgumentException if <code>key</code> is empty.
*/
public static String getSystemProperty(String key, String defaultValue) {
String propertyValue = PropertyUtils.getSystemProperty(key);
return propertyValue != null ? propertyValue : defaultValue;
}
}
14 changes: 8 additions & 6 deletions src/test/java/net/spy/memcached/TestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

package net.spy.memcached;

import net.spy.memcached.util.PropertyUtils;

/**
* A testConfig.
*/
Expand All @@ -33,22 +35,22 @@ public final class TestConfig {
public static final String TYPE_TEST_UNIT = "unit";
public static final String TYPE_TEST_CI = "ci";

public static final String IPV4_ADDR = System.getProperty(IPV4_PROP,
"127.0.0.1");
public static final String IPV4_ADDR =
PropertyUtils.getSystemProperty(IPV4_PROP, "127.0.0.1");
public static final String IPV6_ADDR = resolveIpv6Addr();

public static final int PORT_NUMBER =
Integer.parseInt(System.getProperty(PORT_PROP, "11211"));
Integer.parseInt(PropertyUtils.getSystemProperty(PORT_PROP, "11211"));

public static final String TEST_TYPE = System.getProperty(TEST_PROP,
TYPE_TEST_UNIT).toLowerCase();
public static final String TEST_TYPE =
PropertyUtils.getSystemProperty(TEST_PROP, TYPE_TEST_UNIT).toLowerCase();

private TestConfig() {
// Empty
}

private static String resolveIpv6Addr() {
String ipv6 = System.getProperty(IPV6_PROP, "::1");
String ipv6 = PropertyUtils.getSystemProperty(IPV6_PROP, "::1");
// If the ipv4 address was set but the ipv6 address wasn't then
// set the ipv6 address to use ipv4.
if (!IPV4_ADDR.equals("127.0.0.1") && !IPV4_ADDR.equals("localhost")
Expand Down