From 043a4ad4ff235ce28bc1bcbd594c50a262caf373 Mon Sep 17 00:00:00 2001 From: gtully Date: Thu, 24 Oct 2013 12:59:15 +0100 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-4820 - find elements by ns and local name to deal with namespace prefix. Ensure we trap all errors on plugin start so we don't cause a failed broker start, and trap no broker element with a better error --- .../plugin/RuntimeConfigurationBroker.java | 22 +++++-- .../apache/activemq/CustomPropertiesBean.java | 37 +++++++++++ .../apache/activemq/NameSpaceXmlLoadTest.java | 58 +++++++++++++++++ .../org/apache/activemq/namespace-prefix.xml | 64 +++++++++++++++++++ 4 files changed, 174 insertions(+), 7 deletions(-) create mode 100644 activemq-runtime-config/src/test/java/org/apache/activemq/CustomPropertiesBean.java create mode 100644 activemq-runtime-config/src/test/java/org/apache/activemq/NameSpaceXmlLoadTest.java create mode 100644 activemq-runtime-config/src/test/resources/org/apache/activemq/namespace-prefix.xml diff --git a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/RuntimeConfigurationBroker.java b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/RuntimeConfigurationBroker.java index 5c38d94994..58a11e19bd 100644 --- a/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/RuntimeConfigurationBroker.java +++ b/activemq-runtime-config/src/main/java/org/apache/activemq/plugin/RuntimeConfigurationBroker.java @@ -657,16 +657,22 @@ public class RuntimeConfigurationBroker extends BrokerFilter { dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(configToMonitor.getInputStream()); - Node brokerRootNode = doc.getElementsByTagName("broker").item(0); + Node brokerRootNode = doc.getElementsByTagNameNS("*","broker").item(0); - JAXBElement brokerJAXBElement = - unMarshaller.unmarshal(brokerRootNode, DtoBroker.class); - jaxbConfig = brokerJAXBElement.getValue(); + if (brokerRootNode != null) { - // if we can parse we can track mods - lastModified = configToMonitor.lastModified(); + JAXBElement brokerJAXBElement = + unMarshaller.unmarshal(brokerRootNode, DtoBroker.class); + jaxbConfig = brokerJAXBElement.getValue(); - loadPropertiesPlaceHolderSupport(doc); + // if we can parse we can track mods + lastModified = configToMonitor.lastModified(); + + loadPropertiesPlaceHolderSupport(doc); + + } else { + info("Failed to find 'broker' element by tag in: " + configToMonitor); + } } catch (IOException e) { info("Failed to access: " + configToMonitor, e); @@ -676,6 +682,8 @@ public class RuntimeConfigurationBroker extends BrokerFilter { info("Failed to document parse: " + configToMonitor, e); } catch (SAXException e) { info("Failed to find broker element in: " + configToMonitor, e); + } catch (Exception e) { + info("Unexpected exception during load of: " + configToMonitor, e); } } return jaxbConfig; diff --git a/activemq-runtime-config/src/test/java/org/apache/activemq/CustomPropertiesBean.java b/activemq-runtime-config/src/test/java/org/apache/activemq/CustomPropertiesBean.java new file mode 100644 index 0000000000..97aebf45eb --- /dev/null +++ b/activemq-runtime-config/src/test/java/org/apache/activemq/CustomPropertiesBean.java @@ -0,0 +1,37 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.activemq; + +import java.util.Properties; +import org.springframework.beans.factory.FactoryBean; + +public class CustomPropertiesBean implements FactoryBean { + @Override + public Properties getObject() throws Exception { + return System.getProperties(); + } + + @Override + public Class getObjectType() { + return Properties.class; + } + + @Override + public boolean isSingleton() { + return false; + } +} diff --git a/activemq-runtime-config/src/test/java/org/apache/activemq/NameSpaceXmlLoadTest.java b/activemq-runtime-config/src/test/java/org/apache/activemq/NameSpaceXmlLoadTest.java new file mode 100644 index 0000000000..f66918c44b --- /dev/null +++ b/activemq-runtime-config/src/test/java/org/apache/activemq/NameSpaceXmlLoadTest.java @@ -0,0 +1,58 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.activemq; + +import java.util.HashMap; +import javax.management.ObjectName; +import org.apache.activemq.plugin.RuntimeConfigurationBroker; +import org.apache.activemq.plugin.jmx.RuntimeConfigurationViewMBean; +import org.apache.activemq.util.IOHelper; +import org.apache.activemq.util.IntrospectionSupport; +import org.junit.Test; + + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +public class NameSpaceXmlLoadTest extends RuntimeConfigTestSupport { + + @Test + public void testCanLoad() throws Exception { + final String brokerConfig = "namespace-prefix"; + System.setProperty("data", IOHelper.getDefaultDataDirectory()); + System.setProperty("broker-name", brokerConfig); + startBroker(brokerConfig); + assertTrue("broker alive", brokerService.isStarted()); + assertEquals("nameMatch", brokerConfig, brokerService.getBrokerName()); + + // verify runtimeConfig active + ObjectName objectName = + new ObjectName(brokerService.getBrokerObjectName().toString() + + RuntimeConfigurationBroker.objectNamePropsAppendage); + RuntimeConfigurationViewMBean runtimeConfigurationView = + (RuntimeConfigurationViewMBean) brokerService.getManagementContext().newProxyInstance(objectName, + RuntimeConfigurationViewMBean.class, false); + + HashMap props = new HashMap(); + IntrospectionSupport.getProperties(runtimeConfigurationView, props, null); + LOG.info("mbean attributes before: " + props); + String propOfInterest = "modified"; + assertNotEquals("modified is valid", "unknown", props.get(propOfInterest)); + + } +} diff --git a/activemq-runtime-config/src/test/resources/org/apache/activemq/namespace-prefix.xml b/activemq-runtime-config/src/test/resources/org/apache/activemq/namespace-prefix.xml new file mode 100644 index 0000000000..40ae71e4b1 --- /dev/null +++ b/activemq-runtime-config/src/test/resources/org/apache/activemq/namespace-prefix.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file