diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java index d3d4487c92..1ce9373e15 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReader.java @@ -57,7 +57,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL private static Log logger = LogFactory.getLog(MetricRegistryMetricReader.class); - private static final Map, Set> NUMBER_KEYS = new ConcurrentHashMap, Set>(); + private static final Map, Set> numberKeys = new ConcurrentHashMap, Set>(); private final Object monitor = new Object(); @@ -129,17 +129,16 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL @Override public void onGaugeAdded(String name, Gauge gauge) { - if (gauge.getValue() instanceof Number) { - this.names.put(name, name); - synchronized (this.monitor) { - this.reverse.add(name, name); + if (!(gauge.getValue() instanceof Number)) { + if (logger.isDebugEnabled()) { + logger.debug("Ignoring gauge '" + name + "' (" + gauge + + ") as its value is not a Number"); } return; } - - if (logger.isDebugEnabled()) { - logger.debug("Ignoring gauge '" + name + "' (" + gauge - + ") as its value is not a Number"); + this.names.put(name, name); + synchronized (this.monitor) { + this.reverse.add(name, name); } } @@ -225,11 +224,9 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL private void remove(String name) { List keys; - synchronized (this.monitor) { keys = this.reverse.remove(name); } - if (keys != null) { for (String key : keys) { this.names.remove(name + "." + key); @@ -238,7 +235,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL } private static Set getNumberKeys(Object metric) { - Set result = NUMBER_KEYS.get(metric.getClass()); + Set result = numberKeys.get(metric.getClass()); if (result == null) { result = new HashSet(); } @@ -249,7 +246,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL result.add(descriptor.getName()); } } - NUMBER_KEYS.put(metric.getClass(), result); + numberKeys.put(metric.getClass(), result); } return result; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java index 39f4e7bd46..1858435a6b 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/reader/MetricRegistryMetricReaderTests.java @@ -31,7 +31,7 @@ import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; /** - * Tests for {@link MetricRegistryMetricReader} + * Tests for {@link MetricRegistryMetricReader}. * * @author Andy Wilkinson */ @@ -73,4 +73,5 @@ public class MetricRegistryMetricReaderTests { this.metricRegistry.remove("test"); assertThat(this.metricReader.findOne("test"), is(nullValue())); } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java index e7a15c419a..c0bcf36f7c 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java @@ -1,5 +1,5 @@ /* - * 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"); * you may not use this file except in compliance with the License. @@ -63,8 +63,8 @@ class DataSourceInitializer implements ApplicationListener 0) { - this.dataSource = applicationContext.getBean(DataSource.class); + if (this.applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 0) { + this.dataSource = this.applicationContext.getBean(DataSource.class); } if (this.dataSource == null) { logger.debug("No DataSource found so not initializing"); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java index 1ddc978714..d9afb6f4f5 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/activemq/ActiveMQConnectionFactoryConfiguration.java @@ -25,6 +25,7 @@ import org.apache.activemq.pool.PooledConnectionFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** @@ -46,15 +47,10 @@ class ActiveMQConnectionFactoryConfiguration { properties).createConnectionFactory(ActiveMQConnectionFactory.class); if (properties.isPooled()) { PooledConnectionFactory pool = new PooledConnectionFactory(); - Method connectionFactorySetter = findConnectionFactorySetter(); - if (connectionFactorySetter != null) { - ReflectionUtils.invokeMethod(connectionFactorySetter, pool, - connectionFactory); - } - else { - throw new IllegalStateException( - "No supported setConnectionFactory method was found"); - } + Method setConnectionFactory = findConnectionFactorySetter(); + Assert.state(setConnectionFactory != null, "No supported " + + "setConnectionFactory method was found"); + ReflectionUtils.invokeMethod(setConnectionFactory, pool, connectionFactory); return pool; } return connectionFactory; diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java index e0d341c71c..fd2c8928e3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainer.java @@ -141,29 +141,35 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine } private Port getPortFromChannel(Object channel) { - Object tcpServer; - String protocol; + Object tcpServer = channel; + String protocol = "http"; Field sslContext = ReflectionUtils.findField(channel.getClass(), "sslContext"); if (sslContext != null) { - Field tcpServerField = ReflectionUtils.findField(channel.getClass(), - "tcpServer"); - ReflectionUtils.makeAccessible(tcpServerField); - tcpServer = ReflectionUtils.getField(tcpServerField, channel); + tcpServer = getTcpServer(channel); protocol = "https"; } - else { - tcpServer = channel; - protocol = "http"; - } - Field socketField = ReflectionUtils.findField(tcpServer.getClass(), "socket"); - if (socketField != null) { - ReflectionUtils.makeAccessible(socketField); - return new Port(((ServerSocket) ReflectionUtils.getField(socketField, - tcpServer)).getLocalPort(), protocol); + ServerSocket socket = getSocket(tcpServer); + if (socket != null) { + 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"); + if (socketField == null) { + return null; + } + ReflectionUtils.makeAccessible(socketField); + return (ServerSocket) ReflectionUtils.getField(socketField, tcpServer); + } + @Override public synchronized void stop() throws EmbeddedServletContainerException { if (this.started) { @@ -178,24 +184,32 @@ public class UndertowEmbeddedServletContainer implements EmbeddedServletContaine if (ports.isEmpty()) { return 0; } - return ports.get(0).portNumber; + return ports.get(0).getNumber(); } + /** + * An active undertow port. + */ private static class Port { - private final int portNumber; + private final int number; private final String protocol; - private Port(int portNumber, String protocol) { - this.portNumber = portNumber; + private Port(int number, String protocol) { + this.number = number; this.protocol = protocol; } + public int getNumber() { + return this.number; + } + @Override public String toString() { - return this.portNumber + " (" + this.protocol + ")"; + return this.number + " (" + this.protocol + ")"; } + } }