Commit 16495d22 authored by Phillip Webb's avatar Phillip Webb

Polish

parent bdd61b8e
...@@ -57,7 +57,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL ...@@ -57,7 +57,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
private static Log logger = LogFactory.getLog(MetricRegistryMetricReader.class); private static Log logger = LogFactory.getLog(MetricRegistryMetricReader.class);
private static final Map<Class<?>, Set<String>> NUMBER_KEYS = new ConcurrentHashMap<Class<?>, Set<String>>(); private static final Map<Class<?>, Set<String>> numberKeys = new ConcurrentHashMap<Class<?>, Set<String>>();
private final Object monitor = new Object(); private final Object monitor = new Object();
...@@ -129,17 +129,16 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL ...@@ -129,17 +129,16 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
@Override @Override
public void onGaugeAdded(String name, Gauge<?> gauge) { public void onGaugeAdded(String name, Gauge<?> gauge) {
if (gauge.getValue() instanceof Number) { if (!(gauge.getValue() instanceof Number)) {
this.names.put(name, name); if (logger.isDebugEnabled()) {
synchronized (this.monitor) { logger.debug("Ignoring gauge '" + name + "' (" + gauge
this.reverse.add(name, name); + ") as its value is not a Number");
} }
return; return;
} }
this.names.put(name, name);
if (logger.isDebugEnabled()) { synchronized (this.monitor) {
logger.debug("Ignoring gauge '" + name + "' (" + gauge this.reverse.add(name, name);
+ ") as its value is not a Number");
} }
} }
...@@ -225,11 +224,9 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL ...@@ -225,11 +224,9 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
private void remove(String name) { private void remove(String name) {
List<String> keys; List<String> keys;
synchronized (this.monitor) { synchronized (this.monitor) {
keys = this.reverse.remove(name); keys = this.reverse.remove(name);
} }
if (keys != null) { if (keys != null) {
for (String key : keys) { for (String key : keys) {
this.names.remove(name + "." + key); this.names.remove(name + "." + key);
...@@ -238,7 +235,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL ...@@ -238,7 +235,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
} }
private static Set<String> getNumberKeys(Object metric) { private static Set<String> getNumberKeys(Object metric) {
Set<String> result = NUMBER_KEYS.get(metric.getClass()); Set<String> result = numberKeys.get(metric.getClass());
if (result == null) { if (result == null) {
result = new HashSet<String>(); result = new HashSet<String>();
} }
...@@ -249,7 +246,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL ...@@ -249,7 +246,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
result.add(descriptor.getName()); result.add(descriptor.getName());
} }
} }
NUMBER_KEYS.put(metric.getClass(), result); numberKeys.put(metric.getClass(), result);
} }
return result; return result;
} }
......
...@@ -31,7 +31,7 @@ import static org.hamcrest.Matchers.nullValue; ...@@ -31,7 +31,7 @@ import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link MetricRegistryMetricReader} * Tests for {@link MetricRegistryMetricReader}.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
...@@ -73,4 +73,5 @@ public class MetricRegistryMetricReaderTests { ...@@ -73,4 +73,5 @@ public class MetricRegistryMetricReaderTests {
this.metricRegistry.remove("test"); this.metricRegistry.remove("test");
assertThat(this.metricReader.findOne("test"), is(nullValue())); assertThat(this.metricReader.findOne("test"), is(nullValue()));
} }
} }
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -63,8 +63,8 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized ...@@ -63,8 +63,8 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
logger.debug("Initialization disabled (not running DDL scripts)"); logger.debug("Initialization disabled (not running DDL scripts)");
return; return;
} }
if (applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) { if (this.applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) {
this.dataSource = applicationContext.getBean(DataSource.class); this.dataSource = this.applicationContext.getBean(DataSource.class);
} }
if (this.dataSource == null) { if (this.dataSource == null) {
logger.debug("No DataSource found so not initializing"); logger.debug("No DataSource found so not initializing");
......
...@@ -25,6 +25,7 @@ import org.apache.activemq.pool.PooledConnectionFactory; ...@@ -25,6 +25,7 @@ import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
/** /**
...@@ -46,15 +47,10 @@ class ActiveMQConnectionFactoryConfiguration { ...@@ -46,15 +47,10 @@ class ActiveMQConnectionFactoryConfiguration {
properties).createConnectionFactory(ActiveMQConnectionFactory.class); properties).createConnectionFactory(ActiveMQConnectionFactory.class);
if (properties.isPooled()) { if (properties.isPooled()) {
PooledConnectionFactory pool = new PooledConnectionFactory(); PooledConnectionFactory pool = new PooledConnectionFactory();
Method connectionFactorySetter = findConnectionFactorySetter(); Method setConnectionFactory = findConnectionFactorySetter();
if (connectionFactorySetter != null) { Assert.state(setConnectionFactory != null, "No supported "
ReflectionUtils.invokeMethod(connectionFactorySetter, pool, + "setConnectionFactory method was found");
connectionFactory); ReflectionUtils.invokeMethod(setConnectionFactory, pool, connectionFactory);
}
else {
throw new IllegalStateException(
"No supported setConnectionFactory method was found");
}
return pool; return pool;
} }
return connectionFactory; return connectionFactory;
......
...@@ -141,27 +141,33 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine ...@@ -141,27 +141,33 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
} }
private Port getPortFromChannel(Object channel) { private Port getPortFromChannel(Object channel) {
Object tcpServer; Object tcpServer = channel;
String protocol; String protocol = "http";
Field sslContext = ReflectionUtils.findField(channel.getClass(), "sslContext"); Field sslContext = ReflectionUtils.findField(channel.getClass(), "sslContext");
if (sslContext != null) { if (sslContext != null) {
Field tcpServerField = ReflectionUtils.findField(channel.getClass(), tcpServer = getTcpServer(channel);
"tcpServer");
ReflectionUtils.makeAccessible(tcpServerField);
tcpServer = ReflectionUtils.getField(tcpServerField, channel);
protocol = "https"; protocol = "https";
} }
else { ServerSocket socket = getSocket(tcpServer);
tcpServer = channel; if (socket != null) {
protocol = "http"; return new Port(socket.getLocalPort(), protocol);
} }
return null;
}
private Object getTcpServer(Object channel) {
Field field = ReflectionUtils.findField(channel.getClass(), "tcpServer");
ReflectionUtils.makeAccessible(field);
return ReflectionUtils.getField(field, channel);
}
private ServerSocket getSocket(Object tcpServer) {
Field socketField = ReflectionUtils.findField(tcpServer.getClass(), "socket"); Field socketField = ReflectionUtils.findField(tcpServer.getClass(), "socket");
if (socketField != null) { if (socketField == null) {
ReflectionUtils.makeAccessible(socketField); return null;
return new Port(((ServerSocket) ReflectionUtils.getField(socketField,
tcpServer)).getLocalPort(), protocol);
} }
return null; ReflectionUtils.makeAccessible(socketField);
return (ServerSocket) ReflectionUtils.getField(socketField, tcpServer);
} }
@Override @Override
...@@ -178,24 +184,32 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine ...@@ -178,24 +184,32 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine
if (ports.isEmpty()) { if (ports.isEmpty()) {
return 0; return 0;
} }
return ports.get(0).portNumber; return ports.get(0).getNumber();
} }
/**
* An active undertow port.
*/
private static class Port { private static class Port {
private final int portNumber; private final int number;
private final String protocol; private final String protocol;
private Port(int portNumber, String protocol) { private Port(int number, String protocol) {
this.portNumber = portNumber; this.number = number;
this.protocol = protocol; this.protocol = protocol;
} }
public int getNumber() {
return this.number;
}
@Override @Override
public String toString() { public String toString() {
return this.portNumber + " (" + this.protocol + ")"; return this.number + " (" + this.protocol + ")";
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment