You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2014/06/24 10:42:57 UTC
svn commit: r1605030 - in /jackrabbit/oak/trunk/oak-commons/src:
main/java/org/apache/jackrabbit/oak/commons/jmx/
test/java/org/apache/jackrabbit/oak/commons/jmx/
Author: chetanm
Date: Tue Jun 24 08:42:57 2014
New Revision: 1605030
URL: http://svn.apache.org/r1605030
Log:
OAK-1906 - Adding information to a Standard MBean interface using annotations
Added:
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java (with props)
jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java (with props)
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/
jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java (with props)
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,100 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import javax.management.DescriptorRead;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanParameterInfo;
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+
+/**
+ * The extension of {@link javax.management.StandardMBean} that will automatically provide JMX
+ * metadata through annotations.
+ *
+ * @see javax.management.MBeanInfo
+ * @see Description
+ * @see Name
+ * @see Impact
+ */
+public class AnnotatedStandardMBean extends StandardMBean {
+ /**
+ * Make a DynamicMBean out of the object implementation, using the specified
+ * mbeanInterface class.
+ *
+ * @see {@link javax.management.StandardMBean#StandardMBean(Object, Class)}
+ */
+ public <T> AnnotatedStandardMBean(T implementation, Class<T> mbeanInterface)
+ throws NotCompliantMBeanException {
+ super(implementation, mbeanInterface);
+ }
+
+ protected AnnotatedStandardMBean(Class<?> mbeanInterface)
+ throws NotCompliantMBeanException {
+ super(mbeanInterface);
+ }
+
+ @Override
+ protected String getDescription(MBeanInfo info) {
+ String desc = getValue(info, Description.NAME);
+ return desc == null ? super.getDescription(info) : desc;
+ }
+
+ @Override
+ protected String getDescription(MBeanAttributeInfo info) {
+ String desc = getValue(info, Description.NAME);
+ return desc == null ? super.getDescription(info) : desc;
+ }
+
+ @Override
+ protected String getDescription(MBeanOperationInfo info) {
+ String desc = getValue(info, Description.NAME);
+ return desc == null ? super.getDescription(info) : desc;
+ }
+
+ @Override
+ protected int getImpact(MBeanOperationInfo info) {
+ String opt = (String)info.getDescriptor().getFieldValue(Impact.NAME);
+ return opt == null ? super.getImpact(info) : ImpactOption.valueOf(opt).value();
+ }
+
+ @Override
+ protected String getParameterName(MBeanOperationInfo op,
+ MBeanParameterInfo param, int sequence) {
+ String name = getValue(param, Name.NAME);
+ return name == null
+ ? super.getParameterName(op, param, sequence)
+ : name;
+ }
+
+ @Override
+ protected String getDescription(MBeanOperationInfo op,
+ MBeanParameterInfo param, int sequence) {
+ String desc = getValue(param, Description.NAME);
+ return desc == null
+ ? super.getDescription(op, param, sequence)
+ : desc;
+ }
+
+ private static String getValue(DescriptorRead dr, String fieldName){
+ return (String) dr.getDescriptor().getFieldValue(fieldName);
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBean.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,43 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.management.DescriptorKey;
+
+/**
+ * Produces a description that will be used by JMX metadata.
+ */
+@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD,
+ ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+@Documented
+public @interface Description {
+ String NAME = "description";
+
+ @DescriptorKey(NAME)
+ String value() default "";
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Description.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,43 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.management.DescriptorKey;
+
+/**
+ * Produces an operation impact that will be returned by JMX
+ * {@link javax.management.MBeanOperationInfo#getImpact()}.
+ */
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+@Documented
+public @interface Impact {
+ String NAME = "impact";
+
+ @DescriptorKey(NAME)
+ ImpactOption value();
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Impact.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,54 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import javax.management.MBeanOperationInfo;
+
+public enum ImpactOption {
+ /**
+ * Indicates that the operation is a write-like,
+ * and would modify the MBean in some way, typically by writing some value
+ * or changing a configuration.
+ */
+ ACTION(MBeanOperationInfo.ACTION),
+ /**
+ * Indicates that the operation is both read-like and write-like.
+ */
+ ACTION_INFO(MBeanOperationInfo.ACTION_INFO),
+ /**
+ * Indicates that the operation is read-like,
+ * it basically returns information.
+ */
+ INFO(MBeanOperationInfo.INFO),
+ /**
+ * Indicates that the operation has an "unknown" nature.
+ */
+ UNKNOWN(MBeanOperationInfo.UNKNOWN);
+
+ public int value(){
+ return value;
+ }
+
+ private final int value;
+
+ private ImpactOption(int value) {
+ this.value = value;
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/ImpactOption.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,43 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import javax.management.DescriptorKey;
+
+/**
+ * Produces a parameter name that will be returned by JMX
+ * {@link javax.management.MBeanParameterInfo#getName()}.
+ */
+@Target({ElementType.PARAMETER})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+@Documented
+public @interface Name {
+ String NAME = "name";
+
+ @DescriptorKey(NAME)
+ String value();
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/Name.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,31 @@
+/*
+ * 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.
+ */
+
+/**
+ * Provides annotation support to produce JMX metadata.
+ *
+ * @version 1.0
+ */
+@Version("1.0")
+@Export(optional = "provide:=true")
+package org.apache.jackrabbit.oak.commons.jmx;
+
+import aQute.bnd.annotation.Export;
+import aQute.bnd.annotation.Version;
+
Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/jmx/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java?rev=1605030&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java Tue Jun 24 08:42:57 2014
@@ -0,0 +1,122 @@
+/*
+ * 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.jackrabbit.oak.commons.jmx;
+
+import java.lang.management.ManagementFactory;
+
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanParameterInfo;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static junit.framework.Assert.assertEquals;
+
+@SuppressWarnings("ALL")
+public class AnnotatedStandardMBeanTest {
+ protected static ObjectName objectName;
+
+ protected static MBeanServer server;
+
+ @BeforeClass
+ public static void init() throws Exception {
+ AnnotatedStandardMBean object = new AnnotatedStandardMBean(new Foo(),
+ FooMBean.class);
+
+ objectName = new ObjectName("abc:TYPE=Test");
+
+ server = ManagementFactory.getPlatformMBeanServer();
+ server.registerMBean(object, objectName);
+ }
+
+ @AfterClass
+ public static void tearDown() throws Exception {
+ server.unregisterMBean(objectName);
+ }
+
+ @Test
+ public void test() throws Exception {
+ MBeanInfo info = server.getMBeanInfo(objectName);
+
+ assertEquals("MBean desc.", info.getDescription());
+
+ MBeanAttributeInfo a0 = findAttribute(info, "Getter");
+ assertEquals("getter", a0.getDescription());
+
+ MBeanAttributeInfo a1 = findAttribute(info, "It");
+ assertEquals("is", a1.getDescription());
+
+ MBeanAttributeInfo a2 = findAttribute(info, "Setter");
+ assertEquals("setter", a2.getDescription());
+
+ MBeanOperationInfo op0 = info.getOperations()[0];
+ assertEquals("run", op0.getDescription());
+ assertEquals(MBeanOperationInfo.INFO, op0.getImpact());
+
+ MBeanParameterInfo p0 = op0.getSignature()[0];
+ assertEquals("timeout", p0.getName());
+ assertEquals("how long?", p0.getDescription());
+ }
+
+ private MBeanAttributeInfo findAttribute(MBeanInfo info, String name) {
+ for (MBeanAttributeInfo a : info.getAttributes()) {
+ if (a.getName().equals(name)) return a;
+ }
+
+ return null;
+ }
+
+ @Description("MBean desc.")
+ public interface FooMBean {
+ @Description("getter")
+ String getGetter();
+
+ @Description("is")
+ boolean isIt();
+
+ @Description("setter")
+ void setSetter(long s);
+
+ @Description("run")
+ @Impact(ImpactOption.INFO)
+ void run(@Name("timeout") @Description("how long?") long timeout);
+ }
+
+ public static class Foo implements FooMBean {
+
+ public String getGetter() {
+ return null;
+ }
+
+ public boolean isIt() {
+ return false;
+ }
+
+ public void setSetter(long s) {
+ }
+
+ public void run(long timeout) {
+ }
+ }
+}
Propchange: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/jmx/AnnotatedStandardMBeanTest.java
------------------------------------------------------------------------------
svn:eol-style = native