author | emcmanus |
Fri, 07 Nov 2008 11:48:07 +0100 | |
changeset 1570 | 4165709c91e3 |
parent 1004 | 5ba8217eb504 |
permissions | -rw-r--r-- |
1004 | 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.lang.management.ManagementFactory; |
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.HashMap; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
29 |
import javax.management.ClientContext; |
1004 | 30 |
import javax.management.MBeanServer; |
31 |
import javax.management.event.EventClientDelegate; |
|
32 |
import javax.management.remote.JMXConnectorServer; |
|
33 |
||
34 |
/* |
|
35 |
* @test |
|
36 |
* @bug 6663757 |
|
37 |
* @summary Tests standard MBeanServerForwarders introduced by connector server |
|
38 |
* options. |
|
39 |
* @author Eamonn McManus |
|
40 |
*/ |
|
41 |
import javax.management.remote.JMXConnectorServerFactory; |
|
42 |
import javax.management.remote.JMXServiceURL; |
|
43 |
import javax.management.remote.MBeanServerForwarder; |
|
44 |
||
45 |
public class StandardForwardersTest { |
|
46 |
private static String failure; |
|
47 |
||
48 |
private static class Forwarder { |
|
49 |
private final String attribute; |
|
50 |
private final boolean defaultEnabled; |
|
51 |
private final Class<?> expectedClass; |
|
52 |
||
53 |
public Forwarder(String attribute, boolean defaultEnabled, |
|
54 |
Class<?> expectedClass) { |
|
55 |
this.attribute = attribute; |
|
56 |
this.defaultEnabled = defaultEnabled; |
|
57 |
this.expectedClass = expectedClass; |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
private static enum Status {DISABLED, ENABLED, DEFAULT} |
|
62 |
||
63 |
public static void main(String[] args) throws Exception { |
|
64 |
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); |
|
65 |
||
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
66 |
MBeanServerForwarder ctxFwd = ClientContext.newContextForwarder(mbs, null); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
67 |
Forwarder ctx = new Forwarder( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
68 |
JMXConnectorServer.CONTEXT_FORWARDER, true, ctxFwd.getClass()); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
69 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
70 |
MBeanServerForwarder locFwd = |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
71 |
ClientContext.newLocalizeMBeanInfoForwarder(mbs); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
72 |
Forwarder loc = new Forwarder( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
73 |
JMXConnectorServer.LOCALIZE_MBEAN_INFO_FORWARDER, false, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
74 |
locFwd.getClass()); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
75 |
|
1004 | 76 |
MBeanServerForwarder ecdFwd = |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
77 |
EventClientDelegate.newForwarder(mbs, null); |
1004 | 78 |
Forwarder ecd = new Forwarder( |
79 |
JMXConnectorServer.EVENT_CLIENT_DELEGATE_FORWARDER, true, |
|
80 |
ecdFwd.getClass()); |
|
81 |
||
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
82 |
Forwarder[] forwarders = {ctx, loc, ecd}; |
1004 | 83 |
|
84 |
// Now go through every combination of forwarders. Each forwarder |
|
85 |
// may be explicitly enabled, explicitly disabled, or left to its |
|
86 |
// default value. |
|
87 |
int nStatus = Status.values().length; |
|
88 |
int limit = (int) Math.pow(nStatus, forwarders.length); |
|
89 |
for (int i = 0; i < limit; i++) { |
|
90 |
Status[] status = new Status[forwarders.length]; |
|
91 |
int ii = i; |
|
92 |
for (int j = 0; j < status.length; j++) { |
|
93 |
status[j] = Status.values()[ii % nStatus]; |
|
94 |
ii /= nStatus; |
|
95 |
} |
|
96 |
Map<String, String> env = new HashMap<String, String>(); |
|
97 |
String test = ""; |
|
98 |
for (int j = 0; j < status.length; j++) { |
|
99 |
if (!test.equals("")) |
|
100 |
test += "; "; |
|
101 |
test += forwarders[j].attribute; |
|
102 |
switch (status[j]) { |
|
103 |
case DISABLED: |
|
104 |
test += "=false"; |
|
105 |
env.put(forwarders[j].attribute, "false"); |
|
106 |
break; |
|
107 |
case ENABLED: |
|
108 |
test += "=true"; |
|
109 |
env.put(forwarders[j].attribute, "true"); |
|
110 |
break; |
|
111 |
case DEFAULT: |
|
112 |
test += "=default(" + forwarders[j].defaultEnabled + ")"; |
|
113 |
break; |
|
114 |
} |
|
115 |
} |
|
116 |
boolean consistent = isConsistent(env); |
|
117 |
test += "; (" + (consistent ? "" : "in") + "consistent)"; |
|
118 |
System.out.println(test); |
|
119 |
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///"); |
|
120 |
try { |
|
121 |
JMXConnectorServer cs = |
|
122 |
JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); |
|
123 |
if (!consistent) { |
|
124 |
fail("Inconsistent attributes should have been rejected " + |
|
125 |
"but were not"); |
|
126 |
} |
|
127 |
checkForwarders(cs, forwarders, status); |
|
128 |
} catch (IllegalArgumentException e) { |
|
129 |
if (consistent) { |
|
130 |
fail("Consistent attributes provoked IllegalArgumentException"); |
|
131 |
e.printStackTrace(System.out); |
|
132 |
} |
|
133 |
} |
|
134 |
} |
|
135 |
||
136 |
if (failure == null) |
|
137 |
System.out.println("TEST PASSED"); |
|
138 |
else |
|
139 |
throw new Exception(failure); |
|
140 |
} |
|
141 |
||
142 |
// Check that the classes of the forwarders in the system chain correspond |
|
143 |
// to what we expect given the options we have passed. This check is a bit |
|
144 |
// superficial in the sense that a forwarder might be for example a |
|
145 |
// SingleMBeanForwarderHandler but that doesn't prove that it is the |
|
146 |
// right Single MBean. Nevertheless the test should expose any severe |
|
147 |
// wrongness. |
|
148 |
// |
|
149 |
// The check here makes some assumptions that could become untrue in the |
|
150 |
// future. First, it assumes that the forwarders that are added have |
|
151 |
// exactly the classes that are in the Forwarder[] array. So for example |
|
152 |
// the forwarder for CONTEXT_FORWARDER must be of the same class as an |
|
153 |
// explicit call to ClientContext.newContextForwarder. The spec doesn't |
|
154 |
// require that - it only requires that the forwarder have the same |
|
155 |
// behaviour. The second assumption is that the connector server doesn't |
|
156 |
// add any forwarders of its own into the system chain, and again the spec |
|
157 |
// doesn't disallow that. |
|
158 |
private static void checkForwarders( |
|
159 |
JMXConnectorServer cs, Forwarder[] forwarders, Status[] status) { |
|
160 |
List<Class<?>> expectedClasses = new ArrayList<Class<?>>(); |
|
161 |
for (int i = 0; i < forwarders.length; i++) { |
|
162 |
if (status[i] == Status.ENABLED || |
|
163 |
(status[i] == Status.DEFAULT && forwarders[i].defaultEnabled)) |
|
164 |
expectedClasses.add(forwarders[i].expectedClass); |
|
165 |
} |
|
166 |
MBeanServer stop = cs.getMBeanServer(); |
|
167 |
List<Class<?>> foundClasses = new ArrayList<Class<?>>(); |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
168 |
for (MBeanServer mbs = cs.getSystemMBeanServerForwarder().getMBeanServer(); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
169 |
mbs != stop; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
170 |
mbs = ((MBeanServerForwarder) mbs).getMBeanServer()) { |
1004 | 171 |
foundClasses.add(mbs.getClass()); |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
172 |
} |
1004 | 173 |
if (!expectedClasses.equals(foundClasses)) { |
174 |
fail("Incorrect forwarder chain: expected " + expectedClasses + |
|
175 |
"; found " + foundClasses); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
// env is consistent if either (a) localizer is not enabled or (b) |
|
180 |
// localizer is enabled and context is enabled. |
|
181 |
private static boolean isConsistent(Map<String, String> env) { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
182 |
String ctxS = env.get(JMXConnectorServer.CONTEXT_FORWARDER); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
183 |
boolean ctx = (ctxS == null) ? true : Boolean.parseBoolean(ctxS); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
184 |
String locS = env.get(JMXConnectorServer.LOCALIZE_MBEAN_INFO_FORWARDER); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
185 |
boolean loc = (locS == null) ? false : Boolean.parseBoolean(locS); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1004
diff
changeset
|
186 |
return !loc || ctx; |
1004 | 187 |
} |
188 |
||
189 |
private static void fail(String why) { |
|
190 |
System.out.println("FAILED: " + why); |
|
191 |
failure = why; |
|
192 |
} |
|
193 |
} |