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 2022/04/04 13:07:51 UTC

[tomee] branch tomee-8.x updated: TOMEE-3889 quote objectnames that contain special characters

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

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


The following commit(s) were added to refs/heads/tomee-8.x by this push:
     new e86a127d6c TOMEE-3889 quote objectnames that contain special characters
e86a127d6c is described below

commit e86a127d6cbb3ea4bf2baad11025a2a3aafe2bab
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Mon Apr 4 14:06:21 2022 +0100

    TOMEE-3889 quote objectnames that contain special characters
---
 .../arquillian/tests/jms/MDBWithWildcardTest.java  | 113 +++++++++++++++++++++
 .../openejb/assembler/classic/Assembler.java       |   2 +
 .../openejb/monitoring/ObjectNameBuilder.java      |   4 +
 .../openejb/monitoring/ObjectNameBuilderTest.java  |  39 +++++++
 4 files changed, 158 insertions(+)

diff --git a/arquillian/arquillian-tomee-tests/arquillian-tomee-jms-tests/src/test/java/org/apache/openejb/arquillian/tests/jms/MDBWithWildcardTest.java b/arquillian/arquillian-tomee-tests/arquillian-tomee-jms-tests/src/test/java/org/apache/openejb/arquillian/tests/jms/MDBWithWildcardTest.java
new file mode 100755
index 0000000000..d5617500e0
--- /dev/null
+++ b/arquillian/arquillian-tomee-tests/arquillian-tomee-jms-tests/src/test/java/org/apache/openejb/arquillian/tests/jms/MDBWithWildcardTest.java
@@ -0,0 +1,113 @@
+/**
+ * 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.openejb.arquillian.tests.jms;
+
+import org.apache.ziplock.IO;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.arquillian.test.api.ArquillianResource;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.StringAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.jboss.shrinkwrap.descriptor.api.Descriptors;
+import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.ejb.ActivationConfigProperty;
+import javax.ejb.MessageDriven;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.lang.management.ManagementFactory;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+
+@RunWith(Arquillian.class)
+public class MDBWithWildcardTest {
+
+    @ArquillianResource
+    private URL url;
+
+    @Deployment(testable = false)
+    public static WebArchive getArchive() {
+
+        return ShrinkWrap.create(WebArchive.class, "jsf-jms-test.war")
+                .addClasses(WildcardMdb.class)
+                .setWebXML(new StringAsset(Descriptors.create(WebAppDescriptor.class)
+                        .version("3.0")
+                        .createServlet()
+                            .servletName("jmx")
+                            .servletClass(JmxServlet.class.getName())
+                        .up()
+                        .createServletMapping()
+                            .servletName("jmx")
+                            .urlPattern("/jmx")
+                        .up()
+                        .exportAsString()));
+    }
+
+    @Test
+    public void test() throws Exception {
+        final URL url = new URL(this.url.toExternalForm() + "/jmx");
+        final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+        Assert.assertEquals(200, urlConnection.getResponseCode());
+        final InputStream inputStream = urlConnection.getInputStream();
+        final String result = IO.slurp(inputStream);
+        Assert.assertEquals("dest.*.event", result.trim());
+    }
+
+
+    @MessageDriven(
+            activationConfig = {
+                    @ActivationConfigProperty(propertyName = "destination", propertyValue = "dest.*.event"),
+                    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
+            }
+    )
+    public static class WildcardMdb implements MessageListener {
+        @Override
+        public void onMessage(final Message message) {
+            System.out.println("Message received");
+        }
+    }
+
+    public static class JmxServlet extends HttpServlet {
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+            try {
+                final MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer();
+                final String name = "openejb.management:J2EEServer=openejb,J2EEApplication=<empty>,j2eeType=Resource,name=\"dest.\\*.event\"";
+                final PrintWriter writer = resp.getWriter();
+                writer.println(platformMBeanServer.getAttribute(new ObjectName(name), "physicalName"));
+                writer.flush();
+            } catch (Throwable t) {
+                throw new ServletException(t);
+            }
+        }
+    }
+}
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
index ab51f420d6..e044d79083 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
@@ -3404,8 +3404,10 @@ public class Assembler extends AssemblerTool implements org.apache.openejb.spi.A
 
             if (DynamicMBean.class.isInstance(resource)) {
                 server.registerMBean(resource, objectName);
+                logger.debug("Registered JMX name: " + objectName.toString());
             } else {
                 server.registerMBean(new MBeanPojoWrapper(name, resource), objectName);
+                logger.debug("Registered JMX name: " + objectName.toString());
             }
         } catch (final Exception e) {
             logger.error("Unable to register MBean ", e);
diff --git a/container/openejb-core/src/main/java/org/apache/openejb/monitoring/ObjectNameBuilder.java b/container/openejb-core/src/main/java/org/apache/openejb/monitoring/ObjectNameBuilder.java
index c1bdb20080..e1321c0d63 100644
--- a/container/openejb-core/src/main/java/org/apache/openejb/monitoring/ObjectNameBuilder.java
+++ b/container/openejb-core/src/main/java/org/apache/openejb/monitoring/ObjectNameBuilder.java
@@ -58,6 +58,10 @@ public class ObjectNameBuilder {
 
                 if (null != sv) {
                     sv = sv.replace(':', '_');
+
+                    if (sv.contains("\"") || sv.contains("*") || sv.contains("\\") || sv.contains(",") || sv.contains("\n") || sv.contains("?")) {
+                        sv = ObjectName.quote(sv);
+                    }
                 }
 
                 sb.append(entry.getKey()).append("=").append(sv).append(",");
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/monitoring/ObjectNameBuilderTest.java b/container/openejb-core/src/test/java/org/apache/openejb/monitoring/ObjectNameBuilderTest.java
new file mode 100644
index 0000000000..1940b4bca5
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/monitoring/ObjectNameBuilderTest.java
@@ -0,0 +1,39 @@
+/*
+ * 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.openejb.monitoring;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.management.ObjectName;
+
+public class ObjectNameBuilderTest {
+
+    @Test
+    public void testQuoting() throws Exception {
+        final ObjectNameBuilder jmxName = new ObjectNameBuilder("openejb.management");
+        jmxName.set("J2EEServer", "openejb");
+        jmxName.set("J2EEApplication", null);
+        jmxName.set("j2eeType", "");
+        jmxName.set("name", "test_with_*_in_it");
+
+        final ObjectName objectName = jmxName.build();
+        Assert.assertEquals("openejb.management:J2EEServer=openejb,J2EEApplication=<empty>,j2eeType=<empty>,name=\"test_with_\\*_in_it\"", objectName.toString());
+    }
+
+
+}
\ No newline at end of file