1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.jmx; |
7 | |
8 | import java.io.ByteArrayInputStream; |
9 | import java.io.IOException; |
10 | import java.util.Properties; |
11 | import javax.management.MBeanAttributeInfo; |
12 | import javax.management.MBeanInfo; |
13 | import javax.management.MBeanOperationInfo; |
14 | import javax.management.NotCompliantMBeanException; |
15 | import javax.management.StandardMBean; |
16 | import org.h2.util.Utils; |
17 | |
18 | /** |
19 | * An MBean that reads the documentation from a resource file. |
20 | */ |
21 | public class DocumentedMBean extends StandardMBean { |
22 | |
23 | private final String interfaceName; |
24 | private Properties resources; |
25 | |
26 | public <T> DocumentedMBean(T impl, Class<T> mbeanInterface) |
27 | throws NotCompliantMBeanException { |
28 | super(impl, mbeanInterface); |
29 | this.interfaceName = impl.getClass().getName() + "MBean"; |
30 | } |
31 | |
32 | private Properties getResources() { |
33 | if (resources == null) { |
34 | resources = new Properties(); |
35 | String resourceName = "/org/h2/res/javadoc.properties"; |
36 | try { |
37 | byte[] buff = Utils.getResource(resourceName); |
38 | if (buff != null) { |
39 | resources.load(new ByteArrayInputStream(buff)); |
40 | } |
41 | } catch (IOException e) { |
42 | // ignore |
43 | } |
44 | } |
45 | return resources; |
46 | } |
47 | |
48 | @Override |
49 | protected String getDescription(MBeanInfo info) { |
50 | String s = getResources().getProperty(interfaceName); |
51 | return s == null ? super.getDescription(info) : s; |
52 | } |
53 | |
54 | @Override |
55 | protected String getDescription(MBeanOperationInfo op) { |
56 | String s = getResources().getProperty(interfaceName + "." + op.getName()); |
57 | return s == null ? super.getDescription(op) : s; |
58 | } |
59 | |
60 | @Override |
61 | protected String getDescription(MBeanAttributeInfo info) { |
62 | String prefix = info.isIs() ? "is" : "get"; |
63 | String s = getResources().getProperty( |
64 | interfaceName + "." + prefix + info.getName()); |
65 | return s == null ? super.getDescription(info) : s; |
66 | } |
67 | |
68 | @Override |
69 | protected int getImpact(MBeanOperationInfo info) { |
70 | if (info.getName().startsWith("list")) { |
71 | return MBeanOperationInfo.INFO; |
72 | } |
73 | return MBeanOperationInfo.ACTION; |
74 | } |
75 | |
76 | } |