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.helpers.collection.IterableWrapper;
23 import org.neo4j.server.rest.repr.ListRepresentation;
24 import org.neo4j.server.rest.repr.ObjectRepresentation;
25 import org.neo4j.server.rest.repr.Representation;
26 import org.neo4j.server.rest.repr.ValueRepresentation;
27
28 import javax.management.InstanceNotFoundException;
29 import javax.management.IntrospectionException;
30 import javax.management.MBeanAttributeInfo;
31 import javax.management.MBeanInfo;
32 import javax.management.MBeanServer;
33 import javax.management.ObjectName;
34 import javax.management.ReflectionException;
35 import java.io.UnsupportedEncodingException;
36 import java.lang.management.ManagementFactory;
37 import java.net.URLEncoder;
38 import java.util.Arrays;
39
40 public class JmxMBeanRepresentation extends ObjectRepresentation
41 {
42
43 protected ObjectName beanName;
44 protected MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
45
46 public JmxMBeanRepresentation( ObjectName beanInstance )
47 {
48 super( "jmxBean" );
49 this.beanName = beanInstance;
50 }
51
52 @Mapping( "name" )
53 public ValueRepresentation getName()
54 {
55 return ValueRepresentation.string( beanName.toString() );
56 }
57
58 @Mapping( "url" )
59 public ValueRepresentation getUrl()
60 {
61 try
62 {
63 String value = URLEncoder.encode( beanName.toString(), "UTF-8" ).replace( "%3A", "/" );
64 return ValueRepresentation.string( value );
65 } catch ( UnsupportedEncodingException e )
66 {
67 throw new RuntimeException( "Could not encode string as UTF-8", e );
68 }
69 }
70
71 @Mapping( "description" )
72 public ValueRepresentation getDescription() throws IntrospectionException, InstanceNotFoundException, ReflectionException
73 {
74 MBeanInfo beanInfo = jmxServer.getMBeanInfo( beanName );
75 return ValueRepresentation.string( beanInfo.getDescription() );
76 }
77
78 @Mapping( "attributes" )
79 public ListRepresentation getAttributes() throws IntrospectionException, InstanceNotFoundException, ReflectionException
80 {
81 MBeanInfo beanInfo = jmxServer.getMBeanInfo( beanName );
82
83 return new ListRepresentation( "jmxAttribute", new IterableWrapper<Representation, MBeanAttributeInfo>( Arrays.asList( beanInfo.getAttributes() ) )
84 {
85 @Override
86 protected Representation underlyingObjectToObject( MBeanAttributeInfo attrInfo )
87 {
88 return new JmxAttributeRepresentation( beanName, attrInfo );
89 }
90 } );
91 }
92 }