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.rrd;
21  
22  import org.neo4j.jmx.Kernel;
23  import org.neo4j.jmx.Primitives;
24  import org.neo4j.kernel.AbstractGraphDatabase;
25  
26  import javax.management.AttributeNotFoundException;
27  import javax.management.InstanceNotFoundException;
28  import javax.management.MBeanException;
29  import javax.management.MBeanServer;
30  import javax.management.MalformedObjectNameException;
31  import javax.management.ObjectName;
32  import javax.management.ReflectionException;
33  import java.lang.management.ManagementFactory;
34  
35  public abstract class JmxSampleableBase  implements Sampleable
36  {
37      private MBeanServer mbeanServer;
38      private ObjectName objectName;
39  
40      public JmxSampleableBase( AbstractGraphDatabase graphDb ) throws MalformedObjectNameException
41      {
42          mbeanServer = ManagementFactory.getPlatformMBeanServer();
43          ObjectName neoQuery = graphDb.getManagementBean( Kernel.class ).getMBeanQuery();
44          String instance = neoQuery.getKeyProperty( "instance" );
45          String baseName = neoQuery.getDomain() + ":instance=" + instance + ",name=";
46          objectName = new ObjectName( baseName + Primitives.NAME );
47      }
48  
49      public abstract String getName();
50  
51      public long getValue()
52      {
53          try
54          {
55              return (Long)mbeanServer.getAttribute( objectName, getJmxAttributeName() );
56          } catch ( MBeanException e )
57          {
58              throw new RuntimeException( e );
59          } catch ( AttributeNotFoundException e )
60          {
61              throw new RuntimeException( e );
62          } catch ( InstanceNotFoundException e )
63          {
64              throw new RuntimeException( e );
65          } catch ( ReflectionException e )
66          {
67              throw new RuntimeException( e );
68          }
69      }
70  
71      protected abstract String getJmxAttributeName();
72  }