You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2020/07/04 19:48:51 UTC

[tomee] 01/03: Turn off ActiveMQ's JMX connection creation, and by default set create to false on vm:// server URLs

This is an automated email from the ASF dual-hosted git repository.

jgallimore pushed a commit to branch tomee-7.1.x
in repository https://gitbox.apache.org/repos/asf/tomee.git

commit a2a06604f5d4e92e34c84715a30d03d3e7121fd1
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Sat Jul 4 20:13:10 2020 +0100

    Turn off ActiveMQ's JMX connection creation, and by default set create to false on vm:// server URLs
---
 .../resource/activemq/ActiveMQResourceAdapter.java | 22 ++++--
 .../java/org/apache/openejb/util/URISupport.java   | 24 +++++++
 .../org/apache/openejb/activemq/ServerUrlTest.java | 78 ++++++++++++++++++++++
 .../org/apache/openejb/util/URISupportTest.java    | 60 +++++++++++++++++
 .../org/apache/openejb/loader/SystemInstance.java  | 14 ++++
 5 files changed, 193 insertions(+), 5 deletions(-)

diff --git a/container/openejb-core/src/main/java/org/apache/openejb/resource/activemq/ActiveMQResourceAdapter.java b/container/openejb-core/src/main/java/org/apache/openejb/resource/activemq/ActiveMQResourceAdapter.java
index 1437a95..0eac18a 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/resource/activemq/ActiveMQResourceAdapter.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/resource/activemq/ActiveMQResourceAdapter.java
@@ -52,11 +52,7 @@ import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.net.URISyntaxException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Properties;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 
@@ -68,6 +64,11 @@ public class ActiveMQResourceAdapter extends org.apache.activemq.ra.ActiveMQReso
     private String startupTimeout = "60000";
     private BootstrapContext bootstrapContext;
     private final Map<BeanContext, ObjectName> mbeanNames = new ConcurrentHashMap<>();
