You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jb...@apache.org on 2018/01/04 20:30:35 UTC

[2/2] activemq-artemis git commit: ARTEMIS-1575 configure custom broker plugins with key/value properties.

ARTEMIS-1575 configure custom broker plugins with key/value properties.

The properties are read and passed into the the broker plugin's init(Map<String,String>)


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/d9575705
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/d9575705
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/d9575705

Branch: refs/heads/master
Commit: d95757057e4094d393a1745846cee2012bb5df35
Parents: 1ef8199
Author: Pat Fox <pa...@gmail.com>
Authored: Sat Dec 23 21:04:31 2017 +0100
Committer: Justin Bertram <jb...@apache.org>
Committed: Thu Jan 4 14:27:41 2018 -0600

----------------------------------------------------------------------
 .../deployers/impl/FileConfigurationParser.java | 29 ++++++---
 .../server/plugin/ActiveMQServerPlugin.java     |  7 +++
 .../resources/schema/artemis-configuration.xsd  | 46 ++++++++------
 .../src/test/resources/brokerPlugin.xml         |  6 +-
 .../test/resources/artemis-configuration.xsd    | 37 +++++------
 docs/user-manual/en/broker-plugins.md           | 10 ++-
 .../plugin/ConfigurationVerifier.java           | 64 ++++++++++++++++++++
 .../integration/plugin/CorePluginTest.java      |  8 +++
 .../integration/plugin/XmlConfigPluginTest.java |  7 ++-
 .../test/resources/broker-plugins-config.xml    |  4 ++
 10 files changed, 168 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
