Source for java.lang.management.MemoryNotificationInfo

   1: /* MemoryNotificationInfo.java - Emitted memory notification info.
   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 java.lang.management;
  39: 
  40: import gnu.java.lang.management.MemoryMXBeanImpl;
  41: 
  42: import javax.management.openmbean.CompositeData;
  43: import javax.management.openmbean.CompositeType;
  44: import javax.management.openmbean.SimpleType;
  45: 
  46: /**
  47:  * <p>
  48:  * Represents the content of a notification emitted by the
  49:  * {@link MemoryMXBean}.  Such notifications are emitted when
  50:  * one of the memory pools exceeds its usage or collection
  51:  * usage threshold.  This object contains the following information,
  52:  * representing the state of the pool at the time of the
  53:  * notification:
  54:  * </p>
  55:  * <ul>
  56:  * <li>The name of the pool.</li>
  57:  * <li>The memory usage of the pool at the time of notification.</li>
  58:  * <li>The number of times the pool has exceeded this particular
  59:  * threshold in the past.</li>
  60:  * </ul>
  61:  * <p>
  62:  * Two types of notification are emitted by the {@link MemoryMXBean}:
  63:  * one for exceeding the usage threshold and one for exceeding the
  64:  * collection usage threshold.  The value returned by {@link #getCount()}
  65:  * is dependent on this type; if the threshold exceeded is the usage
  66:  * threshold, then the usage threshold count is returned.  If, instead,
  67:  * the collection usage threshold is exceeded, then the collection usage
  68:  * threshold count is returned.
  69:  * </p>
  70:  * <p>
  71:  * This data is held in the user data part of the notification (returned
  72:  * by {@link javax.management.Notification#getUserData()}) encapsulated in
  73:  * a {@link javax.management.openmbean.CompositeData} object.  The
  74:  * {@link #from(javax.management.openmbean.CompositeData)} method may be
  75:  * used to unwrap the value and obtain an instance of this class.
  76:  * </p>
  77:  * 
  78:  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  79:  * @since 1.5
  80:  */
  81: public class MemoryNotificationInfo
  82: {
  83: 
  84:   /**
  85:    * The type of notification emitted when the usage threshold is exceeded.
  86:    * After a notification is emitted, the usage level must drop below the
  87:    * threshold again before another notification is emitted.  The value is
  88:    * <code>java.management.memory.threshold.exceeded</code>.
  89:    */
  90:   public static final String MEMORY_THRESHOLD_EXCEEDED = 
  91:     "java.management.memory.threshold.exceeded";
  92: 
  93:   /**
  94:    * The type of notification emitted when the collection usage threshold
  95:    * is exceeded, following a garbage collection cycle.  The value is
  96:    * <code>java.management.memory.collection.threshold.exceeded</code>.
  97:    */
  98:   public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED =
  99:     "java.management.memory.collection.threshold.exceeded";
 100: 
 101:   /**
 102:    * The name of the memory pool which exceeded the threshold.
 103:    */
 104:   private String poolName;
 105: 
 106:   /**
 107:    * The usage level of the memory pool at the time of notification.
 108:    */
 109:   private MemoryUsage usage;
 110: 
 111:   /**
 112:    * The number of times the threshold has been crossed.
 113:    */
 114:   private long count;
 115: 
 116:   /**
 117:    * Constructs a new {@link MemoryNotificationInfo} object using the
 118:    * specified pool name, usage level and threshold crossing count.
 119:    *
 120:    * @param poolName the name of the pool which has exceeded a threshold.
 121:    * @param usage the usage level of the pool at the time of notification.
 122:    * @param count the number of times the threshold has been crossed.
 123:    */
 124:   public MemoryNotificationInfo(String poolName, MemoryUsage usage, long count)
 125:   {
 126:     this.poolName = poolName;
 127:     this.usage = usage;
 128:     this.count = count;
 129:   }
 130: 
 131:   /**
 132:    * <p>
 133:    * Returns a {@link MemoryNotificationInfo} instance using the values
 134:    * given in the supplied
 135:    * {@link javax.management.openmbean.CompositeData} object.
 136:    * The composite data instance should contain the following
 137:    * attributes with the specified types:
 138:    * </p>
 139:    * <table>
 140:    * <th><td>Name</td><td>Type</td></th>
 141:    * <tr><td>poolName</td><td>java.lang.String</td></tr>
 142:    * <tr><td>usage</td><td>javax.management.openmbean.CompositeData
 143:    * </td></tr>
 144:    * <tr><td>count</td><td>java.lang.Long</td></tr>
 145:    * </table>
 146:    * <p>
 147:    * The usage level is further described as:
 148:    * </p>
 149:    * <table>
 150:    * <th><td>Name</td><td>Type</td></th>
 151:    * <tr><td>init</td><td>java.lang.Long</td></tr>
 152:    * <tr><td>used</td><td>java.lang.Long</td></tr>
 153:    * <tr><td>committed</td><td>java.lang.Long</td></tr>
 154:    * <tr><td>max</td><td>java.lang.Long</td></tr>
 155:    * </table>
 156:    * 
 157:    * @param data the composite data structure to take values from.
 158:    * @return a new instance containing the values from the 
 159:    *         composite data structure, or <code>null</code>
 160:    *         if the data structure was also <code>null</code>.
 161:    * @throws IllegalArgumentException if the composite data structure
 162:    *                                  does not match the structure
 163:    *                                  outlined above.
 164:    */
 165:   public static MemoryNotificationInfo from(CompositeData data)
 166:   {
 167:     if (data == null)
 168:       return null;
 169:     CompositeType type = data.getCompositeType();
 170:     ThreadInfo.checkAttribute(type, "poolName", SimpleType.STRING);
 171:     ThreadInfo.checkAttribute(type, "usage", MemoryMXBeanImpl.usageType);
 172:     ThreadInfo.checkAttribute(type, "count", SimpleType.LONG);
 173:     MemoryUsage usage = MemoryUsage.from((CompositeData) data.get("usage"));
 174:     return new MemoryNotificationInfo(((String) data.get("poolName")),
 175:                       usage,
 176:                       ((Long) data.get("count")).longValue());
 177:   }
 178: 
 179:   /**
 180:    * Returns the number of times the memory pool has crossed the usage
 181:    * threshold, as of the time of notification.  If this is the notification
 182:    * represented by the type {@link #MEMORY_THRESHOLD_EXCEEDED}, then the
 183:    * count is the usage threshold count.  If this is the notification
 184:    * represented by the type {@link #MEMORY_COLLECTION_THRESHOLD_EXCEEDED},
 185:    * then the count is the collection usage threshold count.
 186:    *
 187:    * @return the number of times the appropriate threshold has been crossed.
 188:    */
 189:   public long getCount()
 190:   {
 191:     return count;
 192:   }
 193: 
 194:   /**
 195:    * Returns the name of the pool which has crossed a threshold.
 196:    *
 197:    * @return the name of the pool.
 198:    */
 199:   public String getPoolName()
 200:   {
 201:     return poolName;
 202:   }
 203: 
 204:   /**
 205:    * Returns the usage levels at the time of notification.
 206:    *
 207:    * @return the usage levels.
 208:    */
 209:   public MemoryUsage getUsage()
 210:   {
 211:     return usage;
 212:   }
 213: 
 214: }