+    private static final Map<String,String> PREVENT_CREATION_PARAMS = new HashMap<String, String>() { {
+        put("create", "false");
+    }};
+
+    private static final Logger LOGGER = Logger.getInstance(LogCategory.ACTIVEMQ, ActiveMQ5Factory.class);
 
     public String getDataSource() {
         return dataSource;
@@ -94,6 +95,17 @@ public class ActiveMQResourceAdapter extends org.apache.activemq.ra.ActiveMQReso
 
     @Override
     public void setServerUrl(final String url) {
+        try {
+            final URISupport.CompositeData compositeData = URISupport.parseComposite(URLs.uri(url));
+            if ("vm".equals(compositeData.getScheme())) {
+                super.setServerUrl(URISupport.addParameters(URLs.uri(url), PREVENT_CREATION_PARAMS).toString());
+                return;
+            }
+        } catch (URISyntaxException e) {
+            // if we hit an exception, we'll log this and simple pass the URL we were given to ActiveMQ.
+            LOGGER.error("Error occurred while processing ActiveMQ ServerUrl: " + url, e);
+        }
+
         super.setServerUrl(url);
     }
 
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/util/URISupport.java b/container/openejb-core/src/main/java/org/apache/openejb/util/URISupport.java
index 208ed85..37debc7 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/util/URISupport.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/util/URISupport.java
@@ -378,4 +378,28 @@ public class URISupport {
 
         return result;
     }
+
+    /**
+     * This method adds new parameters to a URI. Any parameters provided that already exist in the URI will *not* be replaced.
+     * @param uri The URI to add parameters to
+     * @param newParameters The parameters to add
+     * @return The URI with the new parameters added
+     * @throws URISyntaxException If there is a syntax error with the provided URI.
+     */
+    public static URI addParameters(final URI uri, final Map<String, String> newParameters) throws URISyntaxException {
+        if (newParameters == null || newParameters.size() == 0) {
+            return uri;
+        }
+
+        final Map<String, String> parameters = new HashMap<>(parseParamters(uri));
+
+        final Set<String> keys = newParameters.keySet();
+        for (final String key : keys) {
+            if (! parameters.containsKey(key)) {
+                parameters.put(key, newParameters.get(key));
+            }
+        }
+
+        return createRemainingURI(uri, parameters);
+    }
 }
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/activemq/ServerUrlTest.java b/container/openejb-core/src/test/java/org/apache/openejb/activemq/ServerUrlTest.java
new file mode 100644
index 0000000..906bcfb
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/activemq/ServerUrlTest.java
@@ -0,0 +1,78 @@
+/**
+ * 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.openejb.activemq;
+
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.SimpleLog;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Singleton;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
+import javax.resource.ResourceException;
+import java.util.Properties;
+
+@SimpleLog
+@RunWith(ApplicationComposer.class)
+@Classes(cdi = true, innerClassesAsBean = true)
+public class ServerUrlTest {
+
+    @EJB
+    private ConnectionTestBean testBean;
+
+    @Configuration
+    public Properties config() {
+        return new PropertiesBuilder()
+                .p("amq", "new://Resource?type=ActiveMQResourceAdapter")
+                .p("amq.BrokerXmlConfig", "broker:(vm://broker)?useJmx=true")
+                .p("amq.ServerUrl", "vm://broker")
+                .p("amqcf", "new://Resource?type=javax.jms.ConnectionFactory")
+                .p("amqcf.ResourceAdapter", "amq")
+
+                .build();
+    }
+
+    @Test
+    public void test() throws Exception {
+        try {
+            testBean.testConnection();
+            Assert.fail("Expected exception not thrown");
+        } catch (JMSException e) {
+            Assert.assertTrue(e.getMessage().contains("Broker named 'broker' does not exist"));
+        }
+    }
+
+    @Singleton
+    public static class ConnectionTestBean {
+
+        @Resource
+        private ConnectionFactory cf;
+
+        public void testConnection() throws Exception {
+            final Connection connection = cf.createConnection();
+            connection.close();
+        }
+    }
+}
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/util/URISupportTest.java b/container/openejb-core/src/test/java/org/apache/openejb/util/URISupportTest.java
index 88f6caa..0e784a6 100644
--- a/container/openejb-core/src/test/java/org/apache/openejb/util/URISupportTest.java
+++ b/container/openejb-core/src/test/java/org/apache/openejb/util/URISupportTest.java
@@ -19,6 +19,9 @@ package org.apache.openejb.util;
 import junit.framework.TestCase;
 
 import java.net.URI;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * @version $Rev$ $Date$
@@ -48,4 +51,61 @@ public class URISupportTest extends TestCase {
         assertTrue(resolvedB.equals(absoluteB));
     }
 
+    public void testAddNewParameters() throws Exception {
+        final Map<String, String> parameters = new HashMap<>();
+        parameters.put("create", "false");
+
+        final URI uri = URISupport.addParameters(URLs.uri("vm://broker"), parameters);
+        assertEquals("vm://broker?create=false", uri.toString());
+    }
+
+    public void testDoNotReplaceAnExistingParameters() throws Exception {
+        final Map<String, String> parameters = new HashMap<>();
+        parameters.put("create", "false");
+
+        final URI uri = URISupport.addParameters(URLs.uri("vm://broker?create=true"), parameters);
+        assertEquals("vm://broker?create=true", uri.toString());
+    }
+
+    public void testAddToSetOfAlreadyExistingParameters() throws Exception {
+        final Map<String, String> parameters = new HashMap<>();
+        parameters.put("create", "false");
+
+        final URI uri = URISupport.addParameters(URLs.uri("vm://broker?foo=bar&boo=baz&welcome=helloworld"), parameters);
+        final Map<String, String> actual = URISupport.parseParamters(uri);
+
+        assertEquals(4, actual.size());
+        assertEquals("bar", actual.get("foo"));
+        assertEquals("baz", actual.get("boo"));
+        assertEquals("helloworld", actual.get("welcome"));
+        assertEquals("false", actual.get("create"));
+    }
+
+    public void testAddToSetOfAlreadyExistingParametersButDontOverwriteExistingParameter() throws Exception {
+        final Map<String, String> parameters = new HashMap<>();
+        parameters.put("create", "false");
+
+        final URI uri = URISupport.addParameters(URLs.uri("vm://broker?foo=bar&boo=baz&welcome=helloworld&create=true"), parameters);
+        final Map<String, String> actual = URISupport.parseParamters(uri);
+
+        assertEquals(4, actual.size());
+        assertEquals("bar", actual.get("foo"));
+        assertEquals("baz", actual.get("boo"));
+        assertEquals("helloworld", actual.get("welcome"));
+        assertEquals("true", actual.get("create"));
+    }
+
+    public void testNullParameters() throws Exception {
+        final URI initial = URLs.uri("vm://broker?foo=bar&boo=baz&welcome=helloworld&create=true");
+        final URI uri = URISupport.addParameters(initial, null);
+
+        assertEquals(initial, uri);
+    }
+
+    public void testEmptyParameters() throws Exception {
+        final URI initial = URLs.uri("vm://broker?foo=bar&boo=baz&welcome=helloworld&create=true");
+        final URI uri = URISupport.addParameters(initial, Collections.emptyMap());
+
+        assertEquals(initial, uri);
+    }
 }
diff --git a/container/openejb-loader/src/main/java/org/apache/openejb/loader/SystemInstance.java b/container/openejb-loader/src/main/java/org/apache/openejb/loader/SystemInstance.java
index 1474c43..db2b04f 100644
--- a/container/openejb-loader/src/main/java/org/apache/openejb/loader/SystemInstance.java
+++ b/container/openejb-loader/src/main/java/org/apache/openejb/loader/SystemInstance.java
@@ -42,6 +42,7 @@ import java.util.concurrent.atomic.AtomicReference;
 public final class SystemInstance {
     private static final String PROFILE_PROP = "openejb.profile";
     private static final String DEFAULT_PROFILE = "development";
+    public static final String ACTIVEMQ_CREATE_JMX_CONNECTOR = "org.apache.activemq.broker.jmx.createConnector";
 
     private final long startTime = System.currentTimeMillis();
 
@@ -112,6 +113,9 @@ public final class SystemInstance {
             }
         }
         this.internalProperties.putAll(properties);
+
+        setDefaults();
+
         this.options = new Options(internalProperties, new Options(System.getProperties()));
         this.home = new FileUtils("openejb.home", "user.dir", this.internalProperties);
         this.base = new FileUtils("openejb.base", "openejb.home", this.internalProperties);
@@ -130,6 +134,16 @@ public final class SystemInstance {
         System.setProperty("derby.system.home", System.getProperty("derby.system.home", baseDirCanonicalPath));
     }
 
+    /**
+     * This method sets some specific defaults. We shouldn't add to this unless there's a really good reason -
+     * e.g. we need to override an upstream component's defaults.
+     */
+    private void setDefaults() {
+        if (! this.internalProperties.containsKey(ACTIVEMQ_CREATE_JMX_CONNECTOR)) {
+            this.internalProperties.setProperty(ACTIVEMQ_CREATE_JMX_CONNECTOR, Boolean.FALSE.toString());
+        }
+    }
+
     public <E> E fireEvent(final E event) {
         return observerManager.fireEvent(event);
     }