Polish
This commit is contained in:
@@ -57,6 +57,7 @@ import org.springframework.web.util.UrlPathHelper;
|
||||
public class MetricFilterAutoConfiguration {
|
||||
|
||||
private static final int UNDEFINED_HTTP_STATUS = 999;
|
||||
|
||||
private static final String UNKNOWN_PATH_SUFFIX = "/unmapped";
|
||||
|
||||
@Autowired
|
||||
@@ -90,10 +91,10 @@ public class MetricFilterAutoConfiguration {
|
||||
finally {
|
||||
stopWatch.stop();
|
||||
int status = getStatus(response);
|
||||
if (request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null) {
|
||||
suffix = request
|
||||
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)
|
||||
.toString().replaceAll("[{}]", "-");
|
||||
Object bestMatchingPattern = request
|
||||
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||
if (bestMatchingPattern != null) {
|
||||
suffix = bestMatchingPattern.toString().replaceAll("[{}]", "-");
|
||||
}
|
||||
else if (HttpStatus.valueOf(status).is4xxClientError()) {
|
||||
suffix = UNKNOWN_PATH_SUFFIX;
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
@@ -52,8 +53,8 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator {
|
||||
private static Map<String, String> queries = new HashMap<String, String>();
|
||||
|
||||
static {
|
||||
queries.put("HSQL Database Engine",
|
||||
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS");
|
||||
queries.put("HSQL Database Engine", "SELECT COUNT(*) FROM "
|
||||
+ "INFORMATION_SCHEMA.SYSTEM_USERS");
|
||||
queries.put("Oracle", "SELECT 'Hello' from DUAL");
|
||||
queries.put("Apache Derby", "SELECT 1 FROM SYSIBM.SYSDUMMY1");
|
||||
}
|
||||
@@ -93,23 +94,11 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator {
|
||||
String query = detectQuery(product);
|
||||
if (StringUtils.hasText(query)) {
|
||||
try {
|
||||
builder.withDetail("hello", DataAccessUtils
|
||||
.requiredSingleResult(this.jdbcTemplate.query(query,
|
||||
new RowMapper<Object>() {
|
||||
|
||||
@Override
|
||||
public Object mapRow(ResultSet rs, int rowNum)
|
||||
throws SQLException {
|
||||
ResultSetMetaData rsmd = rs.getMetaData();
|
||||
int nrOfColumns = rsmd.getColumnCount();
|
||||
if (nrOfColumns != 1) {
|
||||
throw new IncorrectResultSetColumnCountException(
|
||||
1, nrOfColumns);
|
||||
}
|
||||
return JdbcUtils.getResultSetValue(rs, 1);
|
||||
}
|
||||
|
||||
})));
|
||||
// Avoid calling getObject as it breaks MySQL on Java 7
|
||||
List<Object> results = this.jdbcTemplate.query(query,
|
||||
new SingleColumnRowMapper());
|
||||
Object result = DataAccessUtils.requiredSingleResult(results);
|
||||
builder.withDetail("hello", result);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
builder.down(ex);
|
||||
@@ -147,4 +136,21 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link RowMapper} that expects and returns results from a single column.
|
||||
*/
|
||||
private static class SingleColumnRowMapper implements RowMapper<Object> {
|
||||
|
||||
@Override
|
||||
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
int columns = metaData.getColumnCount();
|
||||
if (columns != 1) {
|
||||
throw new IncorrectResultSetColumnCountException(1, columns);
|
||||
}
|
||||
return JdbcUtils.getResultSetValue(rs, 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -169,4 +169,4 @@ class MetricFilterTestController {
|
||||
public String testKnownPathWith404Response(@PathVariable String someVariable) {
|
||||
return someVariable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user