Skip to content

Commit

Permalink
Issue #96: Add endpoint management (get, delete) support in the backe…
Browse files Browse the repository at this point in the history
…nd API.
  • Loading branch information
mk23 committed Mar 13, 2016
1 parent 55a6e9d commit 82a83a6
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/com/github/mk23/jmxproxy/JMXProxyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.validation.Valid;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
Expand Down Expand Up @@ -76,6 +77,43 @@ public final Response getConfiguration() {
return Response.ok(manager.getConfiguration()).build();
}

/**
* <p>Anonymous GET handler for <code>/</code>.</p>
*
* Accepts HTTP GET requests. Responds with a list of cached endpoints
* available on the {@link ConnectionManager}.
*
* @return List of cached endpoints, marshalled into an API Response.
*/
@GET
public final Response getCachedEndpoints() {
LOG.debug("fetching cached endpoints list");
return Response.ok(manager.getHosts()).build();
}

/**
* <p>Anonymous DELETE handler for <code>/&lt;host:port&gt;</code>.</p>
*
* Accepts HTTP DELETE requests. Attemps to remove the specified endpoint
* and returns operation success status as a boolean.
*
* @param host requested endpoint DNS host name or IP address.
* Specified on the URI path.
* @param port requested endpoint TCP port.
* Specified on the URI path.
*
* @return boolean true mapped into an API Response.
*/
@DELETE
@Path("{host}:{port:\\d+}")
public final Response delCachedEndpoint(
@PathParam("host") final String host,
@PathParam("port") final int port
) {
LOG.debug(String.format("removing %s:%d", host, port));
return Response.ok(manager.delHost(host + ":" + port)).build();
}

/**
* <p>Anonymous GET handler for <code>/&lt;host&gt;:&lt;port&gt;/&lt;mbean&gt;/&lt;attribute&gt;</code>.</p>
*
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -72,6 +73,39 @@ public final AppConfig getConfiguration() {
return config;
}

/**
* <p>Getter for hosts.</p>
*
* Fetches the {@link Set} of {@link ConnectionWorker} name {@link String}s.
*
* @return {@link Set} of {@link ConnectionWorker} name {@link String}s.
*/
public final synchronized Set<String> getHosts() {
return hosts.keySet();
}

/**
* <p>Deleter for host.</p>
*
* Attempts to remove the specified host from the {@link ConnectionWorker} map store.
*
* @param host endpoint host:port {@link String}.
*
* @return true if the key is found in the map store.
*
* @throws WebApplicationException if key is not found in the map store.
*/
public final synchronized boolean delHost(final String host) throws WebApplicationException {
if (hosts.containsKey(host)) {
LOG.debug("purging " + host);
hosts.remove(host).shutdown();
} else {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}

return true;
}

/**
* <p>Anonymous getter for host.</p>
*
Expand Down

0 comments on commit 82a83a6

Please sign in to comment.