Use ServletWrappingController for jolokia instead of Servlet
We get more control over the handling and in particular the registration of the endpoint this way. It was practically impossible to disable the AgentServlet bean when in a parent context of the management server because of lifecyce issues - you don't know that the user wants a separate management server until too late. This approach also makes it possible to test with spring-test MVC support.
This commit is contained in:
@@ -249,23 +249,30 @@ public class PropertiesConfigurationFactory<T> implements FactoryBean<T>,
|
||||
customizeBinder(dataBinder);
|
||||
|
||||
Set<String> names = new HashSet<String>();
|
||||
Set<String> patterns = new HashSet<String>();
|
||||
if (this.target != null) {
|
||||
PropertyDescriptor[] descriptors = BeanUtils
|
||||
.getPropertyDescriptors(this.target.getClass());
|
||||
String[] prefixes = this.targetName != null ? new String[] {
|
||||
this.targetName + ".", this.targetName + "_" } : new String[] { "" };
|
||||
String[] suffixes = new String[] { ".*", "_*" };
|
||||
for (PropertyDescriptor descriptor : descriptors) {
|
||||
String name = descriptor.getName();
|
||||
if (!name.equals("class")) {
|
||||
names.add(name);
|
||||
names.add(name + ".*");
|
||||
names.add(name + "_*");
|
||||
names.add("*_"+name);
|
||||
for (String prefix : prefixes) {
|
||||
names.add(prefix + name);
|
||||
patterns.add(prefix + name);
|
||||
for (String suffix : suffixes) {
|
||||
patterns.add(prefix + name + suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PropertyValues propertyValues = (this.properties != null ? new MutablePropertyValues(
|
||||
this.properties) : new PropertySourcesPropertyValues(
|
||||
this.propertySources, names));
|
||||
this.propertySources, patterns, names));
|
||||
dataBinder.bind(propertyValues);
|
||||
|
||||
if (this.validator != null) {
|
||||
|
||||
@@ -45,37 +45,39 @@ public class PropertySourcesPropertyValues implements PropertyValues {
|
||||
|
||||
private PropertySources propertySources;
|
||||
|
||||
private Collection<String> NON_ENUMERABLES = Arrays.asList(
|
||||
private Collection<String> NON_ENUMERABLE_ENUMERABLES = Arrays.asList(
|
||||
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
|
||||
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);;
|
||||
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
|
||||
/**
|
||||
* Create a new PropertyValues from the given PropertySources
|
||||
* @param propertySources a PropertySources instance
|
||||
*/
|
||||
public PropertySourcesPropertyValues(PropertySources propertySources) {
|
||||
this(propertySources, null);
|
||||
this(propertySources, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new PropertyValues from the given PropertySources
|
||||
* @param propertySources a PropertySources instance
|
||||
* @param systemPropertyNames property names to include from system properties and
|
||||
* @param patterns property name patterns to include from system properties and
|
||||
* environment variables
|
||||
* @param names exact property names to include
|
||||
*/
|
||||
public PropertySourcesPropertyValues(PropertySources propertySources,
|
||||
Collection<String> systemPropertyNames) {
|
||||
Collection<String> patterns, Collection<String> names) {
|
||||
this.propertySources = propertySources;
|
||||
PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(
|
||||
propertySources);
|
||||
String[] includes = systemPropertyNames == null ? new String[0]
|
||||
: systemPropertyNames.toArray(new String[0]);
|
||||
String[] includes = patterns == null ? new String[0] : patterns
|
||||
.toArray(new String[0]);
|
||||
String[] exacts = names == null ? new String[0] : names.toArray(new String[0]);
|
||||
for (PropertySource<?> source : propertySources) {
|
||||
if (source instanceof EnumerablePropertySource) {
|
||||
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
|
||||
if (enumerable.getPropertyNames().length > 0) {
|
||||
for (String propertyName : enumerable.getPropertyNames()) {
|
||||
if (this.NON_ENUMERABLES.contains(source.getName())
|
||||
if (this.NON_ENUMERABLE_ENUMERABLES.contains(source.getName())
|
||||
&& !PatternMatchUtils.simpleMatch(includes, propertyName)) {
|
||||
continue;
|
||||
}
|
||||
@@ -91,6 +93,25 @@ public class PropertySourcesPropertyValues implements PropertyValues {
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
// We can only do exact matches for non-enumerable property names, but
|
||||
// that's better than nothing...
|
||||
for (String propertyName : exacts) {
|
||||
Object value;
|
||||
value = source.getProperty(propertyName);
|
||||
if (value != null) {
|
||||
this.propertyValues.put(propertyName, new PropertyValue(
|
||||
propertyName, value));
|
||||
continue;
|
||||
}
|
||||
value = source.getProperty(propertyName.toUpperCase());
|
||||
if (value != null) {
|
||||
this.propertyValues.put(propertyName, new PropertyValue(
|
||||
propertyName, value));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ public class ServletRegistrationBean extends RegistrationBean {
|
||||
|
||||
private Set<String> urlMappings = new LinkedHashSet<String>();
|
||||
|
||||
private int loadOnStartup = 1;
|
||||
private int loadOnStartup = -1;
|
||||
|
||||
private MultipartConfigElement multipartConfig;
|
||||
|
||||
|
||||
@@ -90,6 +90,15 @@ public class PropertySourcesPropertyValuesTests {
|
||||
assertEquals("bar", target.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaceholdersBindingNonEnumerable() {
|
||||
FooBean target = new FooBean();
|
||||
DataBinder binder = new DataBinder(target);
|
||||
binder.bind(new PropertySourcesPropertyValues(this.propertySources, null,
|
||||
Collections.singleton("foo")));
|
||||
assertEquals("bar", target.getFoo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPlaceholdersBindingWithError() {
|
||||
TestBean target = new TestBean();
|
||||
@@ -112,4 +121,16 @@ public class PropertySourcesPropertyValuesTests {
|
||||
}
|
||||
}
|
||||
|
||||
public static class FooBean {
|
||||
private String foo;
|
||||
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -88,8 +88,7 @@ public class SpringApplicationContextLoader extends AbstractContextLoader {
|
||||
private Map<String, Object> getArgs(MergedContextConfiguration mergedConfig) {
|
||||
Map<String, Object> args = new LinkedHashMap<String, Object>();
|
||||
// Not running an embedded server, just setting up web context
|
||||
args.put("server.port", "0");
|
||||
args.put("management.port", "0");
|
||||
args.put("server.port", "-1");
|
||||
return args;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user