index b29e1b4..9b9050b 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java
@@ -703,6 +703,8 @@ public final class FileConfigurationParser extends XMLConfigurationUtil {
    private ActiveMQServerPlugin parseActiveMQServerPlugin(Node item) {
       final String clazz = item.getAttributes().getNamedItem("class-name").getNodeValue();
 
+      Map<String, String> properties = getMapOfChildPropertyElements(item);
+
       ActiveMQServerPlugin serverPlugin = AccessController.doPrivileged(new PrivilegedAction<ActiveMQServerPlugin>() {
          @Override
          public ActiveMQServerPlugin run() {
@@ -710,9 +712,25 @@ public final class FileConfigurationParser extends XMLConfigurationUtil {
          }
       });
 
+      serverPlugin.init(properties);
+
       return serverPlugin;
    }
 
+   private Map<String, String> getMapOfChildPropertyElements(Node item) {
+      Map<String, String> properties = new HashMap<>();
+      NodeList children = item.getChildNodes();
+      for (int i = 0; i < children.getLength(); i++) {
+         Node child = children.item(i);
+         if (child.getNodeName().equals("property")) {
+            String key = getAttributeValue(child, "key");
+            String value = getAttributeValue(child, "value");
+            properties.put(key, value);
+         }
+      }
+      return properties;
+   }
+
    /**
     * @param e
     * @param config
@@ -1656,16 +1674,7 @@ public final class FileConfigurationParser extends XMLConfigurationUtil {
       Element element = (Element) node;
       String className = getString(element, "class-name", null, Validators.NO_CHECK);
 
-      Map<String, String> properties = new HashMap<>();
-      NodeList children = element.getChildNodes();
-      for (int j = 0; j < children.getLength(); j++) {
-         Node child = children.item(j);
-         if (child.getNodeName().equals("property")) {
-            String key = getAttributeValue(child, "key");
-            String value = getAttributeValue(child, "value");
-            properties.put(key, value);
-         }
-      }
+      Map<String, String> properties = getMapOfChildPropertyElements(element);
       return new TransformerConfiguration(className).setProperties(properties);
    }
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
index fddec48..c5e22ef 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/plugin/ActiveMQServerPlugin.java
@@ -460,4 +460,11 @@ public interface ActiveMQServerPlugin {
    default void criticalFailure(CriticalComponent components) throws ActiveMQException {
    }
 
+   /**
+    * used to pass configured properties to Plugin
+    *
+    * @param properties
+    */
+   default void init(Map<String, String> properties) {
+   }
 }

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/artemis-server/src/main/resources/schema/artemis-configuration.xsd
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/resources/schema/artemis-configuration.xsd b/artemis-server/src/main/resources/schema/artemis-configuration.xsd
index 18ad6e4..58abd67 100644
--- a/artemis-server/src/main/resources/schema/artemis-configuration.xsd
+++ b/artemis-server/src/main/resources/schema/artemis-configuration.xsd
@@ -934,6 +934,15 @@
                               a broker plugin
                            </xsd:documentation>
                         </xsd:annotation>
+                        <xsd:sequence>
+                           <xsd:element ref="property" maxOccurs="unbounded" minOccurs="0">
+                              <xsd:annotation>
+                                 <xsd:documentation>
+                                    properties to configure a plugin
+                                 </xsd:documentation>
+                              </xsd:annotation>
+                           </xsd:element>
+                        </xsd:sequence>
                         <xsd:attribute name="class-name" type="xsd:string" use="required">
                            <xsd:annotation>
                               <xsd:documentation>
@@ -1443,7 +1452,7 @@
                </xsd:documentation>
             </xsd:annotation>
          </xsd:element>
-         <xsd:element name="property" type="transformerProperty" maxOccurs="unbounded" minOccurs="0">
+         <xsd:element ref="property"  maxOccurs="unbounded" minOccurs="0">
             <xsd:annotation>
                <xsd:documentation>
                   properties to configure the transformer class
@@ -1453,23 +1462,24 @@
       </xsd:sequence>
    </xsd:complexType>
 
-
-    <xsd:complexType name="transformerProperty">
-       <xsd:attribute name="key" type="xsd:string" use="required">
-          <xsd:annotation>
-             <xsd:documentation>
-                key for the property
-             </xsd:documentation>
-          </xsd:annotation>
-       </xsd:attribute>
-       <xsd:attribute name="value" type="xsd:string" use="required">
-          <xsd:annotation>
-             <xsd:documentation>
-                value for the property
-             </xsd:documentation>
-          </xsd:annotation>
-       </xsd:attribute>
-    </xsd:complexType>
+   <xsd:element name="property">
+      <xsd:complexType>
+         <xsd:attribute name="key" type="xsd:string" use="required">
+            <xsd:annotation>
+               <xsd:documentation>
+                  key for the property
+               </xsd:documentation>
+            </xsd:annotation>
+         </xsd:attribute>
+         <xsd:attribute name="value" type="xsd:string" use="required">
+            <xsd:annotation>
+               <xsd:documentation>
+                  value for the property
+               </xsd:documentation>
+            </xsd:annotation>
+         </xsd:attribute>
+      </xsd:complexType>
+   </xsd:element>
 
 
    <!-- CLUSTER CONNECTION CONFIGURATION -->

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/artemis-server/src/test/resources/brokerPlugin.xml
----------------------------------------------------------------------
diff --git a/artemis-server/src/test/resources/brokerPlugin.xml b/artemis-server/src/test/resources/brokerPlugin.xml
index 6b1da34..828b0ec 100644
--- a/artemis-server/src/test/resources/brokerPlugin.xml
+++ b/artemis-server/src/test/resources/brokerPlugin.xml
@@ -20,7 +20,11 @@
         xsi:schemaLocation="urn:activemq ../../../../activemq-server/src/main/resources/schema/artemis-server.xsd">
    <core xmlns="urn:activemq:core">
       <broker-plugins>
-         <broker-plugin class-name="org.apache.activemq.artemis.core.config.impl.FileConfigurationTest$EmptyPlugin1" />
+         <broker-plugin class-name="org.apache.activemq.artemis.core.config.impl.FileConfigurationTest$EmptyPlugin1">
+            <property key="key1" value="value1"/>
+            <property key="key2" value="value2"/>
+            <property key="key3" value="value3"/>
+         </broker-plugin>
          <broker-plugin class-name="org.apache.activemq.artemis.core.config.impl.FileConfigurationTest$EmptyPlugin2" />
       </broker-plugins>
    </core>

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/artemis-tools/src/test/resources/artemis-configuration.xsd
----------------------------------------------------------------------
diff --git a/artemis-tools/src/test/resources/artemis-configuration.xsd b/artemis-tools/src/test/resources/artemis-configuration.xsd
index 47a92ff..c0b88f9 100644
--- a/artemis-tools/src/test/resources/artemis-configuration.xsd
+++ b/artemis-tools/src/test/resources/artemis-configuration.xsd
@@ -1271,7 +1271,7 @@
                </xsd:documentation>
             </xsd:annotation>
          </xsd:element>
-         <xsd:element name="property" type="transformerProperty" maxOccurs="unbounded" minOccurs="0">
+         <xsd:element ref="property"  maxOccurs="unbounded" minOccurs="0">
             <xsd:annotation>
                <xsd:documentation>
                   properties to configure the transformer class
@@ -1281,23 +1281,24 @@
       </xsd:sequence>
    </xsd:complexType>
 
-
-   <xsd:complexType name="transformerProperty">
-      <xsd:attribute name="key" type="xsd:string" use="required">
-         <xsd:annotation>
-            <xsd:documentation>
-               key for the property
-            </xsd:documentation>
-         </xsd:annotation>
-      </xsd:attribute>
-      <xsd:attribute name="value" type="xsd:string" use="required">
-         <xsd:annotation>
-            <xsd:documentation>
-               value for the property
-            </xsd:documentation>
-         </xsd:annotation>
-      </xsd:attribute>
-   </xsd:complexType>
+   <xsd:element name="property">
+      <xsd:complexType>
+         <xsd:attribute name="key" type="xsd:string" use="required">
+            <xsd:annotation>
+               <xsd:documentation>
+                  key for the property
+               </xsd:documentation>
+            </xsd:annotation>
+         </xsd:attribute>
+         <xsd:attribute name="value" type="xsd:string" use="required">
+            <xsd:annotation>
+               <xsd:documentation>
+                  value for the property
+               </xsd:documentation>
+            </xsd:annotation>
+         </xsd:attribute>
+      </xsd:complexType>
+   </xsd:element>
 
 
    <!-- CLUSTER CONNECTION CONFIGURATION -->

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/docs/user-manual/en/broker-plugins.md
----------------------------------------------------------------------
diff --git a/docs/user-manual/en/broker-plugins.md b/docs/user-manual/en/broker-plugins.md
index d524f68..9d19c16 100644
--- a/docs/user-manual/en/broker-plugins.md
+++ b/docs/user-manual/en/broker-plugins.md
@@ -16,14 +16,20 @@ If you are using an embed system than you will need the jar under the regular cl
 
 ## Registering a Plugin
 
-To register a plugin with by XML you need to add the `broker-plugins` element at the `broker.xml`.
+To register a plugin with by XML you need to add the `broker-plugins` element at the `broker.xml`. It is also possible
+to pass configuration to a plugin using the `property` child element(s). These properties (zero to many)
+will be read and passed into the Plugin's `init(Map<String, String>)` operation after the plugin
+has been instantiated.
 
 ```xml
 <configuration ...>
 
 ...
     <broker-plugins>
-        <broker-plugin class-name="some.plugin.UserPlugin" />
+        <broker-plugin class-name="some.plugin.UserPlugin">
+            <property key="property1" value="val_1" />
+            <property key="property2" value="val_2" />
+        </broker-plugin>
     </broker-plugins>
 ...
 

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ConfigurationVerifier.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ConfigurationVerifier.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ConfigurationVerifier.java
new file mode 100644
index 0000000..88f0ec3
--- /dev/null
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/ConfigurationVerifier.java
@@ -0,0 +1,64 @@
+/*
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.artemis.tests.integration.plugin;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.activemq.artemis.api.core.ActiveMQException;
+import org.apache.activemq.artemis.api.core.Message;
+import org.apache.activemq.artemis.core.postoffice.RoutingStatus;
+import org.apache.activemq.artemis.core.server.ServerSession;
+import org.apache.activemq.artemis.core.server.plugin.ActiveMQServerPlugin;
+import org.apache.activemq.artemis.core.transaction.Transaction;
+
+/**
+ * Used in tests to verify configuration passed into plugin correctly.
+ */
+public class ConfigurationVerifier implements ActiveMQServerPlugin, Serializable {
+
+   public static final String PROPERTY1 = "property1";
+   public static final String PROPERTY2 = "property2";
+   public static final String PROPERTY3 = "property3";
+
+   public String value1;
+   public String value2;
+   public String value3;
+   public AtomicInteger afterSendCounter = new AtomicInteger();
+
+   @Override
+   public void init(Map<String, String> properties) {
+      value1 = properties.get(PROPERTY1);
+      value2 = properties.get(PROPERTY2);
+      value3 = properties.get(PROPERTY3);
+   }
+
+   /**
+    * Used to ensure the plugin is being invoked
+    */
+   @Override
+   public void afterSend(ServerSession session,
+                         Transaction tx,
+                         Message message,
+                         boolean direct,
+                         boolean noAutoCreateQueue,
+                         RoutingStatus result) throws ActiveMQException {
+      afterSendCounter.incrementAndGet();
+   }
+
+}

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/CorePluginTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/CorePluginTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/CorePluginTest.java
index 191869d..7df0ac0 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/CorePluginTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/CorePluginTest.java
@@ -76,12 +76,17 @@ public class CorePluginTest extends JMSTestBase {
 
    private final Map<String, AtomicInteger> methodCalls = new HashMap<>();
    private final MethodCalledVerifier verifier = new MethodCalledVerifier(methodCalls);
+   private final ConfigurationVerifier configurationVerifier = new ConfigurationVerifier();
    public static final String INVM_CONNECTOR_FACTORY = InVMConnectorFactory.class.getCanonicalName();
 
    @Override
    protected Configuration createDefaultConfig(boolean netty) throws Exception {
       Configuration config = super.createDefaultConfig(netty);
       config.registerBrokerPlugin(verifier);
+      Map<String, String> props = new HashMap<>(1);
+      props.put(ConfigurationVerifier.PROPERTY1, "val_1");
+      configurationVerifier.init(props);
+      config.registerBrokerPlugin(configurationVerifier);
       config.setMessageExpiryScanPeriod(0); // disable expiry scan so it's alwyas through delivery
       return config;
    }
@@ -118,6 +123,9 @@ public class CorePluginTest extends JMSTestBase {
             AFTER_MESSAGE_ROUTE);
       verifier.validatePluginMethodsEquals(2, BEFORE_CREATE_SESSION, AFTER_CREATE_SESSION, BEFORE_CLOSE_SESSION,
             AFTER_CLOSE_SESSION);
+
+      assertEquals("configurationVerifier is invoked", 1, configurationVerifier.afterSendCounter.get());
+      assertEquals("configurationVerifier config set", "val_1", configurationVerifier.value1);
    }
 
    @Test

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/XmlConfigPluginTest.java
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/XmlConfigPluginTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/XmlConfigPluginTest.java
index fc81098..2dcd9cc 100644
--- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/XmlConfigPluginTest.java
+++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/plugin/XmlConfigPluginTest.java
@@ -38,8 +38,13 @@ public class XmlConfigPluginTest extends ActiveMQTestBase {
       ActiveMQServer server = addServer(new ActiveMQServerImpl(fc));
       try {
          server.start();
-         assertEquals(1, server.getBrokerPlugins().size());
+         assertEquals(2, server.getBrokerPlugins().size());
          assertTrue(server.getBrokerPlugins().get(0) instanceof MethodCalledVerifier);
+         assertTrue(server.getBrokerPlugins().get(1) instanceof ConfigurationVerifier);
+         ConfigurationVerifier configurationVerifier = (ConfigurationVerifier) server.getBrokerPlugins().get(1);
+         assertEquals("value1", "val_1", configurationVerifier.value1);
+         assertEquals("value2", "val_2", configurationVerifier.value2);
+         assertNull("value3 should not have been set", configurationVerifier.value3);
       } finally {
          if (server != null) {
             server.stop();

http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/d9575705/tests/integration-tests/src/test/resources/broker-plugins-config.xml
----------------------------------------------------------------------
diff --git a/tests/integration-tests/src/test/resources/broker-plugins-config.xml b/tests/integration-tests/src/test/resources/broker-plugins-config.xml
index a8ac3fb..b795e38 100644
--- a/tests/integration-tests/src/test/resources/broker-plugins-config.xml
+++ b/tests/integration-tests/src/test/resources/broker-plugins-config.xml
@@ -42,6 +42,10 @@
             
       <broker-plugins>
          <broker-plugin class-name="org.apache.activemq.artemis.tests.integration.plugin.MethodCalledVerifier" />
+         <broker-plugin class-name="org.apache.activemq.artemis.tests.integration.plugin.ConfigurationVerifier">
+             <property key="property1" value="val_1"/>
+             <property key="property2" value="val_2"/>
+         </broker-plugin>
       </broker-plugins>
    </core>