Reinstate JMX customizations of Endpoints ObjectName
This commit restores the configuration properties used to configure how the ObjectName of an endpoint is generated. For consistency, those properties have been renamed to `management.jmx` Closes gh-10005pull/10012/merge
parent
35cf0c56a8
commit
151f7ef325
@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.endpoint.infrastructure;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration properties for JMX export of endpoints.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
* @since 2.0.0
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties("management.jmx")
|
||||||
|
public class JmxEndpointExporterProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoints JMX domain name. Fallback to 'spring.jmx.default-domain' if set.
|
||||||
|
*/
|
||||||
|
private String domain = "org.springframework.boot";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that ObjectNames are modified in case of conflict.
|
||||||
|
*/
|
||||||
|
private boolean uniqueNames = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Additional static properties to append to all ObjectNames of MBeans representing
|
||||||
|
* Endpoints.
|
||||||
|
*/
|
||||||
|
private final Properties staticNames = new Properties();
|
||||||
|
|
||||||
|
public JmxEndpointExporterProperties(Environment environment) {
|
||||||
|
String defaultDomain = environment.getProperty("spring.jmx.default-domain");
|
||||||
|
if (StringUtils.hasText(defaultDomain)) {
|
||||||
|
this.domain = defaultDomain;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDomain() {
|
||||||
|
return this.domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDomain(String domain) {
|
||||||
|
this.domain = domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isUniqueNames() {
|
||||||
|
return this.uniqueNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUniqueNames(boolean uniqueNames) {
|
||||||
|
this.uniqueNames = uniqueNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Properties getStaticNames() {
|
||||||
|
return this.staticNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2017 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.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.actuate.autoconfigure.endpoint.infrastructure;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
import javax.management.MBeanServer;
|
||||||
|
import javax.management.MalformedObjectNameException;
|
||||||
|
import javax.management.ObjectName;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.endpoint.jmx.EndpointMBean;
|
||||||
|
import org.springframework.mock.env.MockEnvironment;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link DefaultEndpointObjectNameFactory}.
|
||||||
|
*
|
||||||
|
* @author Stephane Nicoll
|
||||||
|
*/
|
||||||
|
public class DefaultEndpointObjectNameFactoryTests {
|
||||||
|
|
||||||
|
private final MockEnvironment environment = new MockEnvironment();
|
||||||
|
|
||||||
|
private final JmxEndpointExporterProperties properties = new JmxEndpointExporterProperties(this.environment);
|
||||||
|
|
||||||
|
private final MBeanServer mBeanServer = mock(MBeanServer.class);
|
||||||
|
|
||||||
|
private String contextId;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectName() {
|
||||||
|
ObjectName objectName = generateObjectName(endpoint("Test"));
|
||||||
|
assertThat(objectName.toString()).isEqualTo(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectNameWithCapitalizedId() {
|
||||||
|
ObjectName objectName = generateObjectName(endpoint("test"));
|
||||||
|
assertThat(objectName.toString()).isEqualTo(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectNameWithCustomDomain() {
|
||||||
|
this.properties.setDomain("com.example.acme");
|
||||||
|
ObjectName objectName = generateObjectName(endpoint("test"));
|
||||||
|
assertThat(objectName.toString()).isEqualTo(
|
||||||
|
"com.example.acme:type=Endpoint,name=Test");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectNameWithUniqueNames() {
|
||||||
|
this.properties.setUniqueNames(true);
|
||||||
|
EndpointMBean endpoint = endpoint("test");
|
||||||
|
String id = ObjectUtils.getIdentityHexString(endpoint);
|
||||||
|
ObjectName objectName = generateObjectName(endpoint);
|
||||||
|
assertThat(objectName.toString()).isEqualTo(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test,identity=" + id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectNameWithStaticNames() {
|
||||||
|
this.properties.getStaticNames().setProperty("counter", "42");
|
||||||
|
this.properties.getStaticNames().setProperty("foo", "bar");
|
||||||
|
ObjectName objectName = generateObjectName(endpoint("test"));
|
||||||
|
assertThat(objectName.getKeyProperty("counter")).isEqualTo("42");
|
||||||
|
assertThat(objectName.getKeyProperty("foo")).isEqualTo("bar");
|
||||||
|
assertThat(objectName.toString()).startsWith(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test,");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateObjectNameWithDuplicate() throws MalformedObjectNameException {
|
||||||
|
this.contextId = "testContext";
|
||||||
|
given(this.mBeanServer.queryNames(new ObjectName(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test,*"), null))
|
||||||
|
.willReturn(Collections.singleton(
|
||||||
|
new ObjectName("org.springframework.boot:type=Endpoint,name=Test")));
|
||||||
|
ObjectName objectName = generateObjectName(endpoint("test"));
|
||||||
|
assertThat(objectName.toString()).isEqualTo(
|
||||||
|
"org.springframework.boot:type=Endpoint,name=Test,context=testContext");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private ObjectName generateObjectName(EndpointMBean endpointMBean) {
|
||||||
|
try {
|
||||||
|
return new DefaultEndpointObjectNameFactory(this.properties, this.mBeanServer,
|
||||||
|
this.contextId).generate(endpointMBean);
|
||||||
|
}
|
||||||
|
catch (MalformedObjectNameException ex) {
|
||||||
|
throw new AssertionError("Invalid object name", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private EndpointMBean endpoint(String id) {
|
||||||
|
EndpointMBean endpointMBean = mock(EndpointMBean.class);
|
||||||
|
given(endpointMBean.getEndpointId()).willReturn(id);
|
||||||
|
return endpointMBean;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue