Debian Codegraph @

owner/src/main/java/org/aeonbits/owner/JMXSupport.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2015, Luigi R. Viggiano
3 * All rights reserved.
4 *
5 * This software is distributable under the BSD license.
6 * See the terms of the BSD license in the documentation provided with this software.
7 */
8
9 package org.aeonbits.owner;
10
11 import javax.management.Attribute;
12 import javax.management.AttributeList;
13 import javax.management.AttributeNotFoundException;
14 import javax.management.InvalidAttributeValueException;
15 import javax.management.MBeanAttributeInfo;
16 import javax.management.MBeanException;
17 import javax.management.MBeanInfo;
18 import javax.management.MBeanOperationInfo;
19 import javax.management.MBeanParameterInfo;
20 import javax.management.ReflectionException;
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Set;
26
27 /**
28 * @author Robin Meißner
29 * @author Luigi R. Viggiano
30 */
31 class JMXSupport implements Serializable {
32
33 private final Class<?> clazz;
34 private final PropertiesManager manager;
35
36 public JMXSupport(Class<?> clazz, PropertiesManager manager) {
37 this.clazz = clazz;
38 this.manager = manager;
39 }
40
41 @Delegate
42 public Object getAttribute(String attribute)
43 throws AttributeNotFoundException, MBeanException,
44 ReflectionException {
45 return manager.getProperty(attribute);
46 }
47
48 @Delegate
49 public void setAttribute(Attribute attribute)
50 throws AttributeNotFoundException, InvalidAttributeValueException,
51 MBeanException, ReflectionException {
52 manager.setProperty(attribute.getName(), (String) attribute.getValue());
53 }
54
55 @Delegate
56 public AttributeList getAttributes(String[] attributes) {
57 List<Attribute> attrList = new LinkedList<Attribute>();
58 for (String propertyName : attributes)
59 attrList.add(new Attribute(propertyName, manager.getProperty(propertyName)));
60 return new AttributeList(attrList);
61 }
62
63 @Delegate
64 public AttributeList setAttributes(AttributeList attributes) {
65 for (Attribute attr : attributes.asList())
66 manager.setProperty(attr.getName(), (String) attr.getValue());
67 return attributes;
68 }
69
70 @Delegate
71 public Object invoke(String actionName, Object[] params, String[] signature)
72 throws MBeanException, ReflectionException {
73 if (actionName.equals("getProperty") && params != null && params.length == 1) {
74 return manager.getProperty((String) params[0]);
75 } else if (actionName.equals("setProperty") && params != null && params.length == 2) {
76 manager.setProperty((String) params[0], (String) params[1]);
77 return null;
78 } else if (actionName.equals("reload") && (params == null || params.length == 0)) {
79 manager.reload();
80 return null;
81 }
82 throw new ReflectionException(new NoSuchMethodException(actionName));
83 }
84
85 @Delegate
86 public MBeanInfo getMBeanInfo() {
87 List<MBeanAttributeInfo> attributesInfo = new ArrayList<MBeanAttributeInfo>();
88 Set<String> propertyNames = manager.propertyNames();
89 for (String name : propertyNames)
90 attributesInfo.add(new MBeanAttributeInfo(name, "java.lang.String", name, true, true, false));
91
92 MBeanAttributeInfo[] attributes = attributesInfo.toArray(new MBeanAttributeInfo[propertyNames.size()]);
93
94 MBeanParameterInfo key = new MBeanParameterInfo("key", "java.lang.String", "Key of the property");
95 MBeanParameterInfo value = new MBeanParameterInfo("value", "java.lang.String", "Value of the property");
96
97 MBeanOperationInfo[] operations = new MBeanOperationInfo[] {
98 new MBeanOperationInfo("getProperty", "Gets value for a property",
99 new MBeanParameterInfo[] { key }, "java.lang.String", MBeanOperationInfo.INFO),
100 new MBeanOperationInfo("setProperty", "Sets the value for a property",
101 new MBeanParameterInfo[] { key, value }, "void", MBeanOperationInfo.ACTION),
102 new MBeanOperationInfo("reload", "Reload properties", null, "void", MBeanOperationInfo.ACTION)
103 };
104
105 return new MBeanInfo(clazz.getName(), clazz.getSimpleName() + " OWNER MBean",
106 attributes, null, operations, null);
107 }
108
109 }