001    /* MemoryNotificationInfo.java - Emitted memory notification info.
002       Copyright (C) 2006 Free Software Foundation
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package java.lang.management;
039    
040    import gnu.java.lang.management.MemoryMXBeanImpl;
041    
042    import javax.management.openmbean.CompositeData;
043    import javax.management.openmbean.CompositeType;
044    import javax.management.openmbean.SimpleType;
045    
046    /**
047     * <p>
048     * Represents the content of a notification emitted by the
049     * {@link MemoryMXBean}.  Such notifications are emitted when
050     * one of the memory pools exceeds its usage or collection
051     * usage threshold.  This object contains the following information,
052     * representing the state of the pool at the time of the
053     * notification:
054     * </p>
055     * <ul>
056     * <li>The name of the pool.</li>
057     * <li>The memory usage of the pool at the time of notification.</li>
058     * <li>The number of times the pool has exceeded this particular
059     * threshold in the past.</li>
060     * </ul>
061     * <p>
062     * Two types of notification are emitted by the {@link MemoryMXBean}:
063     * one for exceeding the usage threshold and one for exceeding the
064     * collection usage threshold.  The value returned by {@link #getCount()}
065     * is dependent on this type; if the threshold exceeded is the usage
066     * threshold, then the usage threshold count is returned.  If, instead,
067     * the collection usage threshold is exceeded, then the collection usage
068     * threshold count is returned.
069     * </p>
070     * <p>
071     * This data is held in the user data part of the notification (returned
072     * by {@link javax.management.Notification#getUserData()}) encapsulated in
073     * a {@link javax.management.openmbean.CompositeData} object.  The
074     * {@link #from(javax.management.openmbean.CompositeData)} method may be
075     * used to unwrap the value and obtain an instance of this class.
076     * </p>
077     * 
078     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
079     * @since 1.5
080     */
081    public class MemoryNotificationInfo
082    {
083    
084      /**
085       * The type of notification emitted when the usage threshold is exceeded.
086       * After a notification is emitted, the usage level must drop below the
087       * threshold again before another notification is emitted.  The value is
088       * <code>java.management.memory.threshold.exceeded</code>.
089       */
090      public static final String MEMORY_THRESHOLD_EXCEEDED = 
091        "java.management.memory.threshold.exceeded";
092    
093      /**
094       * The type of notification emitted when the collection usage threshold
095       * is exceeded, following a garbage collection cycle.  The value is
096       * <code>java.management.memory.collection.threshold.exceeded</code>.
097       */
098      public static final String MEMORY_COLLECTION_THRESHOLD_EXCEEDED =
099        "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    }
215