This commit is contained in:
Phillip Webb
2015-03-16 14:11:43 -07:00
parent bdd61b8ec2
commit 16495d223a
5 changed files with 54 additions and 46 deletions

View File

@@ -57,7 +57,7 @@ public class MetricRegistryMetricReader implements MetricReader, MetricRegistryL
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();
@@ -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<String> 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<String> getNumberKeys(Object metric) {
Set<String> result = NUMBER_KEYS.get(metric.getClass());
Set<String> result = numberKeys.get(metric.getClass());
if (result == null) {
result = new HashSet<String>();
}
@@ -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;
}

View File

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

View File

@@ -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<DataSourceInitialized
logger.debug("Initialization disabled (not running DDL scripts)");
return;
}
if (applicationContext.getBeanNamesForType(DataSource.class, false, false).length > 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");

View File

@@ -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;

View File

@@ -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 + ")";
}
}
}