|
1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.util.Random; |
|
25 import java.util.Set; |
|
26 import javax.management.AttributeChangeNotification; |
|
27 import javax.management.ListenerNotFoundException; |
|
28 import javax.management.MBeanAttributeInfo; |
|
29 import javax.management.MBeanInfo; |
|
30 import javax.management.MBeanNotificationInfo; |
|
31 import javax.management.MBeanOperationInfo; |
|
32 import javax.management.MBeanParameterInfo; |
|
33 import javax.management.MBeanRegistration; |
|
34 import javax.management.MBeanServer; |
|
35 import javax.management.NotCompliantMBeanException; |
|
36 import javax.management.NotificationBroadcasterSupport; |
|
37 import javax.management.NotificationEmitter; |
|
38 import javax.management.NotificationFilter; |
|
39 import javax.management.NotificationListener; |
|
40 import javax.management.ObjectName; |
|
41 import javax.management.StandardMBean; |
|
42 |
|
43 |
|
44 /** |
|
45 * Dynamic MBean based on StandardMBean |
|
46 * Class Wombat |
|
47 * Wombat Description |
|
48 * @author dfuchs |
|
49 */ |
|
50 public class Wombat extends StandardMBean |
|
51 implements WombatMBean, NotificationEmitter, MBeanRegistration { |
|
52 |
|
53 /** |
|
54 * Attribute : Caption |
|
55 */ |
|
56 private String caption = "I'm a wombat"; |
|
57 |
|
58 private final long MAX_SEED = 36000; |
|
59 private final long seed; |
|
60 private final long period; |
|
61 private volatile int mood = 0; |
|
62 |
|
63 public int getMood() { |
|
64 final long degree = seed + (System.currentTimeMillis()/period)%MAX_SEED; |
|
65 final double angle = ((double)degree)/100; |
|
66 mood = (int)(100.0*Math.sin(angle)); |
|
67 return mood; |
|
68 } |
|
69 |
|
70 public Wombat() throws NotCompliantMBeanException { |
|
71 super(WombatMBean.class); |
|
72 final Random r = new Random(); |
|
73 seed = ((r.nextLong() % MAX_SEED) + MAX_SEED)%MAX_SEED; |
|
74 period = 200 + (((r.nextLong()%80)+80)%80)*10; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Next are the methods to compute MBeanInfo. |
|
79 * You shouldn't update these methods. |
|
80 */ |
|
81 @Override |
|
82 protected String getDescription(MBeanInfo info) { |
|
83 return "Wombats are strange beasts. You will find them down under " + |
|
84 "and in some computer programms."; |
|
85 } |
|
86 |
|
87 @Override |
|
88 protected String getDescription(MBeanAttributeInfo info) { |
|
89 String description = null; |
|
90 if (info.getName().equals("Caption")) { |
|
91 description = "A simple caption to describe a wombat"; |
|
92 } |
|
93 if (info.getName().equals("Mood")) { |
|
94 description = "This Wombat's mood on a [-100,+100] scale."+ |
|
95 " -100 means that this wombat is very angry."; |
|
96 } |
|
97 return description; |
|
98 } |
|
99 |
|
100 @Override |
|
101 protected String getDescription(MBeanOperationInfo op, |
|
102 MBeanParameterInfo param, |
|
103 int sequence) { |
|
104 return null; |
|
105 } |
|
106 |
|
107 @Override |
|
108 protected String getParameterName(MBeanOperationInfo op, |
|
109 MBeanParameterInfo param, |
|
110 int sequence) { |
|
111 return null; |
|
112 } |
|
113 |
|
114 @Override |
|
115 protected String getDescription(MBeanOperationInfo info) { |
|
116 String description = null; |
|
117 return description; |
|
118 } |
|
119 |
|
120 @Override |
|
121 public MBeanInfo getMBeanInfo() { |
|
122 MBeanInfo mbinfo = super.getMBeanInfo(); |
|
123 return new MBeanInfo(mbinfo.getClassName(), |
|
124 mbinfo.getDescription(), |
|
125 mbinfo.getAttributes(), |
|
126 mbinfo.getConstructors(), |
|
127 mbinfo.getOperations(), |
|
128 getNotificationInfo()); |
|
129 } |
|
130 |
|
131 /** |
|
132 * Get A simple caption to describe a wombat |
|
133 */ |
|
134 public synchronized String getCaption() { |
|
135 return caption; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Set A simple caption to describe a wombat |
|
140 */ |
|
141 public void setCaption(String value) { |
|
142 final String oldValue; |
|
143 synchronized (this) { |
|
144 oldValue = caption; |
|
145 caption = value; |
|
146 } |
|
147 final AttributeChangeNotification notif = |
|
148 new AttributeChangeNotification(objectName, |
|
149 getNextSeqNumber(), |
|
150 System.currentTimeMillis(), |
|
151 "Caption changed","Caption", |
|
152 String.class.getName(),oldValue,value); |
|
153 broadcaster.sendNotification(notif); |
|
154 } |
|
155 |
|
156 /** |
|
157 * MBeanNotification support |
|
158 * You shouldn't update these methods |
|
159 */ |
|
160 public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException { |
|
161 broadcaster.addNotificationListener(listener, filter, handback); |
|
162 } |
|
163 |
|
164 public MBeanNotificationInfo[] getNotificationInfo() { |
|
165 return new MBeanNotificationInfo[] { |
|
166 new MBeanNotificationInfo(new String[] { |
|
167 AttributeChangeNotification.ATTRIBUTE_CHANGE}, |
|
168 javax.management.AttributeChangeNotification.class.getName(), |
|
169 "Sent when the caption changes") |
|
170 }; |
|
171 } |
|
172 |
|
173 public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException { |
|
174 broadcaster.removeNotificationListener(listener); |
|
175 } |
|
176 |
|
177 public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException { |
|
178 broadcaster.removeNotificationListener(listener, filter, handback); |
|
179 } |
|
180 |
|
181 private synchronized long getNextSeqNumber() { |
|
182 return seqNumber++; |
|
183 } |
|
184 |
|
185 private long seqNumber; |
|
186 |
|
187 private final NotificationBroadcasterSupport broadcaster = new NotificationBroadcasterSupport(); |
|
188 |
|
189 /** |
|
190 * Allows the MBean to perform any operations it needs before being |
|
191 * registered in the MBean server. If the name of the MBean is not |
|
192 * specified, the MBean can provide a name for its registration. If |
|
193 * any exception is raised, the MBean will not be registered in the |
|
194 * MBean server. |
|
195 * @param server The MBean server in which the MBean will be registered. |
|
196 * @param name The object name of the MBean. This name is null if the |
|
197 * name parameter to one of the createMBean or registerMBean methods in |
|
198 * the MBeanServer interface is null. In that case, this method must |
|
199 * return a non-null ObjectName for the new MBean. |
|
200 * @return The name under which the MBean is to be registered. This value |
|
201 * must not be null. If the name parameter is not null, it will usually |
|
202 * but not necessarily be the returned value. |
|
203 * @throws Exception This exception will be caught by the MBean server and |
|
204 * re-thrown as an MBeanRegistrationException. |
|
205 */ |
|
206 @Override |
|
207 public ObjectName preRegister(MBeanServer server, ObjectName name) |
|
208 throws Exception { |
|
209 objectName = name; |
|
210 mbeanServer = server; |
|
211 return super.preRegister(server, name); |
|
212 } |
|
213 |
|
214 /** |
|
215 * Allows the MBean to perform any operations needed after having |
|
216 * been registered in the MBean server or after the registration has |
|
217 * failed. |
|
218 * @param registrationDone Indicates wether or not the MBean has been |
|
219 * successfully registered in the MBean server. The value false means |
|
220 * that the registration has failed. |
|
221 */ |
|
222 @Override |
|
223 public void postRegister(Boolean registrationDone) { |
|
224 super.postRegister(registrationDone); |
|
225 } |
|
226 |
|
227 /** |
|
228 * Allows the MBean to perform any operations it needs before being |
|
229 * unregistered by the MBean server. |
|
230 * @throws Exception This exception will be caught by the MBean server and |
|
231 * re-thrown as an MBeanRegistrationException. |
|
232 */ |
|
233 @Override |
|
234 public void preDeregister() throws Exception { |
|
235 super.preDeregister(); |
|
236 } |
|
237 |
|
238 /** |
|
239 * Allows the MBean to perform any operations needed after having been |
|
240 * unregistered in the MBean server. |
|
241 */ |
|
242 @Override |
|
243 public void postDeregister() { |
|
244 super.postDeregister(); |
|
245 } |
|
246 |
|
247 public Set<ObjectName> listMatching(ObjectName pattern) { |
|
248 return mbeanServer.queryNames(pattern, null); |
|
249 } |
|
250 |
|
251 private MBeanServer mbeanServer; |
|
252 |
|
253 private ObjectName objectName; |
|
254 } |