Source for gnu.java.lang.management.MemoryPoolMXBeanImpl

   1: /* MemoryPoolMXBeanImpl.java - Implementation of a memory pool bean
   2:    Copyright (C) 2006 Free Software Foundation
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.lang.management;
  39: 
  40: import gnu.classpath.SystemProperties;
  41: 
  42: import java.lang.management.MemoryPoolMXBean;
  43: import java.lang.management.MemoryUsage;
  44: 
  45: import javax.management.NotCompliantMBeanException;
  46: 
  47: /**
  48:  * Provides access to information about one of the memory 
  49:  * resources or pools used by the current invocation of the
  50:  * virtual machine.  An instance of this bean for each memory
  51:  * pool is obtained by calling
  52:  * {@link ManagementFactory#getMemoryPoolMXBeans()}.
  53:  *
  54:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  55:  * @since 1.5
  56:  */
  57: public final class MemoryPoolMXBeanImpl
  58:   extends BeanImpl
  59:   implements MemoryPoolMXBean
  60: {
  61: 
  62:   /**
  63:    * The name of the pool.
  64:    */
  65:   private String name;
  66: 
  67:   /**
  68:    * Constant for collection usage threshold.
  69:    */
  70:   private static final String COLLECTION_USAGE_THRESHOLD = 
  71:     "gnu.java.lang.management.CollectionUsageThresholdSupport";
  72: 
  73:   /**
  74:    * Constant for thread time support.
  75:    */
  76:   private static final String USAGE_THRESHOLD = 
  77:     "gnu.java.lang.management.UsageThresholdSupport";
  78: 
  79:   /**
  80:    * Constructs a new <code>MemoryPoolMXBeanImpl</code>.
  81:    *
  82:    * @param name the name of the pool this bean represents.
  83:    * @throws NotCompliantMBeanException if this class doesn't implement
  84:    *                                    the interface or a method appears
  85:    *                                    in the interface that doesn't comply
  86:    *                                    with the naming conventions.
  87:    */
  88:   public MemoryPoolMXBeanImpl(String name)
  89:     throws NotCompliantMBeanException
  90:   {
  91:     super(MemoryPoolMXBean.class);
  92:     this.name = name;
  93:   }
  94: 
  95:   public MemoryUsage getCollectionUsage()
  96:   {
  97:     return VMMemoryPoolMXBeanImpl.getCollectionUsage(name);
  98:   }
  99: 
 100:   public long getCollectionUsageThreshold()
 101:   {
 102:     if (isCollectionUsageThresholdSupported())
 103:       return VMMemoryPoolMXBeanImpl.getCollectionUsageThreshold(name);
 104:     else
 105:       throw new UnsupportedOperationException("A collection usage "+
 106:                           "threshold is not supported.");
 107:   }
 108: 
 109:   public long getCollectionUsageThresholdCount()
 110:   {
 111:     if (isCollectionUsageThresholdSupported())
 112:       return VMMemoryPoolMXBeanImpl.getCollectionUsageThresholdCount(name);
 113:     else
 114:       throw new UnsupportedOperationException("A collection usage "+
 115:                           "threshold is not supported.");
 116:   }
 117: 
 118:   public String[] getMemoryManagerNames()
 119:   {
 120:     return VMMemoryPoolMXBeanImpl.getMemoryManagerNames(name);
 121:   }
 122: 
 123:   public String getName()
 124:   {
 125:     return name;
 126:   }
 127: 
 128:   public MemoryUsage getPeakUsage()
 129:   {
 130:     if (isValid())
 131:       return VMMemoryPoolMXBeanImpl.getPeakUsage(name);
 132:     else
 133:       return null;
 134:   }
 135: 
 136:   public String getType()
 137:   {
 138:     return VMMemoryPoolMXBeanImpl.getType(name);
 139:   }
 140: 
 141:   public MemoryUsage getUsage()
 142:   {
 143:     if (isValid())
 144:       return VMMemoryPoolMXBeanImpl.getUsage(name);
 145:     else
 146:       return null;
 147:   }
 148: 
 149:   public long getUsageThreshold()
 150:   {
 151:     if (isUsageThresholdSupported())
 152:       return VMMemoryPoolMXBeanImpl.getUsageThreshold(name);
 153:     else
 154:       throw new UnsupportedOperationException("A usage threshold " +
 155:                           "is not supported.");
 156:   }
 157: 
 158:   public long getUsageThresholdCount()
 159:   {
 160:     if (isUsageThresholdSupported())
 161:       return VMMemoryPoolMXBeanImpl.getUsageThresholdCount(name);
 162:     else
 163:       throw new UnsupportedOperationException("A usage threshold " +
 164:                           "is not supported.");
 165:   }
 166: 
 167:   public boolean isCollectionUsageThresholdExceeded()
 168:   {
 169:     return getCollectionUsage().getUsed() >= getCollectionUsageThreshold();
 170:   }
 171: 
 172:   public boolean isCollectionUsageThresholdSupported()
 173:   {
 174:     return SystemProperties.getProperty(COLLECTION_USAGE_THRESHOLD) != null;
 175:   }
 176: 
 177:   public boolean isUsageThresholdExceeded()
 178:   {
 179:     return getUsage().getUsed() >= getUsageThreshold();
 180:   }
 181: 
 182:   public boolean isUsageThresholdSupported()
 183:   {
 184:     return SystemProperties.getProperty(USAGE_THRESHOLD) != null;
 185:   }
 186: 
 187:   public boolean isValid()
 188:   {
 189:     return VMMemoryPoolMXBeanImpl.isValid(name);
 190:   }
 191: 
 192:   public void resetPeakUsage()
 193:   {
 194:     checkControlPermissions();
 195:     VMMemoryPoolMXBeanImpl.resetPeakUsage(name);
 196:   }
 197: 
 198:   public void setCollectionUsageThreshold(long threshold)
 199:   {
 200:     checkControlPermissions();
 201:     if (threshold < 0)
 202:       throw new IllegalArgumentException("Threshold of " + threshold +
 203:                      "is less than zero.");
 204:     if (isCollectionUsageThresholdSupported())
 205:       VMMemoryPoolMXBeanImpl.setCollectionUsageThreshold(name, threshold);
 206:     else
 207:       throw new UnsupportedOperationException("A collection usage "+
 208:                           "threshold is not supported.");
 209:   }
 210: 
 211:   public void setUsageThreshold(long threshold)
 212:   {
 213:     checkControlPermissions();
 214:     if (threshold < 0)
 215:       throw new IllegalArgumentException("Threshold of " + threshold +
 216:                      "is less than zero.");
 217:     if (isUsageThresholdSupported())
 218:       VMMemoryPoolMXBeanImpl.setUsageThreshold(name, threshold);
 219:     else
 220:       throw new UnsupportedOperationException("A usage threshold " +
 221:                           "is not supported.");
 222:   }
 223: 
 224: }