Skip to content

Commit

Permalink
Refactored backend configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
r1j0 committed May 5, 2012
1 parent 218eb0d commit 4c50b9c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 14 deletions.
4 changes: 1 addition & 3 deletions src/com/github/r1j0/statsd/backend/Backend.java
Expand Up @@ -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();

}
9 changes: 9 additions & 0 deletions 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);

}
12 changes: 8 additions & 4 deletions src/com/github/r1j0/statsd/backend/DebugBackend.java
Expand Up @@ -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) {
Expand All @@ -18,7 +22,7 @@ public boolean notify(List<String> messages) {
}


public void setConfiguration(StatsdConfiguration configuration) {
// NoOp
public DebugBackendConfiguration getConfiguration() {
return configuration;
}
}
11 changes: 11 additions & 0 deletions 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
}

}
15 changes: 9 additions & 6 deletions src/com/github/r1j0/statsd/backend/GraphiteBackend.java
Expand Up @@ -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) {
Expand All @@ -23,8 +26,8 @@ public boolean notify(List<String> messages) {
}


public void setConfiguration(StatsdConfiguration statsdConfiguration) {
this.configuration = statsdConfiguration;
public BackendConfiguration getConfiguration() {
return configuration;
}


Expand All @@ -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();
Expand Down
@@ -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;
}

}
4 changes: 3 additions & 1 deletion src/com/github/r1j0/statsd/server/StatsdConfiguration.java
Expand Up @@ -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 {

Expand Down Expand Up @@ -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() + ".");
Expand Down

0 comments on commit 4c50b9c

Please sign in to comment.