ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ptupit...@apache.org
Subject [8/8] ignite git commit: ignite-7334 Added escaping of special symbols during MBean initialization
Date Wed, 17 Jan 2018 07:24:18 GMT
ignite-7334 Added escaping of special symbols during MBean initialization

Signed-off-by: Andrey Gura <agura@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/536009b4
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/536009b4
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/536009b4

Branch: refs/heads/ignite-2.4
Commit: 536009b40a415cfc07de5a117383e71f7457e2a1
Parents: db69d90
Author: Stanislav Lukyanov <stanlukyanov@gmail.com>
Authored: Tue Jan 16 17:47:58 2018 +0300
Committer: Andrey Gura <agura@apache.org>
Committed: Tue Jan 16 17:47:58 2018 +0300

----------------------------------------------------------------------
 .../processors/cache/GridCacheProcessor.java    |  4 +-
 .../ignite/internal/util/IgniteUtils.java       | 90 ++++----------------
 .../cache/CacheMetricsEnableRuntimeTest.java    |  2 +-
 .../testsuites/IgniteUtilSelfTestSuite.java     |  2 +
 .../util/mbeans/GridMBeanDisableSelfTest.java   | 69 +++++++--------
 .../mbeans/GridMBeanExoticNamesSelfTest.java    | 70 +++++++++++++++
 6 files changed, 123 insertions(+), 114 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
