Permalink
Browse files

Refactored backend configuration

  • Loading branch information...
1 parent 218eb0d commit 4c50b9c75ddbb7d2037d46452ea18007fde95fae r1j0 committed May 5, 2012
View
4 src/com/github/r1j0/statsd/backend/Backend.java
@@ -2,13 +2,11 @@
import java.util.List;
-import com.github.r1j0.statsd.server.StatsdConfiguration;
-
public interface Backend {
boolean notify(List<String> messages);
- void setConfiguration(StatsdConfiguration statsdConfiguration);
+ BackendConfiguration getConfiguration();
}
View
9 src/com/github/r1j0/statsd/backend/BackendConfiguration.java
@@ -0,0 +1,9 @@
+package com.github.r1j0.statsd.backend;
+
+import com.github.r1j0.statsd.server.StatsdConfiguration;
+
+public interface BackendConfiguration {
+
+ void setConfiguration(StatsdConfiguration configuration);
+
+}
View
12 src/com/github/r1j0/statsd/backend/DebugBackend.java
@@ -5,11 +5,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.github.r1j0.statsd.server.StatsdConfiguration;
-
public class DebugBackend implements Backend {
private final Logger log = LoggerFactory.getLogger(getClass());
+ private final DebugBackendConfiguration configuration;
+
+
+ public DebugBackend() {
+ this.configuration = new DebugBackendConfiguration();
+ }
public boolean notify(List<String> messages) {
@@ -18,7 +22,7 @@ public boolean notify(List<String> messages) {
}
- public void setConfiguration(StatsdConfiguration configuration) {
- // NoOp
+ public DebugBackendConfiguration getConfiguration() {
+ return configuration;
}
}
View
11 src/com/github/r1j0/statsd/backend/DebugBackendConfiguration.java
@@ -0,0 +1,11 @@
+package com.github.r1j0.statsd.backend;
+
+import com.github.r1j0.statsd.server.StatsdConfiguration;
+
+public class DebugBackendConfiguration implements BackendConfiguration {
+
+ public void setConfiguration(StatsdConfiguration configuration) {
+ // NoOp
+ }
+
+}
View
15 src/com/github/r1j0/statsd/backend/GraphiteBackend.java
@@ -8,12 +8,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.github.r1j0.statsd.server.StatsdConfiguration;
-
public class GraphiteBackend implements Backend {
private final Logger log = LoggerFactory.getLogger(getClass());
- private StatsdConfiguration configuration;
+ private final GraphiteBackendConfiguration configuration;
+
+
+ public GraphiteBackend() {
+ this.configuration = new GraphiteBackendConfiguration();
+ }
public boolean notify(List<String> messages) {
@@ -23,8 +26,8 @@ public boolean notify(List<String> messages) {
}
- public void setConfiguration(StatsdConfiguration statsdConfiguration) {
- this.configuration = statsdConfiguration;
+ public BackendConfiguration getConfiguration() {
+ return configuration;
}
@@ -41,7 +44,7 @@ private String createMessage(final List<String> messages) {
private boolean doSend(final String message) {
try {
- Socket socket = new Socket(configuration.getValue("backend.graphite.host"), Integer.parseInt(configuration.getValue("backend.graphite.port")));
+ Socket socket = new Socket(configuration.getHost(), configuration.getPort());
Writer writer = new OutputStreamWriter(socket.getOutputStream());
writer.write(message);
writer.flush();
View
26 src/com/github/r1j0/statsd/backend/GraphiteBackendConfiguration.java
@@ -0,0 +1,26 @@
+package com.github.r1j0.statsd.backend;
+
+import com.github.r1j0.statsd.server.StatsdConfiguration;
+
+public class GraphiteBackendConfiguration implements BackendConfiguration {
+
+ private StatsdConfiguration configuration;
+ private static final String HOST = "backend.graphite.host";
+ private static final String PORT = "backend.graphite.port";
+
+
+ public String getHost() {
+ return configuration.getValue(HOST);
+ }
+
+
+ public int getPort() {
+ return Integer.parseInt(configuration.getValue(PORT));
+ }
+
+
+ public void setConfiguration(StatsdConfiguration configuration) {
+ this.configuration = configuration;
+ }
+
+}
View
4 src/com/github/r1j0/statsd/server/StatsdConfiguration.java
@@ -17,6 +17,7 @@
import org.slf4j.LoggerFactory;
import com.github.r1j0.statsd.backend.Backend;
+import com.github.r1j0.statsd.backend.BackendConfiguration;
public class StatsdConfiguration {
@@ -118,7 +119,8 @@ private void initializeBackends() {
for (String backendToUse : backendsToUse) {
try {
Backend backend = (Backend) Class.forName("com.github.r1j0.statsd.backend." + StringUtils.capitalize(backendToUse.trim().toLowerCase()) + "Backend").newInstance();
- backend.setConfiguration(this);
+ BackendConfiguration configuration = backend.getConfiguration();
+ configuration.setConfiguration(this);
backends.add(backend);
log.info("Added backend: " + backend.getClass().getSimpleName() + ".");

0 comments on commit 4c50b9c

Please sign in to comment.