2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.dynamicmbean;
10 import java.lang.reflect.Method;
11 import java.util.HashSet;
12 import java.util.List;
15 import javax.annotation.Nullable;
16 import javax.annotation.concurrent.Immutable;
17 import javax.management.MBeanAttributeInfo;
18 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.annotations.Description;
21 import org.opendaylight.controller.config.api.annotations.RequireInterface;
24 class AttributeHolder {
26 private final String name;
27 private final String description;
28 private final Object object;
29 private final boolean writable;
32 private final RequireInterface requireInterfaceAnnotation;
33 private final String attributeType;
35 public AttributeHolder(String name, Object object, String returnType,
37 @Nullable RequireInterface requireInterfaceAnnotation,
40 throw new NullPointerException();
44 throw new NullPointerException();
47 this.writable = writable;
48 this.requireInterfaceAnnotation = requireInterfaceAnnotation;
49 this.attributeType = returnType;
50 this.description = description;
53 public MBeanAttributeInfo toMBeanAttributeInfo() {
54 MBeanAttributeInfo info = new MBeanAttributeInfo(name, attributeType,
55 description, true, true, false);
60 * @return annotation if setter sets ObjectName or ObjectName[], and is
61 * annotated. Return null otherwise.
63 RequireInterface getRequireInterfaceOrNull() {
64 return requireInterfaceAnnotation;
67 public String getName() {
71 public Object getObject() {
75 public String getAttributeType() {
79 public boolean isWritable() {
83 public String getDescription() {
88 * Find @Description annotations in method class and all its exported
92 * @param jmxInterfaces
93 * @return empty string if no annotation is found, or list of descriptions
94 * separated by newline
96 static String findDescription(Method setter, Set<Class<?>> jmxInterfaces) {
97 List<Description> descriptions = AnnotationsHelper
98 .findMethodAnnotationInSuperClassesAndIfcs(setter, Description.class, jmxInterfaces);
99 return AnnotationsHelper.aggregateDescriptions(descriptions);
103 * Find @RequireInterface annotation by searching method class and all
104 * exported interfaces.
107 * @param inspectedInterfaces
108 * @throws IllegalStateException
109 * if more than one value is specified by found annotations
110 * @throws IllegalArgumentException
111 * if set of exported interfaces contains non interface type
112 * @return null if no annotation is found, otherwise return the annotation
114 static RequireInterface findRequireInterfaceAnnotation(final Method setter,
115 Set<Class<?>> inspectedInterfaces) {
117 // only allow setX(ObjectName y) or setX(ObjectName[] y) to continue
118 if (setter.getParameterTypes().length != 1
119 || (setter.getParameterTypes()[0].equals(ObjectName.class) == false && setter
120 .getParameterTypes()[0].equals(ObjectName[].class) == false)) {
124 List<RequireInterface> foundRequireInterfaces = AnnotationsHelper
125 .findMethodAnnotationInSuperClassesAndIfcs(setter, RequireInterface.class, inspectedInterfaces);
126 // make sure the list if not empty contains always annotation with same
128 Set<Class<?>> foundValues = new HashSet<Class<?>>();
129 for (RequireInterface ri : foundRequireInterfaces) {
130 foundValues.add(ri.value());
132 if (foundValues.size() == 0) {
134 } else if (foundValues.size() > 1) {
135 throw new IllegalStateException("Error finding @RequireInterface. "
136 + "More than one value specified as required interface "
137 + foundValues + " of " + setter + " of "
138 + setter.getDeclaringClass());
140 return foundRequireInterfaces.get(0);