INT-1675: add illegal bean id check for clash with core mbean exporter

This commit is contained in:
Dave Syer
2010-12-10 10:35:28 +01:00
parent 72166141d4
commit c293edc7fa
9 changed files with 108 additions and 29 deletions

View File

@@ -1,24 +1,23 @@
/*
* Copyright 2002-2010 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.
*
* 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.integration.jmx.config;
import javax.management.MBeanServerFactory;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
@@ -31,7 +30,9 @@ import org.w3c.dom.Element;
* @since 2.0
*/
public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
private static final String ILLEGAL_NAME = "mbeanExporter";
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
@@ -54,10 +55,22 @@ public class MBeanExporterParser extends AbstractSingleBeanDefinitionParser {
String mbeanServer = element.getAttribute("server");
if (StringUtils.hasText(mbeanServer)) {
return new RuntimeBeanReference(mbeanServer);
}
else {
} else {
return MBeanServerFactory.createMBeanServer();
}
}
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
throws BeanDefinitionStoreException {
String id = super.resolveId(element, definition, parserContext);
if (ILLEGAL_NAME.equals(id)) {
parserContext.getReaderContext().error(
"Illegal bean id for <jmx:mbean-export/>: " + ILLEGAL_NAME
+ " (clashes with <context:mbean-export/> default). Please choose another bean id.",
definition);
}
return id;
}
}

View File

@@ -21,6 +21,6 @@
<si:control-bus input-channel="controlChannel"/>
<jmx:mbean-export id="mbeanExporter" server="mbs" default-domain="tests.ControlBusParser"/>
<jmx:mbean-export id="integrationMbeanExporter" server="mbs" default-domain="tests.ControlBusParser"/>
</beans>

View File

@@ -41,7 +41,7 @@ public class ControlBusParserTests {
MessageChannel control = this.context.getBean("controlChannel", MessageChannel.class);
MessagingTemplate messagingTemplate = new MessagingTemplate();
Object value = messagingTemplate.convertSendAndReceive(control,
"@mbeanExporter.getChannelSendRate('testChannel').count");
"@integrationMbeanExporter.getChannelSendRate('testChannel').count");
assertEquals(new Integer(0), value);
MBeanExporter exporter = this.context.getBean(MBeanExporter.class);
exporter.destroy();

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<jmx:mbean-export id="mbeanExporter" server="mbs" default-domain="test.MBeanExporterName"/>
<context:mbean-server id="mbs" />
<context:mbean-export server="mbs" default-domain="test.MBeanExporterName"/>
<int:channel id="testChannel" />
<int:service-activator id="service" input-channel="testChannel" method="get">
<bean class="org.springframework.integration.jmx.config.MBeanExporterNameTests$Source"/>
</int:service-activator>
<int:logging-channel-adapter id="logger" channel="testChannel"/>
</beans>

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-2010 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.integration.jmx.config;
import org.junit.Test;
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Dave Syer
* @since 2.0
*/
public class MBeanExporterNameTests {
@Test(expected = BeanDefinitionParsingException.class)
public void testHandlerMBeanRegistration() throws Exception {
new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml", getClass());
}
public static class Source {
public String get() {
return "foo";
}
}
}

View File

@@ -17,6 +17,6 @@
<si:channel id="testChannel"/>
<jmx:mbean-export id="mbeanExporter" server="mbs" default-domain="tests.MBeanExpoerterParser"/>
<jmx:mbean-export id="integratioMbeanExporter" server="mbs" default-domain="tests.MBeanExpoerterParser"/>
</beans>

View File

@@ -12,11 +12,13 @@
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<jmx:mbean-export id="integrationMbeanExporter" server="mbs" default-domain="test.MBeanRegistration"/>
<context:mbean-server id="mbs" />
<context:mbean-export server="mbs" default-domain="test.MBeanRegistration"/>
<int:publish-subscribe-channel id="testChannel" />
<int:channel id="testChannel" />
<int:service-activator id="service" input-channel="testChannel" method="get">
<bean class="org.springframework.integration.jmx.config.MBeanRegistrationTests$Source"/>
@@ -24,6 +26,4 @@
<int:logging-channel-adapter id="logger" channel="testChannel"/>
<jmx:mbean-export id="integrationMbeanExporter" server="mbs" default-domain="test.MBeanRegistration"/>
</beans>

View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:int="http://www.springframework.org/schema/integration"
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:int="http://www.springframework.org/schema/integration"
xmlns:jmx="http://www.springframework.org/schema/integration/jmx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
@@ -12,19 +11,19 @@
http://www.springframework.org/schema/integration/jmx
http://www.springframework.org/schema/integration/jmx/spring-integration-jmx.xsd">
<jmx:mbean-export server="mbs" default-domain="test.RouterMBean" />
<context:mbean-export server="mbs" default-domain="test.RouterMBean" />
<context:mbean-server id="mbs" />
<int:channel id="testChannel" />
<int:channel id="intChannel" />
<int:channel id="stringChannel" />
<int:payload-type-router id="ptRouter"
input-channel="testChannel">
<int:payload-type-router id="ptRouter" input-channel="testChannel">
<int:mapping type="java.lang.String" channel="stringChannel" />
<int:mapping type="java.lang.Integer" channel="intChannel" />
</int:payload-type-router>
<jmx:mbean-export id="mbeanExporter" server="mbs"
default-domain="test.RouterMBean" />
</beans>

View File

@@ -39,6 +39,7 @@ public class RouterMBeanTests {
@Test
public void testRouterMBeanExists() throws Exception {
// System.err.println(server.queryNames(new ObjectName("test.RouterMBean:*"), null));
Set<ObjectName> names = server.queryNames(new ObjectName("test.RouterMBean:type=MessageHandler,name=ptRouter,*"), null);
assertEquals(1, names.size());
}