author | sjiang |
Thu, 31 Jul 2008 15:31:13 +0200 | |
changeset 1004 | 5ba8217eb504 |
parent 715 | f16baef3a20e |
child 1229 | 8d219f46ab69 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2002-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 |
package javax.management.remote.rmi; |
|
27 |
||
1004 | 28 |
import com.sun.jmx.event.DaemonThreadFactory; |
29 |
import com.sun.jmx.event.EventConnection; |
|
30 |
import com.sun.jmx.mbeanserver.PerThreadGroupPool; |
|
2 | 31 |
import com.sun.jmx.remote.internal.ClientCommunicatorAdmin; |
32 |
import com.sun.jmx.remote.internal.ClientListenerInfo; |
|
33 |
import com.sun.jmx.remote.internal.ClientNotifForwarder; |
|
34 |
import com.sun.jmx.remote.internal.ProxyInputStream; |
|
35 |
import com.sun.jmx.remote.internal.ProxyRef; |
|
36 |
import com.sun.jmx.remote.util.ClassLogger; |
|
37 |
import com.sun.jmx.remote.util.EnvHelp; |
|
38 |
import java.io.ByteArrayInputStream; |
|
39 |
import java.io.IOException; |
|
40 |
import java.io.InputStream; |
|
41 |
import java.io.InvalidObjectException; |
|
42 |
import java.io.NotSerializableException; |
|
43 |
import java.io.ObjectInputStream; |
|
44 |
import java.io.ObjectStreamClass; |
|
45 |
import java.io.Serializable; |
|
46 |
import java.io.WriteAbortedException; |
|
47 |
import java.lang.ref.WeakReference; |
|
48 |
import java.lang.reflect.Constructor; |
|
49 |
import java.lang.reflect.InvocationHandler; |
|
50 |
import java.lang.reflect.InvocationTargetException; |
|
51 |
import java.lang.reflect.Proxy; |
|
52 |
import java.net.MalformedURLException; |
|
53 |
import java.rmi.MarshalException; |
|
54 |
import java.rmi.MarshalledObject; |
|
55 |
import java.rmi.NoSuchObjectException; |
|
56 |
import java.rmi.Remote; |
|
57 |
import java.rmi.ServerException; |
|
58 |
import java.rmi.UnmarshalException; |
|
59 |
import java.rmi.server.RMIClientSocketFactory; |
|
60 |
import java.rmi.server.RemoteObject; |
|
61 |
import java.rmi.server.RemoteObjectInvocationHandler; |
|
62 |
import java.rmi.server.RemoteRef; |
|
63 |
import java.security.AccessController; |
|
64 |
import java.security.PrivilegedAction; |
|
65 |
import java.security.PrivilegedExceptionAction; |
|
66 |
import java.security.ProtectionDomain; |
|
67 |
import java.util.Arrays; |
|
68 |
import java.util.Collections; |
|
69 |
import java.util.HashMap; |
|
70 |
import java.util.Map; |
|
71 |
import java.util.Properties; |
|
72 |
import java.util.Set; |
|
73 |
import java.util.WeakHashMap; |
|
1004 | 74 |
import java.util.concurrent.ArrayBlockingQueue; |
75 |
import java.util.concurrent.Executor; |
|
76 |
import java.util.concurrent.LinkedBlockingDeque; |
|
77 |
import java.util.concurrent.ThreadFactory; |
|
78 |
import java.util.concurrent.ThreadPoolExecutor; |
|
79 |
import java.util.concurrent.TimeUnit; |
|
2 | 80 |
import javax.management.Attribute; |
81 |
import javax.management.AttributeList; |
|
82 |
import javax.management.AttributeNotFoundException; |
|
83 |
import javax.management.InstanceAlreadyExistsException; |
|
84 |
import javax.management.InstanceNotFoundException; |
|
85 |
import javax.management.IntrospectionException; |
|
86 |
import javax.management.InvalidAttributeValueException; |
|
1004 | 87 |
import javax.management.JMX; |
2 | 88 |
import javax.management.ListenerNotFoundException; |
89 |
import javax.management.MBeanException; |
|
90 |
import javax.management.MBeanInfo; |
|
91 |
import javax.management.MBeanRegistrationException; |
|
92 |
import javax.management.MBeanServerConnection; |
|
93 |
import javax.management.MBeanServerDelegate; |
|
94 |
import javax.management.MBeanServerNotification; |
|
95 |
import javax.management.NotCompliantMBeanException; |
|
96 |
import javax.management.Notification; |
|
97 |
import javax.management.NotificationBroadcasterSupport; |
|
98 |
import javax.management.NotificationFilter; |
|
99 |
import javax.management.NotificationFilterSupport; |
|
100 |
import javax.management.NotificationListener; |
|
101 |
import javax.management.ObjectInstance; |
|
102 |
import javax.management.ObjectName; |
|
103 |
import javax.management.QueryExp; |
|
104 |
import javax.management.ReflectionException; |
|
1004 | 105 |
import javax.management.event.EventClient; |
106 |
import javax.management.event.EventClientDelegateMBean; |
|
2 | 107 |
import javax.management.remote.JMXConnectionNotification; |
108 |
import javax.management.remote.JMXConnector; |
|
109 |
import javax.management.remote.JMXConnectorFactory; |
|
110 |
import javax.management.remote.JMXServiceURL; |
|
111 |
import javax.management.remote.NotificationResult; |
|
112 |
import javax.management.remote.JMXAddressable; |
|
113 |
import javax.naming.InitialContext; |
|
114 |
import javax.naming.NamingException; |
|
115 |
import javax.rmi.CORBA.Stub; |
|
116 |
import javax.rmi.PortableRemoteObject; |
|
117 |
import javax.rmi.ssl.SslRMIClientSocketFactory; |
|
118 |
import javax.security.auth.Subject; |
|
119 |
import org.omg.CORBA.BAD_OPERATION; |
|
120 |
import org.omg.CORBA.ORB; |
|
121 |
import sun.rmi.server.UnicastRef2; |
|
122 |
import sun.rmi.transport.LiveRef; |
|
123 |
||
124 |
/** |
|
125 |
* <p>A connection to a remote RMI connector. Usually, such |
|
126 |
* connections are made using {@link |
|
127 |
* javax.management.remote.JMXConnectorFactory JMXConnectorFactory}. |
|
128 |
* However, specialized applications can use this class directly, for |
|
129 |
* example with an {@link RMIServer} stub obtained without going |
|
130 |
* through JNDI.</p> |
|
131 |
* |
|
132 |
* @since 1.5 |
|
133 |
*/ |
|
134 |
public class RMIConnector implements JMXConnector, Serializable, JMXAddressable { |
|
135 |
||
136 |
private static final ClassLogger logger = |
|
137 |
new ClassLogger("javax.management.remote.rmi", "RMIConnector"); |
|
138 |
||
139 |
private static final long serialVersionUID = 817323035842634473L; |
|
140 |
||
141 |
private RMIConnector(RMIServer rmiServer, JMXServiceURL address, |
|
142 |
Map<String, ?> environment) { |
|
143 |
if (rmiServer == null && address == null) throw new |
|
144 |
IllegalArgumentException("rmiServer and jmxServiceURL both null"); |
|
145 |
||
146 |
initTransients(); |
|
147 |
||
148 |
this.rmiServer = rmiServer; |
|
149 |
this.jmxServiceURL = address; |
|
150 |
if (environment == null) { |
|
151 |
this.env = Collections.emptyMap(); |
|
152 |
} else { |
|
153 |
EnvHelp.checkAttributes(environment); |
|
154 |
this.env = Collections.unmodifiableMap(environment); |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* <p>Constructs an <code>RMIConnector</code> that will connect |
|
160 |
* the RMI connector server with the given address.</p> |
|
161 |
* |
|
162 |
* <p>The address can refer directly to the connector server, |
|
163 |
* using one of the following syntaxes:</p> |
|
164 |
* |
|
165 |
* <pre> |
|
166 |
* service:jmx:rmi://<em>[host[:port]]</em>/stub/<em>encoded-stub</em> |
|
167 |
* service:jmx:iiop://<em>[host[:port]]</em>/ior/<em>encoded-IOR</em> |
|
168 |
* </pre> |
|
169 |
* |
|
170 |
* <p>(Here, the square brackets <code>[]</code> are not part of the |
|
171 |
* address but indicate that the host and port are optional.)</p> |
|
172 |
* |
|
173 |
* <p>The address can instead indicate where to find an RMI stub |
|
174 |
* through JNDI, using one of the following syntaxes:</p> |
|
175 |
* |
|
176 |
* <pre> |
|
177 |
* service:jmx:rmi://<em>[host[:port]]</em>/jndi/<em>jndi-name</em> |
|
178 |
* service:jmx:iiop://<em>[host[:port]]</em>/jndi/<em>jndi-name</em> |
|
179 |
* </pre> |
|
180 |
* |
|
181 |
* <p>An implementation may also recognize additional address |
|
182 |
* syntaxes, for example:</p> |
|
183 |
* |
|
184 |
* <pre> |
|
185 |
* service:jmx:iiop://<em>[host[:port]]</em>/stub/<em>encoded-stub</em> |
|
186 |
* </pre> |
|
187 |
* |
|
188 |
* @param url the address of the RMI connector server. |
|
189 |
* |
|
190 |
* @param environment additional attributes specifying how to make |
|
191 |
* the connection. For JNDI-based addresses, these attributes can |
|
192 |
* usefully include JNDI attributes recognized by {@link |
|
193 |
* InitialContext#InitialContext(Hashtable) InitialContext}. This |
|
194 |
* parameter can be null, which is equivalent to an empty Map. |
|
195 |
* |
|
196 |
* @exception IllegalArgumentException if <code>url</code> |
|
197 |
* is null. |
|
198 |
*/ |
|
199 |
public RMIConnector(JMXServiceURL url, Map<String,?> environment) { |
|
200 |
this(null, url, environment); |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* <p>Constructs an <code>RMIConnector</code> using the given RMI stub. |
|
205 |
* |
|
206 |
* @param rmiServer an RMI stub representing the RMI connector server. |
|
207 |
* @param environment additional attributes specifying how to make |
|
208 |
* the connection. This parameter can be null, which is |
|
209 |
* equivalent to an empty Map. |
|
210 |
* |
|
211 |
* @exception IllegalArgumentException if <code>rmiServer</code> |
|
212 |
* is null. |
|
213 |
*/ |
|
214 |
public RMIConnector(RMIServer rmiServer, Map<String,?> environment) { |
|
215 |
this(rmiServer, null, environment); |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* <p>Returns a string representation of this object. In general, |
|
220 |
* the <code>toString</code> method returns a string that |
|
221 |
* "textually represents" this object. The result should be a |
|
222 |
* concise but informative representation that is easy for a |
|
223 |
* person to read.</p> |
|
224 |
* |
|
225 |
* @return a String representation of this object. |
|
226 |
**/ |
|
227 |
public String toString() { |
|
228 |
final StringBuilder b = new StringBuilder(this.getClass().getName()); |
|
229 |
b.append(":"); |
|
230 |
if (rmiServer != null) { |
|
231 |
b.append(" rmiServer=").append(rmiServer.toString()); |
|
232 |
} |
|
233 |
if (jmxServiceURL != null) { |
|
234 |
if (rmiServer!=null) b.append(","); |
|
235 |
b.append(" jmxServiceURL=").append(jmxServiceURL.toString()); |
|
236 |
} |
|
237 |
return b.toString(); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* <p>The address of this connector.</p> |
|
242 |
* |
|
243 |
* @return the address of this connector, or null if it |
|
244 |
* does not have one. |
|
245 |
* |
|
246 |
* @since 1.6 |
|
247 |
*/ |
|
248 |
public JMXServiceURL getAddress() { |
|
249 |
return jmxServiceURL; |
|
250 |
} |
|
251 |
||
252 |
//-------------------------------------------------------------------- |
|
253 |
// implements JMXConnector interface |
|
254 |
//-------------------------------------------------------------------- |
|
255 |
public void connect() throws IOException { |
|
256 |
connect(null); |
|
257 |
} |
|
258 |
||
259 |
public synchronized void connect(Map<String,?> environment) |
|
260 |
throws IOException { |
|
261 |
final boolean tracing = logger.traceOn(); |
|
262 |
String idstr = (tracing?"["+this.toString()+"]":null); |
|
263 |
||
264 |
if (terminated) { |
|
265 |
logger.trace("connect",idstr + " already closed."); |
|
266 |
throw new IOException("Connector closed"); |
|
267 |
} |
|
268 |
if (connected) { |
|
269 |
logger.trace("connect",idstr + " already connected."); |
|
270 |
return; |
|
271 |
} |
|
272 |
||
273 |
try { |
|
274 |
if (tracing) logger.trace("connect",idstr + " connecting..."); |
|
275 |
||
276 |
final Map<String, Object> usemap = |
|
277 |
new HashMap<String, Object>((this.env==null) ? |
|
278 |
Collections.<String, Object>emptyMap() : this.env); |
|
279 |
||
280 |
||
281 |
if (environment != null) { |
|
282 |
EnvHelp.checkAttributes(environment); |
|
283 |
usemap.putAll(environment); |
|
284 |
} |
|
285 |
||
286 |
// Get RMIServer stub from directory or URL encoding if needed. |
|
287 |
if (tracing) logger.trace("connect",idstr + " finding stub..."); |
|
288 |
RMIServer stub = (rmiServer!=null)?rmiServer: |
|
289 |
findRMIServer(jmxServiceURL, usemap); |
|
290 |
||
291 |
// Check for secure RMIServer stub if the corresponding |
|
292 |
// client-side environment property is set to "true". |
|
293 |
// |
|
294 |
boolean checkStub = EnvHelp.computeBooleanFromString( |
|
1004 | 295 |
usemap, |
296 |
"jmx.remote.x.check.stub",false); |
|
2 | 297 |
if (checkStub) checkStub(stub, rmiServerImplStubClass); |
298 |
||
299 |
// Connect IIOP Stub if needed. |
|
300 |
if (tracing) logger.trace("connect",idstr + " connecting stub..."); |
|
301 |
stub = connectStub(stub,usemap); |
|
302 |
idstr = (tracing?"["+this.toString()+"]":null); |
|
303 |
||
304 |
// Calling newClient on the RMIServer stub. |
|
305 |
if (tracing) |
|
306 |
logger.trace("connect",idstr + " getting connection..."); |
|
307 |
Object credentials = usemap.get(CREDENTIALS); |
|
308 |
connection = getConnection(stub, credentials, checkStub); |
|
309 |
||
310 |
// Always use one of: |
|
311 |
// ClassLoader provided in Map at connect time, |
|
312 |
// or contextClassLoader at connect time. |
|
313 |
if (tracing) |
|
314 |
logger.trace("connect",idstr + " getting class loader..."); |
|
315 |
defaultClassLoader = EnvHelp.resolveClientClassLoader(usemap); |
|
316 |
||
317 |
usemap.put(JMXConnectorFactory.DEFAULT_CLASS_LOADER, |
|
318 |
defaultClassLoader); |
|
319 |
||
320 |
rmiNotifClient = new RMINotifClient(defaultClassLoader, usemap); |
|
321 |
||
322 |
env = usemap; |
|
323 |
final long checkPeriod = EnvHelp.getConnectionCheckPeriod(usemap); |
|
324 |
communicatorAdmin = new RMIClientCommunicatorAdmin(checkPeriod); |
|
325 |
||
326 |
connected = true; |
|
327 |
||
328 |
// The connectionId variable is used in doStart(), when |
|
329 |
// reconnecting, to identify the "old" connection. |
|
330 |
// |
|
331 |
connectionId = getConnectionId(); |
|
332 |
||
1004 | 333 |
eventServiceEnabled = EnvHelp.eventServiceEnabled(env); |
334 |
||
2 | 335 |
Notification connectedNotif = |
336 |
new JMXConnectionNotification(JMXConnectionNotification.OPENED, |
|
337 |
this, |
|
338 |
connectionId, |
|
339 |
clientNotifSeqNo++, |
|
340 |
"Successful connection", |
|
341 |
null); |
|
342 |
sendNotification(connectedNotif); |
|
343 |
||
1004 | 344 |
// whether or not event service |
345 |
||
2 | 346 |
if (tracing) logger.trace("connect",idstr + " done..."); |
347 |
} catch (IOException e) { |
|
348 |
if (tracing) |
|
349 |
logger.trace("connect",idstr + " failed to connect: " + e); |
|
350 |
throw e; |
|
351 |
} catch (RuntimeException e) { |
|
352 |
if (tracing) |
|
353 |
logger.trace("connect",idstr + " failed to connect: " + e); |
|
354 |
throw e; |
|
355 |
} catch (NamingException e) { |
|
356 |
final String msg = "Failed to retrieve RMIServer stub: " + e; |
|
357 |
if (tracing) logger.trace("connect",idstr + " " + msg); |
|
358 |
throw EnvHelp.initCause(new IOException(msg),e); |
|
359 |
} |
|
360 |
} |
|
361 |
||
362 |
public synchronized String getConnectionId() throws IOException { |
|
363 |
if (terminated || !connected) { |
|
364 |
if (logger.traceOn()) |
|
365 |
logger.trace("getConnectionId","["+this.toString()+ |
|
366 |
"] not connected."); |
|
367 |
||
368 |
throw new IOException("Not connected"); |
|
369 |
} |
|
370 |
||
371 |
// we do a remote call to have an IOException if the connection is broken. |
|
372 |
// see the bug 4939578 |
|
373 |
return connection.getConnectionId(); |
|
374 |
} |
|
375 |
||
376 |
public synchronized MBeanServerConnection getMBeanServerConnection() |
|
377 |
throws IOException { |
|
378 |
return getMBeanServerConnection(null); |
|
379 |
} |
|
380 |
||
381 |
public synchronized MBeanServerConnection |
|
382 |
getMBeanServerConnection(Subject delegationSubject) |
|
383 |
throws IOException { |
|
384 |
||
385 |
if (terminated) { |
|
386 |
if (logger.traceOn()) |
|
387 |
logger.trace("getMBeanServerConnection","[" + this.toString() + |
|
388 |
"] already closed."); |
|
389 |
throw new IOException("Connection closed"); |
|
390 |
} else if (!connected) { |
|
391 |
if (logger.traceOn()) |
|
392 |
logger.trace("getMBeanServerConnection","[" + this.toString() + |
|
393 |
"] is not connected."); |
|
394 |
throw new IOException("Not connected"); |
|
395 |
} |
|
396 |
||
1004 | 397 |
MBeanServerConnection rmbsc = rmbscMap.get(delegationSubject); |
398 |
if (rmbsc != null) { |
|
399 |
return rmbsc; |
|
400 |
} |
|
401 |
||
402 |
rmbsc = new RemoteMBeanServerConnection(delegationSubject); |
|
403 |
if (eventServiceEnabled) { |
|
404 |
EventClientDelegateMBean ecd = JMX.newMBeanProxy( |
|
405 |
rmbsc, EventClientDelegateMBean.OBJECT_NAME, |
|
406 |
EventClientDelegateMBean.class); |
|
407 |
EventClient ec = new EventClient(ecd, null, defaultExecutor(), null, |
|
408 |
EventClient.DEFAULT_LEASE_TIMEOUT); |
|
2 | 409 |
|
1004 | 410 |
rmbsc = EventConnection.Factory.make(rmbsc, ec); |
411 |
ec.addEventClientListener( |
|
412 |
lostNotifListener, null, null); |
|
413 |
} |
|
414 |
rmbscMap.put(delegationSubject, rmbsc); |
|
415 |
return rmbsc; |
|
416 |
} |
|
417 |
||
418 |
private static Executor defaultExecutor() { |
|
419 |
PerThreadGroupPool.Create<ThreadPoolExecutor> create = |
|
420 |
new PerThreadGroupPool.Create<ThreadPoolExecutor>() { |
|
421 |
public ThreadPoolExecutor createThreadPool(ThreadGroup group) { |
|
422 |
ThreadFactory daemonThreadFactory = new DaemonThreadFactory( |
|
423 |
"RMIConnector listener dispatch %d"); |
|
424 |
ThreadPoolExecutor exec = new ThreadPoolExecutor( |
|
425 |
1, 10, 1, TimeUnit.SECONDS, |
|
426 |
new LinkedBlockingDeque<Runnable>(), |
|
427 |
daemonThreadFactory); |
|
428 |
exec.allowCoreThreadTimeOut(true); |
|
429 |
return exec; |
|
430 |
} |
|
431 |
}; |
|
432 |
return listenerDispatchThreadPool.getThreadPoolExecutor(create); |
|
2 | 433 |
} |
434 |
||
435 |
public void |
|
436 |
addConnectionNotificationListener(NotificationListener listener, |
|
437 |
NotificationFilter filter, |
|
438 |
Object handback) { |
|
439 |
if (listener == null) |
|
440 |
throw new NullPointerException("listener"); |
|
441 |
connectionBroadcaster.addNotificationListener(listener, filter, |
|
442 |
handback); |
|
443 |
} |
|
444 |
||
445 |
public void |
|
446 |
removeConnectionNotificationListener(NotificationListener listener) |
|
447 |
throws ListenerNotFoundException { |
|
448 |
if (listener == null) |
|
449 |
throw new NullPointerException("listener"); |
|
450 |
connectionBroadcaster.removeNotificationListener(listener); |
|
451 |
} |
|
452 |
||
453 |
public void |
|
454 |
removeConnectionNotificationListener(NotificationListener listener, |
|
455 |
NotificationFilter filter, |
|
456 |
Object handback) |
|
457 |
throws ListenerNotFoundException { |
|
458 |
if (listener == null) |
|
459 |
throw new NullPointerException("listener"); |
|
460 |
connectionBroadcaster.removeNotificationListener(listener, filter, |
|
461 |
handback); |
|
462 |
} |
|
463 |
||
464 |
private void sendNotification(Notification n) { |
|
465 |
connectionBroadcaster.sendNotification(n); |
|
466 |
} |
|
467 |
||
468 |
public synchronized void close() throws IOException { |
|
469 |
close(false); |
|
470 |
} |
|
471 |
||
472 |
// allows to do close after setting the flag "terminated" to true. |
|
473 |
// It is necessary to avoid a deadlock, see 6296324 |
|
474 |
private synchronized void close(boolean intern) throws IOException { |
|
475 |
final boolean tracing = logger.traceOn(); |
|
476 |
final boolean debug = logger.debugOn(); |
|
477 |
final String idstr = (tracing?"["+this.toString()+"]":null); |
|
478 |
||
479 |
if (!intern) { |
|
480 |
// Return if already cleanly closed. |
|
481 |
// |
|
482 |
if (terminated) { |
|
483 |
if (closeException == null) { |
|
484 |
if (tracing) logger.trace("close",idstr + " already closed."); |
|
485 |
return; |
|
486 |
} |
|
487 |
} else { |
|
488 |
terminated = true; |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
if (closeException != null && tracing) { |
|
493 |
// Already closed, but not cleanly. Attempt again. |
|
494 |
// |
|
495 |
if (tracing) { |
|
496 |
logger.trace("close",idstr + " had failed: " + closeException); |
|
497 |
logger.trace("close",idstr + " attempting to close again."); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
String savedConnectionId = null; |
|
502 |
if (connected) { |
|
503 |
savedConnectionId = connectionId; |
|
504 |
} |
|
505 |
||
506 |
closeException = null; |
|
507 |
||
508 |
if (tracing) logger.trace("close",idstr + " closing."); |
|
509 |
||
510 |
if (communicatorAdmin != null) { |
|
511 |
communicatorAdmin.terminate(); |
|
512 |
} |
|
513 |
||
1004 | 514 |
// close all EventClient |
515 |
for (MBeanServerConnection rmbsc : rmbscMap.values()) { |
|
516 |
if (rmbsc instanceof EventConnection) { |
|
517 |
try { |
|
518 |
((EventConnection)rmbsc).getEventClient().close(); |
|
519 |
} catch (Exception e) { |
|
520 |
// OK |
|
521 |
} |
|
522 |
} |
|
523 |
} |
|
524 |
||
2 | 525 |
if (rmiNotifClient != null) { |
526 |
try { |
|
527 |
rmiNotifClient.terminate(); |
|
528 |
if (tracing) logger.trace("close",idstr + |
|
529 |
" RMI Notification client terminated."); |
|
530 |
} catch (RuntimeException x) { |
|
531 |
closeException = x; |
|
532 |
if (tracing) logger.trace("close",idstr + |
|
533 |
" Failed to terminate RMI Notification client: " + x); |
|
534 |
if (debug) logger.debug("close",x); |
|
535 |
} |
|
536 |
} |
|
537 |
||
538 |
if (connection != null) { |
|
539 |
try { |
|
540 |
connection.close(); |
|
541 |
if (tracing) logger.trace("close",idstr + " closed."); |
|
542 |
} catch (NoSuchObjectException nse) { |
|
543 |
// OK, the server maybe closed itself. |
|
544 |
} catch (IOException e) { |
|
545 |
closeException = e; |
|
546 |
if (tracing) logger.trace("close",idstr + |
|
547 |
" Failed to close RMIServer: " + e); |
|
548 |
if (debug) logger.debug("close",e); |
|
549 |
} |
|
550 |
} |
|
551 |
||
552 |
// Clean up MBeanServerConnection table |
|
553 |
// |
|
554 |
rmbscMap.clear(); |
|
555 |
||
556 |
/* Send notification of closure. We don't do this if the user |
|
557 |
* never called connect() on the connector, because there's no |
|
558 |
* connection id in that case. */ |
|
559 |
||
560 |
if (savedConnectionId != null) { |
|
561 |
Notification closedNotif = |
|
562 |
new JMXConnectionNotification(JMXConnectionNotification.CLOSED, |
|
563 |
this, |
|
564 |
savedConnectionId, |
|
565 |
clientNotifSeqNo++, |
|
566 |
"Client has been closed", |
|
567 |
null); |
|
568 |
sendNotification(closedNotif); |
|
569 |
} |
|
570 |
||
571 |
// throw exception if needed |
|
572 |
// |
|
573 |
if (closeException != null) { |
|
574 |
if (tracing) logger.trace("close",idstr + " failed to close: " + |
|
575 |
closeException); |
|
576 |
if (closeException instanceof IOException) |
|
577 |
throw (IOException) closeException; |
|
578 |
if (closeException instanceof RuntimeException) |
|
579 |
throw (RuntimeException) closeException; |
|
580 |
final IOException x = |
|
581 |
new IOException("Failed to close: " + closeException); |
|
582 |
throw EnvHelp.initCause(x,closeException); |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
// added for re-connection |
|
587 |
private Integer addListenerWithSubject(ObjectName name, |
|
588 |
MarshalledObject filter, |
|
589 |
Subject delegationSubject, |
|
590 |
boolean reconnect) |
|
591 |
throws InstanceNotFoundException, IOException { |
|
592 |
||
593 |
final boolean debug = logger.debugOn(); |
|
594 |
if (debug) |
|
595 |
logger.debug("addListenerWithSubject", |
|
596 |
"(ObjectName,MarshalledObject,Subject)"); |
|
597 |
||
598 |
final ObjectName[] names = new ObjectName[] {name}; |
|
599 |
final MarshalledObject[] filters = new MarshalledObject[] {filter}; |
|
600 |
final Subject[] delegationSubjects = new Subject[] { |
|
601 |
delegationSubject |
|
602 |
}; |
|
603 |
||
604 |
final Integer[] listenerIDs = |
|
605 |
addListenersWithSubjects(names,filters,delegationSubjects, |
|
606 |
reconnect); |
|
607 |
||
608 |
if (debug) logger.debug("addListenerWithSubject","listenerID=" |
|
609 |
+ listenerIDs[0]); |
|
610 |
return listenerIDs[0]; |
|
611 |
} |
|
612 |
||
613 |
// added for re-connection |
|
614 |
private Integer[] addListenersWithSubjects(ObjectName[] names, |
|
615 |
MarshalledObject[] filters, |
|
616 |
Subject[] delegationSubjects, |
|
617 |
boolean reconnect) |
|
618 |
throws InstanceNotFoundException, IOException { |
|
619 |
||
620 |
final boolean debug = logger.debugOn(); |
|
621 |
if (debug) |
|
622 |
logger.debug("addListenersWithSubjects", |
|
623 |
"(ObjectName[],MarshalledObject[],Subject[])"); |
|
624 |
||
625 |
final ClassLoader old = pushDefaultClassLoader(); |
|
626 |
Integer[] listenerIDs = null; |
|
627 |
||
628 |
try { |
|
629 |
listenerIDs = connection.addNotificationListeners(names, |
|
630 |
filters, |
|
631 |
delegationSubjects); |
|
632 |
} catch (NoSuchObjectException noe) { |
|
633 |
// maybe reconnect |
|
634 |
if (reconnect) { |
|
635 |
communicatorAdmin.gotIOException(noe); |
|
636 |
||
637 |
listenerIDs = connection.addNotificationListeners(names, |
|
638 |
filters, |
|
639 |
delegationSubjects); |
|
640 |
} else { |
|
641 |
throw noe; |
|
642 |
} |
|
643 |
} catch (IOException ioe) { |
|
644 |
// send a failed notif if necessary |
|
645 |
communicatorAdmin.gotIOException(ioe); |
|
646 |
} finally { |
|
647 |
popDefaultClassLoader(old); |
|
648 |
} |
|
649 |
||
650 |
if (debug) logger.debug("addListenersWithSubjects","registered " |
|
1004 | 651 |
+ ((listenerIDs==null)?0:listenerIDs.length) |
652 |
+ " listener(s)"); |
|
2 | 653 |
return listenerIDs; |
654 |
} |
|
655 |
||
656 |
//-------------------------------------------------------------------- |
|
657 |
// Implementation of MBeanServerConnection |
|
658 |
//-------------------------------------------------------------------- |
|
1004 | 659 |
private class RemoteMBeanServerConnection implements MBeanServerConnection { |
660 |
private Subject delegationSubject; |
|
2 | 661 |
|
1004 | 662 |
public EventClient eventClient = null; |
2 | 663 |
|
664 |
public RemoteMBeanServerConnection() { |
|
665 |
this(null); |
|
666 |
} |
|
667 |
||
668 |
public RemoteMBeanServerConnection(Subject delegationSubject) { |
|
669 |
this.delegationSubject = delegationSubject; |
|
670 |
} |
|
671 |
||
672 |
public ObjectInstance createMBean(String className, |
|
673 |
ObjectName name) |
|
674 |
throws ReflectionException, |
|
675 |
InstanceAlreadyExistsException, |
|
676 |
MBeanRegistrationException, |
|
677 |
MBeanException, |
|
678 |
NotCompliantMBeanException, |
|
679 |
IOException { |
|
680 |
if (logger.debugOn()) |
|
681 |
logger.debug("createMBean(String,ObjectName)", |
|
682 |
"className=" + className + ", name=" + |
|
683 |
name); |
|
684 |
||
685 |
final ClassLoader old = pushDefaultClassLoader(); |
|
686 |
try { |
|
687 |
return connection.createMBean(className, |
|
688 |
name, |
|
689 |
delegationSubject); |
|
690 |
} catch (IOException ioe) { |
|
691 |
communicatorAdmin.gotIOException(ioe); |
|
692 |
||
693 |
return connection.createMBean(className, |
|
694 |
name, |
|
695 |
delegationSubject); |
|
696 |
} finally { |
|
697 |
popDefaultClassLoader(old); |
|
698 |
} |
|
699 |
} |
|
700 |
||
701 |
public ObjectInstance createMBean(String className, |
|
702 |
ObjectName name, |
|
703 |
ObjectName loaderName) |
|
704 |
throws ReflectionException, |
|
705 |
InstanceAlreadyExistsException, |
|
706 |
MBeanRegistrationException, |
|
707 |
MBeanException, |
|
708 |
NotCompliantMBeanException, |
|
709 |
InstanceNotFoundException, |
|
710 |
IOException { |
|
711 |
||
712 |
if (logger.debugOn()) |
|
713 |
logger.debug("createMBean(String,ObjectName,ObjectName)", |
|
714 |
"className=" + className + ", name=" |
|
715 |
+ name + ", loaderName=" |
|
716 |
+ loaderName + ")"); |
|
717 |
||
718 |
final ClassLoader old = pushDefaultClassLoader(); |
|
719 |
try { |
|
720 |
return connection.createMBean(className, |
|
721 |
name, |
|
722 |
loaderName, |
|
723 |
delegationSubject); |
|
724 |
||
725 |
} catch (IOException ioe) { |
|
726 |
communicatorAdmin.gotIOException(ioe); |
|
727 |
||
728 |
return connection.createMBean(className, |
|
729 |
name, |
|
730 |
loaderName, |
|
731 |
delegationSubject); |
|
732 |
||
733 |
} finally { |
|
734 |
popDefaultClassLoader(old); |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
public ObjectInstance createMBean(String className, |
|
739 |
ObjectName name, |
|
740 |
Object params[], |
|
741 |
String signature[]) |
|
742 |
throws ReflectionException, |
|
743 |
InstanceAlreadyExistsException, |
|
744 |
MBeanRegistrationException, |
|
745 |
MBeanException, |
|
746 |
NotCompliantMBeanException, |
|
747 |
IOException { |
|
748 |
if (logger.debugOn()) |
|
749 |
logger.debug("createMBean(String,ObjectName,Object[],String[])", |
|
750 |
"className=" + className + ", name=" |
|
751 |
+ name + ", params=" |
|
752 |
+ objects(params) + ", signature=" |
|
753 |
+ strings(signature)); |
|
754 |
||
755 |
final MarshalledObject<Object[]> sParams = |
|
756 |
new MarshalledObject<Object[]>(params); |
|
757 |
final ClassLoader old = pushDefaultClassLoader(); |
|
758 |
try { |
|
759 |
return connection.createMBean(className, |
|
760 |
name, |
|
761 |
sParams, |
|
762 |
signature, |
|
763 |
delegationSubject); |
|
764 |
} catch (IOException ioe) { |
|
765 |
communicatorAdmin.gotIOException(ioe); |
|
766 |
||
767 |
return connection.createMBean(className, |
|
768 |
name, |
|
769 |
sParams, |
|
770 |
signature, |
|
771 |
delegationSubject); |
|
772 |
} finally { |
|
773 |
popDefaultClassLoader(old); |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
public ObjectInstance createMBean(String className, |
|
778 |
ObjectName name, |
|
779 |
ObjectName loaderName, |
|
780 |
Object params[], |
|
781 |
String signature[]) |
|
782 |
throws ReflectionException, |
|
783 |
InstanceAlreadyExistsException, |
|
784 |
MBeanRegistrationException, |
|
785 |
MBeanException, |
|
786 |
NotCompliantMBeanException, |
|
787 |
InstanceNotFoundException, |
|
788 |
IOException { |
|
789 |
if (logger.debugOn()) logger.debug( |
|
790 |
"createMBean(String,ObjectName,ObjectName,Object[],String[])", |
|
791 |
"className=" + className + ", name=" + name + ", loaderName=" |
|
792 |
+ loaderName + ", params=" + objects(params) |
|
793 |
+ ", signature=" + strings(signature)); |
|
794 |
||
795 |
final MarshalledObject<Object[]> sParams = |
|
796 |
new MarshalledObject<Object[]>(params); |
|
797 |
final ClassLoader old = pushDefaultClassLoader(); |
|
798 |
try { |
|
799 |
return connection.createMBean(className, |
|
800 |
name, |
|
801 |
loaderName, |
|
802 |
sParams, |
|
803 |
signature, |
|
804 |
delegationSubject); |
|
805 |
} catch (IOException ioe) { |
|
806 |
communicatorAdmin.gotIOException(ioe); |
|
807 |
||
808 |
return connection.createMBean(className, |
|
809 |
name, |
|
810 |
loaderName, |
|
811 |
sParams, |
|
812 |
signature, |
|
813 |
delegationSubject); |
|
814 |
} finally { |
|
815 |
popDefaultClassLoader(old); |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
public void unregisterMBean(ObjectName name) |
|
820 |
throws InstanceNotFoundException, |
|
821 |
MBeanRegistrationException, |
|
822 |
IOException { |
|
823 |
if (logger.debugOn()) |
|
824 |
logger.debug("unregisterMBean", "name=" + name); |
|
825 |
||
826 |
final ClassLoader old = pushDefaultClassLoader(); |
|
827 |
try { |
|
828 |
connection.unregisterMBean(name, delegationSubject); |
|
829 |
} catch (IOException ioe) { |
|
830 |
communicatorAdmin.gotIOException(ioe); |
|
831 |
||
832 |
connection.unregisterMBean(name, delegationSubject); |
|
833 |
} finally { |
|
834 |
popDefaultClassLoader(old); |
|
835 |
} |
|
836 |
} |
|
837 |
||
838 |
public ObjectInstance getObjectInstance(ObjectName name) |
|
839 |
throws InstanceNotFoundException, |
|
840 |
IOException { |
|
841 |
if (logger.debugOn()) |
|
842 |
logger.debug("getObjectInstance", "name=" + name); |
|
843 |
||
844 |
final ClassLoader old = pushDefaultClassLoader(); |
|
845 |
try { |
|
846 |
return connection.getObjectInstance(name, delegationSubject); |
|
847 |
} catch (IOException ioe) { |
|
848 |
communicatorAdmin.gotIOException(ioe); |
|
849 |
||
850 |
return connection.getObjectInstance(name, delegationSubject); |
|
851 |
} finally { |
|
852 |
popDefaultClassLoader(old); |
|
853 |
} |
|
854 |
} |
|
855 |
||
856 |
public Set<ObjectInstance> queryMBeans(ObjectName name, |
|
857 |
QueryExp query) |
|
858 |
throws IOException { |
|
859 |
if (logger.debugOn()) logger.debug("queryMBeans", |
|
860 |
"name=" + name + ", query=" + query); |
|
861 |
||
862 |
final MarshalledObject<QueryExp> sQuery = |
|
863 |
new MarshalledObject<QueryExp>(query); |
|
864 |
final ClassLoader old = pushDefaultClassLoader(); |
|
865 |
try { |
|
866 |
return connection.queryMBeans(name, sQuery, delegationSubject); |
|
867 |
} catch (IOException ioe) { |
|
868 |
communicatorAdmin.gotIOException(ioe); |
|
869 |
||
870 |
return connection.queryMBeans(name, sQuery, delegationSubject); |
|
871 |
} finally { |
|
872 |
popDefaultClassLoader(old); |
|
873 |
} |
|
874 |
} |
|
875 |
||
876 |
public Set<ObjectName> queryNames(ObjectName name, |
|
877 |
QueryExp query) |
|
878 |
throws IOException { |
|
879 |
if (logger.debugOn()) logger.debug("queryNames", |
|
880 |
"name=" + name + ", query=" + query); |
|
881 |
||
882 |
final MarshalledObject<QueryExp> sQuery = |
|
883 |
new MarshalledObject<QueryExp>(query); |
|
884 |
final ClassLoader old = pushDefaultClassLoader(); |
|
885 |
try { |
|
886 |
return connection.queryNames(name, sQuery, delegationSubject); |
|
887 |
} catch (IOException ioe) { |
|
888 |
communicatorAdmin.gotIOException(ioe); |
|
889 |
||
890 |
return connection.queryNames(name, sQuery, delegationSubject); |
|
891 |
} finally { |
|
892 |
popDefaultClassLoader(old); |
|
893 |
} |
|
894 |
} |
|
895 |
||
896 |
public boolean isRegistered(ObjectName name) |
|
897 |
throws IOException { |
|
898 |
if (logger.debugOn()) |
|
899 |
logger.debug("isRegistered", "name=" + name); |
|
900 |
||
901 |
final ClassLoader old = pushDefaultClassLoader(); |
|
902 |
try { |
|
903 |
return connection.isRegistered(name, delegationSubject); |
|
904 |
} catch (IOException ioe) { |
|
905 |
communicatorAdmin.gotIOException(ioe); |
|
906 |
||
907 |
return connection.isRegistered(name, delegationSubject); |
|
908 |
} finally { |
|
909 |
popDefaultClassLoader(old); |
|
910 |
} |
|
911 |
} |
|
912 |
||
913 |
public Integer getMBeanCount() |
|
914 |
throws IOException { |
|
915 |
if (logger.debugOn()) logger.debug("getMBeanCount", ""); |
|
916 |
||
917 |
final ClassLoader old = pushDefaultClassLoader(); |
|
918 |
try { |
|
919 |
return connection.getMBeanCount(delegationSubject); |
|
920 |
} catch (IOException ioe) { |
|
921 |
communicatorAdmin.gotIOException(ioe); |
|
922 |
||
923 |
return connection.getMBeanCount(delegationSubject); |
|
924 |
} finally { |
|
925 |
popDefaultClassLoader(old); |
|
926 |
} |
|
927 |
} |
|
928 |
||
929 |
public Object getAttribute(ObjectName name, |
|
930 |
String attribute) |
|
931 |
throws MBeanException, |
|
932 |
AttributeNotFoundException, |
|
933 |
InstanceNotFoundException, |
|
934 |
ReflectionException, |
|
935 |
IOException { |
|
936 |
if (logger.debugOn()) logger.debug("getAttribute", |
|
937 |
"name=" + name + ", attribute=" |
|
938 |
+ attribute); |
|
939 |
||
940 |
final ClassLoader old = pushDefaultClassLoader(); |
|
941 |
try { |
|
942 |
return connection.getAttribute(name, |
|
943 |
attribute, |
|
944 |
delegationSubject); |
|
945 |
} catch (IOException ioe) { |
|
946 |
communicatorAdmin.gotIOException(ioe); |
|
947 |
||
948 |
return connection.getAttribute(name, |
|
949 |
attribute, |
|
950 |
delegationSubject); |
|
951 |
} finally { |
|
952 |
popDefaultClassLoader(old); |
|
953 |
} |
|
954 |
} |
|
955 |
||
956 |
public AttributeList getAttributes(ObjectName name, |
|
957 |
String[] attributes) |
|
958 |
throws InstanceNotFoundException, |
|
959 |
ReflectionException, |
|
960 |
IOException { |
|
961 |
if (logger.debugOn()) logger.debug("getAttributes", |
|
962 |
"name=" + name + ", attributes=" |
|
963 |
+ strings(attributes)); |
|
964 |
||
965 |
final ClassLoader old = pushDefaultClassLoader(); |
|
966 |
try { |
|
967 |
return connection.getAttributes(name, |
|
968 |
attributes, |
|
969 |
delegationSubject); |
|
970 |
||
971 |
} catch (IOException ioe) { |
|
972 |
communicatorAdmin.gotIOException(ioe); |
|
973 |
||
974 |
return connection.getAttributes(name, |
|
975 |
attributes, |
|
976 |
delegationSubject); |
|
977 |
} finally { |
|
978 |
popDefaultClassLoader(old); |
|
979 |
} |
|
980 |
} |
|
981 |
||
982 |
||
983 |
public void setAttribute(ObjectName name, |
|
984 |
Attribute attribute) |
|
985 |
throws InstanceNotFoundException, |
|
986 |
AttributeNotFoundException, |
|
987 |
InvalidAttributeValueException, |
|
988 |
MBeanException, |
|
989 |
ReflectionException, |
|
990 |
IOException { |
|
991 |
||
992 |
if (logger.debugOn()) logger.debug("setAttribute", |
|
993 |
"name=" + name + ", attribute=" |
|
994 |
+ attribute); |
|
995 |
||
996 |
final MarshalledObject<Attribute> sAttribute = |
|
997 |
new MarshalledObject<Attribute>(attribute); |
|
998 |
final ClassLoader old = pushDefaultClassLoader(); |
|
999 |
try { |
|
1000 |
connection.setAttribute(name, sAttribute, delegationSubject); |
|
1001 |
} catch (IOException ioe) { |
|
1002 |
communicatorAdmin.gotIOException(ioe); |
|
1003 |
||
1004 |
connection.setAttribute(name, sAttribute, delegationSubject); |
|
1005 |
} finally { |
|
1006 |
popDefaultClassLoader(old); |
|
1007 |
} |
|
1008 |
} |
|
1009 |
||
1010 |
public AttributeList setAttributes(ObjectName name, |
|
1011 |
AttributeList attributes) |
|
1012 |
throws InstanceNotFoundException, |
|
1013 |
ReflectionException, |
|
1014 |
IOException { |
|
1015 |
||
1016 |
if (logger.debugOn()) logger.debug("setAttributes", |
|
1017 |
"name=" + name + ", attributes=" |
|
1018 |
+ attributes); |
|
1019 |
||
1020 |
final MarshalledObject<AttributeList> sAttributes = |
|
1021 |
new MarshalledObject<AttributeList>(attributes); |
|
1022 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1023 |
try { |
|
1024 |
return connection.setAttributes(name, |
|
1025 |
sAttributes, |
|
1026 |
delegationSubject); |
|
1027 |
} catch (IOException ioe) { |
|
1028 |
communicatorAdmin.gotIOException(ioe); |
|
1029 |
||
1030 |
return connection.setAttributes(name, |
|
1031 |
sAttributes, |
|
1032 |
delegationSubject); |
|
1033 |
} finally { |
|
1034 |
popDefaultClassLoader(old); |
|
1035 |
} |
|
1036 |
} |
|
1037 |
||
1038 |
||
1039 |
public Object invoke(ObjectName name, |
|
1040 |
String operationName, |
|
1041 |
Object params[], |
|
1042 |
String signature[]) |
|
1043 |
throws InstanceNotFoundException, |
|
1044 |
MBeanException, |
|
1045 |
ReflectionException, |
|
1046 |
IOException { |
|
1047 |
||
1048 |
if (logger.debugOn()) logger.debug("invoke", |
|
1049 |
"name=" + name |
|
1050 |
+ ", operationName=" + operationName |
|
1051 |
+ ", params=" + objects(params) |
|
1052 |
+ ", signature=" + strings(signature)); |
|
1053 |
||
1054 |
final MarshalledObject<Object[]> sParams = |
|
1055 |
new MarshalledObject<Object[]>(params); |
|
1056 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1057 |
try { |
|
1058 |
return connection.invoke(name, |
|
1059 |
operationName, |
|
1060 |
sParams, |
|
1061 |
signature, |
|
1062 |
delegationSubject); |
|
1063 |
} catch (IOException ioe) { |
|
1064 |
communicatorAdmin.gotIOException(ioe); |
|
1065 |
||
1066 |
return connection.invoke(name, |
|
1067 |
operationName, |
|
1068 |
sParams, |
|
1069 |
signature, |
|
1070 |
delegationSubject); |
|
1071 |
} finally { |
|
1072 |
popDefaultClassLoader(old); |
|
1073 |
} |
|
1074 |
} |
|
1075 |
||
1076 |
||
1077 |
public String getDefaultDomain() |
|
1078 |
throws IOException { |
|
1079 |
if (logger.debugOn()) logger.debug("getDefaultDomain", ""); |
|
1080 |
||
1081 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1082 |
try { |
|
1083 |
return connection.getDefaultDomain(delegationSubject); |
|
1084 |
} catch (IOException ioe) { |
|
1085 |
communicatorAdmin.gotIOException(ioe); |
|
1086 |
||
1087 |
return connection.getDefaultDomain(delegationSubject); |
|
1088 |
} finally { |
|
1089 |
popDefaultClassLoader(old); |
|
1090 |
} |
|
1091 |
} |
|
1092 |
||
1093 |
public String[] getDomains() throws IOException { |
|
1094 |
if (logger.debugOn()) logger.debug("getDomains", ""); |
|
1095 |
||
1096 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1097 |
try { |
|
1098 |
return connection.getDomains(delegationSubject); |
|
1099 |
} catch (IOException ioe) { |
|
1100 |
communicatorAdmin.gotIOException(ioe); |
|
1101 |
||
1102 |
return connection.getDomains(delegationSubject); |
|
1103 |
} finally { |
|
1104 |
popDefaultClassLoader(old); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
||
1108 |
public MBeanInfo getMBeanInfo(ObjectName name) |
|
1109 |
throws InstanceNotFoundException, |
|
1110 |
IntrospectionException, |
|
1111 |
ReflectionException, |
|
1112 |
IOException { |
|
1113 |
||
1114 |
if (logger.debugOn()) logger.debug("getMBeanInfo", "name=" + name); |
|
1115 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1116 |
try { |
|
1117 |
return connection.getMBeanInfo(name, delegationSubject); |
|
1118 |
} catch (IOException ioe) { |
|
1119 |
communicatorAdmin.gotIOException(ioe); |
|
1120 |
||
1121 |
return connection.getMBeanInfo(name, delegationSubject); |
|
1122 |
} finally { |
|
1123 |
popDefaultClassLoader(old); |
|
1124 |
} |
|
1125 |
} |
|
1126 |
||
1127 |
||
1128 |
public boolean isInstanceOf(ObjectName name, |
|
1129 |
String className) |
|
1130 |
throws InstanceNotFoundException, |
|
1131 |
IOException { |
|
1132 |
if (logger.debugOn()) |
|
1133 |
logger.debug("isInstanceOf", "name=" + name + |
|
1134 |
", className=" + className); |
|
1135 |
||
1136 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1137 |
try { |
|
1138 |
return connection.isInstanceOf(name, |
|
1139 |
className, |
|
1140 |
delegationSubject); |
|
1141 |
} catch (IOException ioe) { |
|
1142 |
communicatorAdmin.gotIOException(ioe); |
|
1143 |
||
1144 |
return connection.isInstanceOf(name, |
|
1145 |
className, |
|
1146 |
delegationSubject); |
|
1147 |
} finally { |
|
1148 |
popDefaultClassLoader(old); |
|
1149 |
} |
|
1150 |
} |
|
1151 |
||
1152 |
public void addNotificationListener(ObjectName name, |
|
1153 |
ObjectName listener, |
|
1154 |
NotificationFilter filter, |
|
1155 |
Object handback) |
|
1156 |
throws InstanceNotFoundException, |
|
1157 |
IOException { |
|
1158 |
||
1159 |
if (logger.debugOn()) |
|
1160 |
logger.debug("addNotificationListener" + |
|
1161 |
"(ObjectName,ObjectName,NotificationFilter,Object)", |
|
1162 |
"name=" + name + ", listener=" + listener |
|
1163 |
+ ", filter=" + filter + ", handback=" + handback); |
|
1164 |
||
1165 |
final MarshalledObject<NotificationFilter> sFilter = |
|
1166 |
new MarshalledObject<NotificationFilter>(filter); |
|
1167 |
final MarshalledObject<Object> sHandback = |
|
1168 |
new MarshalledObject<Object>(handback); |
|
1169 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1170 |
try { |
|
1171 |
connection.addNotificationListener(name, |
|
1172 |
listener, |
|
1173 |
sFilter, |
|
1174 |
sHandback, |
|
1175 |
delegationSubject); |
|
1176 |
} catch (IOException ioe) { |
|
1177 |
communicatorAdmin.gotIOException(ioe); |
|
1178 |
||
1179 |
connection.addNotificationListener(name, |
|
1180 |
listener, |
|
1181 |
sFilter, |
|
1182 |
sHandback, |
|
1183 |
delegationSubject); |
|
1184 |
} finally { |
|
1185 |
popDefaultClassLoader(old); |
|
1186 |
} |
|
1187 |
} |
|
1188 |
||
1189 |
public void removeNotificationListener(ObjectName name, |
|
1190 |
ObjectName listener) |
|
1191 |
throws InstanceNotFoundException, |
|
1192 |
ListenerNotFoundException, |
|
1193 |
IOException { |
|
1194 |
||
1195 |
if (logger.debugOn()) logger.debug("removeNotificationListener" + |
|
1196 |
"(ObjectName,ObjectName)", |
|
1197 |
"name=" + name |
|
1198 |
+ ", listener=" + listener); |
|
1199 |
||
1200 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1201 |
try { |
|
1202 |
connection.removeNotificationListener(name, |
|
1203 |
listener, |
|
1204 |
delegationSubject); |
|
1205 |
} catch (IOException ioe) { |
|
1206 |
communicatorAdmin.gotIOException(ioe); |
|
1207 |
||
1208 |
connection.removeNotificationListener(name, |
|
1209 |
listener, |
|
1210 |
delegationSubject); |
|
1211 |
} finally { |
|
1212 |
popDefaultClassLoader(old); |
|
1213 |
} |
|
1214 |
} |
|
1215 |
||
1216 |
public void removeNotificationListener(ObjectName name, |
|
1217 |
ObjectName listener, |
|
1218 |
NotificationFilter filter, |
|
1219 |
Object handback) |
|
1220 |
throws InstanceNotFoundException, |
|
1221 |
ListenerNotFoundException, |
|
1222 |
IOException { |
|
1223 |
if (logger.debugOn()) |
|
1224 |
logger.debug("removeNotificationListener" + |
|
1225 |
"(ObjectName,ObjectName,NotificationFilter,Object)", |
|
1226 |
"name=" + name |
|
1227 |
+ ", listener=" + listener |
|
1228 |
+ ", filter=" + filter |
|
1229 |
+ ", handback=" + handback); |
|
1230 |
||
1231 |
final MarshalledObject<NotificationFilter> sFilter = |
|
1232 |
new MarshalledObject<NotificationFilter>(filter); |
|
1233 |
final MarshalledObject<Object> sHandback = |
|
1234 |
new MarshalledObject<Object>(handback); |
|
1235 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1236 |
try { |
|
1237 |
connection.removeNotificationListener(name, |
|
1238 |
listener, |
|
1239 |
sFilter, |
|
1240 |
sHandback, |
|
1241 |
delegationSubject); |
|
1242 |
} catch (IOException ioe) { |
|
1243 |
communicatorAdmin.gotIOException(ioe); |
|
1244 |
||
1245 |
connection.removeNotificationListener(name, |
|
1246 |
listener, |
|
1247 |
sFilter, |
|
1248 |
sHandback, |
|
1249 |
delegationSubject); |
|
1250 |
} finally { |
|
1251 |
popDefaultClassLoader(old); |
|
1252 |
} |
|
1253 |
} |
|
1254 |
||
1255 |
// Specific Notification Handle ---------------------------------- |
|
1256 |
||
1257 |
public void addNotificationListener(ObjectName name, |
|
1258 |
NotificationListener listener, |
|
1259 |
NotificationFilter filter, |
|
1260 |
Object handback) |
|
1261 |
throws InstanceNotFoundException, |
|
1262 |
IOException { |
|
1263 |
||
1264 |
final boolean debug = logger.debugOn(); |
|
1004 | 1265 |
|
2 | 1266 |
if (debug) |
1267 |
logger.debug("addNotificationListener" + |
|
1268 |
"(ObjectName,NotificationListener,"+ |
|
1269 |
"NotificationFilter,Object)", |
|
1270 |
"name=" + name |
|
1271 |
+ ", listener=" + listener |
|
1272 |
+ ", filter=" + filter |
|
1273 |
+ ", handback=" + handback); |
|
1274 |
||
1275 |
final Integer listenerID = |
|
1276 |
addListenerWithSubject(name, |
|
1277 |
new MarshalledObject<NotificationFilter>(filter), |
|
1278 |
delegationSubject,true); |
|
1279 |
rmiNotifClient.addNotificationListener(listenerID, name, listener, |
|
1280 |
filter, handback, |
|
1281 |
delegationSubject); |
|
1282 |
} |
|
1283 |
||
1284 |
public void removeNotificationListener(ObjectName name, |
|
1285 |
NotificationListener listener) |
|
1286 |
throws InstanceNotFoundException, |
|
1004 | 1287 |
ListenerNotFoundException, |
1288 |
IOException { |
|
1289 |
||
2 | 1290 |
final boolean debug = logger.debugOn(); |
1291 |
||
1292 |
if (debug) logger.debug("removeNotificationListener"+ |
|
1293 |
"(ObjectName,NotificationListener)", |
|
1294 |
"name=" + name |
|
1295 |
+ ", listener=" + listener); |
|
1296 |
||
1297 |
final Integer[] ret = |
|
1298 |
rmiNotifClient.removeNotificationListener(name, listener); |
|
1299 |
||
1300 |
if (debug) logger.debug("removeNotificationListener", |
|
1301 |
"listenerIDs=" + objects(ret)); |
|
1302 |
||
1303 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1304 |
||
1305 |
try { |
|
1306 |
connection.removeNotificationListeners(name, |
|
1307 |
ret, |
|
1308 |
delegationSubject); |
|
1309 |
} catch (IOException ioe) { |
|
1310 |
communicatorAdmin.gotIOException(ioe); |
|
1311 |
||
1312 |
connection.removeNotificationListeners(name, |
|
1313 |
ret, |
|
1314 |
delegationSubject); |
|
1315 |
} finally { |
|
1316 |
popDefaultClassLoader(old); |
|
1317 |
} |
|
1318 |
||
1319 |
} |
|
1320 |
||
1321 |
public void removeNotificationListener(ObjectName name, |
|
1322 |
NotificationListener listener, |
|
1323 |
NotificationFilter filter, |
|
1324 |
Object handback) |
|
1325 |
throws InstanceNotFoundException, |
|
1326 |
ListenerNotFoundException, |
|
1327 |
IOException { |
|
1328 |
final boolean debug = logger.debugOn(); |
|
1329 |
||
1330 |
if (debug) |
|
1331 |
logger.debug("removeNotificationListener"+ |
|
1332 |
"(ObjectName,NotificationListener,"+ |
|
1333 |
"NotificationFilter,Object)", |
|
1334 |
"name=" + name |
|
1335 |
+ ", listener=" + listener |
|
1336 |
+ ", filter=" + filter |
|
1337 |
+ ", handback=" + handback); |
|
1338 |
||
1339 |
final Integer ret = |
|
1340 |
rmiNotifClient.removeNotificationListener(name, listener, |
|
1341 |
filter, handback); |
|
1342 |
||
1343 |
if (debug) logger.debug("removeNotificationListener", |
|
1344 |
"listenerID=" + ret); |
|
1345 |
||
1346 |
final ClassLoader old = pushDefaultClassLoader(); |
|
1347 |
try { |
|
1348 |
connection.removeNotificationListeners(name, |
|
1349 |
new Integer[] {ret}, |
|
1350 |
delegationSubject); |
|
1351 |
} catch (IOException ioe) { |
|
1352 |
communicatorAdmin.gotIOException(ioe); |
|
1353 |
||
1354 |
connection.removeNotificationListeners(name, |
|
1355 |
new Integer[] {ret}, |
|
1356 |
delegationSubject); |
|
1357 |
} finally { |
|
1358 |
popDefaultClassLoader(old); |
|
1359 |
} |
|
1360 |
||
1361 |
} |
|
1362 |
} |
|
1363 |
||
1364 |
//-------------------------------------------------------------------- |
|
1365 |
private class RMINotifClient extends ClientNotifForwarder { |
|
1366 |
public RMINotifClient(ClassLoader cl, Map env) { |
|
1367 |
super(cl, env); |
|
1368 |
} |
|
1369 |
||
1370 |
protected NotificationResult fetchNotifs(long clientSequenceNumber, |
|
1371 |
int maxNotifications, |
|
1372 |
long timeout) |
|
1373 |
throws IOException, ClassNotFoundException { |
|
1374 |
IOException org; |
|
1375 |
||
1376 |
while (true) { // used for a successful re-connection |
|
1377 |
try { |
|
1378 |
return connection.fetchNotifications(clientSequenceNumber, |
|
1379 |
maxNotifications, |
|
1380 |
timeout); |
|
1381 |
} catch (IOException ioe) { |
|
1382 |
org = ioe; |
|
1383 |
||
1384 |
// inform of IOException |
|
1385 |
try { |
|
1386 |
communicatorAdmin.gotIOException(ioe); |
|
1387 |
||
1388 |
// The connection should be re-established. |
|
1389 |
continue; |
|
1390 |
} catch (IOException ee) { |
|
1391 |
// No more fetch, the Exception will be re-thrown. |
|
1392 |
break; |
|
1393 |
} // never reached |
|
1394 |
} // never reached |
|
1395 |
} |
|
1396 |
||
1397 |
// specially treating for an UnmarshalException |
|
1398 |
if (org instanceof UnmarshalException) { |
|
1399 |
UnmarshalException ume = (UnmarshalException)org; |
|
1400 |
||
1401 |
if (ume.detail instanceof ClassNotFoundException) |
|
1402 |
throw (ClassNotFoundException) ume.detail; |
|
1403 |
||
1404 |
/* In Sun's RMI implementation, if a method return |
|
1405 |
contains an unserializable object, then we get |
|
1406 |
UnmarshalException wrapping WriteAbortedException |
|
1407 |
wrapping NotSerializableException. In that case we |
|
1408 |
extract the NotSerializableException so that our |
|
1409 |
caller can realize it should try to skip past the |
|
1410 |
notification that presumably caused it. It's not |
|
1411 |
certain that every other RMI implementation will |
|
1412 |
generate this exact exception sequence. If not, we |
|
1413 |
will not detect that the problem is due to an |
|
1414 |
unserializable object, and we will stop trying to |
|
1415 |
receive notifications from the server. It's not |
|
1416 |
clear we can do much better. */ |
|
1417 |
if (ume.detail instanceof WriteAbortedException) { |
|
1418 |
WriteAbortedException wae = |
|
1419 |
(WriteAbortedException) ume.detail; |
|
1420 |
if (wae.detail instanceof IOException) |
|
1421 |
throw (IOException) wae.detail; |
|
1422 |
} |
|
1423 |
} else if (org instanceof MarshalException) { |
|
1424 |
// IIOP will throw MarshalException wrapping a NotSerializableException |
|
1425 |
// when a server fails to serialize a response. |
|
1426 |
MarshalException me = (MarshalException)org; |
|
1427 |
if (me.detail instanceof NotSerializableException) { |
|
1428 |
throw (NotSerializableException)me.detail; |
|
1429 |
} |
|
1430 |
} |
|
1431 |
||
1432 |
// Not serialization problem, simply re-throw the orginal exception |
|
1433 |
throw org; |
|
1434 |
} |
|
1435 |
||
1436 |
protected Integer addListenerForMBeanRemovedNotif() |
|
1437 |
throws IOException, InstanceNotFoundException { |
|
1438 |
NotificationFilterSupport clientFilter = |
|
1439 |
new NotificationFilterSupport(); |
|
1440 |
clientFilter.enableType( |
|
1441 |
MBeanServerNotification.UNREGISTRATION_NOTIFICATION); |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
1442 |
MarshalledObject<NotificationFilter> sFilter = |
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
1443 |
new MarshalledObject<NotificationFilter>(clientFilter); |
2 | 1444 |
|
1445 |
Integer[] listenerIDs; |
|
1446 |
final ObjectName[] names = |
|
1447 |
new ObjectName[] {MBeanServerDelegate.DELEGATE_NAME}; |
|
1448 |
final MarshalledObject[] filters = |
|
1449 |
new MarshalledObject[] {sFilter}; |
|
1450 |
final Subject[] subjects = new Subject[] {null}; |
|
1451 |
try { |
|
1452 |
listenerIDs = |
|
1453 |
connection.addNotificationListeners(names, |
|
1454 |
filters, |
|
1455 |
subjects); |
|
1456 |
||
1457 |
} catch (IOException ioe) { |
|
1458 |
communicatorAdmin.gotIOException(ioe); |
|
1459 |
||
1460 |
listenerIDs = |
|
1461 |
connection.addNotificationListeners(names, |
|
1462 |
filters, |
|
1463 |
subjects); |
|
1464 |
} |
|
1465 |
return listenerIDs[0]; |
|
1466 |
} |
|
1467 |
||
1468 |
protected void removeListenerForMBeanRemovedNotif(Integer id) |
|
1469 |
throws IOException, InstanceNotFoundException, |
|
1470 |
ListenerNotFoundException { |
|
1471 |
try { |
|
1472 |
connection.removeNotificationListeners( |
|
1473 |
MBeanServerDelegate.DELEGATE_NAME, |
|
1474 |
new Integer[] {id}, |
|
1475 |
null); |
|
1476 |
} catch (IOException ioe) { |
|
1477 |
communicatorAdmin.gotIOException(ioe); |
|
1478 |
||
1479 |
connection.removeNotificationListeners( |
|
1480 |
MBeanServerDelegate.DELEGATE_NAME, |
|
1481 |
new Integer[] {id}, |
|
1482 |
null); |
|
1483 |
} |
|
1484 |
||
1485 |
} |
|
1486 |
||
1487 |
protected void lostNotifs(String message, long number) { |
|
1488 |
final String notifType = JMXConnectionNotification.NOTIFS_LOST; |
|
1489 |
||
1490 |
final JMXConnectionNotification n = |
|
1491 |
new JMXConnectionNotification(notifType, |
|
1492 |
RMIConnector.this, |
|
1493 |
connectionId, |
|
1494 |
clientNotifCounter++, |
|
1495 |
message, |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
1496 |
Long.valueOf(number)); |
2 | 1497 |
sendNotification(n); |
1498 |
} |
|
1499 |
} |
|
1500 |
||
1501 |
private class RMIClientCommunicatorAdmin extends ClientCommunicatorAdmin { |
|
1502 |
public RMIClientCommunicatorAdmin(long period) { |
|
1503 |
super(period); |
|
1504 |
} |
|
1505 |
||
1506 |
public void gotIOException (IOException ioe) throws IOException { |
|
1507 |
if (ioe instanceof NoSuchObjectException) { |
|
1508 |
// need to restart |
|
1509 |
super.gotIOException(ioe); |
|
1510 |
||
1511 |
return; |
|
1512 |
} |
|
1513 |
||
1514 |
// check if the connection is broken |
|
1515 |
try { |
|
1516 |
connection.getDefaultDomain(null); |
|
1517 |
} catch (IOException ioexc) { |
|
1518 |
boolean toClose = false; |
|
1519 |
||
1520 |
synchronized(this) { |
|
1521 |
if (!terminated) { |
|
1522 |
terminated = true; |
|
1523 |
||
1524 |
toClose = true; |
|
1525 |
} |
|
1526 |
} |
|
1527 |
||
1528 |
if (toClose) { |
|
1529 |
// we should close the connection, |
|
1530 |
// but send a failed notif at first |
|
1531 |
final Notification failedNotif = |
|
1532 |
new JMXConnectionNotification( |
|
1533 |
JMXConnectionNotification.FAILED, |
|
1534 |
this, |
|
1535 |
connectionId, |
|
1536 |
clientNotifSeqNo++, |
|
1537 |
"Failed to communicate with the server: "+ioe.toString(), |
|
1538 |
ioe); |
|
1539 |
||
1540 |
sendNotification(failedNotif); |
|
1541 |
||
1542 |
try { |
|
1543 |
close(true); |
|
1544 |
} catch (Exception e) { |
|
1545 |
// OK. |
|
1546 |
// We are closing |
|
1547 |
} |
|
1548 |
} |
|
1549 |
} |
|
1550 |
||
1551 |
// forward the exception |
|
1552 |
if (ioe instanceof ServerException) { |
|
1553 |
/* Need to unwrap the exception. |
|
1554 |
Some user-thrown exception at server side will be wrapped by |
|
1555 |
rmi into a ServerException. |
|
1556 |
For example, a RMIConnnectorServer will wrap a |
|
1557 |
ClassNotFoundException into a UnmarshalException, and rmi |
|
1558 |
will throw a ServerException at client side which wraps this |
|
1559 |
UnmarshalException. |
|
1560 |
No failed notif here. |
|
1561 |
*/ |
|
1562 |
Throwable tt = ((ServerException)ioe).detail; |
|
1563 |
||
1564 |
if (tt instanceof IOException) { |
|
1565 |
throw (IOException)tt; |
|
1566 |
} else if (tt instanceof RuntimeException) { |
|
1567 |
throw (RuntimeException)tt; |
|
1568 |
} |
|
1569 |
} |
|
1570 |
||
1571 |
throw ioe; |
|
1572 |
} |
|
1573 |
||
1574 |
public void reconnectNotificationListeners(ClientListenerInfo[] old) throws IOException { |
|
1575 |
final int len = old.length; |
|
1576 |
int i; |
|
1577 |
||
1578 |
ClientListenerInfo[] clis = new ClientListenerInfo[len]; |
|
1579 |
||
1580 |
final Subject[] subjects = new Subject[len]; |
|
1581 |
final ObjectName[] names = new ObjectName[len]; |
|
1582 |
final NotificationListener[] listeners = new NotificationListener[len]; |
|
1583 |
final NotificationFilter[] filters = new NotificationFilter[len]; |
|
1584 |
final MarshalledObject[] mFilters = new MarshalledObject[len]; |
|
1585 |
final Object[] handbacks = new Object[len]; |
|
1586 |
||
1587 |
for (i=0;i<len;i++) { |
|
1588 |
subjects[i] = old[i].getDelegationSubject(); |
|
1589 |
names[i] = old[i].getObjectName(); |
|
1590 |
listeners[i] = old[i].getListener(); |
|
1591 |
filters[i] = old[i].getNotificationFilter(); |
|
1592 |
mFilters[i] = new MarshalledObject<NotificationFilter>(filters[i]); |
|
1593 |
handbacks[i] = old[i].getHandback(); |
|
1594 |
} |
|
1595 |
||
1596 |
try { |
|
1597 |
Integer[] ids = addListenersWithSubjects(names,mFilters,subjects,false); |
|
1598 |
||
1599 |
for (i=0;i<len;i++) { |
|
1600 |
clis[i] = new ClientListenerInfo(ids[i], |
|
1601 |
names[i], |
|
1602 |
listeners[i], |
|
1603 |
filters[i], |
|
1604 |
handbacks[i], |
|
1605 |
subjects[i]); |
|
1606 |
} |
|
1607 |
||
1608 |
rmiNotifClient.postReconnection(clis); |
|
1609 |
||
1610 |
return; |
|
1611 |
} catch (InstanceNotFoundException infe) { |
|
1612 |
// OK, we will do one by one |
|
1613 |
} |
|
1614 |
||
1615 |
int j = 0; |
|
1616 |
for (i=0;i<len;i++) { |
|
1617 |
try { |
|
1618 |
Integer id = addListenerWithSubject(names[i], |
|
1619 |
new MarshalledObject<NotificationFilter>(filters[i]), |
|
1620 |
subjects[i], |
|
1621 |
false); |
|
1622 |
||
1623 |
clis[j++] = new ClientListenerInfo(id, |
|
1624 |
names[i], |
|
1625 |
listeners[i], |
|
1626 |
filters[i], |
|
1627 |
handbacks[i], |
|
1628 |
subjects[i]); |
|
1629 |
} catch (InstanceNotFoundException infe) { |
|
1630 |
logger.warning("reconnectNotificationListeners", |
|
1631 |
"Can't reconnect listener for " + |
|
1632 |
names[i]); |
|
1633 |
} |
|
1634 |
} |
|
1635 |
||
1636 |
if (j != len) { |
|
1637 |
ClientListenerInfo[] tmp = clis; |
|
1638 |
clis = new ClientListenerInfo[j]; |
|
1639 |
System.arraycopy(tmp, 0, clis, 0, j); |
|
1640 |
} |
|
1641 |
||
1642 |
rmiNotifClient.postReconnection(clis); |
|
1643 |
} |
|
1644 |
||
1645 |
protected void checkConnection() throws IOException { |
|
1646 |
if (logger.debugOn()) |
|
1647 |
logger.debug("RMIClientCommunicatorAdmin-checkConnection", |
|
1648 |
"Calling the method getDefaultDomain."); |
|
1649 |
||
1650 |
connection.getDefaultDomain(null); |
|
1651 |
} |
|
1652 |
||
1653 |
protected void doStart() throws IOException { |
|
1654 |
// Get RMIServer stub from directory or URL encoding if needed. |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
1655 |
RMIServer stub; |
2 | 1656 |
try { |
1657 |
stub = (rmiServer!=null)?rmiServer: |
|
1658 |
findRMIServer(jmxServiceURL, env); |
|
1659 |
} catch (NamingException ne) { |
|
1660 |
throw new IOException("Failed to get a RMI stub: "+ne); |
|
1661 |
} |
|
1662 |
||
1663 |
// Connect IIOP Stub if needed. |
|
1664 |
stub = connectStub(stub,env); |
|
1665 |
||
1666 |
// Calling newClient on the RMIServer stub. |
|
1667 |
Object credentials = env.get(CREDENTIALS); |
|
1668 |
connection = stub.newClient(credentials); |
|
1669 |
||
1670 |
// notif issues |
|
1671 |
final ClientListenerInfo[] old = rmiNotifClient.preReconnection(); |
|
1672 |
||
1673 |
reconnectNotificationListeners(old); |
|
1674 |
||
1675 |
connectionId = getConnectionId(); |
|
1676 |
||
1677 |
Notification reconnectedNotif = |
|
1678 |
new JMXConnectionNotification(JMXConnectionNotification.OPENED, |
|
1679 |
this, |
|
1680 |
connectionId, |
|
1681 |
clientNotifSeqNo++, |
|
1682 |
"Reconnected to server", |
|
1683 |
null); |
|
1684 |
sendNotification(reconnectedNotif); |
|
1685 |
||
1686 |
} |
|
1687 |
||
1688 |
protected void doStop() { |
|
1689 |
try { |
|
1690 |
close(); |
|
1691 |
} catch (IOException ioe) { |
|
1692 |
logger.warning("RMIClientCommunicatorAdmin-doStop", |
|
1693 |
"Failed to call the method close():" + ioe); |
|
1694 |
logger.debug("RMIClientCommunicatorAdmin-doStop",ioe); |
|
1695 |
} |
|
1696 |
} |
|
1697 |
} |
|
1698 |
||
1699 |
//-------------------------------------------------------------------- |
|
1700 |
// Private stuff - Serialization |
|
1701 |
//-------------------------------------------------------------------- |
|
1702 |
/** |
|
1703 |
* <p>In order to be usable, an IIOP stub must be connected to an ORB. |
|
1704 |
* The stub is automatically connected to the ORB if: |
|
1705 |
* <ul> |
|
1706 |
* <li> It was returned by the COS naming</li> |
|
1707 |
* <li> Its server counterpart has been registered in COS naming |
|
1708 |
* through JNDI.</li> |
|
1709 |
* </ul> |
|
1710 |
* Otherwise, it is not connected. A stub which is deserialized |
|
1711 |
* from Jini is not connected. A stub which is obtained from a |
|
1712 |
* non registered RMIIIOPServerImpl is not a connected.<br> |
|
1713 |
* A stub which is not connected can't be serialized, and thus |
|
1714 |
* can't be registered in Jini. A stub which is not connected can't |
|
1715 |
* be used to invoke methods on the server. |
|
1716 |
* <p> |
|
1717 |
* In order to palliate this, this method will connect the |
|
1718 |
* given stub if it is not yet connected. If the given |
|
1719 |
* <var>RMIServer</var> is not an instance of |
|
1720 |
* {@link javax.rmi.CORBA.Stub javax.rmi.CORBA.Stub}, then the |
|
1721 |
* method do nothing and simply returns that stub. Otherwise, |
|
1722 |
* this method will attempt to connect the stub to an ORB as |
|
1723 |
* follows: |
|
1724 |
* <ul> |
|
1725 |
* <p>This method looks in the provided <var>environment</var> for |
|
1726 |
* the "java.naming.corba.orb" property. If it is found, the |
|
1727 |
* referenced object (an {@link org.omg.CORBA.ORB ORB}) is used to |
|
1728 |
* connect the stub. Otherwise, a new org.omg.CORBA.ORB is created |
|
1729 |
* by calling {@link |
|
1730 |
* org.omg.CORBA.ORB#init(String[], Properties) |
|
1731 |
* org.omg.CORBA.ORB.init((String[])null,(Properties)null)} |
|
1732 |
* <p>The new created ORB is kept in a static |
|
1733 |
* {@link WeakReference} and can be reused for connecting other |
|
1734 |
* stubs. However, no reference is ever kept on the ORB provided |
|
1735 |
* in the <var>environment</var> map, if any. |
|
1736 |
* </ul> |
|
1737 |
* @param rmiServer A RMI Server Stub. |
|
1738 |
* @param environment An environment map, possibly containing an ORB. |
|
1739 |
* @return the given stub. |
|
1740 |
* @exception IllegalArgumentException if the |
|
1741 |
* <tt>java.naming.corba.orb</tt> property is specified and |
|
1742 |
* does not point to an {@link org.omg.CORBA.ORB ORB}. |
|
1743 |
* @exception IOException if the connection to the ORB failed. |
|
1744 |
**/ |
|
1745 |
static RMIServer connectStub(RMIServer rmiServer, |
|
1746 |
Map environment) |
|
1747 |
throws IOException { |
|
1748 |
if (rmiServer instanceof javax.rmi.CORBA.Stub) { |
|
1749 |
javax.rmi.CORBA.Stub stub = (javax.rmi.CORBA.Stub) rmiServer; |
|
1750 |
try { |
|
1751 |
stub._orb(); |
|
1752 |
} catch (BAD_OPERATION x) { |
|
1753 |
stub.connect(resolveOrb(environment)); |
|
1754 |
} |
|
1755 |
} |
|
1756 |
return rmiServer; |
|
1757 |
} |
|
1758 |
||
1759 |
/** |
|
1760 |
* Get the ORB specified by <var>environment</var>, or create a |
|
1761 |
* new one. |
|
1762 |
* <p>This method looks in the provided <var>environment</var> for |
|
1763 |
* the "java.naming.corba.orb" property. If it is found, the |
|
1764 |
* referenced object (an {@link org.omg.CORBA.ORB ORB}) is |
|
1765 |
* returned. Otherwise, a new org.omg.CORBA.ORB is created |
|
1766 |
* by calling {@link |
|
1767 |
* org.omg.CORBA.ORB#init(String[], java.util.Properties) |
|
1768 |
* org.omg.CORBA.ORB.init((String[])null,(Properties)null)} |
|
1769 |
* <p>The new created ORB is kept in a static |
|
1770 |
* {@link WeakReference} and can be reused for connecting other |
|
1771 |
* stubs. However, no reference is ever kept on the ORB provided |
|
1772 |
* in the <var>environment</var> map, if any. |
|
1773 |
* @param environment An environment map, possibly containing an ORB. |
|
1774 |
* @return An ORB. |
|
1775 |
* @exception IllegalArgumentException if the |
|
1776 |
* <tt>java.naming.corba.orb</tt> property is specified and |
|
1777 |
* does not point to an {@link org.omg.CORBA.ORB ORB}. |
|
1778 |
* @exception IOException if the ORB initialization failed. |
|
1779 |
**/ |
|
1780 |
static ORB resolveOrb(Map environment) |
|
1781 |
throws IOException { |
|
1782 |
if (environment != null) { |
|
1783 |
final Object orb = environment.get(EnvHelp.DEFAULT_ORB); |
|
1784 |
if (orb != null && !(orb instanceof ORB)) |
|
1785 |
throw new IllegalArgumentException(EnvHelp.DEFAULT_ORB + |
|
1786 |
" must be an instance of org.omg.CORBA.ORB."); |
|
1787 |
if (orb != null) return (ORB)orb; |
|
1788 |
} |
|
1789 |
final ORB orb = |
|
1790 |
(RMIConnector.orb==null)?null:RMIConnector.orb.get(); |
|
1791 |
if (orb != null) return orb; |
|
1792 |
||
1793 |
final ORB newOrb = |
|
1794 |
ORB.init((String[])null, (Properties)null); |
|
1795 |
RMIConnector.orb = new WeakReference<ORB>(newOrb); |
|
1796 |
return newOrb; |
|
1797 |
} |
|
1798 |
||
1799 |
/** |
|
1800 |
* Read RMIConnector fields from an {@link java.io.ObjectInputStream |
|
1801 |
* ObjectInputStream}. |
|
1802 |
* Calls <code>s.defaultReadObject()</code> and then initializes |
|
1803 |
* all transient variables that need initializing. |
|
1804 |
* @param s The ObjectInputStream to read from. |
|
1805 |
* @exception InvalidObjectException if none of <var>rmiServer</var> stub |
|
1806 |
* or <var>jmxServiceURL</var> are set. |
|
1807 |
* @see #RMIConnector(JMXServiceURL,Map) |
|
1808 |
* @see #RMIConnector(RMIServer,Map) |
|
1809 |
**/ |
|
1810 |
private void readObject(java.io.ObjectInputStream s) |
|
1811 |
throws IOException, ClassNotFoundException { |
|
1812 |
s.defaultReadObject(); |
|
1813 |
||
1814 |
if (rmiServer == null && jmxServiceURL == null) throw new |
|
1815 |
InvalidObjectException("rmiServer and jmxServiceURL both null"); |
|
1816 |
||
1817 |
initTransients(); |
|
1818 |
} |
|
1819 |
||
1820 |
/** |
|
1821 |
* Writes the RMIConnector fields to an {@link java.io.ObjectOutputStream |
|
1822 |
* ObjectOutputStream}. |
|
1823 |
* <p>Connects the underlying RMIServer stub to an ORB, if needed, |
|
1824 |
* before serializing it. This is done using the environment |
|
1825 |
* map that was provided to the constructor, if any, and as documented |
|
1826 |
* in {@link javax.management.remote.rmi}.</p> |
|
1827 |
* <p>This method then calls <code>s.defaultWriteObject()</code>. |
|
1828 |
* Usually, <var>rmiServer</var> is null if this object |
|
1829 |
* was constructed with a JMXServiceURL, and <var>jmxServiceURL</var> |
|
1830 |
* is null if this object is constructed with a RMIServer stub. |
|
1831 |
* <p>Note that the environment Map is not serialized, since the objects |
|
1832 |
* it contains are assumed to be contextual and relevant only |
|
1833 |
* with respect to the local environment (class loader, ORB, etc...).</p> |
|
1834 |
* <p>After an RMIConnector is deserialized, it is assumed that the |
|
1835 |
* user will call {@link #connect(Map)}, providing a new Map that |
|
1836 |
* can contain values which are contextually relevant to the new |
|
1837 |
* local environment.</p> |
|
1838 |
* <p>Since connection to the ORB is needed prior to serializing, and |
|
1839 |
* since the ORB to connect to is one of those contextual parameters, |
|
1840 |
* it is not recommended to re-serialize a just de-serialized object - |
|
1841 |
* as the de-serialized object has no map. Thus, when an RMIConnector |
|
1842 |
* object is needed for serialization or transmission to a remote |
|
1843 |
* application, it is recommended to obtain a new RMIConnector stub |
|
1844 |
* by calling {@link RMIConnectorServer#toJMXConnector(Map)}.</p> |
|
1845 |
* @param s The ObjectOutputStream to write to. |
|
1846 |
* @exception InvalidObjectException if none of <var>rmiServer</var> stub |
|
1847 |
* or <var>jmxServiceURL</var> are set. |
|
1848 |
* @see #RMIConnector(JMXServiceURL,Map) |
|
1849 |
* @see #RMIConnector(RMIServer,Map) |
|
1850 |
**/ |
|
1851 |
private void writeObject(java.io.ObjectOutputStream s) |
|
1852 |
throws IOException { |
|
1853 |
if (rmiServer == null && jmxServiceURL == null) throw new |
|
1854 |
InvalidObjectException("rmiServer and jmxServiceURL both null."); |
|
1855 |
connectStub(this.rmiServer,env); |
|
1856 |
s.defaultWriteObject(); |
|
1857 |
} |
|
1858 |
||
1859 |
// Initialization of transient variables. |
|
1860 |
private void initTransients() { |
|
1861 |
rmbscMap = new WeakHashMap<Subject, MBeanServerConnection>(); |
|
1862 |
connected = false; |
|
1863 |
terminated = false; |
|
1864 |
||
1865 |
connectionBroadcaster = new NotificationBroadcasterSupport(); |
|
1004 | 1866 |
|
1867 |
lostNotifListener = |
|
1868 |
new NotificationListener() { |
|
1869 |
public void handleNotification(Notification n, Object hb) { |
|
1870 |
if (n != null && EventClient.NOTIFS_LOST.equals(n.getType())) { |
|
1871 |
Long lost = (Long)n.getUserData(); |
|
1872 |
final String msg = |
|
1873 |
"May have lost up to " + lost + |
|
1874 |
" notification" + (lost.longValue() == 1 ? "" : "s"); |
|
1875 |
sendNotification(new JMXConnectionNotification( |
|
1876 |
JMXConnectionNotification.NOTIFS_LOST, |
|
1877 |
RMIConnector.this, |
|
1878 |
connectionId, |
|
1879 |
clientNotifCounter++, |
|
1880 |
msg, |
|
1881 |
lost)); |
|
1882 |
||
1883 |
} |
|
1884 |
} |
|
1885 |
}; |
|
2 | 1886 |
} |
1887 |
||
1888 |
//-------------------------------------------------------------------- |
|
1889 |
// Private stuff - Check if stub can be trusted. |
|
1890 |
//-------------------------------------------------------------------- |
|
1891 |
||
1892 |
private static void checkStub(Remote stub, |
|
1893 |
Class<?> stubClass) { |
|
1894 |
||
1895 |
// Check remote stub is from the expected class. |
|
1896 |
// |
|
1897 |
if (stub.getClass() != stubClass) { |
|
1898 |
if (!Proxy.isProxyClass(stub.getClass())) { |
|
1899 |
throw new SecurityException( |
|
1900 |
"Expecting a " + stubClass.getName() + " stub!"); |
|
1901 |
} else { |
|
1902 |
InvocationHandler handler = Proxy.getInvocationHandler(stub); |
|
1903 |
if (handler.getClass() != RemoteObjectInvocationHandler.class) |
|
1904 |
throw new SecurityException( |
|
1905 |
"Expecting a dynamic proxy instance with a " + |
|
1906 |
RemoteObjectInvocationHandler.class.getName() + |
|
1907 |
" invocation handler!"); |
|
1908 |
else |
|
1909 |
stub = (Remote) handler; |
|
1910 |
} |
|
1911 |
} |
|
1912 |
||
1913 |
// Check RemoteRef in stub is from the expected class |
|
1914 |
// "sun.rmi.server.UnicastRef2". |
|
1915 |
// |
|
1916 |
RemoteRef ref = ((RemoteObject)stub).getRef(); |
|
1917 |
if (ref.getClass() != UnicastRef2.class) |
|
1918 |
throw new SecurityException( |
|
1919 |
"Expecting a " + UnicastRef2.class.getName() + |
|
1920 |
" remote reference in stub!"); |
|
1921 |
||
1922 |
// Check RMIClientSocketFactory in stub is from the expected class |
|
1923 |
// "javax.rmi.ssl.SslRMIClientSocketFactory". |
|
1924 |
// |
|
1925 |
LiveRef liveRef = ((UnicastRef2)ref).getLiveRef(); |
|
1926 |
RMIClientSocketFactory csf = liveRef.getClientSocketFactory(); |
|
1927 |
if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) |
|
1928 |
throw new SecurityException( |
|
1929 |
"Expecting a " + SslRMIClientSocketFactory.class.getName() + |
|
1930 |
" RMI client socket factory in stub!"); |
|
1931 |
} |
|
1932 |
||
1933 |
//-------------------------------------------------------------------- |
|
1934 |
// Private stuff - RMIServer creation |
|
1935 |
//-------------------------------------------------------------------- |
|
1936 |
||
1937 |
private RMIServer findRMIServer(JMXServiceURL directoryURL, |
|
1938 |
Map<String, Object> environment) |
|
1939 |
throws NamingException, IOException { |
|
1940 |
final boolean isIiop = RMIConnectorServer.isIiopURL(directoryURL,true); |
|
1941 |
if (isIiop) { |
|
1942 |
// Make sure java.naming.corba.orb is in the Map. |
|
1943 |
environment.put(EnvHelp.DEFAULT_ORB,resolveOrb(environment)); |
|
1944 |
} |
|
1945 |
||
1946 |
String path = directoryURL.getURLPath(); |
|
1947 |
int end = path.indexOf(';'); |
|
1948 |
if (end < 0) end = path.length(); |
|
1949 |
if (path.startsWith("/jndi/")) |
|
1950 |
return findRMIServerJNDI(path.substring(6,end), environment, isIiop); |
|
1951 |
else if (path.startsWith("/stub/")) |
|
1952 |
return findRMIServerJRMP(path.substring(6,end), environment, isIiop); |
|
1953 |
else if (path.startsWith("/ior/")) |
|
1954 |
return findRMIServerIIOP(path.substring(5,end), environment, isIiop); |
|
1955 |
else { |
|
1956 |
final String msg = "URL path must begin with /jndi/ or /stub/ " + |
|
1957 |
"or /ior/: " + path; |
|
1958 |
throw new MalformedURLException(msg); |
|
1959 |
} |
|
1960 |
} |
|
1961 |
||
1962 |
/** |
|
1963 |
* Lookup the RMIServer stub in a directory. |
|
1964 |
* @param jndiURL A JNDI URL indicating the location of the Stub |
|
1965 |
* (see {@link javax.management.remote.rmi}), e.g.: |
|
1966 |
* <ul><li><tt>rmi://registry-host:port/rmi-stub-name</tt></li> |
|
1967 |
* <li>or <tt>iiop://cosnaming-host:port/iiop-stub-name</tt></li> |
|
1968 |
* <li>or <tt>ldap://ldap-host:port/java-container-dn</tt></li> |
|
1969 |
* </ul> |
|
1970 |
* @param env the environment Map passed to the connector. |
|
1971 |
* @param isIiop true if the stub is expected to be an IIOP stub. |
|
1972 |
* @return The retrieved RMIServer stub. |
|
1973 |
* @exception NamingException if the stub couldn't be found. |
|
1974 |
**/ |
|
1975 |
private RMIServer findRMIServerJNDI(String jndiURL, Map<String, ?> env, |
|
1976 |
boolean isIiop) |
|
1977 |
throws NamingException { |
|
1978 |
||
1979 |
InitialContext ctx = new InitialContext(EnvHelp.mapToHashtable(env)); |
|
1980 |
||
1981 |
Object objref = ctx.lookup(jndiURL); |
|
1982 |
ctx.close(); |
|
1983 |
||
1984 |
if (isIiop) |
|
1985 |
return narrowIIOPServer(objref); |
|
1986 |
else |
|
1987 |
return narrowJRMPServer(objref); |
|
1988 |
} |
|
1989 |
||
1990 |
private static RMIServer narrowJRMPServer(Object objref) { |
|
1991 |
||
1992 |
return (RMIServer) objref; |
|
1993 |
} |
|
1994 |
||
1995 |
private static RMIServer narrowIIOPServer(Object objref) { |
|
1996 |
try { |
|
1997 |
return (RMIServer) |
|
1998 |
PortableRemoteObject.narrow(objref, RMIServer.class); |
|
1999 |
} catch (ClassCastException e) { |
|
2000 |
if (logger.traceOn()) |
|
2001 |
logger.trace("narrowIIOPServer","Failed to narrow objref=" + |
|
2002 |
objref + ": " + e); |
|
2003 |
if (logger.debugOn()) logger.debug("narrowIIOPServer",e); |
|
2004 |
return null; |
|
2005 |
} |
|
2006 |
} |
|
2007 |
||
2008 |
private RMIServer findRMIServerIIOP(String ior, Map env, boolean isIiop) { |
|
2009 |
// could forbid "rmi:" URL here -- but do we need to? |
|
2010 |
final ORB orb = (ORB) |
|
2011 |
env.get(EnvHelp.DEFAULT_ORB); |
|
2012 |
final Object stub = orb.string_to_object(ior); |
|
2013 |
return (RMIServer) PortableRemoteObject.narrow(stub, RMIServer.class); |
|
2014 |
} |
|
2015 |
||
2016 |
private RMIServer findRMIServerJRMP(String base64, Map env, boolean isIiop) |
|
2017 |
throws IOException { |
|
2018 |
// could forbid "iiop:" URL here -- but do we need to? |
|
2019 |
final byte[] serialized; |
|
2020 |
try { |
|
2021 |
serialized = base64ToByteArray(base64); |
|
2022 |
} catch (IllegalArgumentException e) { |
|
2023 |
throw new MalformedURLException("Bad BASE64 encoding: " + |
|
2024 |
e.getMessage()); |
|
2025 |
} |
|
2026 |
final ByteArrayInputStream bin = new ByteArrayInputStream(serialized); |
|
2027 |
||
2028 |
final ClassLoader loader = EnvHelp.resolveClientClassLoader(env); |
|
2029 |
final ObjectInputStream oin = |
|
2030 |
(loader == null) ? |
|
2031 |
new ObjectInputStream(bin) : |
|
2032 |
new ObjectInputStreamWithLoader(bin, loader); |
|
2033 |
final Object stub; |
|
2034 |
try { |
|
2035 |
stub = oin.readObject(); |
|
2036 |
} catch (ClassNotFoundException e) { |
|
2037 |
throw new MalformedURLException("Class not found: " + e); |
|
2038 |
} |
|
2039 |
return (RMIServer) PortableRemoteObject.narrow(stub, RMIServer.class); |
|
2040 |
} |
|
2041 |
||
2042 |
private static final class ObjectInputStreamWithLoader |
|
2043 |
extends ObjectInputStream { |
|
2044 |
ObjectInputStreamWithLoader(InputStream in, ClassLoader cl) |
|
2045 |
throws IOException { |
|
2046 |
super(in); |
|
2047 |
this.loader = cl; |
|
2048 |
} |
|
2049 |
||
2050 |
protected Class resolveClass(ObjectStreamClass classDesc) |
|
2051 |
throws IOException, ClassNotFoundException { |
|
2052 |
return Class.forName(classDesc.getName(), false, loader); |
|
2053 |
} |
|
2054 |
||
2055 |
private final ClassLoader loader; |
|
2056 |
} |
|
2057 |
||
2058 |
/* |
|
2059 |
The following section of code avoids a class loading problem |
|
2060 |
with RMI. The problem is that an RMI stub, when deserializing |
|
2061 |
a remote method return value or exception, will first of all |
|
2062 |
consult the first non-bootstrap class loader it finds in the |
|
2063 |
call stack. This can lead to behavior that is not portable |
|
2064 |
between implementations of the JMX Remote API. Notably, an |
|
2065 |
implementation on J2SE 1.4 will find the RMI stub's loader on |
|
2066 |
the stack. But in J2SE 5, this stub is loaded by the |
|
2067 |
bootstrap loader, so RMI will find the loader of the user code |
|
2068 |
that called an MBeanServerConnection method. |
|
2069 |
||
2070 |
To avoid this problem, we take advantage of what the RMI stub |
|
2071 |
is doing internally. Each remote call will end up calling |
|
2072 |
ref.invoke(...), where ref is the RemoteRef parameter given to |
|
2073 |
the RMI stub's constructor. It is within this call that the |
|
2074 |
deserialization will happen. So we fabricate our own RemoteRef |
|
2075 |
that delegates everything to the "real" one but that is loaded |
|
2076 |
by a class loader that knows no other classes. The class |
|
2077 |
loader NoCallStackClassLoader does this: the RemoteRef is an |
|
2078 |
instance of the class named by proxyRefClassName, which is |
|
2079 |
fabricated by the class loader using byte code that is defined |
|
2080 |
by the string below. |
|
2081 |
||
2082 |
The call stack when the deserialization happens is thus this: |
|
2083 |
MBeanServerConnection.getAttribute (or whatever) |
|
2084 |
-> RMIConnectionImpl_Stub.getAttribute |
|
2085 |
-> ProxyRef.invoke(...getAttribute...) |
|
2086 |
-> UnicastRef.invoke(...getAttribute...) |
|
2087 |
-> internal RMI stuff |
|
2088 |
||
2089 |
Here UnicastRef is the RemoteRef created when the stub was |
|
2090 |
deserialized (which is of some RMI internal class). It and the |
|
2091 |
"internal RMI stuff" are loaded by the bootstrap loader, so are |
|
2092 |
transparent to the stack search. The first non-bootstrap |
|
2093 |
loader found is our ProxyRefLoader, as required. |
|
2094 |
||
2095 |
In a future version of this code as integrated into J2SE 5, |
|
2096 |
this workaround could be replaced by direct access to the |
|
2097 |
internals of RMI. For now, we use the same code base for J2SE |
|
2098 |
and for the standalone Reference Implementation. |
|
2099 |
||
2100 |
The byte code below encodes the following class, compiled using |
|
2101 |
J2SE 1.4.2 with the -g:none option. |
|
2102 |
||
2103 |
package com.sun.jmx.remote.internal; |
|
2104 |
||
2105 |
import java.lang.reflect.Method; |
|
2106 |
import java.rmi.Remote; |
|
2107 |
import java.rmi.server.RemoteRef; |
|
2108 |
import com.sun.jmx.remote.internal.ProxyRef; |
|
2109 |
||
2110 |
public class PRef extends ProxyRef { |
|
2111 |
public PRef(RemoteRef ref) { |
|
2112 |
super(ref); |
|
2113 |
} |
|
2114 |
||
2115 |
public Object invoke(Remote obj, Method method, |
|
2116 |
Object[] params, long opnum) |
|
2117 |
throws Exception { |
|
2118 |
return ref.invoke(obj, method, params, opnum); |
|
2119 |
} |
|
2120 |
} |
|
2121 |
*/ |
|
2122 |
||
2123 |
private static final String rmiServerImplStubClassName = |
|
2124 |
RMIServer.class.getName() + "Impl_Stub"; |
|
2125 |
private static final Class rmiServerImplStubClass; |
|
2126 |
private static final String rmiConnectionImplStubClassName = |
|
2127 |
RMIConnection.class.getName() + "Impl_Stub"; |
|
2128 |
private static final Class<?> rmiConnectionImplStubClass; |
|
2129 |
private static final String pRefClassName = |
|
2130 |
"com.sun.jmx.remote.internal.PRef"; |
|
2131 |
private static final Constructor proxyRefConstructor; |
|
2132 |
static { |
|
2133 |
final String pRefByteCodeString = |
|
2134 |
"\312\376\272\276\0\0\0.\0\27\12\0\5\0\15\11\0\4\0\16\13\0\17\0"+ |
|
2135 |
"\20\7\0\21\7\0\22\1\0\6<init>\1\0\36(Ljava/rmi/server/RemoteRef;"+ |
|
2136 |
")V\1\0\4Code\1\0\6invoke\1\0S(Ljava/rmi/Remote;Ljava/lang/reflec"+ |
|
2137 |
"t/Method;[Ljava/lang/Object;J)Ljava/lang/Object;\1\0\12Exception"+ |
|
2138 |
"s\7\0\23\14\0\6\0\7\14\0\24\0\25\7\0\26\14\0\11\0\12\1\0\40com/"+ |
|
2139 |
"sun/jmx/remote/internal/PRef\1\0$com/sun/jmx/remote/internal/Pr"+ |
|
2140 |
"oxyRef\1\0\23java/lang/Exception\1\0\3ref\1\0\33Ljava/rmi/serve"+ |
|
2141 |
"r/RemoteRef;\1\0\31java/rmi/server/RemoteRef\0!\0\4\0\5\0\0\0\0"+ |
|
2142 |
"\0\2\0\1\0\6\0\7\0\1\0\10\0\0\0\22\0\2\0\2\0\0\0\6*+\267\0\1\261"+ |
|
2143 |
"\0\0\0\0\0\1\0\11\0\12\0\2\0\10\0\0\0\33\0\6\0\6\0\0\0\17*\264\0"+ |
|
2144 |
"\2+,-\26\4\271\0\3\6\0\260\0\0\0\0\0\13\0\0\0\4\0\1\0\14\0\0"; |
|
2145 |
final byte[] pRefByteCode = |
|
2146 |
NoCallStackClassLoader.stringToBytes(pRefByteCodeString); |
|
2147 |
PrivilegedExceptionAction<Constructor<?>> action = |
|
2148 |
new PrivilegedExceptionAction<Constructor<?>>() { |
|
2149 |
public Constructor<?> run() throws Exception { |
|
2150 |
Class thisClass = RMIConnector.class; |
|
2151 |
ClassLoader thisLoader = thisClass.getClassLoader(); |
|
2152 |
ProtectionDomain thisProtectionDomain = |
|
2153 |
thisClass.getProtectionDomain(); |
|
2154 |
String[] otherClassNames = {ProxyRef.class.getName()}; |
|
2155 |
ClassLoader cl = |
|
2156 |
new NoCallStackClassLoader(pRefClassName, |
|
2157 |
pRefByteCode, |
|
2158 |
otherClassNames, |
|
2159 |
thisLoader, |
|
2160 |
thisProtectionDomain); |
|
2161 |
Class<?> c = cl.loadClass(pRefClassName); |
|
2162 |
return c.getConstructor(RemoteRef.class); |
|
2163 |
} |
|
2164 |
}; |
|
2165 |
||
2166 |
Class serverStubClass; |
|
2167 |
try { |
|
2168 |
serverStubClass = Class.forName(rmiServerImplStubClassName); |
|
2169 |
} catch (Exception e) { |
|
2170 |
logger.error("<clinit>", |
|
2171 |
"Failed to instantiate " + |
|
2172 |
rmiServerImplStubClassName + ": " + e); |
|
2173 |
logger.debug("<clinit>",e); |
|
2174 |
serverStubClass = null; |
|
2175 |
} |
|
2176 |
rmiServerImplStubClass = serverStubClass; |
|
2177 |
||
2178 |
Class<?> stubClass; |
|
2179 |
Constructor constr; |
|
2180 |
try { |
|
2181 |
stubClass = Class.forName(rmiConnectionImplStubClassName); |
|
2182 |
constr = (Constructor) AccessController.doPrivileged(action); |
|
2183 |
} catch (Exception e) { |
|
2184 |
logger.error("<clinit>", |
|
2185 |
"Failed to initialize proxy reference constructor "+ |
|
2186 |
"for " + rmiConnectionImplStubClassName + ": " + e); |
|
2187 |
logger.debug("<clinit>",e); |
|
2188 |
stubClass = null; |
|
2189 |
constr = null; |
|
2190 |
} |
|
2191 |
rmiConnectionImplStubClass = stubClass; |
|
2192 |
proxyRefConstructor = constr; |
|
2193 |
} |
|
2194 |
||
2195 |
private static RMIConnection shadowJrmpStub(RemoteObject stub) |
|
2196 |
throws InstantiationException, IllegalAccessException, |
|
2197 |
InvocationTargetException, ClassNotFoundException, |
|
2198 |
NoSuchMethodException { |
|
2199 |
RemoteRef ref = stub.getRef(); |
|
2200 |
RemoteRef proxyRef = (RemoteRef) |
|
2201 |
proxyRefConstructor.newInstance(new Object[] {ref}); |
|
2202 |
final Class[] constrTypes = {RemoteRef.class}; |
|
2203 |
final Constructor rmiConnectionImplStubConstructor = |
|
2204 |
rmiConnectionImplStubClass.getConstructor(constrTypes); |
|
2205 |
Object[] args = {proxyRef}; |
|
2206 |
RMIConnection proxyStub = (RMIConnection) |
|
2207 |
rmiConnectionImplStubConstructor.newInstance(args); |
|
2208 |
return proxyStub; |
|
2209 |
} |
|
2210 |
||
2211 |
/* |
|
2212 |
The following code performs a similar trick for RMI/IIOP to the |
|
2213 |
one described above for RMI/JRMP. Unlike JRMP, though, we |
|
2214 |
can't easily insert an object between the RMIConnection stub |
|
2215 |
and the RMI/IIOP deserialization code, as explained below. |
|
2216 |
||
2217 |
A method in an RMI/IIOP stub does the following. It makes an |
|
2218 |
org.omg.CORBA_2_3.portable.OutputStream for each request, and |
|
2219 |
writes the parameters to it. Then it calls |
|
2220 |
_invoke(OutputStream) which it inherits from CORBA's |
|
2221 |
ObjectImpl. That returns an |
|
2222 |
org.omg.CORBA_2_3.portable.InputStream. The return value is |
|
2223 |
read from this InputStream. So the stack during |
|
2224 |
deserialization looks like this: |
|
2225 |
||
2226 |
MBeanServerConnection.getAttribute (or whatever) |
|
2227 |
-> _RMIConnection_Stub.getAttribute |
|
2228 |
-> Util.readAny (a CORBA method) |
|
2229 |
-> InputStream.read_any |
|
2230 |
-> internal CORBA stuff |
|
2231 |
||
2232 |
What we would have *liked* to have done would be the same thing |
|
2233 |
as for RMI/JRMP. We create a "ProxyDelegate" that is an |
|
2234 |
org.omg.CORBA.portable.Delegate that simply forwards every |
|
2235 |
operation to the real original Delegate from the RMIConnection |
|
2236 |
stub, except that the InputStream returned by _invoke is |
|
2237 |
wrapped by a "ProxyInputStream" that is loaded by our |
|
2238 |
NoCallStackClassLoader. |
|
2239 |
||
2240 |
Unfortunately, this doesn't work, at least with Sun's J2SE |
|
2241 |
1.4.2, because the CORBA code is not designed to allow you to |
|
2242 |
change Delegates arbitrarily. You get a ClassCastException |
|
2243 |
from code that expects the Delegate to implement an internal |
|
2244 |
interface. |
|
2245 |
||
2246 |
So instead we do the following. We create a subclass of the |
|
2247 |
stub that overrides the _invoke method so as to wrap the |
|
2248 |
returned InputStream in a ProxyInputStream. We create a |
|
2249 |
subclass of ProxyInputStream using the NoCallStackClassLoader |
|
2250 |
and override its read_any and read_value(Class) methods. |
|
2251 |
(These are the only methods called during deserialization of |
|
2252 |
MBeanServerConnection return values.) We extract the Delegate |
|
2253 |
from the original stub and insert it into our subclass stub, |
|
2254 |
and away we go. The state of a stub consists solely of its |
|
2255 |
Delegate. |
|
2256 |
||
2257 |
We also need to catch ApplicationException, which will encode |
|
2258 |
any exceptions declared in the throws clause of the called |
|
2259 |
method. Its InputStream needs to be wrapped in a |
|
2260 |
ProxyInputSteam too. |
|
2261 |
||
2262 |
We override _releaseReply in the stub subclass so that it |
|
2263 |
replaces a ProxyInputStream argument with the original |
|
2264 |
InputStream. This avoids problems if the implementation of |
|
2265 |
_releaseReply ends up casting this InputStream to an |
|
2266 |
implementation-specific interface (which in Sun's J2SE 5 it |
|
2267 |
does). |
|
2268 |
||
2269 |
It is not strictly necessary for the stub subclass to be loaded |
|
2270 |
by a NoCallStackClassLoader, since the call-stack search stops |
|
2271 |
at the ProxyInputStream subclass. However, it is convenient |
|
2272 |
for two reasons. One is that it means that the |
|
2273 |
ProxyInputStream subclass can be accessed directly, without |
|
2274 |
using reflection. The other is that it avoids build problems, |
|
2275 |
since usually stubs are created after other classes are |
|
2276 |
compiled, so we can't access them from this class without, |
|
2277 |
again, using reflection. |
|
2278 |
||
2279 |
The strings below encode the following two Java classes, |
|
2280 |
compiled using J2SE 1.4.2 with javac -g:none. |
|
2281 |
||
2282 |
package com.sun.jmx.remote.internal; |
|
2283 |
||
2284 |
import org.omg.stub.javax.management.remote.rmi._RMIConnection_Stub; |
|
2285 |
||
2286 |
import org.omg.CORBA.portable.ApplicationException; |
|
2287 |
import org.omg.CORBA.portable.InputStream; |
|
2288 |
import org.omg.CORBA.portable.OutputStream; |
|
2289 |
import org.omg.CORBA.portable.RemarshalException; |
|
2290 |
||
2291 |
public class ProxyStub extends _RMIConnection_Stub { |
|
2292 |
public InputStream _invoke(OutputStream out) |
|
2293 |
throws ApplicationException, RemarshalException { |
|
2294 |
try { |
|
2295 |
return new PInputStream(super._invoke(out)); |
|
2296 |
} catch (ApplicationException e) { |
|
2297 |
InputStream pis = new PInputStream(e.getInputStream()); |
|
2298 |
throw new ApplicationException(e.getId(), pis); |
|
2299 |
} |
|
2300 |
} |
|
2301 |
||
2302 |
public void _releaseReply(InputStream in) { |
|
2303 |
PInputStream pis = (PInputStream) in; |
|
2304 |
super._releaseReply(pis.getProxiedInputStream()); |
|
2305 |
} |
|
2306 |
} |
|
2307 |
||
2308 |
package com.sun.jmx.remote.internal; |
|
2309 |
||
2310 |
public class PInputStream extends ProxyInputStream { |
|
2311 |
public PInputStream(org.omg.CORBA.portable.InputStream in) { |
|
2312 |
super(in); |
|
2313 |
} |
|
2314 |
||
2315 |
public org.omg.CORBA.Any read_any() { |
|
2316 |
return in.read_any(); |
|
2317 |
} |
|
2318 |
||
2319 |
public java.io.Serializable read_value(Class clz) { |
|
2320 |
return narrow().read_value(clz); |
|
2321 |
} |
|
2322 |
} |
|
2323 |
||
2324 |
||
2325 |
*/ |
|
2326 |
private static final String iiopConnectionStubClassName = |
|
2327 |
"org.omg.stub.javax.management.remote.rmi._RMIConnection_Stub"; |
|
2328 |
private static final String proxyStubClassName = |
|
2329 |
"com.sun.jmx.remote.internal.ProxyStub"; |
|
2330 |
private static final String pInputStreamClassName = |
|
2331 |
"com.sun.jmx.remote.internal.PInputStream"; |
|
2332 |
private static final Class proxyStubClass; |
|
2333 |
static { |
|
2334 |
final String proxyStubByteCodeString = |
|
2335 |
"\312\376\272\276\0\0\0.\0)\12\0\14\0\26\7\0\27\12\0\14\0\30\12"+ |
|
2336 |
"\0\2\0\31\7\0\32\12\0\5\0\33\12\0\5\0\34\12\0\5\0\35\12\0\2\0"+ |
|
2337 |
"\36\12\0\14\0\37\7\0\40\7\0!\1\0\6<init>\1\0\3()V\1\0\4Code\1"+ |
|
2338 |
"\0\7_invoke\1\0K(Lorg/omg/CORBA/portable/OutputStream;)Lorg/o"+ |
|
2339 |
"mg/CORBA/portable/InputStream;\1\0\12Exceptions\7\0\"\1\0\15_"+ |
|
2340 |
"releaseReply\1\0'(Lorg/omg/CORBA/portable/InputStream;)V\14\0"+ |
|
2341 |
"\15\0\16\1\0(com/sun/jmx/remote/internal/PInputStream\14\0\20"+ |
|
2342 |
"\0\21\14\0\15\0\25\1\0+org/omg/CORBA/portable/ApplicationExce"+ |
|
2343 |
"ption\14\0#\0$\14\0%\0&\14\0\15\0'\14\0(\0$\14\0\24\0\25\1\0%"+ |
|
2344 |
"com/sun/jmx/remote/internal/ProxyStub\1\0<org/omg/stub/javax/"+ |
|
2345 |
"management/remote/rmi/_RMIConnection_Stub\1\0)org/omg/CORBA/p"+ |
|
2346 |
"ortable/RemarshalException\1\0\16getInputStream\1\0&()Lorg/om"+ |
|
2347 |
"g/CORBA/portable/InputStream;\1\0\5getId\1\0\24()Ljava/lang/S"+ |
|
2348 |
"tring;\1\09(Ljava/lang/String;Lorg/omg/CORBA/portable/InputSt"+ |
|
2349 |
"ream;)V\1\0\25getProxiedInputStream\0!\0\13\0\14\0\0\0\0\0\3\0"+ |
|
2350 |
"\1\0\15\0\16\0\1\0\17\0\0\0\21\0\1\0\1\0\0\0\5*\267\0\1\261\0"+ |
|
2351 |
"\0\0\0\0\1\0\20\0\21\0\2\0\17\0\0\0;\0\4\0\4\0\0\0'\273\0\2Y*"+ |
|
2352 |
"+\267\0\3\267\0\4\260M\273\0\2Y,\266\0\6\267\0\4N\273\0\5Y,\266"+ |
|
2353 |
"\0\7-\267\0\10\277\0\1\0\0\0\14\0\15\0\5\0\0\0\22\0\0\0\6\0\2"+ |
|
2354 |
"\0\5\0\23\0\1\0\24\0\25\0\1\0\17\0\0\0\36\0\2\0\2\0\0\0\22+\306"+ |
|
2355 |
"\0\13+\300\0\2\266\0\11L*+\267\0\12\261\0\0\0\0\0\0"; |
|
2356 |
final String pInputStreamByteCodeString = |
|
2357 |
"\312\376\272\276\0\0\0.\0\36\12\0\7\0\17\11\0\6\0\20\12\0\21\0"+ |
|
2358 |
"\22\12\0\6\0\23\12\0\24\0\25\7\0\26\7\0\27\1\0\6<init>\1\0'(L"+ |
|
2359 |
"org/omg/CORBA/portable/InputStream;)V\1\0\4Code\1\0\10read_an"+ |
|
2360 |
"y\1\0\25()Lorg/omg/CORBA/Any;\1\0\12read_value\1\0)(Ljava/lan"+ |
|
2361 |
"g/Class;)Ljava/io/Serializable;\14\0\10\0\11\14\0\30\0\31\7\0"+ |
|
2362 |
"\32\14\0\13\0\14\14\0\33\0\34\7\0\35\14\0\15\0\16\1\0(com/sun"+ |
|
2363 |
"/jmx/remote/internal/PInputStream\1\0,com/sun/jmx/remote/inte"+ |
|
2364 |
"rnal/ProxyInputStream\1\0\2in\1\0$Lorg/omg/CORBA/portable/Inp"+ |
|
2365 |
"utStream;\1\0\"org/omg/CORBA/portable/InputStream\1\0\6narrow"+ |
|
2366 |
"\1\0*()Lorg/omg/CORBA_2_3/portable/InputStream;\1\0&org/omg/C"+ |
|
2367 |
"ORBA_2_3/portable/InputStream\0!\0\6\0\7\0\0\0\0\0\3\0\1\0\10"+ |
|
2368 |
"\0\11\0\1\0\12\0\0\0\22\0\2\0\2\0\0\0\6*+\267\0\1\261\0\0\0\0"+ |
|
2369 |
"\0\1\0\13\0\14\0\1\0\12\0\0\0\24\0\1\0\1\0\0\0\10*\264\0\2\266"+ |
|
2370 |
"\0\3\260\0\0\0\0\0\1\0\15\0\16\0\1\0\12\0\0\0\25\0\2\0\2\0\0\0"+ |
|
2371 |
"\11*\266\0\4+\266\0\5\260\0\0\0\0\0\0"; |
|
2372 |
final byte[] proxyStubByteCode = |
|
2373 |
NoCallStackClassLoader.stringToBytes(proxyStubByteCodeString); |
|
2374 |
final byte[] pInputStreamByteCode = |
|
2375 |
NoCallStackClassLoader.stringToBytes(pInputStreamByteCodeString); |
|
2376 |
final String[] classNames={proxyStubClassName, pInputStreamClassName}; |
|
2377 |
final byte[][] byteCodes = {proxyStubByteCode, pInputStreamByteCode}; |
|
2378 |
final String[] otherClassNames = { |
|
2379 |
iiopConnectionStubClassName, |
|
2380 |
ProxyInputStream.class.getName(), |
|
2381 |
}; |
|
2382 |
PrivilegedExceptionAction<Class<?>> action = |
|
2383 |
new PrivilegedExceptionAction<Class<?>>() { |
|
2384 |
public Class<?> run() throws Exception { |
|
2385 |
||
2386 |
Class thisClass = RMIConnector.class; |
|
2387 |
ClassLoader thisLoader = thisClass.getClassLoader(); |
|
2388 |
ProtectionDomain thisProtectionDomain = |
|
2389 |
thisClass.getProtectionDomain(); |
|
2390 |
ClassLoader cl = |
|
2391 |
new NoCallStackClassLoader(classNames, |
|
2392 |
byteCodes, |
|
2393 |
otherClassNames, |
|
2394 |
thisLoader, |
|
2395 |
thisProtectionDomain); |
|
2396 |
return cl.loadClass(proxyStubClassName); |
|
2397 |
} |
|
2398 |
}; |
|
2399 |
Class<?> stubClass; |
|
2400 |
try { |
|
2401 |
stubClass = AccessController.doPrivileged(action); |
|
2402 |
} catch (Exception e) { |
|
2403 |
logger.error("<clinit>", |
|
2404 |
"Unexpected exception making shadow IIOP stub class: "+e); |
|
2405 |
logger.debug("<clinit>",e); |
|
2406 |
stubClass = null; |
|
2407 |
} |
|
2408 |
proxyStubClass = stubClass; |
|
2409 |
} |
|
2410 |
||
2411 |
private static RMIConnection shadowIiopStub(Stub stub) |
|
2412 |
throws InstantiationException, IllegalAccessException { |
|
2413 |
Stub proxyStub = (Stub) proxyStubClass.newInstance(); |
|
2414 |
proxyStub._set_delegate(stub._get_delegate()); |
|
2415 |
return (RMIConnection) proxyStub; |
|
2416 |
} |
|
2417 |
||
2418 |
private static RMIConnection getConnection(RMIServer server, |
|
2419 |
Object credentials, |
|
2420 |
boolean checkStub) |
|
2421 |
throws IOException { |
|
2422 |
RMIConnection c = server.newClient(credentials); |
|
2423 |
if (checkStub) checkStub(c, rmiConnectionImplStubClass); |
|
2424 |
try { |
|
2425 |
if (c.getClass() == rmiConnectionImplStubClass) |
|
2426 |
return shadowJrmpStub((RemoteObject) c); |
|
2427 |
if (c.getClass().getName().equals(iiopConnectionStubClassName)) |
|
2428 |
return shadowIiopStub((Stub) c); |
|
2429 |
logger.trace("getConnection", |
|
2430 |
"Did not wrap " + c.getClass() + " to foil " + |
|
2431 |
"stack search for classes: class loading semantics " + |
|
2432 |
"may be incorrect"); |
|
2433 |
} catch (Exception e) { |
|
2434 |
logger.error("getConnection", |
|
2435 |
"Could not wrap " + c.getClass() + " to foil " + |
|
2436 |
"stack search for classes: class loading semantics " + |
|
2437 |
"may be incorrect: " + e); |
|
2438 |
logger.debug("getConnection",e); |
|
2439 |
// so just return the original stub, which will work for all |
|
2440 |
// but the most exotic class loading situations |
|
2441 |
} |
|
2442 |
return c; |
|
2443 |
} |
|
2444 |
||
2445 |
private static byte[] base64ToByteArray(String s) { |
|
2446 |
int sLen = s.length(); |
|
2447 |
int numGroups = sLen/4; |
|
2448 |
if (4*numGroups != sLen) |
|
2449 |
throw new IllegalArgumentException( |
|
2450 |
"String length must be a multiple of four."); |
|
2451 |
int missingBytesInLastGroup = 0; |
|
2452 |
int numFullGroups = numGroups; |
|
2453 |
if (sLen != 0) { |
|
2454 |
if (s.charAt(sLen-1) == '=') { |
|
2455 |
missingBytesInLastGroup++; |
|
2456 |
numFullGroups--; |
|
2457 |
} |
|
2458 |
if (s.charAt(sLen-2) == '=') |
|
2459 |
missingBytesInLastGroup++; |
|
2460 |
} |
|
2461 |
byte[] result = new byte[3*numGroups - missingBytesInLastGroup]; |
|
2462 |
||
2463 |
// Translate all full groups from base64 to byte array elements |
|
2464 |
int inCursor = 0, outCursor = 0; |
|
2465 |
for (int i=0; i<numFullGroups; i++) { |
|
2466 |
int ch0 = base64toInt(s.charAt(inCursor++)); |
|
2467 |
int ch1 = base64toInt(s.charAt(inCursor++)); |
|
2468 |
int ch2 = base64toInt(s.charAt(inCursor++)); |
|
2469 |
int ch3 = base64toInt(s.charAt(inCursor++)); |
|
2470 |
result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); |
|
2471 |
result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); |
|
2472 |
result[outCursor++] = (byte) ((ch2 << 6) | ch3); |
|
2473 |
} |
|
2474 |
||
2475 |
// Translate partial group, if present |
|
2476 |
if (missingBytesInLastGroup != 0) { |
|
2477 |
int ch0 = base64toInt(s.charAt(inCursor++)); |
|
2478 |
int ch1 = base64toInt(s.charAt(inCursor++)); |
|
2479 |
result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); |
|
2480 |
||
2481 |
if (missingBytesInLastGroup == 1) { |
|
2482 |
int ch2 = base64toInt(s.charAt(inCursor++)); |
|
2483 |
result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); |
|
2484 |
} |
|
2485 |
} |
|
2486 |
// assert inCursor == s.length()-missingBytesInLastGroup; |
|
2487 |
// assert outCursor == result.length; |
|
2488 |
return result; |
|
2489 |
} |
|
2490 |
||
2491 |
/** |
|
2492 |
* Translates the specified character, which is assumed to be in the |
|
2493 |
* "Base 64 Alphabet" into its equivalent 6-bit positive integer. |
|
2494 |
* |
|
2495 |
* @throws IllegalArgumentException if |
|
2496 |
* c is not in the Base64 Alphabet. |
|
2497 |
*/ |
|
2498 |
private static int base64toInt(char c) { |
|
2499 |
int result; |
|
2500 |
||
2501 |
if (c >= base64ToInt.length) |
|
2502 |
result = -1; |
|
2503 |
else |
|
2504 |
result = base64ToInt[c]; |
|
2505 |
||
2506 |
if (result < 0) |
|
2507 |
throw new IllegalArgumentException("Illegal character " + c); |
|
2508 |
return result; |
|
2509 |
} |
|
2510 |
||
2511 |
/** |
|
2512 |
* This array is a lookup table that translates unicode characters |
|
2513 |
* drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) |
|
2514 |
* into their 6-bit positive integer equivalents. Characters that |
|
2515 |
* are not in the Base64 alphabet but fall within the bounds of the |
|
2516 |
* array are translated to -1. |
|
2517 |
*/ |
|
2518 |
private static final byte base64ToInt[] = { |
|
2519 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
2520 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
|
2521 |
-1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, |
|
2522 |
55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, |
|
2523 |
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
|
2524 |
24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
|
2525 |
35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
|
2526 |
}; |
|
2527 |
||
2528 |
//-------------------------------------------------------------------- |
|
2529 |
// Private stuff - Find / Set default class loader |
|
2530 |
//-------------------------------------------------------------------- |
|
2531 |
private ClassLoader pushDefaultClassLoader() { |
|
2532 |
final Thread t = Thread.currentThread(); |
|
2533 |
final ClassLoader old = t.getContextClassLoader(); |
|
2534 |
if (defaultClassLoader != null) |
|
2535 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
2536 |
public Void run() { |
|
2537 |
t.setContextClassLoader(defaultClassLoader); |
|
2538 |
return null; |
|
2539 |
} |
|
2540 |
}); |
|
2541 |
return old; |
|
2542 |
} |
|
2543 |
||
2544 |
private void popDefaultClassLoader(final ClassLoader old) { |
|
2545 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
2546 |
public Void run() { |
|
2547 |
Thread.currentThread().setContextClassLoader(old); |
|
2548 |
return null; |
|
2549 |
} |
|
2550 |
}); |
|
2551 |
} |
|
2552 |
||
2553 |
//-------------------------------------------------------------------- |
|
2554 |
// Private variables |
|
2555 |
//-------------------------------------------------------------------- |
|
2556 |
/** |
|
2557 |
* @serial The RMIServer stub of the RMI JMX Connector server to |
|
2558 |
* which this client connector is (or will be) connected. This |
|
2559 |
* field can be null when <var>jmxServiceURL</var> is not |
|
2560 |
* null. This includes the case where <var>jmxServiceURL</var> |
|
2561 |
* contains a serialized RMIServer stub. If both |
|
2562 |
* <var>rmiServer</var> and <var>jmxServiceURL</var> are null then |
|
2563 |
* serialization will fail. |
|
2564 |
* |
|
2565 |
* @see #RMIConnector(RMIServer,Map) |
|
2566 |
**/ |
|
2567 |
private final RMIServer rmiServer; |
|
2568 |
||
2569 |
/** |
|
2570 |
* @serial The JMXServiceURL of the RMI JMX Connector server to |
|
2571 |
* which this client connector will be connected. This field can |
|
2572 |
* be null when <var>rmiServer</var> is not null. If both |
|
2573 |
* <var>rmiServer</var> and <var>jmxServiceURL</var> are null then |
|
2574 |
* serialization will fail. |
|
2575 |
* |
|
2576 |
* @see #RMIConnector(JMXServiceURL,Map) |
|
2577 |
**/ |
|
2578 |
private final JMXServiceURL jmxServiceURL; |
|
2579 |
||
2580 |
// --------------------------------------------------------- |
|
2581 |
// WARNING - WARNING - WARNING - WARNING - WARNING - WARNING |
|
2582 |
// --------------------------------------------------------- |
|
2583 |
// Any transient variable which needs to be initialized should |
|
2584 |
// be initialized in the method initTransient() |
|
2585 |
private transient Map<String, Object> env; |
|
2586 |
private transient ClassLoader defaultClassLoader; |
|
2587 |
private transient RMIConnection connection; |
|
2588 |
private transient String connectionId; |
|
2589 |
||
2590 |
private transient long clientNotifSeqNo = 0; |
|
2591 |
||
2592 |
private transient WeakHashMap<Subject, MBeanServerConnection> rmbscMap; |
|
2593 |
||
2594 |
private transient RMINotifClient rmiNotifClient; |
|
2595 |
// = new RMINotifClient(new Integer(0)); |
|
2596 |
||
2597 |
private transient long clientNotifCounter = 0; |
|
2598 |
||
2599 |
private transient boolean connected; |
|
2600 |
// = false; |
|
2601 |
private transient boolean terminated; |
|
2602 |
// = false; |
|
2603 |
||
2604 |
private transient Exception closeException; |
|
2605 |
||
2606 |
private transient NotificationBroadcasterSupport connectionBroadcaster; |
|
2607 |
||
2608 |
private transient ClientCommunicatorAdmin communicatorAdmin; |
|
2609 |
||
1004 | 2610 |
private boolean eventServiceEnabled; |
2611 |
// private transient EventRelay eventRelay; |
|
2612 |
||
2613 |
private transient NotificationListener lostNotifListener; |
|
2614 |
||
2 | 2615 |
/** |
2616 |
* A static WeakReference to an {@link org.omg.CORBA.ORB ORB} to |
|
2617 |
* connect unconnected stubs. |
|
2618 |
**/ |
|
526
61ba2d5ea9da
6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents:
2
diff
changeset
|
2619 |
private static volatile WeakReference<ORB> orb = null; |
2 | 2620 |
|
2621 |
// TRACES & DEBUG |
|
2622 |
//--------------- |
|
2623 |
private static String objects(final Object[] objs) { |
|
2624 |
if (objs == null) |
|
2625 |
return "null"; |
|
2626 |
else |
|
2627 |
return Arrays.asList(objs).toString(); |
|
2628 |
} |
|
2629 |
||
2630 |
private static String strings(final String[] strs) { |
|
2631 |
return objects(strs); |
|
2632 |
} |
|
1004 | 2633 |
|
2634 |
private static final PerThreadGroupPool<ThreadPoolExecutor> listenerDispatchThreadPool = |
|
2635 |
PerThreadGroupPool.make(); |
|
2 | 2636 |
} |