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/08 20:09:51 UTC
[tomee] 02/04: 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-1.7.x
in repository https://gitbox.apache.org/repos/asf/tomee.git
commit ac21ca8e8b6edc9632602d553064889fd538aa56
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 | 20 +++++-
.../java/org/apache/openejb/util/URISupport.java | 31 +++++++--
.../org/apache/openejb/activemq/ServerUrlTest.java | 78 ++++++++++++++++++++++
.../org/apache/openejb/util/URISupportTest.java | 60 +++++++++++++++++
.../org/apache/openejb/loader/SystemInstance.java | 13 +++-
5 files changed, 192 insertions(+), 10 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 8880875..7fc8041 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
@@ -28,9 +28,7 @@ import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ResourceAdapterInternalException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Properties;
+import java.util.Map;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("UnusedDeclaration")
@@ -40,6 +38,11 @@ public class ActiveMQResourceAdapter extends org.apache.activemq.ra.ActiveMQReso
private String useDatabaseLock;
private String startupTimeout = "60000";
private BootstrapContext bootstrapContext;
+ 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;
@@ -66,6 +69,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..76bbf1f 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
@@ -22,13 +22,8 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
* Swiped verbatim from ActiveMQ... the URI kings.
@@ -378,4 +373,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 f96ba48..6ed667c 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
@@ -40,6 +40,7 @@ import java.util.Properties;
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();
@@ -118,6 +119,8 @@ 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);
@@ -127,8 +130,16 @@ public final class SystemInstance {
this.internalProperties.setProperty("openejb.home", home.getDirectory().getCanonicalPath());
this.internalProperties.setProperty("openejb.base", base.getDirectory().getCanonicalPath());
System.setProperty("derby.system.home", System.getProperty("derby.system.home", base.getDirectory().getCanonicalPath()));
+ }
-
+ /**
+ * 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) {