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.MemoryType;
  44: import java.lang.management.MemoryUsage;
  45: 
  46: import javax.management.NotCompliantMBeanException;
  47: 
  48: /**
  49:  * Provides access to information about one of the memory
  50:  * resources or pools used by the current invocation of the
  51:  * virtual machine.  An instance of this bean for each memory
  52:  * pool is obtained by calling
  53:  * {@link ManagementFactory#getMemoryPoolMXBeans()}.
  54:  *
  55:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  56:  * @since 1.5
  57:  */
  58: public final class MemoryPoolMXBeanImpl
  59:   extends BeanImpl
  60:   implements MemoryPoolMXBean
  61: {
  62: 
  63:   /**
  64:    * The name of the pool.
  65:    */
  66:   private String name;
  67: 
  68:   /**
  69:    * Constant for collection usage threshold.
  70:    */
  71:   private static final String COLLECTION_USAGE_THRESHOLD =
  72:     "gnu.java.lang.management.CollectionUsageThresholdSupport";
  73: 
  74:   /**
  75:    * Constant for thread time support.
  76:    */
  77:   private static final String USAGE_THRESHOLD =
  78:     "gnu.java.lang.management.UsageThresholdSupport";
  79: 
  80:   /**
  81:    * Constructs a new <code>MemoryPoolMXBeanImpl</code>.
  82:    *
  83:    * @param name the name of the pool this bean represents.
  84:    * @throws NotCompliantMBeanException if this class doesn't implement
  85:    *                                    the interface or a method appears
  86:    *                                    in the interface that doesn't comply
  87:    *                                    with the naming conventions.
  88:    */
  89:   public MemoryPoolMXBeanImpl(String name)
  90:     throws NotCompliantMBeanException
  91:   {
  92:     super(MemoryPoolMXBean.class);
  93:     this.name = name;
  94:   }
  95: 
  96:   public MemoryUsage getCollectionUsage()
  97:   {
  98:     return VMMemoryPoolMXBeanImpl.getCollectionUsage(name);
  99:   }
 100: 
 101:   public long getCollectionUsageThreshold()
 102:   {
 103:     if (isCollectionUsageThresholdSupported())
 104:       return VMMemoryPoolMXBeanImpl.getCollectionUsageThreshold(name);
 105:     else
 106:       throw new UnsupportedOperationException("A collection usage "+
 107:                                               "threshold is not supported.");
 108:   }
 109: 
 110:   public long getCollectionUsageThresholdCount()
 111:   {
 112:     if (isCollectionUsageThresholdSupported())
 113:       return VMMemoryPoolMXBeanImpl.getCollectionUsageThresholdCount(name);
 114:     else
 115:       throw new UnsupportedOperationException("A collection usage "+
 116:                                               "threshold is not supported.");
 117:   }
 118: 
 119:   public String[] getMemoryManagerNames()
 120:   {
 121:     return VMMemoryPoolMXBeanImpl.getMemoryManagerNames(name);
 122:   }
 123: 
 124:   public String getName()
 125:   {
 126:     return name;
 127:   }
 128: 
 129:   public MemoryUsage getPeakUsage()
 130:   {
 131:     if (isValid())
 132:       return VMMemoryPoolMXBeanImpl.getPeakUsage(name);
 133:     else
 134:       return null;
 135:   }
 136: 
 137:   public MemoryType getType()
 138:   {
 139:     return
 140:       MemoryType.valueOf(VMMemoryPoolMXBeanImpl.getType(name));
 141:   }
 142: 
 143:   public MemoryUsage getUsage()
 144:   {
 145:     if (isValid())
 146:       return VMMemoryPoolMXBeanImpl.getUsage(name);
 147:     else
 148:       return null;
 149:   }
 150: 
 151:   public long getUsageThreshold()
 152:   {
 153:     if (isUsageThresholdSupported())
 154:       return VMMemoryPoolMXBeanImpl.getUsageThreshold(name);
 155:     else
 156:       throw new UnsupportedOperationException("A usage threshold " +
 157:                                               "is not supported.");
 158:   }
 159: 
 160:   public long getUsageThresholdCount()
 161:   {
 162:     if (isUsageThresholdSupported())
 163:       return VMMemoryPoolMXBeanImpl.getUsageThresholdCount(name);
 164:     else
 165:       throw new UnsupportedOperationException("A usage threshold " +
 166:                                               "is not supported.");
 167:   }
 168: 
 169:   public boolean isCollectionUsageThresholdExceeded()
 170:   {
 171:     return getCollectionUsage().getUsed() >= getCollectionUsageThreshold();
 172:   }
 173: 
 174:   public boolean isCollectionUsageThresholdSupported()
 175:   {
 176:     return SystemProperties.getProperty(COLLECTION_USAGE_THRESHOLD) != null;
 177:   }
 178: 
 179:   public boolean isUsageThresholdExceeded()
 180:   {
 181:     return getUsage().getUsed() >= getUsageThreshold();
 182:   }
 183: 
 184:   public boolean isUsageThresholdSupported()
 185:   {
 186:     return SystemProperties.getProperty(USAGE_THRESHOLD) != null;
 187:   }
 188: 
 189:   public boolean isValid()
 190:   {
 191:     return VMMemoryPoolMXBeanImpl.isValid(name);
 192:   }
 193: 
 194:   public void resetPeakUsage()
 195:   {
 196:     checkControlPermissions();
 197:     VMMemoryPoolMXBeanImpl.resetPeakUsage(name);
 198:   }
 199: 
 200:   public void setCollectionUsageThreshold(long threshold)
 201:   {
 202:     checkControlPermissions();
 203:     if (threshold < 0)
 204:       throw new IllegalArgumentException("Threshold of " + threshold +
 205:                                          "is less than zero.");
 206:     if (isCollectionUsageThresholdSupported())
 207:       VMMemoryPoolMXBeanImpl.setCollectionUsageThreshold(name, threshold);
 208:     else
 209:       throw new UnsupportedOperationException("A collection usage "+
 210:                                               "threshold is not supported.");
 211:   }
 212: 
 213:   public void setUsageThreshold(long threshold)
 214:   {
 215:     checkControlPermissions();
 216:     if (threshold < 0)
 217:       throw new IllegalArgumentException("Threshold of " + threshold +
 218:                                          "is less than zero.");
 219:     if (isUsageThresholdSupported())
 220:       VMMemoryPoolMXBeanImpl.setUsageThreshold(name, threshold);
 221:     else
 222:       throw new UnsupportedOperationException("A usage threshold " +
 223:                                               "is not supported.");
 224:   }
 225: 
 226: }