|
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 |
|
26 * @bug 5108776 |
|
27 * @summary Test the properties use case for Virtual MBeans that is documented |
|
28 * in MBeanServerSupport. |
|
29 * @author Eamonn McManus |
|
30 */ |
|
31 |
|
32 import java.lang.management.ManagementFactory; |
|
33 import java.util.Properties; |
|
34 import java.util.Set; |
|
35 import java.util.TreeSet; |
|
36 import java.util.concurrent.ArrayBlockingQueue; |
|
37 import java.util.concurrent.BlockingQueue; |
|
38 import java.util.concurrent.TimeUnit; |
|
39 import javax.management.DynamicMBean; |
|
40 import javax.management.InstanceNotFoundException; |
|
41 import javax.management.JMX; |
|
42 import javax.management.MBeanServer; |
|
43 import javax.management.MalformedObjectNameException; |
|
44 import javax.management.Notification; |
|
45 import javax.management.NotificationEmitter; |
|
46 import javax.management.NotificationListener; |
|
47 import javax.management.ObjectName; |
|
48 import javax.management.StandardMBean; |
|
49 import javax.management.namespace.JMXNamespace; |
|
50 import javax.management.namespace.JMXNamespaces; |
|
51 import javax.management.namespace.VirtualEventManager; |
|
52 import javax.management.namespace.MBeanServerSupport; |
|
53 |
|
54 public class VirtualPropsTest { |
|
55 public static interface PropertyMBean { |
|
56 public String getValue(); |
|
57 } |
|
58 |
|
59 public static class PropsMBS extends MBeanServerSupport { |
|
60 private static ObjectName newObjectName(String name) { |
|
61 try { |
|
62 return new ObjectName(name); |
|
63 } catch (MalformedObjectNameException e) { |
|
64 throw new AssertionError(e); |
|
65 } |
|
66 } |
|
67 |
|
68 public static class PropertyImpl implements PropertyMBean { |
|
69 private final String name; |
|
70 |
|
71 public PropertyImpl(String name) { |
|
72 this.name = name; |
|
73 } |
|
74 |
|
75 public String getValue() { |
|
76 return System.getProperty(name); |
|
77 } |
|
78 } |
|
79 |
|
80 @Override |
|
81 public DynamicMBean getDynamicMBeanFor(ObjectName name) |
|
82 throws InstanceNotFoundException { |
|
83 ObjectName namePattern = newObjectName( |
|
84 "com.example:type=Property,name=\"*\""); |
|
85 if (!namePattern.apply(name)) |
|
86 throw new InstanceNotFoundException(name); |
|
87 |
|
88 String propName = ObjectName.unquote(name.getKeyProperty("name")); |
|
89 if (System.getProperty(propName) == null) |
|
90 throw new InstanceNotFoundException(name); |
|
91 PropertyMBean propMBean = new PropertyImpl(propName); |
|
92 return new StandardMBean(propMBean, PropertyMBean.class, false); |
|
93 } |
|
94 |
|
95 @Override |
|
96 protected Set<ObjectName> getNames() { |
|
97 Set<ObjectName> names = new TreeSet<ObjectName>(); |
|
98 Properties props = System.getProperties(); |
|
99 for (String propName : props.stringPropertyNames()) { |
|
100 ObjectName objectName = newObjectName( |
|
101 "com.example:type=Property,name=" + |
|
102 ObjectName.quote(propName)); |
|
103 names.add(objectName); |
|
104 } |
|
105 return names; |
|
106 } |
|
107 |
|
108 private final VirtualEventManager vem = new VirtualEventManager(); |
|
109 |
|
110 @Override |
|
111 public NotificationEmitter getNotificationEmitterFor( |
|
112 ObjectName name) throws InstanceNotFoundException { |
|
113 getDynamicMBeanFor(name); // check that the name is valid |
|
114 return vem.getNotificationEmitterFor(name); |
|
115 } |
|
116 |
|
117 public void propertyChanged(String name, String newValue) { |
|
118 ObjectName objectName = newObjectName( |
|
119 "com.example:type=Property,name=" + ObjectName.quote(name)); |
|
120 Notification n = new Notification( |
|
121 "com.example.property.changed", objectName, 0L, |
|
122 "Property " + name + " changed"); |
|
123 n.setUserData(newValue); |
|
124 vem.publish(objectName, n); |
|
125 } |
|
126 } |
|
127 |
|
128 static class QueueListener implements NotificationListener { |
|
129 BlockingQueue<Notification> q = new ArrayBlockingQueue<Notification>(10); |
|
130 public void handleNotification(Notification notification, |
|
131 Object handback) { |
|
132 q.add(notification); |
|
133 } |
|
134 } |
|
135 |
|
136 public static void main(String[] args) throws Exception { |
|
137 MBeanServer mmbs = ManagementFactory.getPlatformMBeanServer(); |
|
138 String namespace = "props"; |
|
139 PropsMBS pmbs = new PropsMBS(); |
|
140 Object namespaceMBean = new JMXNamespace(pmbs); |
|
141 mmbs.registerMBean(namespaceMBean, new ObjectName( |
|
142 namespace + "//:type=JMXNamespace")); |
|
143 MBeanServer mbs = JMXNamespaces.narrowToNamespace(mmbs, namespace); |
|
144 |
|
145 Properties props = System.getProperties(); |
|
146 |
|
147 int nprops = props.stringPropertyNames().size(); |
|
148 if (nprops != mbs.getMBeanCount()) { |
|
149 throw new Exception(String.format("Properties: %d; MBeans: %d", |
|
150 nprops, mbs.getMBeanCount())); |
|
151 } |
|
152 |
|
153 for (String propName : props.stringPropertyNames()) { |
|
154 ObjectName propObjectName = new ObjectName( |
|
155 "com.example:type=Property,name=" + ObjectName.quote(propName)); |
|
156 PropertyMBean propProx = JMX.newMBeanProxy( |
|
157 mbs, propObjectName, PropertyMBean.class); |
|
158 String propValue = propProx.getValue(); |
|
159 String realPropValue = props.getProperty(propName); |
|
160 if (!realPropValue.equals(propValue)) { |
|
161 throw new Exception(String.format("Property %s: value is \"%s\"; " + |
|
162 "mbean says \"%s\"", propName, realPropValue, propValue)); |
|
163 } |
|
164 } |
|
165 |
|
166 ObjectName fooPropObjectName = |
|
167 new ObjectName("com.example:type=Property,name=\"java.home\""); |
|
168 QueueListener ql = new QueueListener(); |
|
169 mbs.addNotificationListener(fooPropObjectName, ql, null, null); |
|
170 pmbs.propertyChanged("java.home", "bar"); |
|
171 Notification n = ql.q.poll(1, TimeUnit.SECONDS); |
|
172 if (n == null) |
|
173 throw new Exception("Notif didn't arrive"); |
|
174 if (!"bar".equals(n.getUserData())) |
|
175 throw new Exception("Bad user data: " + n.getUserData()); |
|
176 |
|
177 System.out.println("TEST PASSED"); |
|
178 } |
|
179 } |