Repository: deltaspike Updated Branches: refs/heads/master 44e5bbb6b -> b3570077a
DELTASPIKE-1008 adding properties() to @MBean Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/b3570077 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/b3570077 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/b3570077 Branch: refs/heads/master Commit: b3570077ab5dc0c7568051bee825fa8ccdc1995f Parents: 44e5bbb Author: Romain Manni-Bucau <[email protected]> Authored: Fri Oct 23 16:50:35 2015 +0200 Committer: Romain Manni-Bucau <[email protected]> Committed: Fri Oct 23 16:50:35 2015 +0200 ---------------------------------------------------------------------- .../apache/deltaspike/core/api/jmx/MBean.java | 6 ++ .../core/impl/jmx/MBeanExtension.java | 42 +++++++++++--- .../test/core/impl/jmx/CustomProperties.java | 42 ++++++++++++++ .../test/core/impl/jmx/CustomProperties2.java | 42 ++++++++++++++ .../core/impl/jmx/CustomPropertiesTest.java | 60 ++++++++++++++++++++ 5 files changed, 184 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b3570077/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 b182318..27b23d4 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,12 @@ public @interface MBean String name() default ""; /** + * @return the properties part of the objectName if no objectName was specified. + * If name and type are specified this segment is concatenated after. + */ + String properties() 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 http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b3570077/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 02366c1..93f8d9f 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 @@ -47,6 +47,8 @@ import java.util.logging.Logger; public class MBeanExtension implements Extension, Deactivatable { private static final Logger LOGGER = Logger.getLogger(MBeanExtension.class.getName()); + private static final String DEFAULT_TYPE = "MBeans"; + private static final String DEFAULT_CATEGORY = "org.apache.deltaspike"; private final Map<Class<?>, DynamicMBeanWrapper> wrappers = new ConcurrentHashMap<Class<?>, DynamicMBeanWrapper>(); @@ -98,16 +100,40 @@ public class MBeanExtension implements Extension, Deactivatable String objectNameValue = mBeanAnnotation.objectName(); if (objectNameValue.isEmpty()) { - String name = mBeanAnnotation.name(); - if (name.isEmpty()) + final String type = getConfigurableAttribute(mBeanAnnotation.type(), DEFAULT_TYPE); + final String category = getConfigurableAttribute(mBeanAnnotation.category(), DEFAULT_CATEGORY); + final String properties = getConfigurableAttribute(mBeanAnnotation.properties(), ""); + final String name = mBeanAnnotation.name(); + + final StringBuilder builder = new StringBuilder(category).append(':'); + if (!properties.contains("type=")) { - name = clazz.getName(); + builder.append("type=").append(type); } - - final String type = getConfigurableAttribute(mBeanAnnotation.type(), "MBeans"); - final String category = getConfigurableAttribute(mBeanAnnotation.category(), "org.apache.deltaspike"); - - objectNameValue = category + ":type=" + type + ",name=" + name; + else if (!DEFAULT_TYPE.equals(type)) + { + LOGGER.warning("type() ignored on " + clazz + " since properties contains it."); + } + if (!properties.contains("name=")) + { + if (!name.isEmpty() || properties.isEmpty()) + { + builder.append(",name="); + if (name.isEmpty()) + { + builder.append(clazz.getName()); + } + else + { + builder.append(name); + } + } // else skip. type is important in JMX but name is a fully custom property so we are able to skip it + } + if (!properties.isEmpty()) + { + builder.append(',').append(properties); + } + objectNameValue = builder.toString(); } final ObjectName objectName = new ObjectName(objectNameValue); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/b3570077/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties.java new file mode 100644 index 0000000..bed6db6 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties.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 = "", properties = "foo=bar,dummy=empty") +public class CustomProperties +{ + @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/b3570077/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties2.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties2.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties2.java new file mode 100644 index 0000000..121006a --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomProperties2.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 = "nom", properties = "foo=bar,dummy=empty") +public class CustomProperties2 +{ + @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/b3570077/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomPropertiesTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomPropertiesTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomPropertiesTest.java new file mode 100644 index 0000000..578fe20 --- /dev/null +++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/jmx/CustomPropertiesTest.java @@ -0,0 +1,60 @@ +/* + * 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.ObjectName; +import java.lang.management.ManagementFactory; + +import static org.junit.Assert.assertTrue; + +@RunWith(Arquillian.class) +public class CustomPropertiesTest +{ + @Deployment + public static Archive<?> war() + { + return ShrinkWrap.create(WebArchive.class, "CustomPropertiesTest.war") + .addAsLibraries(ArchiveUtils.getDeltaSpikeCoreArchive()) + .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") + .addClasses(CustomProperties.class, CustomProperties2.class); + } + + @Inject + private CustomProperties myMBean; + + @Test + public void checkMBean() throws Exception + { + assertTrue(ManagementFactory.getPlatformMBeanServer().isRegistered( + new ObjectName("cat:type=and,foo=bar,dummy=empty"))); + assertTrue(ManagementFactory.getPlatformMBeanServer().isRegistered( + new ObjectName("cat:type=and,name=nom,foo=bar,dummy=empty"))); + } +}
