Skip to content

Commit 9289947

Browse files
author
Phillip Webb
committed
Polish
1 parent 7d21395 commit 9289947

File tree

8 files changed

+58
-56
lines changed

8 files changed

+58
-56
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
public class MetricFilterAutoConfiguration {
5858

5959
private static final int UNDEFINED_HTTP_STATUS = 999;
60+
6061
private static final String UNKNOWN_PATH_SUFFIX = "/unmapped";
6162

6263
@Autowired
@@ -90,10 +91,10 @@ protected void doFilterInternal(HttpServletRequest request,
9091
finally {
9192
stopWatch.stop();
9293
int status = getStatus(response);
93-
if (request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE) != null) {
94-
suffix = request
95-
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE)
96-
.toString().replaceAll("[{}]", "-");
94+
Object bestMatchingPattern = request
95+
.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
96+
if (bestMatchingPattern != null) {
97+
suffix = bestMatchingPattern.toString().replaceAll("[{}]", "-");
9798
}
9899
else if (HttpStatus.valueOf(status).is4xxClientError()) {
99100
suffix = UNKNOWN_PATH_SUFFIX;

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/DataSourceHealthIndicator.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.sql.ResultSetMetaData;
2222
import java.sql.SQLException;
2323
import java.util.HashMap;
24+
import java.util.List;
2425
import java.util.Map;
2526

2627
import javax.sql.DataSource;
@@ -52,8 +53,8 @@ public class DataSourceHealthIndicator extends AbstractHealthIndicator {
5253
private static Map<String, String> queries = new HashMap<String, String>();
5354

5455
static {
55-
queries.put("HSQL Database Engine",
56-
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.SYSTEM_USERS");
56+
queries.put("HSQL Database Engine", "SELECT COUNT(*) FROM "
57+
+ "INFORMATION_SCHEMA.SYSTEM_USERS");
5758
queries.put("Oracle", "SELECT 'Hello' from DUAL");
5859
queries.put("Apache Derby", "SELECT 1 FROM SYSIBM.SYSDUMMY1");
5960
}
@@ -93,23 +94,11 @@ private void doDataSourceHealthCheck(Health.Builder builder) throws Exception {
9394
String query = detectQuery(product);
9495
if (StringUtils.hasText(query)) {
9596
try {
96-
builder.withDetail("hello", DataAccessUtils
97-
.requiredSingleResult(this.jdbcTemplate.query(query,
98-
new RowMapper<Object>() {
99-
100-
@Override
101-
public Object mapRow(ResultSet rs, int rowNum)
102-
throws SQLException {
103-
ResultSetMetaData rsmd = rs.getMetaData();
104-
int nrOfColumns = rsmd.getColumnCount();
105-
if (nrOfColumns != 1) {
106-
throw new IncorrectResultSetColumnCountException(
107-
1, nrOfColumns);
108-
}
109-
return JdbcUtils.getResultSetValue(rs, 1);
110-
}
111-
112-
})));
97+
// Avoid calling getObject as it breaks MySQL on Java 7
98+
List<Object> results = this.jdbcTemplate.query(query,
99+
new SingleColumnRowMapper());
100+
Object result = DataAccessUtils.requiredSingleResult(results);
101+
builder.withDetail("hello", result);
113102
}
114103
catch (Exception ex) {
115104
builder.down(ex);
@@ -147,4 +136,21 @@ public void setQuery(String query) {
147136
this.query = query;
148137
}
149138

139+
/**
140+
* {@link RowMapper} that expects and returns results from a single column.
141+
*/
142+
private static class SingleColumnRowMapper implements RowMapper<Object> {
143+
144+
@Override
145+
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
146+
ResultSetMetaData metaData = rs.getMetaData();
147+
int columns = metaData.getColumnCount();
148+
if (columns != 1) {
149+
throw new IncorrectResultSetColumnCountException(1, columns);
150+
}
151+
return JdbcUtils.getResultSetValue(rs, 1);
152+
}
153+
154+
}
155+
150156
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/MetricFilterAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,4 @@ public String testTemplateVariableResolution(@PathVariable String someVariable)
169169
public String testKnownPathWith404Response(@PathVariable String someVariable) {
170170
return someVariable;
171171
}
172-
}
172+
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/jar/JarCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private void addManifest(JarWriter writer, Class<?>[] compiledClasses)
191191
manifest.getMainAttributes().putValue("Start-Class",
192192
PackagedSpringApplicationLauncher.class.getName());
193193
manifest.getMainAttributes().putValue(
194-
PackagedSpringApplicationLauncher.SOURCE_MANIFEST_ENTRY,
194+
PackagedSpringApplicationLauncher.SOURCE_ENTRY,
195195
commaDelimitedClassNames(compiledClasses));
196196
writer.writeManifest(manifest);
197197
}

spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/DependencyCustomizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ protected boolean canAdd() {
257257
}
258258

259259
/**
260-
* @return the dependencyResolutionContext
260+
* Returns the {@link DependencyResolutionContext}.
261+
* @return the dependency resolution context
261262
*/
262263
public DependencyResolutionContext getDependencyResolutionContext() {
263264
return this.dependencyResolutionContext;

spring-boot-cli/src/main/java/org/springframework/boot/cli/jar/PackagedSpringApplicationLauncher.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.URL;
2121
import java.net.URLClassLoader;
2222
import java.util.Enumeration;
23+
import java.util.jar.Attributes;
2324
import java.util.jar.Manifest;
2425

2526
/**
@@ -30,9 +31,9 @@
3031
*/
3132
public class PackagedSpringApplicationLauncher {
3233

33-
public static final String SOURCE_MANIFEST_ENTRY = "Spring-Application-Source-Classes";
34+
public static final String SOURCE_ENTRY = "Spring-Application-Source-Classes";
3435

35-
public static final String MAIN_CLASS_MANIFEST_ENTRY = "Start-Class";
36+
public static final String START_CLASS_ENTRY = "Start-Class";
3637

3738
private static final String SPRING_APPLICATION_CLASS = "org.springframework.boot.SpringApplication";
3839

@@ -45,21 +46,25 @@ private void run(String[] args) throws Exception {
4546
}
4647

4748
private Object[] getSources(URLClassLoader classLoader) throws Exception {
48-
for (Enumeration<URL> urls = classLoader.findResources("META-INF/MANIFEST.MF"); urls
49-
.hasMoreElements();) {
49+
Enumeration<URL> urls = classLoader.findResources("META-INF/MANIFEST.MF");
50+
while (urls.hasMoreElements()) {
5051
URL url = urls.nextElement();
5152
Manifest manifest = new Manifest(url.openStream());
52-
if (getClass().getName().equals(
53-
manifest.getMainAttributes().getValue(MAIN_CLASS_MANIFEST_ENTRY))) {
54-
String attribute = manifest.getMainAttributes().getValue(
55-
SOURCE_MANIFEST_ENTRY);
56-
return loadClasses(classLoader, attribute.split(","));
53+
if (isCliPackaged(manifest)) {
54+
String sources = manifest.getMainAttributes().getValue(SOURCE_ENTRY);
55+
return loadClasses(classLoader, sources.split(","));
5756
}
5857
}
59-
throw new IllegalStateException("Cannot locate " + SOURCE_MANIFEST_ENTRY
58+
throw new IllegalStateException("Cannot locate " + SOURCE_ENTRY
6059
+ " in MANIFEST.MF");
6160
}
6261

62+
private boolean isCliPackaged(Manifest manifest) {
63+
Attributes attributes = manifest.getMainAttributes();
64+
String startClass = attributes.getValue(START_CLASS_ENTRY);
65+
return getClass().getName().equals(startClass);
66+
}
67+
6368
private Class<?>[] loadClasses(ClassLoader classLoader, String[] names)
6469
throws ClassNotFoundException {
6570
Class<?>[] classes = new Class<?>[names.length];

spring-boot-tools/spring-boot-loader-tools/src/main/java/org/springframework/boot/loader/tools/Repackager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ public void repackage(File destination, Libraries libraries) throws IOException
139139

140140
private boolean alreadyRepackaged() throws IOException {
141141
JarFile jarFile = new JarFile(this.source);
142-
143142
try {
144143
Manifest manifest = jarFile.getManifest();
145-
return manifest != null
146-
&& manifest.getMainAttributes().getValue(BOOT_VERSION_ATTRIBUTE) != null;
144+
return (manifest != null && manifest.getMainAttributes().getValue(
145+
BOOT_VERSION_ATTRIBUTE) != null);
147146
}
148147
finally {
149148
jarFile.close();
@@ -226,7 +225,7 @@ private Manifest buildManifest(JarFile source) throws IOException {
226225
String launcherClassName = this.layout.getLauncherClassName();
227226
if (launcherClassName != null) {
228227
manifest.getMainAttributes()
229-
.putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
228+
.putValue(MAIN_CLASS_ATTRIBUTE, launcherClassName);
230229
if (startClass == null) {
231230
throw new IllegalStateException("Unable to find main class");
232231
}

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.springframework.beans.InvalidPropertyException;
2929
import org.springframework.beans.MutablePropertyValues;
3030
import org.springframework.beans.PropertyValue;
31-
import org.springframework.core.convert.ConversionService;
3231
import org.springframework.core.convert.TypeDescriptor;
3332
import org.springframework.util.StringUtils;
3433
import org.springframework.validation.DataBinder;
@@ -48,8 +47,6 @@ public class RelaxedDataBinder extends DataBinder {
4847

4948
private boolean ignoreNestedProperties;
5049

51-
private ConversionService relaxedConversionService;
52-
5350
/**
5451
* Create a new {@link RelaxedDataBinder} instance.
5552
* @param target the target into which properties are bound
@@ -79,19 +76,12 @@ public void setIgnoreNestedProperties(boolean ignoreNestedProperties) {
7976
this.ignoreNestedProperties = ignoreNestedProperties;
8077
}
8178

82-
@Override
83-
public void setConversionService(ConversionService conversionService) {
84-
super.setConversionService(conversionService);
85-
this.relaxedConversionService = new RelaxedConversionService(getConversionService());
86-
}
87-
8879
@Override
8980
public void initBeanPropertyAccess() {
9081
super.initBeanPropertyAccess();
91-
this.relaxedConversionService = (this.relaxedConversionService != null
92-
? this.relaxedConversionService : new RelaxedConversionService(getConversionService()));
9382
// Hook in the RelaxedConversionService
94-
getInternalBindingResult().initConversion(relaxedConversionService);
83+
getInternalBindingResult().initConversion(
84+
new RelaxedConversionService(getConversionService()));
9585
}
9686

9787
@Override
@@ -120,13 +110,13 @@ private MutablePropertyValues modifyProperties(MutablePropertyValues propertyVal
120110
propertyValues = addMapPrefix(propertyValues);
121111
}
122112

123-
BeanWrapper targetWrapper = new BeanWrapperImpl(target);
124-
targetWrapper.setConversionService(this.relaxedConversionService);
125-
targetWrapper.setAutoGrowNestedPaths(true);
113+
BeanWrapper wrapper = new BeanWrapperImpl(target);
114+
wrapper.setConversionService(new RelaxedConversionService(getConversionService()));
115+
wrapper.setAutoGrowNestedPaths(true);
126116

127117
List<PropertyValue> list = propertyValues.getPropertyValueList();
128118
for (int i = 0; i < list.size(); i++) {
129-
modifyProperty(propertyValues, targetWrapper, list.get(i), i);
119+
modifyProperty(propertyValues, wrapper, list.get(i), i);
130120
}
131121
return propertyValues;
132122
}

0 commit comments

Comments
 (0)