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 * |
|
25 * @test DomainCreationTest.java |
|
26 * @bug 5072476 |
|
27 * @summary Test the creation and registration of JMXDomain instances. |
|
28 * @author Daniel Fuchs |
|
29 * @run clean DomainCreationTest Wombat WombatMBean |
|
30 * @run build DomainCreationTest Wombat WombatMBean |
|
31 * @run main DomainCreationTest |
|
32 */ |
|
33 |
|
34 |
|
35 import java.util.Collections; |
|
36 import java.util.Map; |
|
37 import java.util.Set; |
|
38 import javax.management.Attribute; |
|
39 import javax.management.AttributeList; |
|
40 import javax.management.AttributeNotFoundException; |
|
41 import javax.management.DynamicMBean; |
|
42 import javax.management.InstanceNotFoundException; |
|
43 import javax.management.InvalidAttributeValueException; |
|
44 import javax.management.MBeanException; |
|
45 import javax.management.MBeanInfo; |
|
46 import javax.management.MBeanRegistration; |
|
47 import javax.management.MBeanServer; |
|
48 import javax.management.MBeanServerFactory; |
|
49 import javax.management.NotificationEmitter; |
|
50 import javax.management.ObjectInstance; |
|
51 import javax.management.ObjectName; |
|
52 import javax.management.ReflectionException; |
|
53 import javax.management.RuntimeMBeanException; |
|
54 import javax.management.RuntimeOperationsException; |
|
55 import javax.management.namespace.JMXDomain; |
|
56 import javax.management.namespace.MBeanServerSupport; |
|
57 |
|
58 /** |
|
59 * Test simple creation/registration of namespace. |
|
60 * |
|
61 */ |
|
62 public class DomainCreationTest { |
|
63 private static Map<String,Object> emptyEnvMap() { |
|
64 return Collections.emptyMap(); |
|
65 } |
|
66 |
|
67 |
|
68 public static class LocalDomainRepository |
|
69 extends MBeanServerSupport { |
|
70 private final MBeanServer server; |
|
71 private final String domain; |
|
72 |
|
73 |
|
74 public class DynamicMBeanProxy implements DynamicMBean { |
|
75 |
|
76 private final MBeanServer server; |
|
77 private final ObjectName name; |
|
78 |
|
79 public DynamicMBeanProxy(MBeanServer s, ObjectName n) { |
|
80 this.server = s; |
|
81 this.name = n; |
|
82 } |
|
83 |
|
84 public Object getAttribute(String attribute) |
|
85 throws AttributeNotFoundException, |
|
86 MBeanException, ReflectionException { |
|
87 try { |
|
88 return server.getAttribute(name, attribute); |
|
89 } catch (RuntimeException x) { |
|
90 throw x; |
|
91 } catch (Exception x) { |
|
92 throw new RuntimeException(x); |
|
93 } |
|
94 } |
|
95 |
|
96 public void setAttribute(Attribute attribute) |
|
97 throws AttributeNotFoundException, |
|
98 InvalidAttributeValueException, MBeanException, |
|
99 ReflectionException { |
|
100 try { |
|
101 server.setAttribute(name, attribute); |
|
102 } catch (RuntimeException x) { |
|
103 throw x; |
|
104 } catch (Exception x) { |
|
105 throw new RuntimeException(x); |
|
106 } |
|
107 } |
|
108 |
|
109 public AttributeList getAttributes(String[] attributes) { |
|
110 try { |
|
111 return server.getAttributes(name, attributes); |
|
112 } catch (RuntimeException x) { |
|
113 throw x; |
|
114 } catch (Exception x) { |
|
115 throw new RuntimeException(x); |
|
116 } |
|
117 } |
|
118 |
|
119 public AttributeList setAttributes(AttributeList attributes) { |
|
120 try { |
|
121 return server.setAttributes(name, attributes); |
|
122 } catch (RuntimeException x) { |
|
123 throw x; |
|
124 } catch (Exception x) { |
|
125 throw new RuntimeException(x); |
|
126 } |
|
127 } |
|
128 |
|
129 public Object invoke(String actionName, Object[] params, |
|
130 String[] signature) throws MBeanException, |
|
131 ReflectionException { |
|
132 try { |
|
133 return server.invoke(name, actionName, params, signature); |
|
134 } catch (RuntimeException x) { |
|
135 throw x; |
|
136 } catch (Exception x) { |
|
137 throw new RuntimeException(x); |
|
138 } |
|
139 } |
|
140 |
|
141 public MBeanInfo getMBeanInfo() { |
|
142 try { |
|
143 return server.getMBeanInfo(name); |
|
144 } catch (RuntimeException x) { |
|
145 throw x; |
|
146 } catch (Exception x) { |
|
147 throw new RuntimeException(x); |
|
148 } |
|
149 } |
|
150 } |
|
151 |
|
152 public LocalDomainRepository(String domain) { |
|
153 this.server = MBeanServerFactory.newMBeanServer(); |
|
154 this.domain = domain; |
|
155 } |
|
156 |
|
157 @Override |
|
158 protected Set<ObjectName> getNames() { |
|
159 try { |
|
160 final ObjectName name = |
|
161 ObjectName.getInstance(domain+":*"); |
|
162 return server.queryNames(name, null); |
|
163 } catch (RuntimeException x) { |
|
164 throw x; |
|
165 } catch (Exception x) { |
|
166 throw new RuntimeException(x); |
|
167 } |
|
168 } |
|
169 |
|
170 @Override |
|
171 public DynamicMBean getDynamicMBeanFor(ObjectName name) |
|
172 throws InstanceNotFoundException { |
|
173 return new DynamicMBeanProxy(server, name); |
|
174 } |
|
175 |
|
176 @Override |
|
177 public NotificationEmitter |
|
178 getNotificationEmitterFor(ObjectName name) |
|
179 throws InstanceNotFoundException { |
|
180 DynamicMBean mbean = getDynamicMBeanFor(name); |
|
181 if (mbean instanceof NotificationEmitter) |
|
182 return (NotificationEmitter) mbean; |
|
183 return null; |
|
184 } |
|
185 |
|
186 } |
|
187 |
|
188 private static MBeanServer newMBeanServer() { |
|
189 return MBeanServerFactory.newMBeanServer(); |
|
190 } |
|
191 |
|
192 public static interface ThingMBean {} |
|
193 public static class Thing implements ThingMBean, MBeanRegistration { |
|
194 public ObjectName preRegister(MBeanServer server, ObjectName name) |
|
195 throws Exception { |
|
196 if (name == null) return new ObjectName(":type=Thing"); |
|
197 else return name; |
|
198 } |
|
199 public void postRegister(Boolean registrationDone) { |
|
200 } |
|
201 |
|
202 public void preDeregister() throws Exception { |
|
203 } |
|
204 public void postDeregister() { |
|
205 } |
|
206 } |
|
207 |
|
208 /** |
|
209 * Test that it is possible to create a dummy MBean with a null |
|
210 * ObjectName - this is just a sanity check - as there are already |
|
211 * other JMX tests that check that. |
|
212 * |
|
213 * @throws java.lang.Exception |
|
214 */ |
|
215 public static void testCreateWithNull() throws Exception { |
|
216 final MBeanServer server = newMBeanServer(); |
|
217 final ObjectInstance oi = server.registerMBean(new Thing(),null); |
|
218 server.unregisterMBean(oi.getObjectName()); |
|
219 System.out.println("testCreateWithNull PASSED"); |
|
220 } |
|
221 |
|
222 /** |
|
223 * Check that we can register a JMXNamespace MBean, using its standard |
|
224 * ObjectName. |
|
225 * @throws java.lang.Exception |
|
226 */ |
|
227 public static void testGoodObjectName() throws Exception { |
|
228 MBeanServer server = newMBeanServer(); |
|
229 final ObjectName name = |
|
230 JMXDomain.getDomainObjectName("gloups"); |
|
231 final ObjectInstance oi = |
|
232 server.registerMBean(new JMXDomain( |
|
233 new LocalDomainRepository("gloups")),name); |
|
234 System.out.println("Succesfully registered namespace: "+name); |
|
235 try { |
|
236 if (! name.equals(oi.getObjectName())) |
|
237 throw new RuntimeException("testGoodObjectName: TEST failed: " + |
|
238 "namespace registered as: "+ |
|
239 oi.getObjectName()+" expected: "+name); |
|
240 } finally { |
|
241 server.unregisterMBean(oi.getObjectName()); |
|
242 } |
|
243 System.out.println("Succesfully unregistered namespace: "+name); |
|
244 System.out.println("testGoodObjectName PASSED"); |
|
245 } |
|
246 |
|
247 /** |
|
248 * Check that we cannot register a JMXNamespace MBean, if we don't use |
|
249 * its standard ObjectName. |
|
250 * @throws java.lang.Exception |
|
251 */ |
|
252 public static void testBadObjectName() throws Exception { |
|
253 MBeanServer server = newMBeanServer(); |
|
254 Throwable exp = null; |
|
255 final ObjectName name = new ObjectName("d:k=v"); |
|
256 try { |
|
257 server.registerMBean(new JMXDomain( |
|
258 new LocalDomainRepository("d")),name); |
|
259 System.out.println("testBadObjectName: " + |
|
260 "Error: MBean registered, no exception thrown."); |
|
261 } catch(RuntimeMBeanException x) { |
|
262 exp = x.getCause(); |
|
263 } catch(Exception x) { |
|
264 throw new RuntimeException("testBadObjectName: TEST failed: " + |
|
265 "expected RuntimeMBeanException - got "+ |
|
266 x); |
|
267 } |
|
268 if (exp == null) server.unregisterMBean(name); |
|
269 if (exp == null) |
|
270 throw new RuntimeException("testBadObjectName: TEST failed: " + |
|
271 "expected IllegalArgumentException - got none"); |
|
272 if (!(exp instanceof IllegalArgumentException)) |
|
273 throw new RuntimeException("testBadObjectName: TEST failed: " + |
|
274 "expected IllegalArgumentException - got "+ |
|
275 exp.toString(),exp); |
|
276 System.out.println("Got expected exception: "+exp); |
|
277 System.out.println("testBadObjectName PASSED"); |
|
278 } |
|
279 |
|
280 /** |
|
281 * Check that we cannot register a Domain MBean in a domain that already |
|
282 * exists. |
|
283 * |
|
284 * @throws java.lang.Exception |
|
285 */ |
|
286 public static void testBadDomain() throws Exception { |
|
287 MBeanServer server = newMBeanServer(); |
|
288 Throwable exp = null; |
|
289 final ObjectName name = new ObjectName("glips:k=v"); |
|
290 server.registerMBean(new Wombat(),name); |
|
291 |
|
292 final ObjectName dname = |
|
293 JMXDomain.getDomainObjectName("glips"); |
|
294 |
|
295 try { |
|
296 server.registerMBean(new JMXDomain( |
|
297 new LocalDomainRepository("glips")),dname); |
|
298 System.out.println("testBadDomain: " + |
|
299 "Error: MBean registered, no exception thrown."); |
|
300 } catch(RuntimeOperationsException x) { |
|
301 exp = x.getCause(); |
|
302 } catch(Exception x) { |
|
303 throw new RuntimeException("testBadDomain: TEST failed: " + |
|
304 "expected RuntimeOperationsException - got "+ |
|
305 x); |
|
306 } finally { |
|
307 server.unregisterMBean(name); |
|
308 } |
|
309 if (exp == null) { |
|
310 server.unregisterMBean(dname); |
|
311 } |
|
312 if (exp == null) |
|
313 throw new RuntimeException("testBadDomain: TEST failed: " + |
|
314 "expected IllegalArgumentException - got none"); |
|
315 if (!(exp instanceof IllegalArgumentException)) |
|
316 throw new RuntimeException("testBadDomain: TEST failed: " + |
|
317 "expected IllegalArgumentException - got "+ |
|
318 exp.toString(),exp); |
|
319 System.out.println("Got expected exception: "+exp); |
|
320 System.out.println("testBadDomain PASSED"); |
|
321 } |
|
322 |
|
323 |
|
324 public static void main(String... args) throws Exception { |
|
325 testCreateWithNull(); |
|
326 testGoodObjectName(); |
|
327 testBadObjectName(); |
|
328 testBadDomain(); |
|
329 } |
|
330 } |
|