You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by rm...@apache.org on 2015/10/23 16:36:05 UTC

deltaspike git commit: DELTASPIKE-1008 adding type to @MBean, thanks Falko Molder for the patch

Repository: deltaspike
Updated Branches:
  refs/heads/master 25b5098fc -> 44e5bbb6b


DELTASPIKE-1008 adding type to @MBean, thanks Falko Molder for the patch


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/44e5bbb6
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/44e5bbb6
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/44e5bbb6

Branch: refs/heads/master
Commit: 44e5bbb6b2a60cb5a1b9876d8465a701155138ce
Parents: 25b5098
Author: Romain Manni-Bucau <rm...@gmail.com>
Authored: Fri Oct 23 16:35:52 2015 +0200
Committer: Romain Manni-Bucau <rm...@gmail.com>
Committed: Fri Oct 23 16:35:52 2015 +0200

----------------------------------------------------------------------
 .../apache/deltaspike/core/api/jmx/MBean.java   |  8 +++
 .../core/impl/jmx/MBeanExtension.java           | 26 ++++----
 .../test/core/impl/jmx/CustomType.java          | 42 +++++++++++++
 .../test/core/impl/jmx/CustomTypeTest.java      | 66 ++++++++++++++++++++
 4 files changed, 131 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/44e5bbb6/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/jmx/MBean.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/jmx/MBean.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/jmx/MBean.java
index 56d8205..b182318 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/jmx/MBean.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/jmx/MBean.java
@@ -52,6 +52,14 @@ public @interface MBean
     String name() default "";
 
     /**
+     * @return the type to use if no objectName was specified. Default is <pre>MBeans</pre> and can be
+     *         overriden either directly by the value or by a key used to resolve a value using
+     *         {@link org.apache.deltaspike.core.api.config.ConfigResolver}. It is a key if the value is between
+     *         brackets.
+     */
+    String type() default "";
+
+    /**
      * @return the direct object name used to export the decorated bean.
      */
     String objectName() default "";

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/44e5bbb6/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/jmx/MBeanExtension.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/jmx/MBeanExtension.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/jmx/MBeanExtension.java
index d07cbce..02366c1 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/jmx/MBeanExtension.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/jmx/MBeanExtension.java
@@ -18,8 +18,8 @@
  */
 package org.apache.deltaspike.core.impl.jmx;
 
-import org.apache.deltaspike.core.api.config.base.CoreBaseConfig;
 import org.apache.deltaspike.core.api.config.ConfigResolver;
+import org.apache.deltaspike.core.api.config.base.CoreBaseConfig;
 import org.apache.deltaspike.core.api.jmx.JmxBroadcaster;
 import org.apache.deltaspike.core.api.jmx.MBean;
 import org.apache.deltaspike.core.spi.activation.Deactivatable;
@@ -99,21 +99,15 @@ public class MBeanExtension implements Extension, Deactivatable
         if (objectNameValue.isEmpty())
         {
             String name = mBeanAnnotation.name();
-
             if (name.isEmpty())
             {
                 name = clazz.getName();
             }
 
-            String category = mBeanAnnotation.category().trim();
-
-            if (category.startsWith("{") && category.endsWith("}"))
-            {
-                category = ConfigResolver.getPropertyValue(
-                    category.substring(1, category.length() - 1), "org.apache.deltaspike");
-            }
+            final String type = getConfigurableAttribute(mBeanAnnotation.type(), "MBeans");
+            final String category = getConfigurableAttribute(mBeanAnnotation.category(), "org.apache.deltaspike");
 
-            objectNameValue = category + ":type=MBeans,name=" + name;
+            objectNameValue = category + ":type=" + type + ",name=" + name;
         }
 
         final ObjectName objectName = new ObjectName(objectNameValue);
@@ -131,7 +125,7 @@ public class MBeanExtension implements Extension, Deactivatable
         objectNames.add(objectName);
         wrappers.put(clazz, mbean);
 
-        LOGGER.info("Registered MBean " + objectName.getCanonicalName());
+        LOGGER.info("Registered MBean " + objectName); // don't use canonical name cause it can reorder properties
     }
 
     private Annotation[] qualifiers(final AnnotatedType<?> annotatedBeanClass, final BeanManager bm)
@@ -163,4 +157,14 @@ public class MBeanExtension implements Extension, Deactivatable
     {
         return ManagementFactory.getPlatformMBeanServer();
     }
+
+    private String getConfigurableAttribute(final String annotationAttributeValue, final String defaultValue)
+    {
+        String val = annotationAttributeValue.trim();
+        if (val.startsWith("{") && val.endsWith("}"))
+        {
+            val = ConfigResolver.getPropertyValue(val.substring(1, val.length() - 1), defaultValue);
+        }
+        return val == null || val.isEmpty() ? defaultValue : val;
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/44e5bbb6/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomType.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomType.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomType.java
new file mode 100644
index 0000000..e1b6554
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomType.java
@@ -0,0 +1,42 @@
+/*
+ * 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.deltaspike.test.core.impl.jmx;
+
+import org.apache.deltaspike.core.api.jmx.JmxManaged;
+import org.apache.deltaspike.core.api.jmx.MBean;
+
+import javax.enterprise.context.ApplicationScoped;
+
+@ApplicationScoped
+@MBean(category = "cat", type = "and", name = "fish")
+public class CustomType
+{
+    @JmxManaged
+    private int counter;
+
+    public int getCounter()
+    {
+        return counter;
+    }
+
+    public void setCounter(final int counter)
+    {
+        this.counter = counter;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/44e5bbb6/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomTypeTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomTypeTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomTypeTest.java
new file mode 100644
index 0000000..a0eb0f2
--- /dev/null
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomTypeTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.deltaspike.test.core.impl.jmx;
+
+import org.apache.deltaspike.test.util.ArchiveUtils;
+import org.jboss.arquillian.container.test.api.Deployment;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.shrinkwrap.api.Archive;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.asset.EmptyAsset;
+import org.jboss.shrinkwrap.api.spec.WebArchive;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.inject.Inject;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+import java.lang.management.ManagementFactory;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Arquillian.class)
+public class CustomTypeTest {
+    @Deployment
+    public static Archive<?> war()
+    {
+        return ShrinkWrap.create(WebArchive.class, "CustomTypeTest.war")
+            .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive())
+            .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
+            .addClasses(CustomType.class);
+    }
+
+    @Inject
+    private CustomType myMBean;
+
+    @Test
+    public void checkMBean() throws Exception
+    {
+        final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        myMBean.setCounter(0);
+        assertEquals(0, myMBean.getCounter());
+        myMBean.setCounter(2);
+        final ObjectName on = new ObjectName("cat:type=and,name=fish");
+        assertTrue(server.isRegistered(on));
+        assertEquals(2, server.getAttribute(on, "counter"));
+        myMBean.setCounter(5);
+        assertEquals(5, server.getAttribute(on, "counter"));
+    }
+}