Home » geronimo-2.2-source-release » org.apache.geronimo.j2ee.management.impl » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   
   18   package org.apache.geronimo.j2ee.management.impl;
   19   
   20   import java.lang.management.ManagementFactory;
   21   import java.lang.management.MemoryMXBean;
   22   import java.lang.management.MemoryUsage;
   23   import java.lang.management.RuntimeMXBean;
   24   import java.net.InetAddress;
   25   import java.net.UnknownHostException;
   26   import java.util.Date;
   27   import java.util.Hashtable;
   28   import java.util.Properties;
   29   
   30   import javax.management.ObjectName;
   31   import javax.management.j2ee.statistics.Stats;
   32   
   33   import org.apache.geronimo.gbean.GBeanInfo;
   34   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   35   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   36   import org.apache.geronimo.kernel.Kernel;
   37   import org.apache.geronimo.kernel.ObjectNameUtil;
   38   import org.apache.geronimo.management.StatisticsProvider;
   39   import org.apache.geronimo.management.geronimo.JVM;
   40   import org.apache.geronimo.management.stats.BoundedRangeStatisticImpl;
   41   import org.apache.geronimo.management.stats.CountStatisticImpl;
   42   import org.apache.geronimo.management.stats.JVMStatsImpl;
   43   import org.apache.geronimo.system.logging.SystemLog;
   44   
   45   /**
   46    *
   47    *
   48    * @version $Rev: 561094 $ $Date: 2007-07-30 12:05:20 -0700 (Mon, 30 Jul 2007) $
   49    */
   50   public class JVMImpl implements JVM, StatisticsProvider {
   51       public static final String JAVA_VERSION = System.getProperty("java.version");
   52       public static final String JAVA_VENDOR = System.getProperty("java.vendor");
   53       public static final String NODE;
   54       private static final Runtime runtime = Runtime.getRuntime();
   55   
   56       static {
   57           String node;
   58           try {
   59               node = InetAddress.getLocalHost().toString();
   60           } catch (UnknownHostException e) {
   61               node = null;
   62           }
   63           NODE = node;
   64       }
   65   
   66       private final String objectName;
   67       private final Kernel kernel;
   68       private final SystemLog systemLog;
   69       private JVMStatsImpl stats;
   70   
   71       public JVMImpl(String objectName, Kernel kernel, SystemLog systemLog) {
   72           this.objectName = objectName;
   73           this.kernel = kernel;
   74           this.systemLog = systemLog;
   75           ObjectName myObjectName = ObjectNameUtil.getObjectName(this.objectName);
   76           verifyObjectName(myObjectName);
   77       }
   78   
   79       /**
   80        * ObjectName must match this pattern:
   81        * <p/>
   82        * domain:j2eeType=JVM,name=MyName
   83        */
   84       private void verifyObjectName(ObjectName objectName) {
   85           if (objectName.isPattern()) {
   86               throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
   87           }
   88           Hashtable keyPropertyList = objectName.getKeyPropertyList();
   89           if (!"JVM".equals(keyPropertyList.get("j2eeType"))) {
   90               throw new InvalidObjectNameException("JVM object name j2eeType property must be 'JVM'", objectName);
   91           }
   92           if (!keyPropertyList.containsKey("name")) {
   93               throw new InvalidObjectNameException("JVM object must contain a name property", objectName);
   94           }
   95           if (!keyPropertyList.containsKey("J2EEServer")) {
   96               throw new InvalidObjectNameException("JVM object must contain a J2EEServer property", objectName);
   97           }
   98           if (keyPropertyList.size() != 3) {
   99               throw new InvalidObjectNameException("JVM object name can only have J2EEServer, j2eeType, and name", objectName);
  100           }
  101       }
  102   
  103       public String getObjectName() {
  104           return objectName;
  105       }
  106   
  107       public boolean isStateManageable() {
  108           return true;
  109       }
  110   
  111       public boolean isStatisticsProvider() {
  112           return true;
  113       }
  114   
  115       public boolean isEventProvider() {
  116           return true;
  117       }
  118   
  119       /**
  120        * The version of the JVMImpl we are running on.
  121        * This is the value of java.version system property
  122        * @see "JSR77.3.4.1.1"
  123        * @return the JVMImpl version
  124        */
  125       public String getJavaVersion() {
  126           return JAVA_VERSION;
  127       }
  128   
  129       /**
  130        * The vendor of the JVMImpl we are running on.
  131        * This is the value of java.vendor system property
  132        * @see "JSR77.3.4.1.2"
  133        * @return the JVMImpl version
  134        */
  135       public String getJavaVendor() {
  136           return JAVA_VENDOR;
  137       }
  138   
  139       /**
  140        * The node we are running on.
  141        * This is the fully qualified host name returned for InetAddress.getLocalHost.toString();
  142        * we return null if there is no network
  143        * @see "JSR77.3.4.1.3"
  144        * @return the node we are running on
  145        */
  146       public String getNode() {
  147           return NODE;
  148       }
  149   
  150       public int getAvailableProcessors() {
  151           return runtime.availableProcessors();
  152       }
  153   
  154       public Date getKernelBootTime() {
  155           return kernel.getBootTime();
  156       }
  157   
  158       public Stats getStats() {
  159           RuntimeMXBean runmxbean = ManagementFactory.getRuntimeMXBean();
  160           MemoryMXBean memmxbean = ManagementFactory.getMemoryMXBean();
  161           MemoryUsage memUsage = memmxbean.getHeapMemoryUsage();
  162           CountStatisticImpl upTime;
  163           BoundedRangeStatisticImpl heapSize;
  164           
  165           if (stats == null) {
  166               stats = new JVMStatsImpl();
  167               // setup UpTime CountStatistic
  168               upTime = stats.getUpTimeImpl();
  169               upTime.setStartTime(runmxbean.getStartTime());
  170               upTime.setCount(runmxbean.getUptime());
  171               // setup Heap BoundedRangeStatistic
  172               heapSize = stats.getHeapSizeImpl();
  173               heapSize.setStartTime(runmxbean.getStartTime());
  174               heapSize.setBounds(0, memUsage.getMax());
  175               heapSize.setCurrent(memUsage.getUsed());
  176               heapSize.setLowWaterMark(memUsage.getUsed());
  177               heapSize.setHighWaterMark(memUsage.getUsed());
  178           } else {
  179               // update UpTime CountStatistic
  180               upTime = stats.getUpTimeImpl();
  181               upTime.setCount(runmxbean.getUptime());
  182               // update Heap BoundedRangeStatistic
  183               heapSize = stats.getHeapSizeImpl();
  184               heapSize.setBounds(0, memUsage.getMax());
  185               heapSize.setCurrent(memUsage.getUsed());
  186           }
  187           long now = upTime.getStartTime() + upTime.getCount();
  188           upTime.setLastSampleTime(now);
  189           heapSize.setLastSampleTime(now);
  190   
  191           return stats;
  192       }
  193   
  194       public void resetStats() {
  195           // TODO
  196       }
  197   
  198       public Properties getSystemProperties() {
  199           return System.getProperties();
  200       }
  201   
  202       public SystemLog getSystemLog() {
  203           return systemLog;
  204       }
  205   
  206       public static final GBeanInfo GBEAN_INFO;
  207   
  208       static {
  209           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic(JVMImpl.class, NameFactory.JVM);
  210           infoFactory.addReference("SystemLog", SystemLog.class);
  211           infoFactory.setConstructor(new String[] {"objectName", "kernel", "SystemLog"});
  212           GBEAN_INFO = infoFactory.getBeanInfo();
  213       }
  214   
  215       public static GBeanInfo getGBeanInfo() {
  216           return GBEAN_INFO;
  217       }
  218   }

Home » geronimo-2.2-source-release » org.apache.geronimo.j2ee.management.impl » [javadoc | source]