author | emcmanus |
Fri, 07 Nov 2008 11:48:07 +0100 | |
changeset 1570 | 4165709c91e3 |
parent 1247 | b4c26443dee5 |
child 1708 | 4e1939e6e6b5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1247 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
||
27 |
package javax.management.remote; |
|
28 |
||
1004 | 29 |
import com.sun.jmx.remote.util.EnvHelp; |
2 | 30 |
import java.io.IOException; |
31 |
import java.util.ArrayList; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
||
1004 | 35 |
import java.util.NoSuchElementException; |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
36 |
import javax.management.ClientContext; |
1004 | 37 |
import javax.management.MBeanInfo; // for javadoc |
2 | 38 |
import javax.management.MBeanNotificationInfo; |
39 |
import javax.management.MBeanRegistration; |
|
40 |
import javax.management.MBeanServer; |
|
41 |
import javax.management.Notification; |
|
42 |
import javax.management.NotificationBroadcasterSupport; |
|
43 |
import javax.management.ObjectName; |
|
1004 | 44 |
import javax.management.event.EventClientDelegate; |
2 | 45 |
|
46 |
/** |
|
47 |
* <p>Superclass of every connector server. A connector server is |
|
48 |
* attached to an MBean server. It listens for client connection |
|
49 |
* requests and creates a connection for each one.</p> |
|
50 |
* |
|
51 |
* <p>A connector server is associated with an MBean server either by |
|
52 |
* registering it in that MBean server, or by passing the MBean server |
|
53 |
* to its constructor.</p> |
|
54 |
* |
|
55 |
* <p>A connector server is inactive when created. It only starts |
|
56 |
* listening for client connections when the {@link #start() start} |
|
57 |
* method is called. A connector server stops listening for client |
|
58 |
* connections when the {@link #stop() stop} method is called or when |
|
59 |
* the connector server is unregistered from its MBean server.</p> |
|
60 |
* |
|
61 |
* <p>Stopping a connector server does not unregister it from its |
|
62 |
* MBean server. A connector server once stopped cannot be |
|
63 |
* restarted.</p> |
|
64 |
* |
|
65 |
* <p>Each time a client connection is made or broken, a notification |
|
66 |
* of class {@link JMXConnectionNotification} is emitted.</p> |
|
67 |
* |
|
68 |
* @since 1.5 |
|
69 |
*/ |
|
70 |
public abstract class JMXConnectorServer |
|
71 |
extends NotificationBroadcasterSupport |
|
72 |
implements JMXConnectorServerMBean, MBeanRegistration, JMXAddressable { |
|
73 |
||
74 |
/** |
|
75 |
* <p>Name of the attribute that specifies the authenticator for a |
|
76 |
* connector server. The value associated with this attribute, if |
|
77 |
* any, must be an object that implements the interface {@link |
|
78 |
* JMXAuthenticator}.</p> |
|
79 |
*/ |
|
80 |
public static final String AUTHENTICATOR = |
|
81 |
"jmx.remote.authenticator"; |
|
82 |
||
1004 | 83 |
/** |
84 |
* <p>Name of the attribute that specifies whether this connector |
|
85 |
* server can delegate notification handling to the |
|
86 |
* {@linkplain javax.management.event Event Service}. |
|
87 |
* The value associated with |
|
88 |
* this attribute, if any, is a String, which must be equal, |
|
89 |
* ignoring case, to {@code "true"} or {@code "false"}.</p> |
|
90 |
* |
|
91 |
* <p>Not all connector servers will understand this attribute, but the |
|
92 |
* standard {@linkplain javax.management.remote.rmi.RMIConnectorServer |
|
93 |
* RMI Connector Server} does.</p> |
|
94 |
* |
|
95 |
* <p>If this attribute is not present, then the system property of the |
|
96 |
* same name (<code>{@value}</code>) is consulted. If that is not set |
|
97 |
* either, then the Event Service is used if the connector server |
|
98 |
* supports it.</p> |
|
99 |
* |
|
100 |
* @since 1.7 |
|
101 |
*/ |
|
102 |
public static final String DELEGATE_TO_EVENT_SERVICE = |
|
103 |
"jmx.remote.delegate.event.service"; |
|
104 |
||
105 |
/** |
|
106 |
* <p>Name of the attribute that specifies whether this connector |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
107 |
* server allows clients to communicate a context with each request. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
108 |
* The value associated with this attribute, if any, must be a string |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
109 |
* that is equal to {@code "true"} or {@code "false"}, ignoring case. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
110 |
* If it is {@code "true"}, then the connector server will simulate |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
111 |
* a namespace {@code jmx.context//}, as described in |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
112 |
* {@link ClientContext#newContextForwarder}. This namespace is needed |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
113 |
* for {@link ClientContext#withContext ClientContext.withContext} to |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
114 |
* function correctly.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
115 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
116 |
* <p>Not all connector servers will understand this attribute, but the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
117 |
* standard {@linkplain javax.management.remote.rmi.RMIConnectorServer |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
118 |
* RMI Connector Server} does. For a connector server that understands |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
119 |
* this attribute, the default value is {@code "true"}.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
120 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
121 |
* @since 1.7 |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
122 |
*/ |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
123 |
public static final String CONTEXT_FORWARDER = |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
124 |
"jmx.remote.context.forwarder"; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
125 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
126 |
/** |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
127 |
* <p>Name of the attribute that specifies whether this connector server |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
128 |
* localizes the descriptions in the {@link MBeanInfo} object returned by |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
129 |
* {@link MBeanServer#getMBeanInfo MBeanServer.getMBeanInfo}, based on the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
130 |
* locale communicated by the client.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
131 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
132 |
* <p>The value associated with this attribute, if any, must be a string |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
133 |
* that is equal to {@code "true"} or {@code "false"}, ignoring case. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
134 |
* If it is {@code "true"}, then the connector server will localize |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
135 |
* {@code MBeanInfo} descriptions as specified in {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
136 |
* ClientContext#newLocalizeMBeanInfoForwarder}.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
137 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
138 |
* <p>Not all connector servers will understand this attribute, but the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
139 |
* standard {@linkplain javax.management.remote.rmi.RMIConnectorServer |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
140 |
* RMI Connector Server} does. For a connector server that understands |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
141 |
* this attribute, the default value is {@code "false"}.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
142 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
143 |
* <p>Because localization requires the client to be able to communicate |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
144 |
* its locale, it does not make sense to specify this attribute as |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
145 |
* {@code "true"} if {@link #CONTEXT_FORWARDER} is not also {@code "true"}. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
146 |
* For a connector server that understands these attributes, specifying |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
147 |
* this inconsistent combination will result in an {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
148 |
* IllegalArgumentException}.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
149 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
150 |
* @since 1.7 |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
151 |
*/ |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
152 |
public static final String LOCALIZE_MBEAN_INFO_FORWARDER = |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
153 |
"jmx.remote.localize.mbean.info"; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
154 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
155 |
/** |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
156 |
* <p>Name of the attribute that specifies whether this connector |
1004 | 157 |
* server simulates the existence of the {@link EventClientDelegate} |
158 |
* MBean. The value associated with this attribute, if any, must |
|
159 |
* be a string that is equal to {@code "true"} or {@code "false"}, |
|
160 |
* ignoring case. If it is {@code "true"}, then the connector server |
|
161 |
* will simulate an EventClientDelegate MBean, as described in {@link |
|
162 |
* EventClientDelegate#newForwarder}. This MBean is needed for {@link |
|
163 |
* javax.management.event.EventClient EventClient} to function correctly.</p> |
|
164 |
* |
|
165 |
* <p>Not all connector servers will understand this attribute, but the |
|
166 |
* standard {@linkplain javax.management.remote.rmi.RMIConnectorServer |
|
167 |
* RMI Connector Server} does. For a connector server that understands |
|
168 |
* this attribute, the default value is {@code "true"}.</p> |
|
169 |
* |
|
170 |
* @since 1.7 |
|
171 |
*/ |
|
172 |
public static final String EVENT_CLIENT_DELEGATE_FORWARDER = |
|
173 |
"jmx.remote.event.client.delegate.forwarder"; |
|
174 |
||
2 | 175 |
/** |
176 |
* <p>Constructs a connector server that will be registered as an |
|
177 |
* MBean in the MBean server it is attached to. This constructor |
|
178 |
* is typically called by one of the <code>createMBean</code> |
|
179 |
* methods when creating, within an MBean server, a connector |
|
180 |
* server that makes it available remotely.</p> |
|
181 |
*/ |
|
182 |
public JMXConnectorServer() { |
|
183 |
this(null); |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* <p>Constructs a connector server that is attached to the given |
|
188 |
* MBean server. A connector server that is created in this way |
|
1004 | 189 |
* can be registered in a different MBean server, or not registered |
190 |
* in any MBean server.</p> |
|
2 | 191 |
* |
192 |
* @param mbeanServer the MBean server that this connector server |
|
193 |
* is attached to. Null if this connector server will be attached |
|
194 |
* to an MBean server by being registered in it. |
|
195 |
*/ |
|
196 |
public JMXConnectorServer(MBeanServer mbeanServer) { |
|
1004 | 197 |
insertUserMBeanServer(mbeanServer); |
2 | 198 |
} |
199 |
||
200 |
/** |
|
201 |
* <p>Returns the MBean server that this connector server is |
|
1004 | 202 |
* attached to, or the first in a chain of user-added |
203 |
* {@link MBeanServerForwarder}s, if any.</p> |
|
2 | 204 |
* |
205 |
* @return the MBean server that this connector server is attached |
|
206 |
* to, or null if it is not yet attached to an MBean server. |
|
1004 | 207 |
* |
208 |
* @see #setMBeanServerForwarder |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
209 |
* @see #getSystemMBeanServerForwarder |
2 | 210 |
*/ |
211 |
public synchronized MBeanServer getMBeanServer() { |
|
1004 | 212 |
return userMBeanServer; |
213 |
} |
|
214 |
||
215 |
public synchronized void setMBeanServerForwarder(MBeanServerForwarder mbsf) { |
|
216 |
if (mbsf == null) |
|
217 |
throw new IllegalArgumentException("Invalid null argument: mbsf"); |
|
218 |
||
219 |
if (userMBeanServer != null) |
|
220 |
mbsf.setMBeanServer(userMBeanServer); |
|
221 |
insertUserMBeanServer(mbsf); |
|
2 | 222 |
} |
223 |
||
1004 | 224 |
/** |
225 |
* <p>Remove a forwarder from the chain of forwarders. The forwarder can |
|
226 |
* be in the system chain or the user chain. On successful return from |
|
227 |
* this method, the first occurrence in the chain of an object that is |
|
228 |
* {@linkplain Object#equals equal} to {@code mbsf} will have been |
|
229 |
* removed.</p> |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
230 |
* |
1004 | 231 |
* @param mbsf the forwarder to remove |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
232 |
* |
1004 | 233 |
* @throws NoSuchElementException if there is no occurrence of {@code mbsf} |
234 |
* in the chain. |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
235 |
* @throws IllegalArgumentException if {@code mbsf} is null or is the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
236 |
* {@linkplain #getSystemMBeanServerForwarder() system forwarder}. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
237 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
238 |
* @since 1.7 |
1004 | 239 |
*/ |
240 |
public synchronized void removeMBeanServerForwarder(MBeanServerForwarder mbsf) { |
|
2 | 241 |
if (mbsf == null) |
242 |
throw new IllegalArgumentException("Invalid null argument: mbsf"); |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
243 |
if (systemMBeanServerForwarder.equals(mbsf)) |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
244 |
throw new IllegalArgumentException("Cannot remove system forwarder"); |
2 | 245 |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
246 |
MBeanServerForwarder prev = systemMBeanServerForwarder; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
247 |
MBeanServer curr; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
248 |
while (true) { |
1004 | 249 |
curr = prev.getMBeanServer(); |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
250 |
if (mbsf.equals(curr)) |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
251 |
break; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
252 |
if (curr instanceof MBeanServerForwarder) |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
253 |
prev = (MBeanServerForwarder) curr; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
254 |
else |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
255 |
throw new NoSuchElementException("MBeanServerForwarder not in chain"); |
1004 | 256 |
} |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
257 |
MBeanServer next = mbsf.getMBeanServer(); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
258 |
prev.setMBeanServer(next); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
259 |
if (userMBeanServer == mbsf) |
1004 | 260 |
userMBeanServer = next; |
261 |
} |
|
262 |
||
263 |
/* |
|
264 |
* Set userMBeanServer to mbs and arrange for the end of the chain of |
|
265 |
* system MBeanServerForwarders to point to it. See the comment before |
|
266 |
* the systemMBeanServer and userMBeanServer field declarations. |
|
267 |
*/ |
|
268 |
private void insertUserMBeanServer(MBeanServer mbs) { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
269 |
MBeanServerForwarder lastSystemMBSF = systemMBeanServerForwarder; |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
270 |
while (true) { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
271 |
MBeanServer mbsi = lastSystemMBSF.getMBeanServer(); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
272 |
if (mbsi == userMBeanServer) |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
273 |
break; |
1004 | 274 |
lastSystemMBSF = (MBeanServerForwarder) mbsi; |
275 |
} |
|
276 |
userMBeanServer = mbs; |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
277 |
lastSystemMBSF.setMBeanServer(mbs); |
1004 | 278 |
} |
279 |
||
280 |
/** |
|
281 |
* <p>Returns the first item in the chain of system and then user |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
282 |
* forwarders. There is a chain of {@link MBeanServerForwarder}s between |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
283 |
* a {@code JMXConnectorServer} and its {@code MBeanServer}. This chain |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
284 |
* consists of two sub-chains: first the <em>system chain</em> and then |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
285 |
* the <em>user chain</em>. Incoming requests are given to the first |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
286 |
* forwarder in the system chain. Each forwarder can handle a request |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
287 |
* itself, or more usually forward it to the next forwarder, perhaps with |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
288 |
* some extra behavior such as logging or security checking before or after |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
289 |
* the forwarding. The last forwarder in the system chain is followed by |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
290 |
* the first forwarder in the user chain.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
291 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
292 |
* <p>The object returned by this method is the first forwarder in the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
293 |
* system chain. For a given {@code JMXConnectorServer}, this method |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
294 |
* always returns the same object, which simply forwards every request |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
295 |
* to the next object in the chain.</p> |
1004 | 296 |
* |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
297 |
* <p>Not all connector servers support a system chain of forwarders, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
298 |
* although the standard {@linkplain |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
299 |
* javax.management.remote.rmi.RMIConnectorServer RMI connector |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
300 |
* server} does. For those that do not, this method will throw {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
301 |
* UnsupportedOperationException}. All |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
302 |
* connector servers do support a user chain of forwarders.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
303 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
304 |
* <p>The <em>system chain</em> is usually defined by a |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
305 |
* connector server based on the environment Map; see {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
306 |
* JMXConnectorServerFactory#newJMXConnectorServer |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
307 |
* JMXConnectorServerFactory.newJMXConnectorServer}. Allowing |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
308 |
* the connector server to define its forwarders in this way |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
309 |
* ensures that they are in the correct order - some forwarders |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
310 |
* need to be inserted before others for correct behavior. It is |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
311 |
* possible to modify the system chain, for example using {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
312 |
* connectorServer.getSystemMBeanServerForwarder().setMBeanServer(mbsf)} or |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
313 |
* {@link #removeMBeanServerForwarder removeMBeanServerForwarder}, but in |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
314 |
* that case the system chain is no longer guaranteed to be correct.</p> |
1004 | 315 |
* |
316 |
* <p>The <em>user chain</em> is defined by calling {@link |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
317 |
* #setMBeanServerForwarder setMBeanServerForwarder} to insert forwarders |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
318 |
* at the head of the user chain.</p> |
1004 | 319 |
* |
320 |
* <p>This code illustrates how the chains can be traversed:</p> |
|
321 |
* |
|
322 |
* <pre> |
|
323 |
* JMXConnectorServer cs; |
|
324 |
* System.out.println("system chain:"); |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
325 |
* MBeanServer mbs = cs.getSystemMBeanServerForwarder(); |
1004 | 326 |
* while (true) { |
327 |
* if (mbs == cs.getMBeanServer()) |
|
328 |
* System.out.println("user chain:"); |
|
329 |
* if (!(mbs instanceof MBeanServerForwarder)) |
|
330 |
* break; |
|
331 |
* MBeanServerForwarder mbsf = (MBeanServerForwarder) mbs; |
|
332 |
* System.out.println("--forwarder: " + mbsf); |
|
333 |
* mbs = mbsf.getMBeanServer(); |
|
334 |
* } |
|
335 |
* System.out.println("--MBean Server"); |
|
336 |
* </pre> |
|
337 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
338 |
* <h4>Note for connector server implementors</h4> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
339 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
340 |
* <p>Existing connector server implementations can be updated to support |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
341 |
* a system chain of forwarders as follows:</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
342 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
343 |
* <ul> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
344 |
* <li><p>Override the {@link #supportsSystemMBeanServerForwarder()} |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
345 |
* method so that it returns true.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
346 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
347 |
* <li><p>Call {@link #installStandardForwarders} from the constructor of |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
348 |
* the connector server.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
349 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
350 |
* <li><p>Direct incoming requests to the result of {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
351 |
* #getSystemMBeanServerForwarder()} instead of the result of {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
352 |
* #getMBeanServer()}.</p> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
353 |
* </ul> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
354 |
* |
1004 | 355 |
* @return the first item in the system chain of forwarders. |
356 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
357 |
* @throws UnsupportedOperationException if {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
358 |
* #supportsSystemMBeanServerForwarder} returns false. |
1004 | 359 |
* |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
360 |
* @see #supportsSystemMBeanServerForwarder |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
361 |
* @see #setMBeanServerForwarder |
1004 | 362 |
* |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
363 |
* @since 1.7 |
1004 | 364 |
*/ |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
365 |
public MBeanServerForwarder getSystemMBeanServerForwarder() { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
366 |
if (!supportsSystemMBeanServerForwarder()) { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
367 |
throw new UnsupportedOperationException( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
368 |
"System MBeanServerForwarder not supported by this " + |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
369 |
"connector server"); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
370 |
} |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
371 |
return systemMBeanServerForwarder; |
1004 | 372 |
} |
373 |
||
374 |
/** |
|
375 |
* <p>Returns true if this connector server supports a system chain of |
|
376 |
* {@link MBeanServerForwarder}s. The default implementation of this |
|
377 |
* method returns false. Connector servers that do support the system |
|
378 |
* chain must override this method to return true. |
|
379 |
* |
|
380 |
* @return true if this connector server supports the system chain of |
|
381 |
* forwarders. |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
382 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
383 |
* @since 1.7 |
1004 | 384 |
*/ |
385 |
public boolean supportsSystemMBeanServerForwarder() { |
|
386 |
return false; |
|
387 |
} |
|
388 |
||
389 |
/** |
|
390 |
* <p>Install {@link MBeanServerForwarder}s in the system chain |
|
391 |
* based on the attributes in the given {@code Map}. A connector |
|
392 |
* server that {@linkplain #supportsSystemMBeanServerForwarder supports} |
|
393 |
* a system chain of {@code MBeanServerForwarder}s can call this method |
|
394 |
* to add forwarders to that chain based on the contents of {@code env}. |
|
395 |
* In order:</p> |
|
396 |
* |
|
397 |
* <ul> |
|
398 |
* |
|
399 |
* <li>If {@link #EVENT_CLIENT_DELEGATE_FORWARDER} is absent, or is |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
400 |
* present with the value {@code "true"}, then a forwarder |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
401 |
* equivalent to {@link EventClientDelegate#newForwarder |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
402 |
* EventClientDelegate.newForwarder}{@code (sysMBSF.getMBeanServer(), |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
403 |
* sysMBSF)} is inserted at the start of the system chain, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
404 |
* where {@code sysMBSF} is the object returned by {@link |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
405 |
* #getSystemMBeanServerForwarder()}. </li> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
406 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
407 |
* <li>If {@link #LOCALIZE_MBEAN_INFO_FORWARDER} is present with the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
408 |
* value {@code "true"}, then a forwarder equivalent to |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
409 |
* {@link ClientContext#newLocalizeMBeanInfoForwarder |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
410 |
* ClientContext.newLocalizeMBeanInfoForwarder}{@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
411 |
* (sysMBSF.getMBeanServer())} is inserted at the start of the system |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
412 |
* chain.</li> |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
413 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
414 |
* <li>If {@link #CONTEXT_FORWARDER} is absent, or is present with |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
415 |
* the value {@code "true"}, then a forwarder equivalent to |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
416 |
* {@link ClientContext#newContextForwarder |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
417 |
* ClientContext.newContextForwarder}{@code (sysMSBF.getMBeanServer(), |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
418 |
* sysMBSF)} is inserted at the tart of the system chain.</li> |
1004 | 419 |
* |
420 |
* </ul> |
|
421 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
422 |
* <p>For {@code EVENT_CLIENT_DELEGATE_FORWARDER} and {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
423 |
* CONTEXT_FORWARDER}, if the attribute is absent from the {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
424 |
* Map} and a system property of the same name is defined, then |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
425 |
* the value of the system property is used as if it were in the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
426 |
* {@code Map}. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
427 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
428 |
* <p>Since each forwarder is inserted at the start of the chain, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
429 |
* the final order of the forwarders is the <b>reverse</b> of the order |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
430 |
* above. This is important, because the {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
431 |
* LOCALIZE_MBEAN_INFO_FORWARDER} can only work if the {@code |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
432 |
* CONTEXT_FORWARDER} has already installed the remote client's locale |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
433 |
* in the {@linkplain ClientContext#getContext context} of the current |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
434 |
* thread.</p> |
1004 | 435 |
* |
436 |
* <p>Attributes in {@code env} that are not listed above are ignored |
|
437 |
* by this method.</p> |
|
438 |
* |
|
439 |
* @throws UnsupportedOperationException if {@link |
|
440 |
* #supportsSystemMBeanServerForwarder} is false. |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
441 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
442 |
* @throws IllegalArgumentException if the relevant attributes in {@code env} are |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
443 |
* inconsistent, for example if {@link #LOCALIZE_MBEAN_INFO_FORWARDER} is |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
444 |
* {@code "true"} but {@link #CONTEXT_FORWARDER} is {@code "false"}; or |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
445 |
* if one of the attributes has an illegal value. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
446 |
* |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
447 |
* @since 1.7 |
1004 | 448 |
*/ |
449 |
protected void installStandardForwarders(Map<String, ?> env) { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
450 |
MBeanServerForwarder sysMBSF = getSystemMBeanServerForwarder(); |
1004 | 451 |
|
452 |
// Remember that forwarders must be added in reverse order! |
|
453 |
||
454 |
boolean ecd = EnvHelp.computeBooleanFromString( |
|
455 |
env, EVENT_CLIENT_DELEGATE_FORWARDER, false, true); |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
456 |
boolean localize = EnvHelp.computeBooleanFromString( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
457 |
env, LOCALIZE_MBEAN_INFO_FORWARDER, false, false); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
458 |
boolean context = EnvHelp.computeBooleanFromString( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
459 |
env, CONTEXT_FORWARDER, false, true); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
460 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
461 |
if (localize && !context) { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
462 |
throw new IllegalArgumentException( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
463 |
"Inconsistent environment parameters: " + |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
464 |
LOCALIZE_MBEAN_INFO_FORWARDER + "=\"true\" requires " + |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
465 |
CONTEXT_FORWARDER + "=\"true\""); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
466 |
} |
1004 | 467 |
|
468 |
if (ecd) { |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
469 |
MBeanServerForwarder mbsf = EventClientDelegate.newForwarder( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
470 |
sysMBSF.getMBeanServer(), sysMBSF); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
471 |
sysMBSF.setMBeanServer(mbsf); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
472 |
} |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
473 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
474 |
if (localize) { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
475 |
MBeanServerForwarder mbsf = ClientContext.newLocalizeMBeanInfoForwarder( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
476 |
sysMBSF.getMBeanServer()); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
477 |
sysMBSF.setMBeanServer(mbsf); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
478 |
} |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
479 |
|
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
480 |
if (context) { |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
481 |
MBeanServerForwarder mbsf = ClientContext.newContextForwarder( |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
482 |
sysMBSF.getMBeanServer(), sysMBSF); |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
483 |
sysMBSF.setMBeanServer(mbsf); |
1004 | 484 |
} |
2 | 485 |
} |
486 |
||
487 |
public String[] getConnectionIds() { |
|
488 |
synchronized (connectionIds) { |
|
489 |
return connectionIds.toArray(new String[connectionIds.size()]); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
/** |
|
494 |
* <p>Returns a client stub for this connector server. A client |
|
495 |
* stub is a serializable object whose {@link |
|
496 |
* JMXConnector#connect(Map) connect} method can be used to make |
|
497 |
* one new connection to this connector server.</p> |
|
498 |
* |
|
499 |
* <p>A given connector need not support the generation of client |
|
500 |
* stubs. However, the connectors specified by the JMX Remote API do |
|
501 |
* (JMXMP Connector and RMI Connector).</p> |
|
502 |
* |
|
503 |
* <p>The default implementation of this method uses {@link |
|
504 |
* #getAddress} and {@link JMXConnectorFactory} to generate the |
|
505 |
* stub, with code equivalent to the following:</p> |
|
506 |
* |
|
507 |
* <pre> |
|
508 |
* JMXServiceURL addr = {@link #getAddress() getAddress()}; |
|
509 |
* return {@link JMXConnectorFactory#newJMXConnector(JMXServiceURL, Map) |
|
510 |
* JMXConnectorFactory.newJMXConnector(addr, env)}; |
|
511 |
* </pre> |
|
512 |
* |
|
513 |
* <p>A connector server for which this is inappropriate must |
|
514 |
* override this method so that it either implements the |
|
515 |
* appropriate logic or throws {@link |
|
516 |
* UnsupportedOperationException}.</p> |
|
517 |
* |
|
518 |
* @param env client connection parameters of the same sort that |
|
519 |
* could be provided to {@link JMXConnector#connect(Map) |
|
520 |
* JMXConnector.connect(Map)}. Can be null, which is equivalent |
|
521 |
* to an empty map. |
|
522 |
* |
|
523 |
* @return a client stub that can be used to make a new connection |
|
524 |
* to this connector server. |
|
525 |
* |
|
526 |
* @exception UnsupportedOperationException if this connector |
|
527 |
* server does not support the generation of client stubs. |
|
528 |
* |
|
529 |
* @exception IllegalStateException if the JMXConnectorServer is |
|
530 |
* not started (see {@link JMXConnectorServerMBean#isActive()}). |
|
531 |
* |
|
532 |
* @exception IOException if a communications problem means that a |
|
533 |
* stub cannot be created. |
|
534 |
**/ |
|
535 |
public JMXConnector toJMXConnector(Map<String,?> env) |
|
536 |
throws IOException |
|
537 |
{ |
|
538 |
if (!isActive()) throw new |
|
539 |
IllegalStateException("Connector is not active"); |
|
540 |
JMXServiceURL addr = getAddress(); |
|
541 |
return JMXConnectorFactory.newJMXConnector(addr, env); |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* <p>Returns an array indicating the notifications that this MBean |
|
546 |
* sends. The implementation in <code>JMXConnectorServer</code> |
|
547 |
* returns an array with one element, indicating that it can emit |
|
548 |
* notifications of class {@link JMXConnectionNotification} with |
|
549 |
* the types defined in that class. A subclass that can emit other |
|
550 |
* notifications should return an array that contains this element |
|
551 |
* plus descriptions of the other notifications.</p> |
|
552 |
* |
|
553 |
* @return the array of possible notifications. |
|
554 |
*/ |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
555 |
@Override |
2 | 556 |
public MBeanNotificationInfo[] getNotificationInfo() { |
557 |
final String[] types = { |
|
558 |
JMXConnectionNotification.OPENED, |
|
559 |
JMXConnectionNotification.CLOSED, |
|
560 |
JMXConnectionNotification.FAILED, |
|
561 |
}; |
|
562 |
final String className = JMXConnectionNotification.class.getName(); |
|
563 |
final String description = |
|
564 |
"A client connection has been opened or closed"; |
|
565 |
return new MBeanNotificationInfo[] { |
|
566 |
new MBeanNotificationInfo(types, className, description), |
|
567 |
}; |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* <p>Called by a subclass when a new client connection is opened. |
|
572 |
* Adds <code>connectionId</code> to the list returned by {@link |
|
573 |
* #getConnectionIds()}, then emits a {@link |
|
574 |
* JMXConnectionNotification} with type {@link |
|
575 |
* JMXConnectionNotification#OPENED}.</p> |
|
576 |
* |
|
577 |
* @param connectionId the ID of the new connection. This must be |
|
578 |
* different from the ID of any connection previously opened by |
|
579 |
* this connector server. |
|
580 |
* |
|
581 |
* @param message the message for the emitted {@link |
|
582 |
* JMXConnectionNotification}. Can be null. See {@link |
|
583 |
* Notification#getMessage()}. |
|
584 |
* |
|
585 |
* @param userData the <code>userData</code> for the emitted |
|
586 |
* {@link JMXConnectionNotification}. Can be null. See {@link |
|
587 |
* Notification#getUserData()}. |
|
588 |
* |
|
589 |
* @exception NullPointerException if <code>connectionId</code> is |
|
590 |
* null. |
|
591 |
*/ |
|
592 |
protected void connectionOpened(String connectionId, |
|
593 |
String message, |
|
594 |
Object userData) { |
|
595 |
||
596 |
if (connectionId == null) |
|
597 |
throw new NullPointerException("Illegal null argument"); |
|
598 |
||
599 |
synchronized (connectionIds) { |
|
600 |
connectionIds.add(connectionId); |
|
601 |
} |
|
602 |
||
603 |
sendNotification(JMXConnectionNotification.OPENED, connectionId, |
|
604 |
message, userData); |
|
605 |
} |
|
606 |
||
607 |
/** |
|
608 |
* <p>Called by a subclass when a client connection is closed |
|
609 |
* normally. Removes <code>connectionId</code> from the list returned |
|
610 |
* by {@link #getConnectionIds()}, then emits a {@link |
|
611 |
* JMXConnectionNotification} with type {@link |
|
612 |
* JMXConnectionNotification#CLOSED}.</p> |
|
613 |
* |
|
614 |
* @param connectionId the ID of the closed connection. |
|
615 |
* |
|
616 |
* @param message the message for the emitted {@link |
|
617 |
* JMXConnectionNotification}. Can be null. See {@link |
|
618 |
* Notification#getMessage()}. |
|
619 |
* |
|
620 |
* @param userData the <code>userData</code> for the emitted |
|
621 |
* {@link JMXConnectionNotification}. Can be null. See {@link |
|
622 |
* Notification#getUserData()}. |
|
623 |
* |
|
624 |
* @exception NullPointerException if <code>connectionId</code> |
|
625 |
* is null. |
|
626 |
*/ |
|
627 |
protected void connectionClosed(String connectionId, |
|
628 |
String message, |
|
629 |
Object userData) { |
|
630 |
||
631 |
if (connectionId == null) |
|
632 |
throw new NullPointerException("Illegal null argument"); |
|
633 |
||
634 |
synchronized (connectionIds) { |
|
635 |
connectionIds.remove(connectionId); |
|
636 |
} |
|
637 |
||
638 |
sendNotification(JMXConnectionNotification.CLOSED, connectionId, |
|
639 |
message, userData); |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* <p>Called by a subclass when a client connection fails. |
|
644 |
* Removes <code>connectionId</code> from the list returned by |
|
645 |
* {@link #getConnectionIds()}, then emits a {@link |
|
646 |
* JMXConnectionNotification} with type {@link |
|
647 |
* JMXConnectionNotification#FAILED}.</p> |
|
648 |
* |
|
649 |
* @param connectionId the ID of the failed connection. |
|
650 |
* |
|
651 |
* @param message the message for the emitted {@link |
|
652 |
* JMXConnectionNotification}. Can be null. See {@link |
|
653 |
* Notification#getMessage()}. |
|
654 |
* |
|
655 |
* @param userData the <code>userData</code> for the emitted |
|
656 |
* {@link JMXConnectionNotification}. Can be null. See {@link |
|
657 |
* Notification#getUserData()}. |
|
658 |
* |
|
659 |
* @exception NullPointerException if <code>connectionId</code> is |
|
660 |
* null. |
|
661 |
*/ |
|
662 |
protected void connectionFailed(String connectionId, |
|
663 |
String message, |
|
664 |
Object userData) { |
|
665 |
||
666 |
if (connectionId == null) |
|
667 |
throw new NullPointerException("Illegal null argument"); |
|
668 |
||
669 |
synchronized (connectionIds) { |
|
670 |
connectionIds.remove(connectionId); |
|
671 |
} |
|
672 |
||
673 |
sendNotification(JMXConnectionNotification.FAILED, connectionId, |
|
674 |
message, userData); |
|
675 |
} |
|
676 |
||
677 |
private void sendNotification(String type, String connectionId, |
|
678 |
String message, Object userData) { |
|
679 |
Notification notif = |
|
680 |
new JMXConnectionNotification(type, |
|
681 |
getNotificationSource(), |
|
682 |
connectionId, |
|
683 |
nextSequenceNumber(), |
|
684 |
message, |
|
685 |
userData); |
|
686 |
sendNotification(notif); |
|
687 |
} |
|
688 |
||
689 |
private synchronized Object getNotificationSource() { |
|
690 |
if (myName != null) |
|
691 |
return myName; |
|
692 |
else |
|
693 |
return this; |
|
694 |
} |
|
695 |
||
696 |
private static long nextSequenceNumber() { |
|
697 |
synchronized (sequenceNumberLock) { |
|
698 |
return sequenceNumber++; |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
// implements MBeanRegistration |
|
703 |
/** |
|
704 |
* <p>Called by an MBean server when this connector server is |
|
705 |
* registered in that MBean server. This connector server becomes |
|
706 |
* attached to the MBean server and its {@link #getMBeanServer()} |
|
707 |
* method will return <code>mbs</code>.</p> |
|
708 |
* |
|
709 |
* <p>If this connector server is already attached to an MBean |
|
710 |
* server, this method has no effect. The MBean server it is |
|
711 |
* attached to is not necessarily the one it is being registered |
|
712 |
* in.</p> |
|
713 |
* |
|
714 |
* @param mbs the MBean server in which this connection server is |
|
715 |
* being registered. |
|
716 |
* |
|
717 |
* @param name The object name of the MBean. |
|
718 |
* |
|
719 |
* @return The name under which the MBean is to be registered. |
|
720 |
* |
|
721 |
* @exception NullPointerException if <code>mbs</code> or |
|
722 |
* <code>name</code> is null. |
|
723 |
*/ |
|
724 |
public synchronized ObjectName preRegister(MBeanServer mbs, |
|
725 |
ObjectName name) { |
|
726 |
if (mbs == null || name == null) |
|
727 |
throw new NullPointerException("Null MBeanServer or ObjectName"); |
|
1004 | 728 |
if (userMBeanServer == null) { |
729 |
insertUserMBeanServer(mbs); |
|
2 | 730 |
myName = name; |
731 |
} |
|
732 |
return name; |
|
733 |
} |
|
734 |
||
735 |
public void postRegister(Boolean registrationDone) { |
|
736 |
// do nothing |
|
737 |
} |
|
738 |
||
739 |
/** |
|
740 |
* <p>Called by an MBean server when this connector server is |
|
741 |
* unregistered from that MBean server. If this connector server |
|
742 |
* was attached to that MBean server by being registered in it, |
|
743 |
* and if the connector server is still active, |
|
744 |
* then unregistering it will call the {@link #stop stop} method. |
|
745 |
* If the <code>stop</code> method throws an exception, the |
|
746 |
* unregistration attempt will fail. It is recommended to call |
|
747 |
* the <code>stop</code> method explicitly before unregistering |
|
748 |
* the MBean.</p> |
|
749 |
* |
|
750 |
* @exception IOException if thrown by the {@link #stop stop} method. |
|
751 |
*/ |
|
752 |
public synchronized void preDeregister() throws Exception { |
|
753 |
if (myName != null && isActive()) { |
|
754 |
stop(); |
|
755 |
myName = null; // just in case stop is buggy and doesn't stop |
|
756 |
} |
|
757 |
} |
|
758 |
||
759 |
public void postDeregister() { |
|
760 |
myName = null; |
|
761 |
} |
|
762 |
||
1004 | 763 |
/* |
764 |
* Fields describing the chains of forwarders (MBeanServerForwarders). |
|
765 |
* In the general case, the forwarders look something like this: |
|
766 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
767 |
* userMBeanServer |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
768 |
* | |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
769 |
* v |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
770 |
* systemMBeanServerForwarder -> mbsf2 -> mbsf3 -> mbsf4 -> mbsf5 -> mbs |
1004 | 771 |
* |
772 |
* Here, each mbsfi is an MBeanServerForwarder, and the arrows |
|
773 |
* illustrate its getMBeanServer() method. The last MBeanServerForwarder |
|
774 |
* can point to an MBeanServer that is not instanceof MBeanServerForwarder, |
|
775 |
* here mbs. |
|
776 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
777 |
* The system chain is never empty because it always has at least |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
778 |
* systemMBeanServerForwarder. Initially, the user chain can be empty if |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
779 |
* this JMXConnectorServer was constructed without an MBeanServer. In |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
780 |
* this case, userMBS will be null. If there is initially an MBeanServer, |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
781 |
* userMBS will point to it. |
1004 | 782 |
* |
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
783 |
* Whenever userMBS is changed, the system chain must be updated. Before |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
784 |
* the update, the last forwarder in the system chain points to the old |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
785 |
* value of userMBS (possibly null). It must be updated to point to |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
786 |
* the new value. The invariant is that starting from systemMBSF and |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
787 |
* repeatedly calling MBSF.getMBeanServer() you will end up at userMBS. |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
788 |
* The implication is that you will not see any MBeanServer object on the |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
789 |
* way that is not also an MBeanServerForwarder. |
1004 | 790 |
* |
791 |
* The method insertUserMBeanServer contains the logic to change userMBS |
|
792 |
* and adjust the system chain appropriately. |
|
793 |
* |
|
794 |
* If userMBS is null and this JMXConnectorServer is registered in an |
|
795 |
* MBeanServer, then userMBS becomes that MBeanServer, and the system |
|
796 |
* chain must be updated as just described. |
|
797 |
* |
|
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
798 |
* When systemMBSF is updated, there is no effect on userMBS. The system |
1004 | 799 |
* chain may contain forwarders even though the user chain is empty |
800 |
* (there is no MBeanServer). In that case an attempt to forward an |
|
801 |
* incoming request through the chain will fall off the end and fail with a |
|
802 |
* NullPointerException. Usually a connector server will refuse to start() |
|
803 |
* if it is not attached to an MBS, so this situation should not arise. |
|
2 | 804 |
*/ |
1004 | 805 |
|
806 |
private MBeanServer userMBeanServer; |
|
807 |
||
1570
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
808 |
private final MBeanServerForwarder systemMBeanServerForwarder = |
4165709c91e3
5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
1247
diff
changeset
|
809 |
new IdentityMBeanServerForwarder(); |
2 | 810 |
|
811 |
/** |
|
812 |
* The name used to registered this server in an MBeanServer. |
|
813 |
* It is null if the this server is not registered or has been unregistered. |
|
814 |
*/ |
|
815 |
private ObjectName myName; |
|
816 |
||
817 |
private List<String> connectionIds = new ArrayList<String>(); |
|
818 |
||
819 |
private static final int[] sequenceNumberLock = new int[0]; |
|
820 |
private static long sequenceNumber; |
|
821 |
} |