View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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  }