Skip to content

Commit

Permalink
Issue #75: Make jmx/ConnectionWorker.java checkstyle clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
mk23 committed Feb 21, 2016
1 parent f0fe4bf commit 2cbc2bf
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import io.dropwizard.lifecycle.Managed;

import java.net.MalformedURLException;

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

Expand Down Expand Up @@ -64,6 +66,8 @@ public Host getHost(String host, ConnectionCredentials auth) throws WebApplicati
return hosts.get(host).getHost();
} catch (SecurityException e) {
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
} catch (MalformedURLException e) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
} catch (Exception e) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
Expand Down
142 changes: 100 additions & 42 deletions src/main/java/com/github/mk23/jmxproxy/jmx/ConnectionWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.IOException;

import java.net.MalformedURLException;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -26,6 +28,19 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* <p>JMX fetcher and tracker.</p>
*
* Creates a {@link ScheduledExecutorService} to periodically make to a {@link JMXConnector}
* to a JMX endpoint. At every execution, discovers and fetches all remote registered mbeans
* and populates the local {@link Host} object with assicated {@link MBean}s and their
* {@link com.github.mk23.jmxproxy.core.Attribute}. Also maintains the {@link Host} access
* time to allow reaping of unaccessed workers.
*
* @author mk23
* @since 2015-05-11
* @version 3.2.0
*/
public class ConnectionWorker {
private static final Logger LOG = LoggerFactory.getLogger(ConnectionWorker.class);

Expand All @@ -40,7 +55,27 @@ public class ConnectionWorker {

private ScheduledExecutorService fetch;

public ConnectionWorker(String hostName, ConnectionCredentials auth, long cacheDuration, int historySize) throws Exception {
/**
* <p>Default constructor.</p>
*
* Initializes the {@link JMXServiceURL} to the specified hostName
* and starts the {@link ScheduledExecutorService} for periodically
* connecting and fetching JMX objects.
*
* @param hostName host:port {@link String} JMX agent target.
* @param auth optional {@link ConnectionCredentials} for the provided JMX agent or null if none.
* @param cacheDuration period in milliseconds for how often to connect to the JMX agent.
* @param historySize number of {@link com.github.mk23.jmxproxy.core.Attribute}s to keep per
* {@link MBean} {@link History}.
*
* @throws MalformedURLException if hostName isn't a valid host:port {@link String}.
*/
public ConnectionWorker(
final String hostName,
final ConnectionCredentials auth,
final long cacheDuration,
final int historySize
) throws MalformedURLException {
this.auth = auth;
this.historySize = historySize;

Expand All @@ -57,20 +92,51 @@ public void run() {
fetchJMXValues();
}

public synchronized Host getHost() throws SecurityException {
/**
* <p>Getter for host.<p>
*
* Fetches the tracked {@link Host} object and resets the request access time.
*
* @return {@link Host} as populated by the most recent fetch operation.
*/
public final synchronized Host getHost() {
accessTime = System.currentTimeMillis();
return host;
}

public boolean checkCredentials(ConnectionCredentials auth) {
return auth == this.auth || auth != null && auth.equals(this.auth) || this.auth != null && this.auth.equals(auth);
/**
* <p>Verifies tracked credentials.</p>
*
* Checks if the tracked {@link ConnectionCredentials} have changed since worker instantiation.
*
* @param peer new request {@link ConnectionCredentials} to compare against.
*
* @return true if {@link ConnectionCredentials} are the same between requests, false otherwise.
*/
public final boolean checkCredentials(final ConnectionCredentials peer) {
return auth == peer || auth != null && auth.equals(peer) || peer != null && peer.equals(auth);
}

public boolean isExpired(long accessDuration) {
/**
* <p>Checks expiration of this worker object.</p>
*
* Checks the last access time against the provided duration limit to determine if this worker
* is expired and can be purged.
*
* @param accessDuration time in milliseconds of no access after which this worker is expired.
*
* @return true if this worker hasn't been accessed recently, false otherwise.
*/
public final boolean isExpired(final long accessDuration) {
return System.currentTimeMillis() - accessTime > accessDuration;
}

public void shutdown() {
/**
* <p>Shuts down the scheduled fetcher.</p>
*
* Signals the {@link ScheduledExecutorService} to shutdown for process termination cleanup.
*/
public final void shutdown() {
if (!fetch.isShutdown()) {
fetch.shutdown();
}
Expand All @@ -79,10 +145,9 @@ public void shutdown() {
private synchronized void fetchJMXValues() throws SecurityException {
JMXConnector connection = null;
MBeanServerConnection server = null;
Map<String, Object> environment = null;

if (this.auth != null) {
environment = new HashMap<String, Object>();
Map<String, Object> environment = new HashMap<String, Object>();
if (auth != null) {
environment.put(JMXConnector.CREDENTIALS, new String[]{auth.getUsername(), auth.getPassword()});
}

Expand All @@ -95,46 +160,39 @@ private synchronized void fetchJMXValues() throws SecurityException {
if (host == null) {
host = new Host();
}

Set<String> freshMBeans = new HashSet<String>();

for (String domainName : server.getDomains()) {
LOG.debug("discovered domain " + domainName);
for (ObjectName mbeanName : server.queryNames(null, null)) {
LOG.debug("discovered mbean " + mbeanName);
freshMBeans.add(mbeanName.toString());

MBean mbean = host.addMBean(mbeanName.toString());
try {
for (ObjectName mbeanName : server.queryNames(new ObjectName(domainName + ":*"), null)) {
LOG.debug("discovered mbean " + mbeanName);
freshMBeans.add(mbeanName.toString());

MBean mbean = host.addMBean(mbeanName.toString());
try {
for (MBeanAttributeInfo attributeObject : server.getMBeanInfo(mbeanName).getAttributes()) {
if (attributeObject.isReadable()) {
try {
History history = mbean.addHistory(attributeObject.getName(), historySize);
history.addAttributeValue(server.getAttribute(mbeanName, attributeObject.getName()));
} catch (java.lang.NullPointerException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (java.rmi.UnmarshalException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.AttributeNotFoundException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.MBeanException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.RuntimeMBeanException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
}
}
for (MBeanAttributeInfo attributeObject : server.getMBeanInfo(mbeanName).getAttributes()) {
if (attributeObject.isReadable()) {
try {
History history = mbean.addHistory(attributeObject.getName(), historySize);
history.addAttributeValue(server.getAttribute(mbeanName, attributeObject.getName()));
} catch (java.lang.NullPointerException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (java.rmi.UnmarshalException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.AttributeNotFoundException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.MBeanException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
} catch (javax.management.RuntimeMBeanException e) {
LOG.error("failed to add attribute " + attributeObject.toString() + ": " + e);
}
} catch (javax.management.InstanceNotFoundException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
} catch (javax.management.IntrospectionException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
} catch (javax.management.ReflectionException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
}
}
} catch (javax.management.MalformedObjectNameException e) {
LOG.error("invalid object name: " + domainName + ":*", e);
} catch (javax.management.InstanceNotFoundException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
} catch (javax.management.IntrospectionException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
} catch (javax.management.ReflectionException e) {
LOG.error("failed to get mbean info for " + mbeanName, e);
}
}

Expand Down

0 comments on commit 2cbc2bf

Please sign in to comment.