1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.webadmin.rest.representations;
21
22 import org.neo4j.server.rest.repr.ObjectRepresentation;
23 import org.neo4j.server.rest.repr.Representation;
24 import org.neo4j.server.rest.repr.ValueRepresentation;
25
26 import javax.management.MBeanAttributeInfo;
27 import javax.management.MBeanServer;
28 import javax.management.ObjectName;
29
30 import java.lang.management.ManagementFactory;
31
32 public class JmxAttributeRepresentation extends ObjectRepresentation
33 {
34
35 protected ObjectName objectName;
36 protected MBeanAttributeInfo attrInfo;
37 protected MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
38
39 public JmxAttributeRepresentation( ObjectName objectName,
40 MBeanAttributeInfo attrInfo )
41 {
42 super( "jmxAttribute" );
43 this.objectName = objectName;
44 this.attrInfo = attrInfo;
45 }
46
47 @Mapping( "name" )
48 public ValueRepresentation getName()
49 {
50 return ValueRepresentation.string( attrInfo.getName() );
51 }
52
53 @Mapping( "description" )
54 public ValueRepresentation getDescription()
55 {
56 return ValueRepresentation.string( attrInfo.getDescription() );
57 }
58
59 @Mapping( "type" )
60 public ValueRepresentation getType()
61 {
62 return ValueRepresentation.string( attrInfo.getType() );
63 }
64
65 @Mapping( "isReadable" )
66 public ValueRepresentation isReadable()
67 {
68 return bool( attrInfo.isReadable() );
69 }
70
71 @Mapping( "isWriteable" )
72 public ValueRepresentation isWriteable()
73 {
74 return bool( attrInfo.isWritable() );
75 }
76
77 @Mapping( "isIs" )
78 public ValueRepresentation isIs()
79 {
80 return bool( attrInfo.isIs() );
81 }
82
83 private ValueRepresentation bool( Boolean value )
84 {
85 return ValueRepresentation.string( value ? "true" : "false " );
86 }
87
88 @Mapping( "value" )
89 public Representation getValue()
90 {
91 try
92 {
93 JmxAttributeRepresentationDispatcher representationDispatcher = new JmxAttributeRepresentationDispatcher();
94 Object value = jmxServer.getAttribute( objectName,
95 attrInfo.getName() );
96 return representationDispatcher.dispatch( value, "" );
97 }
98 catch ( Exception e )
99 {
100 return ValueRepresentation.string( "N/A" );
101 }
102 }
103
104 }