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.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 }