index 4f849da..e635e62 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheProcessor.java
@@ -3844,7 +3844,7 @@ public class GridCacheProcessor extends GridProcessorAdapter {
         for (Class<?> itf : mbeanImpl.getClass().getInterfaces()) {
             if (itf.getName().endsWith("MBean") || itf.getName().endsWith("MXBean")) {
                 try {
-                    U.registerCacheMBean(srvr, ctx.igniteInstanceName(), cacheName, obj.getClass().getName(),
mbeanImpl,
+                    U.registerMBean(srvr, ctx.igniteInstanceName(), cacheName, obj.getClass().getName(),
mbeanImpl,
                         (Class<Object>)itf);
                 }
                 catch (Throwable e) {
@@ -3891,7 +3891,7 @@ public class GridCacheProcessor extends GridProcessorAdapter {
 
         if (needToUnregister) {
             try {
-                srvr.unregisterMBean(U.makeCacheMBeanName(ctx.igniteInstanceName(), cacheName,
o.getClass().getName()));
+                srvr.unregisterMBean(U.makeMBeanName(ctx.igniteInstanceName(), cacheName,
o.getClass().getName()));
             }
             catch (Throwable e) {
                 U.error(log, "Failed to unregister MBean for component: " + o, e);

http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
index 138f73e..c522572 100755
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
@@ -4434,9 +4434,9 @@ public abstract class IgniteUtils {
             sb.a("igniteInstanceName=").a(igniteInstanceName).a(',');
 
         if (grp != null)
-            sb.a("group=").a(grp).a(',');
+            sb.a("group=").a(escapeObjectNameValue(grp)).a(',');
 
-        sb.a("name=").a(name);
+        sb.a("name=").a(escapeObjectNameValue(name));
 
         return new ObjectName(sb.toString());
     }
@@ -4474,37 +4474,16 @@ public abstract class IgniteUtils {
     }
 
     /**
-     * Constructs JMX object name with given properties.
-     * Map with ordered {@code groups} used for proper object name construction.
+     * Escapes the given string to be used as a value in the ObjectName syntax.
      *
-     * @param igniteInstanceName Ignite instance name.
-     * @param cacheName Name of the cache.
-     * @param name Name of mbean.
-     * @return JMX object name.
-     * @throws MalformedObjectNameException Thrown in case of any errors.
+     * @param s A string to be escape.
+     * @return An escaped string.
      */
-    public static ObjectName makeCacheMBeanName(
-        @Nullable String igniteInstanceName, @Nullable String cacheName, String name
-    ) throws MalformedObjectNameException {
-        SB sb = new SB(JMX_DOMAIN + ':');
-
-        appendClassLoaderHash(sb);
-
-        appendJvmId(sb);
-
-        if (igniteInstanceName != null && !igniteInstanceName.isEmpty())
-            sb.a("igniteInstanceName=").a(igniteInstanceName).a(',');
+    private static String escapeObjectNameValue(String s) {
+        if (MBEAN_CACHE_NAME_PATTERN.matcher(s).matches())
+            return s;
 
-        cacheName = maskName(cacheName);
-
-        if (!MBEAN_CACHE_NAME_PATTERN.matcher(cacheName).matches())
-            sb.a("group=").a('\"').a(cacheName).a('\"').a(',');
-        else
-            sb.a("group=").a(cacheName).a(',');
-
-        sb.a("name=").a(name);
-
-        return new ObjectName(sb.toString());
+        return '\"' + s.replaceAll("[\\\\\"?*]", "\\\\$0") + '\"';
     }
 
     /**
@@ -4521,18 +4500,14 @@ public abstract class IgniteUtils {
      * @throws MBeanRegistrationException if MBeans are disabled.
      * @throws JMException If MBean creation failed.
      */
-    public static <T> ObjectName registerMBean(MBeanServer mbeanSrv, @Nullable String
igniteInstanceName, @Nullable String grp,
-        String name, T impl, @Nullable Class<T> itf) throws JMException {if(IGNITE_MBEANS_DISABLED)
-            throw new MBeanRegistrationException(new IgniteIllegalStateException("No MBeans
are allowed."));
-        assert mbeanSrv != null;
-        assert name != null;
-        assert itf != null;
-
-        DynamicMBean mbean = new IgniteStandardMXBean(impl, itf);
-
-        mbean.getMBeanInfo();
-
-        return mbeanSrv.registerMBean(mbean, makeMBeanName(igniteInstanceName, grp, name)).getObjectName();
+    public static <T> ObjectName registerMBean(
+        MBeanServer mbeanSrv,
+        @Nullable String igniteInstanceName,
+        @Nullable String grp,
+        String name, T impl,
+        @Nullable Class<T> itf
+    ) throws JMException {
+        return registerMBean(mbeanSrv, makeMBeanName(igniteInstanceName, grp, name), impl,
itf);
     }
 
     /**
@@ -4565,37 +4540,6 @@ public abstract class IgniteUtils {
     }
 
     /**
-     * Registers MBean with the server.
-     *
-     * @param <T> Type of mbean.
-     * @param mbeanSrv MBean server.
-     * @param igniteInstanceName Ignite instance name.
-     * @param cacheName Name of the cache.
-     * @param name Name of mbean.
-     * @param impl MBean implementation.
-     * @param itf MBean interface.
-     * @return JMX object name.
-     * @throws MBeanRegistrationException if MBeans are disabled.
-     * @throws JMException If MBean creation failed.
-     * @throws IgniteException If MBean creation are not allowed.
-     */
-    public static <T> ObjectName registerCacheMBean(MBeanServer mbeanSrv, @Nullable
String igniteInstanceName,
-        @Nullable String cacheName, String name, T impl, Class<T> itf) throws JMException
{
-        if(IGNITE_MBEANS_DISABLED)
-            throw new MBeanRegistrationException(new IgniteIllegalStateException("MBeans
are disabled."));
-
-        assert mbeanSrv != null;
-        assert name != null;
-        assert itf != null;
-
-        DynamicMBean mbean = new IgniteStandardMXBean(impl, itf);
-
-        mbean.getMBeanInfo();
-
-        return mbeanSrv.registerMBean(mbean, makeCacheMBeanName(igniteInstanceName, cacheName,
name)).getObjectName();
-    }
-
-    /**
      * Convenience method that interrupts a given thread if it's not {@code null}.
      *
      * @param t Thread to interrupt.

http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheMetricsEnableRuntimeTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheMetricsEnableRuntimeTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheMetricsEnableRuntimeTest.java
index 0cf1b43..c50f011 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheMetricsEnableRuntimeTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/CacheMetricsEnableRuntimeTest.java
@@ -325,7 +325,7 @@ public class CacheMetricsEnableRuntimeTest extends GridCommonAbstractTest
{
      */
     private CacheMetricsMXBean mxBean(int nodeIdx, String cacheName, Class<? extends CacheMetricsMXBean>
clazz)
         throws MalformedObjectNameException {
-        ObjectName mbeanName = U.makeCacheMBeanName(getTestIgniteInstanceName(nodeIdx), cacheName,
+        ObjectName mbeanName = U.makeMBeanName(getTestIgniteInstanceName(nodeIdx), cacheName,
             clazz.getName());
 
         MBeanServer mbeanSrv = ManagementFactory.getPlatformMBeanServer();

http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
index 7c99817..d1cf930 100644
--- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
+++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteUtilSelfTestSuite.java
@@ -48,6 +48,7 @@ import org.apache.ignite.util.GridQueueSelfTest;
 import org.apache.ignite.util.GridSpinReadWriteLockSelfTest;
 import org.apache.ignite.util.GridStringBuilderFactorySelfTest;
 import org.apache.ignite.util.mbeans.GridMBeanDisableSelfTest;
+import org.apache.ignite.util.mbeans.GridMBeanExoticNamesSelfTest;
 import org.apache.ignite.util.mbeans.GridMBeanSelfTest;
 
 /**
@@ -81,6 +82,7 @@ public class IgniteUtilSelfTestSuite extends TestSuite {
         suite.addTestSuite(GridByteArrayListSelfTest.class);
         suite.addTestSuite(GridMBeanSelfTest.class);
         suite.addTestSuite(GridMBeanDisableSelfTest.class);
+        suite.addTestSuite(GridMBeanExoticNamesSelfTest.class);
         suite.addTestSuite(GridLongListSelfTest.class);
         suite.addTestSuite(GridIntListSelfTest.class);
         suite.addTestSuite(GridArraysSelfTest.class);

http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanDisableSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanDisableSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanDisableSelfTest.java
index f08f58b..9a23832 100644
--- a/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanDisableSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanDisableSelfTest.java
@@ -23,7 +23,6 @@ import javax.management.MBeanServer;
 import javax.management.ObjectName;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.IgniteCache;
-import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.internal.util.IgniteUtils;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.testframework.GridTestUtils;
@@ -55,56 +54,50 @@ public class GridMBeanDisableSelfTest extends GridCommonAbstractTest {
     public void testCorrectMBeanInfo() throws Exception {
         // Node should start and stopped with no errors.
         try (final Ignite ignite = startGrid(0)) {
+            final MBeanServer server = ignite.configuration().getMBeanServer();
 
-            // Cache should be created and closed with no errors.
-            try (IgniteCache cache = ignite.getOrCreateCache(new CacheConfiguration("MyCache")))
{
+            GridTestUtils.assertThrowsWithCause(
+                new Callable<Void>() {
+                    @Override public Void call() throws Exception {
+                        U.registerMBean(server, ignite.name(), "dummy", "DummyMbean1", new
DummyMBeanImpl(), DummyMBean.class);
 
-                final MBeanServer server = ignite.configuration().getMBeanServer();
+                        return null;
+                    }
+                }, MBeanRegistrationException.class);
 
-                GridTestUtils.assertThrowsWithCause(
-                    new Callable<Void>() {
-                        @Override public Void call() throws Exception {
-                            U.registerMBean(server, ignite.name(), "dummy", "DummyMbean1",
new DummyMBeanImpl(), DummyMBean.class);
+            GridTestUtils.assertThrowsWithCause(
+                new Callable<Void>() {
+                    @Override public Void call() throws Exception {
+                        ObjectName objName = U.makeMBeanName(
+                            ignite.name(),
+                            "dummy",
+                            "DummyMbean2"
+                        );
 
-                            return null;
+                        U.registerMBean(server, objName, new DummyMBeanImpl(), DummyMBean.class);
 
-                        }
-                    }, MBeanRegistrationException.class);
+                        return null;
 
-                GridTestUtils.assertThrowsWithCause(
-                    new Callable<Void>() {
-                        @Override public Void call() throws Exception {
-                            ObjectName objName = U.makeMBeanName(
-                                ignite.name(),
-                                "dummy",
-                                "DummyMbean2"
-                            );
-
-                            U.registerMBean(server, objName, new DummyMBeanImpl(), DummyMBean.class);
-
-                            return null;
-
-                        }
-                    }, MBeanRegistrationException.class);
-
-                GridTestUtils.assertThrowsWithCause(
-                    new Callable<Void>() {
-                        @Override public Void call() throws Exception {
-                            U.registerCacheMBean(server, ignite.name(), "MyCache", "DummyMbean3",
-                                new DummyMBeanImpl(), DummyMBean.class);
-
-                            return null;
+                    }
+                }, MBeanRegistrationException.class);
+        }
+    }
 
-                        }
-                    }, MBeanRegistrationException.class);
-            }
+    /** Check that a cache can be started when MBeans are disabled. */
+    public void testCacheStart() throws Exception {
+        try (
+            Ignite ignite = startGrid(0);
+            IgniteCache<String, String> cache = ignite.getOrCreateCache("MyCache")
+        ) {
+            cache.put("foo", "bar");
+            assertEquals("bar", cache.get("foo"));
         }
     }
 
     /**
      * MBean dummy interface.
      */
-    interface DummyMBean {
+    public interface DummyMBean {
         /** */
         void noop();
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/536009b4/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanExoticNamesSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanExoticNamesSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanExoticNamesSelfTest.java
new file mode 100644
index 0000000..3b79f24
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/util/mbeans/GridMBeanExoticNamesSelfTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.ignite.util.mbeans;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+/**
+ * Testing registration of MBeans with special characters in group name or bean name.
+ */
+public class GridMBeanExoticNamesSelfTest extends GridCommonAbstractTest {
+    /** Test registration of a bean with special characters in group name. */
+    public void testGroupWithSpecialSymbols() throws Exception {
+        checkMBeanRegistration("dummy!@#$^&*()?\\grp", "dummy");
+    }
+
+    /** Test registration of a bean with special characters in name. */
+    public void testNameWithSpecialSymbols() throws Exception {
+        checkMBeanRegistration("dummygrp", "dum!@#$^&*()?\\my");
+    }
+
+    /** Test MBean registration. */
+    private void checkMBeanRegistration(String grp, String name) throws Exception {
+        // Node should start and stop with no errors.
+        try (Ignite ignite = startGrid(0)) {
+            MBeanServer srv = ignite.configuration().getMBeanServer();
+
+            U.registerMBean(srv, ignite.name(), grp, name, new DummyMBeanImpl(), DummyMBean.class);
+
+            ObjectName objName = U.makeMBeanName(ignite.name(), grp, name + '2');
+            U.registerMBean(srv, objName, new DummyMBeanImpl(), DummyMBean.class);
+        }
+    }
+
+    /**
+     * MBean dummy interface.
+     */
+    public interface DummyMBean {
+        /** */
+        void noop();
+    }
+
+    /**
+     * MBean stub.
+     */
+    private static class DummyMBeanImpl implements DummyMBean {
+        /** {@inheritDoc} */
+        @Override public void noop() {
+            // No op.
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message