1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57:
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64:
65:
74: public final class MemoryMXBeanImpl
75: extends BeanImpl
76: implements MemoryMXBean, NotificationEmitter
77: {
78:
79: private List listeners;
80:
81: private long notificationCount;
82:
83: public static CompositeType notifType;
84:
85: public static CompositeType usageType;
86:
87: static
88: {
89: try
90: {
91: CompositeType usageType =
92: new CompositeType(MemoryUsage.class.getName(),
93: "Describes the usage levels of a pool",
94: new String[] { "init", "used",
95: "committed", "max"
96: },
97: new String[] { "Initial level",
98: "Used level",
99: "Committed level",
100: "Maximum level"
101: },
102: new OpenType[] {
103: SimpleType.LONG, SimpleType.LONG,
104: SimpleType.LONG, SimpleType.LONG
105: });
106: CompositeType notifType =
107: new CompositeType(MemoryNotificationInfo.class.getName(),
108: "Provides the notification info on memory usage",
109: new String[] { "poolName", "usage", "count" },
110: new String[] { "Name of the memory pool",
111: "Usage level of the memory pool",
112: "Number of times the threshold " +
113: "has been crossed"
114: },
115: new OpenType[] {
116: SimpleType.STRING, usageType, SimpleType.LONG
117: });
118: }
119: catch (OpenDataException e)
120: {
121: throw new IllegalStateException("Something went wrong in creating " +
122: "the composite data types.", e);
123: }
124: }
125:
126:
134: public MemoryMXBeanImpl()
135: throws NotCompliantMBeanException
136: {
137: super(MemoryMXBean.class);
138: listeners = new ArrayList();
139: notificationCount = 0;
140: }
141:
142: public void gc()
143: {
144: System.gc();
145: }
146:
147: public MemoryUsage getHeapMemoryUsage()
148: {
149: return VMMemoryMXBeanImpl.getHeapMemoryUsage();
150: }
151:
152: public MemoryUsage getNonHeapMemoryUsage()
153: {
154: return VMMemoryMXBeanImpl.getNonHeapMemoryUsage();
155: }
156:
157: public int getObjectPendingFinalizationCount()
158: {
159: return VMMemoryMXBeanImpl.getObjectPendingFinalizationCount();
160: }
161:
162: public boolean isVerbose()
163: {
164: return VMMemoryMXBeanImpl.isVerbose();
165: }
166:
167: public void setVerbose(boolean verbose)
168: {
169: checkControlPermissions();
170: VMMemoryMXBeanImpl.setVerbose(verbose);
171: }
172:
173: public void addNotificationListener(NotificationListener listener,
174: NotificationFilter filter,
175: Object passback)
176: {
177: if (listener == null)
178: throw new IllegalArgumentException("Null listener added to bean.");
179: listeners.add(new ListenerData(listener, filter, passback));
180: }
181:
182: public MBeanNotificationInfo[] getNotificationInfo()
183: {
184: return new MBeanNotificationInfo[]
185: {
186: new MBeanNotificationInfo(new String[]
187: {
188: MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
189: MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED
190: },
191: Notification.class.getName(),
192: "Memory Usage Notifications")
193: };
194: }
195:
196: public void removeNotificationListener(NotificationListener listener)
197: throws ListenerNotFoundException
198: {
199: Iterator it = listeners.iterator();
200: boolean foundOne = false;
201: while (it.hasNext())
202: {
203: ListenerData data = (ListenerData) it.next();
204: if (data.getListener() == listener)
205: {
206: it.remove();
207: foundOne = true;
208: }
209: }
210: if (!foundOne)
211: throw new ListenerNotFoundException("The specified listener, " + listener +
212: "is not registered with this bean.");
213: }
214:
215: public void removeNotificationListener(NotificationListener listener,
216: NotificationFilter filter,
217: Object passback)
218: throws ListenerNotFoundException
219: {
220: if (!(listeners.remove(new ListenerData(listener, filter, passback))))
221: {
222: throw new ListenerNotFoundException("The specified listener, " + listener +
223: " with filter " + filter +
224: "and passback " + passback +
225: ", is not registered with this bean.");
226: }
227: }
228:
229: void fireNotification(String type, String poolName, long init, long used,
230: long committed, long max, long count)
231: {
232: Notification notif = new Notification(type, this, notificationCount);
233: MemoryUsage usage = new MemoryUsage(init, used, committed, max);
234: CompositeData data;
235: try
236: {
237: data = new CompositeDataSupport(notifType,
238: new String[] {
239: "poolName", "usage", "count"
240: },
241: new Object[] {
242: poolName, usage, Long.valueOf(count)
243: });
244: }
245: catch (OpenDataException e)
246: {
247: throw new IllegalStateException("Something went wrong in creating " +
248: "the composite data instance.", e);
249: }
250: notif.setUserData(data);
251: Iterator it = listeners.iterator();
252: while (it.hasNext())
253: {
254: ListenerData ldata = (ListenerData) it.next();
255: NotificationFilter filter = ldata.getFilter();
256: if (filter == null || filter.isNotificationEnabled(notif))
257: ldata.getListener().handleNotification(notif, ldata.getPassback());
258: }
259: ++notificationCount;
260: }
261:
262: void fireThresholdExceededNotification(String poolName, long init,
263: long used, long committed,
264: long max, long count)
265: {
266: fireNotification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED,
267: poolName, init, used, committed, max, count);
268: }
269:
270: void fireCollectionThresholdExceededNotification(String poolName,
271: long init,
272: long used,
273: long committed,
274: long max,
275: long count)
276: {
277: fireNotification(MemoryNotificationInfo.MEMORY_COLLECTION_THRESHOLD_EXCEEDED,
278: poolName, init, used, committed, max, count);
279: }
280:
281: }