KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > remote > rmi > RMIConnectionImpl


1 /*
2  * @(#)RMIConnectionImpl.java 1.88 06/10/23
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management.remote.rmi;
9
10 import java.io.InterruptedIOException JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.Serializable JavaDoc;
13 import java.rmi.MarshalledObject JavaDoc;
14 import java.rmi.server.RMIClientSocketFactory JavaDoc;
15 import java.rmi.server.RMIServerSocketFactory JavaDoc;
16 import java.rmi.server.UnicastRemoteObject JavaDoc;
17 import java.rmi.server.Unreferenced JavaDoc;
18 import java.rmi.NoSuchObjectException JavaDoc;
19 import java.security.AccessControlContext JavaDoc;
20 import java.security.AccessController JavaDoc;
21 import java.security.Principal JavaDoc;
22 import java.security.PrivilegedAction JavaDoc;
23 import java.security.PrivilegedActionException JavaDoc;
24 import java.security.PrivilegedExceptionAction JavaDoc;
25 import java.security.ProtectionDomain JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.rmi.UnmarshalException JavaDoc;
32
33 import javax.management.Attribute JavaDoc;
34 import javax.management.AttributeList JavaDoc;
35 import javax.management.AttributeNotFoundException JavaDoc;
36 import javax.management.InstanceAlreadyExistsException JavaDoc;
37 import javax.management.InstanceNotFoundException JavaDoc;
38 import javax.management.IntrospectionException JavaDoc;
39 import javax.management.InvalidAttributeValueException JavaDoc;
40 import javax.management.ListenerNotFoundException JavaDoc;
41 import javax.management.MalformedObjectNameException JavaDoc;
42 import javax.management.MBeanException JavaDoc;
43 import javax.management.MBeanInfo JavaDoc;
44 import javax.management.MBeanRegistrationException JavaDoc;
45 import javax.management.MBeanServer JavaDoc;
46 import javax.management.NotCompliantMBeanException JavaDoc;
47 import javax.management.Notification JavaDoc;
48 import javax.management.NotificationFilter JavaDoc;
49 import javax.management.NotificationListener JavaDoc;
50 import javax.management.ObjectInstance JavaDoc;
51 import javax.management.ObjectName JavaDoc;
52 import javax.management.QueryExp JavaDoc;
53 import javax.management.ReflectionException JavaDoc;
54 import javax.management.RuntimeOperationsException JavaDoc;
55 import javax.management.loading.ClassLoaderRepository JavaDoc;
56 import javax.management.remote.NotificationResult JavaDoc;
57 import javax.management.remote.SubjectDelegationPermission JavaDoc;
58 import javax.management.remote.TargetedNotification JavaDoc;
59 import javax.management.remote.JMXServerErrorException JavaDoc;
60
61 import javax.security.auth.Subject JavaDoc;
62
63 import com.sun.jmx.remote.internal.ServerNotifForwarder;
64 import com.sun.jmx.remote.internal.ServerCommunicatorAdmin;
65 import com.sun.jmx.remote.internal.Unmarshal;
66 import com.sun.jmx.remote.security.JMXSubjectDomainCombiner;
67 import com.sun.jmx.remote.security.SubjectDelegator;
68 import com.sun.jmx.remote.util.CacheMap;
69 import com.sun.jmx.remote.util.ClassLoaderWithRepository;
70 import com.sun.jmx.remote.util.ClassLogger;
71 import com.sun.jmx.remote.util.EnvHelp;
72 import com.sun.jmx.remote.util.OrderClassLoaders;
73
74 /**
75  * <p>Implementation of the {@link RMIConnection} interface. User
76  * code will not usually reference this class.</p>
77  *
78  * @since 1.5
79  * @since.unbundled 1.0
80  */

81 public class RMIConnectionImpl implements RMIConnection JavaDoc, Unreferenced JavaDoc {
82
83     /**
84      * Constructs a new {@link RMIConnection}. This connection can be
85      * used with either the JRMP or IIOP transport. This object does
86      * not export itself: it is the responsibility of the caller to
87      * export it appropriately (see {@link
88      * RMIJRMPServerImpl#makeClient(String,Subject)} and {@link
89      * RMIIIOPServerImpl#makeClient(String,Subject)}.
90      *
91      * @param rmiServer The RMIServerImpl object for which this
92      * connection is created. The behavior is unspecified if this
93      * parameter is null.
94      * @param connectionId The ID for this connection. The behavior
95      * is unspecified if this parameter is null.
96      * @param defaultClassLoader The default ClassLoader to be used
97      * when deserializing marshalled objects. Can be null, to signify
98      * the bootstrap class loader.
99      * @param subject the authenticated subject to be used for
100      * authorization. Can be null, to signify that no subject has
101      * been authenticated.
102      * @param env the environment containing attributes for the new
103      * <code>RMIServerImpl</code>. Can be null, equivalent to an
104      * empty map.
105      */

106     public RMIConnectionImpl(RMIServerImpl JavaDoc rmiServer,
107                              String JavaDoc connectionId,
108                              ClassLoader JavaDoc defaultClassLoader,
109                              Subject JavaDoc subject,
110                  Map JavaDoc<String JavaDoc,?> env) {
111     if (rmiServer == null || connectionId == null)
112         throw new NullPointerException JavaDoc("Illegal null argument");
113     if (env == null)
114         env = Collections.EMPTY_MAP;
115         this.rmiServer = rmiServer;
116         this.connectionId = connectionId;
117         this.defaultClassLoader = defaultClassLoader;
118
119         this.subjectDelegator = new SubjectDelegator();
120         this.subject = subject;
121         if (subject == null) {
122             this.acc = null;
123         } else {
124             this.acc = JMXSubjectDomainCombiner.getContext(subject);
125         }
126         this.mbeanServer = rmiServer.getMBeanServer();
127
128         final ClassLoader JavaDoc dcl = defaultClassLoader;
129         this.classLoaderWithRepository = (ClassLoaderWithRepository)
130             AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
131                     public Object JavaDoc run() {
132                         return new ClassLoaderWithRepository(
133                                               getClassLoaderRepository(),
134                                               dcl);
135                     }
136                 });
137
138     serverCommunicatorAdmin = new
139       RMIServerCommunicatorAdmin(EnvHelp.getServerConnectionTimeout(env));
140
141     this.env = env;
142     }
143     
144     private synchronized ServerNotifForwarder getServerNotifFwd() {
145     // Lazily created when first use. Mainly when
146
// addNotificationListener is first called.
147
if(serverNotifForwarder == null)
148         serverNotifForwarder =
149         new ServerNotifForwarder(mbeanServer,
150                      env,
151                      rmiServer.getNotifBuffer());
152     
153     return serverNotifForwarder;
154     }
155
156     public String JavaDoc getConnectionId() throws IOException JavaDoc {
157     // We should call reqIncomming() here... shouldn't we?
158
return connectionId;
159     }
160
161     public void close() throws IOException JavaDoc {
162         final boolean debug = logger.debugOn();
163         final String JavaDoc idstr = (debug?"["+this.toString()+"]":null);
164
165     synchronized(this) {
166             if (terminated) {
167                 if (debug) logger.debug("close",idstr + " already terminated.");
168                 return;
169             }
170
171             if (debug) logger.debug("close",idstr + " closing.");
172
173             terminated = true;
174
175         if (serverCommunicatorAdmin != null) {
176             serverCommunicatorAdmin.terminate();
177         }
178
179         if (serverNotifForwarder != null) {
180             serverNotifForwarder.terminate();
181         }
182     }
183
184         rmiServer.clientClosed(this);
185
186         if (debug) logger.debug("close",idstr + " closed.");
187     }
188
189     public void unreferenced() {
190         logger.debug("unreferenced", "called");
191         try {
192             close();
193             logger.debug("unreferenced", "done");
194         } catch (IOException JavaDoc e) {
195             logger.fine("unreferenced", e);
196         }
197     }
198
199     //-------------------------------------------------------------------------
200
// MBeanServerConnection Wrapper
201
//-------------------------------------------------------------------------
202

203     public ObjectInstance JavaDoc createMBean(String JavaDoc className,
204                                       ObjectName JavaDoc name,
205                                       Subject JavaDoc delegationSubject)
206         throws
207         ReflectionException JavaDoc,
208         InstanceAlreadyExistsException JavaDoc,
209         MBeanRegistrationException JavaDoc,
210         MBeanException JavaDoc,
211         NotCompliantMBeanException JavaDoc,
212         IOException JavaDoc {
213         try {
214             final Object JavaDoc params[] =
215                 new Object JavaDoc[] { className, name };
216
217             if (logger.debugOn())
218         logger.debug("createMBean(String,ObjectName)",
219                  "connectionId=" + connectionId +", className=" +
220                  className+", name=" + name);
221
222             return (ObjectInstance JavaDoc)
223                 doPrivilegedOperation(
224                   CREATE_MBEAN,
225                   params,
226                   delegationSubject);
227         } catch (PrivilegedActionException JavaDoc pe) {
228             Exception JavaDoc e = extractException(pe);
229             if (e instanceof ReflectionException JavaDoc)
230                 throw (ReflectionException JavaDoc) e;
231             if (e instanceof InstanceAlreadyExistsException JavaDoc)
232                 throw (InstanceAlreadyExistsException JavaDoc) e;
233             if (e instanceof MBeanRegistrationException JavaDoc)
234                 throw (MBeanRegistrationException JavaDoc) e;
235             if (e instanceof MBeanException JavaDoc)
236                 throw (MBeanException JavaDoc) e;
237             if (e instanceof NotCompliantMBeanException JavaDoc)
238                 throw (NotCompliantMBeanException JavaDoc) e;
239             if (e instanceof IOException JavaDoc)
240                 throw (IOException JavaDoc) e;
241             throw newIOException("Got unexpected server exception: " + e, e);
242         }
243     }
244
245     public ObjectInstance JavaDoc createMBean(String JavaDoc className,
246                                       ObjectName JavaDoc name,
247                                       ObjectName JavaDoc loaderName,
248                                       Subject JavaDoc delegationSubject)
249         throws
250         ReflectionException JavaDoc,
251         InstanceAlreadyExistsException JavaDoc,
252         MBeanRegistrationException JavaDoc,
253         MBeanException JavaDoc,
254         NotCompliantMBeanException JavaDoc,
255         InstanceNotFoundException JavaDoc,
256         IOException JavaDoc {
257         try {
258             final Object JavaDoc params[] =
259                 new Object JavaDoc[] { className, name, loaderName };
260
261             if (logger.debugOn())
262         logger.debug("createMBean(String,ObjectName,ObjectName)",
263               "connectionId=" + connectionId
264               +", className=" + className
265               +", name=" + name
266               +", loaderName=" + loaderName);
267
268             return (ObjectInstance JavaDoc)
269                 doPrivilegedOperation(
270                   CREATE_MBEAN_LOADER,
271                   params,
272                   delegationSubject);
273         } catch (PrivilegedActionException JavaDoc pe) {
274             Exception JavaDoc e = extractException(pe);
275             if (e instanceof ReflectionException JavaDoc)
276                 throw (ReflectionException JavaDoc) e;
277             if (e instanceof InstanceAlreadyExistsException JavaDoc)
278                 throw (InstanceAlreadyExistsException JavaDoc) e;
279             if (e instanceof MBeanRegistrationException JavaDoc)
280                 throw (MBeanRegistrationException JavaDoc) e;
281             if (e instanceof MBeanException JavaDoc)
282                 throw (MBeanException JavaDoc) e;
283             if (e instanceof NotCompliantMBeanException JavaDoc)
284                 throw (NotCompliantMBeanException JavaDoc) e;
285             if (e instanceof InstanceNotFoundException JavaDoc)
286                 throw (InstanceNotFoundException JavaDoc) e;
287             if (e instanceof IOException JavaDoc)
288                 throw (IOException JavaDoc) e;
289             throw newIOException("Got unexpected server exception: " + e, e);
290         }
291     }
292
293     public ObjectInstance JavaDoc createMBean(String JavaDoc className,
294                                       ObjectName JavaDoc name,
295                                       MarshalledObject JavaDoc params,
296                                       String JavaDoc signature[],
297                                       Subject JavaDoc delegationSubject)
298         throws
299         ReflectionException JavaDoc,
300         InstanceAlreadyExistsException JavaDoc,
301         MBeanRegistrationException JavaDoc,
302         MBeanException JavaDoc,
303         NotCompliantMBeanException JavaDoc,
304         IOException JavaDoc {
305
306         final Object JavaDoc[] values;
307         final boolean debug = logger.debugOn();
308
309     if (debug) logger.debug(
310           "createMBean(String,ObjectName,Object[],String[])",
311                   "connectionId=" + connectionId
312                   +", unwrapping parameters using classLoaderWithRepository.");
313
314     values = nullIsEmpty((Object JavaDoc[]) unwrap(params,
315                            classLoaderWithRepository));
316
317         try {
318             final Object JavaDoc params2[] =
319                 new Object JavaDoc[] { className, name, values,
320                    nullIsEmpty(signature) };
321
322             if (debug)
323                logger.debug("createMBean(String,ObjectName,Object[],String[])",
324                              "connectionId=" + connectionId
325                              +", className=" + className
326                              +", name=" + name
327                              +", params=" + objects(values)
328                              +", signature=" + strings(signature));
329
330             return (ObjectInstance JavaDoc)
331                 doPrivilegedOperation(
332                   CREATE_MBEAN_PARAMS,
333                   params2,
334                   delegationSubject);
335         } catch (PrivilegedActionException JavaDoc pe) {
336             Exception JavaDoc e = extractException(pe);
337             if (e instanceof ReflectionException JavaDoc)
338                 throw (ReflectionException JavaDoc) e;
339             if (e instanceof InstanceAlreadyExistsException JavaDoc)
340                 throw (InstanceAlreadyExistsException JavaDoc) e;
341             if (e instanceof MBeanRegistrationException JavaDoc)
342                 throw (MBeanRegistrationException JavaDoc) e;
343             if (e instanceof MBeanException JavaDoc)
344                 throw (MBeanException JavaDoc) e;
345             if (e instanceof NotCompliantMBeanException JavaDoc)
346                 throw (NotCompliantMBeanException JavaDoc) e;
347             if (e instanceof IOException JavaDoc)
348                 throw (IOException JavaDoc) e;
349             throw newIOException("Got unexpected server exception: " + e, e);
350         }
351     }
352
353     public ObjectInstance JavaDoc createMBean(String JavaDoc className,
354                                       ObjectName JavaDoc name,
355                                       ObjectName JavaDoc loaderName,
356                                       MarshalledObject JavaDoc params,
357                                       String JavaDoc signature[],
358                                       Subject JavaDoc delegationSubject)
359         throws
360         ReflectionException JavaDoc,
361         InstanceAlreadyExistsException JavaDoc,
362         MBeanRegistrationException JavaDoc,
363         MBeanException JavaDoc,
364         NotCompliantMBeanException JavaDoc,
365         InstanceNotFoundException JavaDoc,
366         IOException JavaDoc {
367
368         final Object JavaDoc[] values;
369         final boolean debug = logger.debugOn();
370
371     if (debug) logger.debug(
372          "createMBean(String,ObjectName,ObjectName,Object[],String[])",
373                  "connectionId=" + connectionId
374                  +", unwrapping params with MBean extended ClassLoader.");
375
376     values = nullIsEmpty((Object JavaDoc[]) unwrap(params,
377                            getClassLoader(loaderName),
378                            defaultClassLoader));
379
380         try {
381             final Object JavaDoc params2[] =
382                new Object JavaDoc[] { className, name, loaderName, values,
383                   nullIsEmpty(signature) };
384
385            if (debug) logger.debug(
386          "createMBean(String,ObjectName,ObjectName,Object[],String[])",
387                  "connectionId=" + connectionId
388                  +", className=" + className
389                  +", name=" + name
390                  +", loaderName=" + loaderName
391                  +", params=" + objects(values)
392                  +", signature=" + strings(signature));
393
394             return (ObjectInstance JavaDoc)
395                 doPrivilegedOperation(
396                   CREATE_MBEAN_LOADER_PARAMS,
397                   params2,
398                   delegationSubject);
399         } catch (PrivilegedActionException JavaDoc pe) {
400             Exception JavaDoc e = extractException(pe);
401             if (e instanceof ReflectionException JavaDoc)
402                 throw (ReflectionException JavaDoc) e;
403             if (e instanceof InstanceAlreadyExistsException JavaDoc)
404                 throw (InstanceAlreadyExistsException JavaDoc) e;
405             if (e instanceof MBeanRegistrationException JavaDoc)
406                 throw (MBeanRegistrationException JavaDoc) e;
407             if (e instanceof MBeanException JavaDoc)
408                 throw (MBeanException JavaDoc) e;
409             if (e instanceof NotCompliantMBeanException JavaDoc)
410                 throw (NotCompliantMBeanException JavaDoc) e;
411             if (e instanceof InstanceNotFoundException JavaDoc)
412                 throw (InstanceNotFoundException JavaDoc) e;
413             if (e instanceof IOException JavaDoc)
414                 throw (IOException JavaDoc) e;
415             throw newIOException("Got unexpected server exception: " + e, e);
416         }
417     }
418
419     public void unregisterMBean(ObjectName JavaDoc name, Subject JavaDoc delegationSubject)
420         throws
421         InstanceNotFoundException JavaDoc,
422         MBeanRegistrationException JavaDoc,
423         IOException JavaDoc {
424         try {
425             final Object JavaDoc params[] = new Object JavaDoc[] { name };
426
427             if (logger.debugOn()) logger.debug("unregisterMBean",
428                  "connectionId=" + connectionId
429                  +", name="+name);
430
431             doPrivilegedOperation(
432               UNREGISTER_MBEAN,
433               params,
434               delegationSubject);
435         } catch (PrivilegedActionException JavaDoc pe) {
436             Exception JavaDoc e = extractException(pe);
437             if (e instanceof InstanceNotFoundException JavaDoc)
438                 throw (InstanceNotFoundException JavaDoc) e;
439             if (e instanceof MBeanRegistrationException JavaDoc)
440                 throw (MBeanRegistrationException JavaDoc) e;
441             if (e instanceof IOException JavaDoc)
442                 throw (IOException JavaDoc) e;
443             throw newIOException("Got unexpected server exception: " + e, e);
444         }
445     }
446
447     public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc name,
448                                             Subject JavaDoc delegationSubject)
449         throws
450         InstanceNotFoundException JavaDoc,
451         IOException JavaDoc {
452
453     checkNonNull("ObjectName", name);
454
455         try {
456             final Object JavaDoc params[] = new Object JavaDoc[] { name };
457
458             if (logger.debugOn()) logger.debug("getObjectInstance",
459                  "connectionId=" + connectionId
460                  +", name="+name);
461
462             return (ObjectInstance JavaDoc)
463                 doPrivilegedOperation(
464                   GET_OBJECT_INSTANCE,
465                   params,
466                   delegationSubject);
467         } catch (PrivilegedActionException JavaDoc pe) {
468             Exception JavaDoc e = extractException(pe);
469             if (e instanceof InstanceNotFoundException JavaDoc)
470                 throw (InstanceNotFoundException JavaDoc) e;
471             if (e instanceof IOException JavaDoc)
472                 throw (IOException JavaDoc) e;
473             throw newIOException("Got unexpected server exception: " + e, e);
474         }
475     }
476
477     public Set JavaDoc<ObjectInstance JavaDoc>
478     queryMBeans(ObjectName JavaDoc name,
479             MarshalledObject JavaDoc query,
480             Subject JavaDoc delegationSubject)
481         throws IOException JavaDoc {
482         final QueryExp JavaDoc queryValue;
483         final boolean debug=logger.debugOn();
484
485     if (debug) logger.debug("queryMBeans",
486                  "connectionId=" + connectionId
487                  +" unwrapping query with defaultClassLoader.");
488
489     queryValue = (QueryExp JavaDoc) unwrap(query, defaultClassLoader);
490
491         try {
492             final Object JavaDoc params[] = new Object JavaDoc[] { name, queryValue };
493
494             if (debug) logger.debug("queryMBeans",
495                  "connectionId=" + connectionId
496                  +", name="+name +", query="+query);
497
498             return (Set JavaDoc)
499                 doPrivilegedOperation(
500                   QUERY_MBEANS,
501                   params,
502                   delegationSubject);
503         } catch (PrivilegedActionException JavaDoc pe) {
504             Exception JavaDoc e = extractException(pe);
505             if (e instanceof IOException JavaDoc)
506                 throw (IOException JavaDoc) e;
507             throw newIOException("Got unexpected server exception: " + e, e);
508         }
509     }
510
511     public Set JavaDoc<ObjectName JavaDoc>
512     queryNames(ObjectName JavaDoc name,
513            MarshalledObject JavaDoc query,
514            Subject JavaDoc delegationSubject)
515         throws IOException JavaDoc {
516         final QueryExp JavaDoc queryValue;
517         final boolean debug=logger.debugOn();
518
519     if (debug) logger.debug("queryNames",
520                  "connectionId=" + connectionId
521                  +" unwrapping query with defaultClassLoader.");
522
523     queryValue = (QueryExp JavaDoc) unwrap(query, defaultClassLoader);
524
525         try {
526             final Object JavaDoc params[] = new Object JavaDoc[] { name, queryValue };
527
528             if (debug) logger.debug("queryNames",
529                  "connectionId=" + connectionId
530                  +", name="+name +", query="+query);
531
532             return (Set JavaDoc)
533                 doPrivilegedOperation(
534                   QUERY_NAMES,
535                   params,
536                   delegationSubject);
537         } catch (PrivilegedActionException JavaDoc pe) {
538             Exception JavaDoc e = extractException(pe);
539             if (e instanceof IOException JavaDoc)
540                 throw (IOException JavaDoc) e;
541             throw newIOException("Got unexpected server exception: " + e, e);
542         }
543     }
544
545     public boolean isRegistered(ObjectName JavaDoc name,
546                                 Subject JavaDoc delegationSubject) throws IOException JavaDoc {
547         try {
548             final Object JavaDoc params[] = new Object JavaDoc[] { name };
549             return ((Boolean JavaDoc)
550                 doPrivilegedOperation(
551                   IS_REGISTERED,
552                   params,
553                   delegationSubject)).booleanValue();
554         } catch (PrivilegedActionException JavaDoc pe) {
555             Exception JavaDoc e = extractException(pe);
556             if (e instanceof IOException JavaDoc)
557                 throw (IOException JavaDoc) e;
558             throw newIOException("Got unexpected server exception: " + e, e);
559         }
560     }
561
562     public Integer JavaDoc getMBeanCount(Subject JavaDoc delegationSubject)
563         throws IOException JavaDoc {
564         try {
565             final Object JavaDoc params[] = new Object JavaDoc[] { };
566
567             if (logger.debugOn()) logger.debug("getMBeanCount",
568                  "connectionId=" + connectionId);
569
570             return (Integer JavaDoc)
571                 doPrivilegedOperation(
572                   GET_MBEAN_COUNT,
573                   params,
574                   delegationSubject);
575         } catch (PrivilegedActionException JavaDoc pe) {
576             Exception JavaDoc e = extractException(pe);
577             if (e instanceof IOException JavaDoc)
578                 throw (IOException JavaDoc) e;
579             throw newIOException("Got unexpected server exception: " + e, e);
580         }
581     }
582
583     public Object JavaDoc getAttribute(ObjectName JavaDoc name,
584                                String JavaDoc attribute,
585                                Subject JavaDoc delegationSubject)
586         throws
587         MBeanException JavaDoc,
588         AttributeNotFoundException JavaDoc,
589         InstanceNotFoundException JavaDoc,
590         ReflectionException JavaDoc,
591         IOException JavaDoc {
592         try {
593             final Object JavaDoc params[] = new Object JavaDoc[] { name, attribute };
594             if (logger.debugOn()) logger.debug("getAttribute",
595                                    "connectionId=" + connectionId
596                                    +", name=" + name
597                                    +", attribute="+ attribute);
598
599             return (Object JavaDoc)
600                 doPrivilegedOperation(
601                   GET_ATTRIBUTE,
602                   params,
603                   delegationSubject);
604         } catch (PrivilegedActionException JavaDoc pe) {
605             Exception JavaDoc e = extractException(pe);
606             if (e instanceof MBeanException JavaDoc)
607                 throw (MBeanException JavaDoc) e;
608             if (e instanceof AttributeNotFoundException JavaDoc)
609                 throw (AttributeNotFoundException JavaDoc) e;
610             if (e instanceof InstanceNotFoundException JavaDoc)
611                 throw (InstanceNotFoundException JavaDoc) e;
612             if (e instanceof ReflectionException JavaDoc)
613                 throw (ReflectionException JavaDoc) e;
614             if (e instanceof IOException JavaDoc)
615                 throw (IOException JavaDoc) e;
616             throw newIOException("Got unexpected server exception: " + e, e);
617         }
618     }
619
620     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name,
621                                        String JavaDoc[] attributes,
622                                        Subject JavaDoc delegationSubject)
623         throws
624         InstanceNotFoundException JavaDoc,
625         ReflectionException JavaDoc,
626         IOException JavaDoc {
627         try {
628             final Object JavaDoc params[] = new Object JavaDoc[] { name, attributes };
629
630             if (logger.debugOn()) logger.debug("getAttributes",
631                                    "connectionId=" + connectionId
632                                    +", name=" + name
633                                    +", attributes="+ strings(attributes));
634
635             return (AttributeList JavaDoc)
636                 doPrivilegedOperation(
637                   GET_ATTRIBUTES,
638                   params,
639                   delegationSubject);
640         } catch (PrivilegedActionException JavaDoc pe) {
641             Exception JavaDoc e = extractException(pe);
642             if (e instanceof InstanceNotFoundException JavaDoc)
643                 throw (InstanceNotFoundException JavaDoc) e;
644             if (e instanceof ReflectionException JavaDoc)
645                 throw (ReflectionException JavaDoc) e;
646             if (e instanceof IOException JavaDoc)
647                 throw (IOException JavaDoc) e;
648             throw newIOException("Got unexpected server exception: " + e, e);
649         }
650     }
651
652     public void setAttribute(ObjectName JavaDoc name,
653                              MarshalledObject JavaDoc attribute,
654                              Subject JavaDoc delegationSubject)
655         throws
656         InstanceNotFoundException JavaDoc,
657         AttributeNotFoundException JavaDoc,
658         InvalidAttributeValueException JavaDoc,
659         MBeanException JavaDoc,
660         ReflectionException JavaDoc,
661         IOException JavaDoc {
662         final Attribute JavaDoc attr;
663         final boolean debug=logger.debugOn();
664
665     if (debug) logger.debug("setAttribute",
666                  "connectionId=" + connectionId
667                  +" unwrapping attribute with MBean extended ClassLoader.");
668
669     attr = (Attribute JavaDoc) unwrap(attribute,
670                                       getClassLoaderFor(name),
671                                       defaultClassLoader);
672
673         try {
674             final Object JavaDoc params[] = new Object JavaDoc[] { name, attr };
675
676             if (debug) logger.debug("setAttribute",
677                              "connectionId=" + connectionId
678                              +", name="+name
679                              +", attribute="+attr);
680
681             doPrivilegedOperation(
682               SET_ATTRIBUTE,
683               params,
684               delegationSubject);
685         } catch (PrivilegedActionException JavaDoc pe) {
686             Exception JavaDoc e = extractException(pe);
687             if (e instanceof InstanceNotFoundException JavaDoc)
688                 throw (InstanceNotFoundException JavaDoc) e;
689             if (e instanceof AttributeNotFoundException JavaDoc)
690                 throw (AttributeNotFoundException JavaDoc) e;
691             if (e instanceof InvalidAttributeValueException JavaDoc)
692                 throw (InvalidAttributeValueException JavaDoc) e;
693             if (e instanceof MBeanException JavaDoc)
694                 throw (MBeanException JavaDoc) e;
695             if (e instanceof ReflectionException JavaDoc)
696                 throw (ReflectionException JavaDoc) e;
697             if (e instanceof IOException JavaDoc)
698                 throw (IOException JavaDoc) e;
699             throw newIOException("Got unexpected server exception: " + e, e);
700         }
701     }
702
703     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name,
704                                        MarshalledObject JavaDoc attributes,
705                                        Subject JavaDoc delegationSubject)
706         throws
707         InstanceNotFoundException JavaDoc,
708         ReflectionException JavaDoc,
709         IOException JavaDoc {
710         final AttributeList JavaDoc attrlist;
711         final boolean debug=logger.debugOn();
712
713     if (debug) logger.debug("setAttributes",
714                  "connectionId=" + connectionId
715                  +" unwrapping attributes with MBean extended ClassLoader.");
716
717     attrlist =
718         (AttributeList JavaDoc) unwrap(attributes,
719                                        getClassLoaderFor(name),
720                                        defaultClassLoader);
721
722         try {
723             final Object JavaDoc params[] = new Object JavaDoc[] { name, attrlist };
724
725             if (debug) logger.debug("setAttributes",
726                              "connectionId=" + connectionId
727                              +", name="+name
728                              +", attributes="+attrlist);
729
730             return (AttributeList JavaDoc)
731                 doPrivilegedOperation(
732                   SET_ATTRIBUTES,
733                   params,
734                   delegationSubject);
735         } catch (PrivilegedActionException JavaDoc pe) {
736             Exception JavaDoc e = extractException(pe);
737             if (e instanceof InstanceNotFoundException JavaDoc)
738                 throw (InstanceNotFoundException JavaDoc) e;
739             if (e instanceof ReflectionException JavaDoc)
740                 throw (ReflectionException JavaDoc) e;
741             if (e instanceof IOException JavaDoc)
742                 throw (IOException JavaDoc) e;
743             throw newIOException("Got unexpected server exception: " + e, e);
744         }
745     }
746
747     public Object JavaDoc invoke(ObjectName JavaDoc name,
748                          String JavaDoc operationName,
749                          MarshalledObject JavaDoc params,
750                          String JavaDoc signature[],
751                          Subject JavaDoc delegationSubject)
752         throws
753         InstanceNotFoundException JavaDoc,
754         MBeanException JavaDoc,
755         ReflectionException JavaDoc,
756         IOException JavaDoc {
757
758     checkNonNull("ObjectName", name);
759     checkNonNull("Operation name", operationName);
760
761         final Object JavaDoc[] values;
762         final boolean debug=logger.debugOn();
763
764     if (debug) logger.debug("invoke",
765                  "connectionId=" + connectionId
766                  +" unwrapping params with MBean extended ClassLoader.");
767
768     values = nullIsEmpty((Object JavaDoc[]) unwrap(params,
769                            getClassLoaderFor(name),
770                            defaultClassLoader));
771
772         try {
773             final Object JavaDoc params2[] =
774                 new Object JavaDoc[] { name, operationName, values,
775                    nullIsEmpty(signature) };
776
777             if (debug) logger.debug("invoke",
778                              "connectionId=" + connectionId
779                              +", name="+name
780                              +", operationName="+operationName
781                              +", params="+objects(values)
782                              +", signature="+strings(signature));
783
784             return (Object JavaDoc)
785                 doPrivilegedOperation(
786                   INVOKE,
787                   params2,
788                   delegationSubject);
789         } catch (PrivilegedActionException JavaDoc pe) {
790             Exception JavaDoc e = extractException(pe);
791             if (e instanceof InstanceNotFoundException JavaDoc)
792                 throw (InstanceNotFoundException JavaDoc) e;
793             if (e instanceof MBeanException JavaDoc)
794                 throw (MBeanException JavaDoc) e;
795             if (e instanceof ReflectionException JavaDoc)
796                 throw (ReflectionException JavaDoc) e;
797             if (e instanceof IOException JavaDoc)
798                 throw (IOException JavaDoc) e;
799             throw newIOException("Got unexpected server exception: " + e, e);
800         }
801     }
802
803     public String JavaDoc getDefaultDomain(Subject JavaDoc delegationSubject)
804         throws IOException JavaDoc {
805         try {
806             final Object JavaDoc params[] = new Object JavaDoc[] { };
807
808             if (logger.debugOn()) logger.debug("getDefaultDomain",
809                                     "connectionId=" + connectionId);
810
811             return (String JavaDoc)
812                 doPrivilegedOperation(
813                   GET_DEFAULT_DOMAIN,
814                   params,
815                   delegationSubject);
816         } catch (PrivilegedActionException JavaDoc pe) {
817             Exception JavaDoc e = extractException(pe);
818             if (e instanceof IOException JavaDoc)
819                 throw (IOException JavaDoc) e;
820             throw newIOException("Got unexpected server exception: " + e, e);
821         }
822     }
823
824     public String JavaDoc[] getDomains(Subject JavaDoc delegationSubject) throws IOException JavaDoc {
825         try {
826             final Object JavaDoc params[] = new Object JavaDoc[] { };
827
828             if (logger.debugOn()) logger.debug("getDomains",
829                                     "connectionId=" + connectionId);
830
831             return (String JavaDoc[])
832                 doPrivilegedOperation(
833                   GET_DOMAINS,
834                   params,
835                   delegationSubject);
836         } catch (PrivilegedActionException JavaDoc pe) {
837             Exception JavaDoc e = extractException(pe);
838             if (e instanceof IOException JavaDoc)
839                 throw (IOException JavaDoc) e;
840             throw newIOException("Got unexpected server exception: " + e, e);
841         }
842     }
843
844     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name, Subject JavaDoc delegationSubject)
845         throws
846         InstanceNotFoundException JavaDoc,
847         IntrospectionException JavaDoc,
848         ReflectionException JavaDoc,
849         IOException JavaDoc {
850
851     checkNonNull("ObjectName", name);
852
853         try {
854             final Object JavaDoc params[] = new Object JavaDoc[] { name };
855
856             if (logger.debugOn()) logger.debug("getMBeanInfo",
857                                     "connectionId=" + connectionId
858                                     +", name="+name);
859
860             return (MBeanInfo JavaDoc)
861                 doPrivilegedOperation(
862                   GET_MBEAN_INFO,
863                   params,
864                   delegationSubject);
865         } catch (PrivilegedActionException JavaDoc pe) {
866             Exception JavaDoc e = extractException(pe);
867             if (e instanceof InstanceNotFoundException JavaDoc)
868                 throw (InstanceNotFoundException JavaDoc) e;
869             if (e instanceof IntrospectionException JavaDoc)
870                 throw (IntrospectionException JavaDoc) e;
871             if (e instanceof ReflectionException JavaDoc)
872                 throw (ReflectionException JavaDoc) e;
873             if (e instanceof IOException JavaDoc)
874                 throw (IOException JavaDoc) e;
875             throw newIOException("Got unexpected server exception: " + e, e);
876         }
877     }
878
879     public boolean isInstanceOf(ObjectName JavaDoc name,
880                                 String JavaDoc className,
881                                 Subject JavaDoc delegationSubject)
882         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
883
884     checkNonNull("ObjectName", name);
885
886         try {
887             final Object JavaDoc params[] = new Object JavaDoc[] { name, className };
888
889             if (logger.debugOn()) logger.debug("isInstanceOf",
890                                     "connectionId=" + connectionId
891                                     +", name="+name
892                                     +", className="+className);
893
894             return ((Boolean JavaDoc)
895                 doPrivilegedOperation(
896                   IS_INSTANCE_OF,
897                   params,
898                   delegationSubject)).booleanValue();
899         } catch (PrivilegedActionException JavaDoc pe) {
900             Exception JavaDoc e = extractException(pe);
901             if (e instanceof InstanceNotFoundException JavaDoc)
902                 throw (InstanceNotFoundException JavaDoc) e;
903             if (e instanceof IOException JavaDoc)
904                 throw (IOException JavaDoc) e;
905             throw newIOException("Got unexpected server exception: " + e, e);
906         }
907     }
908     
909     public Integer JavaDoc[] addNotificationListeners(ObjectName JavaDoc[] names,
910                                               MarshalledObject JavaDoc[] filters,
911                                               Subject JavaDoc[] delegationSubjects)
912         throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
913
914     if (names == null || filters == null) {
915         throw new IllegalArgumentException JavaDoc("Got null arguments.");
916     }
917
918     Subject JavaDoc[] sbjs = (delegationSubjects != null) ? delegationSubjects :
919     new Subject JavaDoc[names.length];
920     if (names.length != filters.length || filters.length != sbjs.length) {
921         final String JavaDoc msg =
922         "The value lengths of 3 parameters are not same.";
923         throw new IllegalArgumentException JavaDoc(msg);
924     }
925
926     for (int i=0; i<names.length; i++) {
927         if (names[i] == null) {
928         throw new IllegalArgumentException JavaDoc("Null Object name.");
929         }
930     }
931
932     int i=0;
933     ClassLoader JavaDoc targetCl;
934     NotificationFilter JavaDoc[] filterValues =
935     new NotificationFilter JavaDoc[names.length];
936     Object JavaDoc params[];
937     Integer JavaDoc[] ids = new Integer JavaDoc[names.length];
938     final boolean debug=logger.debugOn();
939  
940     try {
941         for (; i<names.length; i++) {
942         targetCl = getClassLoaderFor(names[i]);
943
944         if (debug) logger.debug("addNotificationListener"+
945                     "(ObjectName,NotificationFilter)",
946                     "connectionId=" + connectionId +
947               " unwrapping filter with target extended ClassLoader.");
948
949         filterValues[i] = (NotificationFilter JavaDoc)unwrap(filters[i],
950                           targetCl, defaultClassLoader);
951
952         if (debug) logger.debug("addNotificationListener"+
953                     "(ObjectName,NotificationFilter)",
954                     "connectionId=" + connectionId
955                     +", name=" + names[i]
956                     +", filter=" + filterValues[i]);
957
958         ids[i] = (Integer JavaDoc)
959             doPrivilegedOperation(ADD_NOTIFICATION_LISTENERS,
960                       new Object JavaDoc[] { names[i],
961                              filterValues[i] },
962                       sbjs[i]);
963         }
964
965         return ids;
966     } catch (Exception JavaDoc e) {
967         // remove all registered listeners
968
for (int j=0; j<i; j++) {
969         try {
970             getServerNotifFwd().removeNotificationListener(names[j],
971                                    ids[j]);
972         } catch (Exception JavaDoc eee) {
973             // strange
974
}
975         }
976         
977         if (e instanceof PrivilegedActionException JavaDoc) {
978         e = extractException(e);
979         }
980
981         if (e instanceof ClassCastException JavaDoc) {
982         throw (ClassCastException JavaDoc) e;
983         } else if (e instanceof IOException JavaDoc) {
984         throw (IOException JavaDoc)e;
985         } else if (e instanceof InstanceNotFoundException JavaDoc) {
986         throw (InstanceNotFoundException JavaDoc) e;
987         } else if (e instanceof RuntimeException JavaDoc) {
988         throw (RuntimeException JavaDoc) e;
989         } else {
990         throw newIOException("Got unexpected server exception: "+e,e);
991         }
992     }
993     }
994
995     public void addNotificationListener(ObjectName JavaDoc name,
996                                         ObjectName JavaDoc listener,
997                                         MarshalledObject JavaDoc filter,
998                                         MarshalledObject JavaDoc handback,
999                                         Subject JavaDoc delegationSubject)
1000        throws InstanceNotFoundException JavaDoc, IOException JavaDoc {
1001
1002    checkNonNull("Target MBean name", name);
1003    checkNonNull("Listener MBean name", listener);
1004
1005        final NotificationFilter JavaDoc filterValue;
1006        final Object JavaDoc handbackValue;
1007        final boolean debug=logger.debugOn();
1008
1009    final ClassLoader JavaDoc targetCl = getClassLoaderFor(name);
1010
1011    if (debug) logger.debug("addNotificationListener"+
1012                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1013                 "connectionId=" + connectionId
1014                 +" unwrapping filter with target extended ClassLoader.");
1015
1016    filterValue = (NotificationFilter JavaDoc)
1017                unwrap(filter, targetCl, defaultClassLoader);
1018
1019    if (debug) logger.debug("addNotificationListener"+
1020                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1021                 "connectionId=" + connectionId
1022                 +" unwrapping handback with target extended ClassLoader.");
1023
1024    handbackValue = unwrap(handback, targetCl, defaultClassLoader);
1025
1026        try {
1027            final Object JavaDoc params[] =
1028                new Object JavaDoc[] { name, listener, filterValue, handbackValue };
1029
1030            if (debug) logger.debug("addNotificationListener"+
1031                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1032                             "connectionId=" + connectionId
1033                             +", name=" + name
1034                             +", listenerName=" + listener
1035                             +", filter=" + filterValue
1036                             +", handback=" + handbackValue);
1037
1038            doPrivilegedOperation(
1039              ADD_NOTIFICATION_LISTENER_OBJECTNAME,
1040              params,
1041              delegationSubject);
1042        } catch (PrivilegedActionException JavaDoc pe) {
1043            Exception JavaDoc e = extractException(pe);
1044            if (e instanceof InstanceNotFoundException JavaDoc)
1045                throw (InstanceNotFoundException JavaDoc) e;
1046            if (e instanceof IOException JavaDoc)
1047                throw (IOException JavaDoc) e;
1048            throw newIOException("Got unexpected server exception: " + e, e);
1049        }
1050    }
1051
1052    public void removeNotificationListeners(ObjectName JavaDoc name,
1053                        Integer JavaDoc[] listenerIDs,
1054                        Subject JavaDoc delegationSubject)
1055        throws
1056        InstanceNotFoundException JavaDoc,
1057        ListenerNotFoundException JavaDoc,
1058        IOException JavaDoc {
1059
1060    if (name == null || listenerIDs == null)
1061        throw new IllegalArgumentException JavaDoc("Illegal null parameter");
1062
1063    for (int i = 0; i < listenerIDs.length; i++) {
1064        if (listenerIDs[i] == null)
1065        throw new IllegalArgumentException JavaDoc("Null listener ID");
1066    }
1067
1068        try {
1069            final Object JavaDoc params[] = new Object JavaDoc[] { name, listenerIDs };
1070
1071            if (logger.debugOn()) logger.debug("removeNotificationListener"+
1072                                   "(ObjectName,Integer[])",
1073                                   "connectionId=" + connectionId
1074                                   +", name=" + name
1075                                   +", listenerIDs=" + objects(listenerIDs));
1076
1077            doPrivilegedOperation(
1078              REMOVE_NOTIFICATION_LISTENER,
1079              params,
1080              delegationSubject);
1081        } catch (PrivilegedActionException JavaDoc pe) {
1082            Exception JavaDoc e = extractException(pe);
1083            if (e instanceof InstanceNotFoundException JavaDoc)
1084                throw (InstanceNotFoundException JavaDoc) e;
1085            if (e instanceof ListenerNotFoundException JavaDoc)
1086                throw (ListenerNotFoundException JavaDoc) e;
1087            if (e instanceof IOException JavaDoc)
1088                throw (IOException JavaDoc) e;
1089            throw newIOException("Got unexpected server exception: " + e, e);
1090        }
1091    }
1092
1093    public void removeNotificationListener(ObjectName JavaDoc name,
1094                                           ObjectName JavaDoc listener,
1095                                           Subject JavaDoc delegationSubject)
1096        throws
1097        InstanceNotFoundException JavaDoc,
1098        ListenerNotFoundException JavaDoc,
1099        IOException JavaDoc {
1100
1101    checkNonNull("Target MBean name", name);
1102    checkNonNull("Listener MBean name", listener);
1103
1104        try {
1105            final Object JavaDoc params[] = new Object JavaDoc[] { name, listener };
1106
1107            if (logger.debugOn()) logger.debug("removeNotificationListener"+
1108                                   "(ObjectName,ObjectName)",
1109                                   "connectionId=" + connectionId
1110                                   +", name=" + name
1111                                   +", listenerName=" + listener);
1112
1113            doPrivilegedOperation(
1114              REMOVE_NOTIFICATION_LISTENER_OBJECTNAME,
1115              params,
1116              delegationSubject);
1117        } catch (PrivilegedActionException JavaDoc pe) {
1118            Exception JavaDoc e = extractException(pe);
1119            if (e instanceof InstanceNotFoundException JavaDoc)
1120                throw (InstanceNotFoundException JavaDoc) e;
1121            if (e instanceof ListenerNotFoundException JavaDoc)
1122                throw (ListenerNotFoundException JavaDoc) e;
1123            if (e instanceof IOException JavaDoc)
1124                throw (IOException JavaDoc) e;
1125            throw newIOException("Got unexpected server exception: " + e, e);
1126        }
1127    }
1128
1129    public void removeNotificationListener(ObjectName JavaDoc name,
1130                                           ObjectName JavaDoc listener,
1131                                           MarshalledObject JavaDoc filter,
1132                                           MarshalledObject JavaDoc handback,
1133                                           Subject JavaDoc delegationSubject)
1134        throws
1135        InstanceNotFoundException JavaDoc,
1136        ListenerNotFoundException JavaDoc,
1137        IOException JavaDoc {
1138
1139    checkNonNull("Target MBean name", name);
1140    checkNonNull("Listener MBean name", listener);
1141
1142        final NotificationFilter JavaDoc filterValue;
1143        final Object JavaDoc handbackValue;
1144        final boolean debug=logger.debugOn();
1145
1146    final ClassLoader JavaDoc targetCl = getClassLoaderFor(name);
1147
1148    if (debug) logger.debug("removeNotificationListener"+
1149                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1150                 "connectionId=" + connectionId
1151                 +" unwrapping filter with target extended ClassLoader.");
1152
1153    filterValue = (NotificationFilter JavaDoc)
1154                unwrap(filter, targetCl, defaultClassLoader);
1155
1156    if (debug) logger.debug("removeNotificationListener"+
1157                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1158                 "connectionId=" + connectionId
1159                 +" unwrapping handback with target extended ClassLoader.");
1160
1161    handbackValue = unwrap(handback, targetCl, defaultClassLoader);
1162
1163        try {
1164            final Object JavaDoc params[] =
1165                new Object JavaDoc[] { name, listener, filterValue, handbackValue };
1166
1167            if (debug) logger.debug("removeNotificationListener"+
1168                 "(ObjectName,ObjectName,NotificationFilter,Object)",
1169                             "connectionId=" + connectionId
1170                             +", name=" + name
1171                             +", listenerName=" + listener
1172                             +", filter=" + filterValue
1173                             +", handback=" + handbackValue);
1174
1175            doPrivilegedOperation(
1176              REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK,
1177              params,
1178              delegationSubject);
1179        } catch (PrivilegedActionException JavaDoc pe) {
1180            Exception JavaDoc e = extractException(pe);
1181            if (e instanceof InstanceNotFoundException JavaDoc)
1182                throw (InstanceNotFoundException JavaDoc) e;
1183            if (e instanceof ListenerNotFoundException JavaDoc)
1184                throw (ListenerNotFoundException JavaDoc) e;
1185            if (e instanceof IOException JavaDoc)
1186                throw (IOException JavaDoc) e;
1187            throw newIOException("Got unexpected server exception: " + e, e);
1188        }
1189    }
1190
1191    public NotificationResult JavaDoc fetchNotifications(long clientSequenceNumber,
1192                                                 int maxNotifications,
1193                                                 long timeout)
1194        throws IOException JavaDoc {
1195
1196        if (logger.debugOn()) logger.debug("fetchNotifications",
1197                               "connectionId=" + connectionId
1198                               +", timeout=" + timeout);
1199
1200    if (maxNotifications < 0 || timeout < 0)
1201        throw new IllegalArgumentException JavaDoc("Illegal negative argument");
1202
1203    final boolean serverTerminated =
1204        serverCommunicatorAdmin.reqIncoming();
1205    try {
1206        if (serverTerminated) {
1207        // we must not call fetchNotifs() if the server is
1208
// terminated (timeout elapsed).
1209
//
1210
return new NotificationResult JavaDoc(0L, 0L,
1211                          new TargetedNotification JavaDoc[0]);
1212
1213        }
1214
1215        return getServerNotifFwd().fetchNotifs(clientSequenceNumber,
1216                           timeout, maxNotifications);
1217    } finally {
1218        serverCommunicatorAdmin.rspOutgoing();
1219    }
1220    }
1221
1222    /**
1223     * <p>Returns a string representation of this object. In general,
1224     * the <code>toString</code> method returns a string that
1225     * "textually represents" this object. The result should be a
1226     * concise but informative representation that is easy for a
1227     * person to read.</p>
1228     *
1229     * @return a String representation of this object.
1230     **/

1231    public String JavaDoc toString() {
1232        return super.toString() + ": connectionId=" + connectionId;
1233    }
1234
1235    //------------------------------------------------------------------------
1236
// private classes
1237
//------------------------------------------------------------------------
1238

1239    private class PrivilegedOperation implements PrivilegedExceptionAction JavaDoc {
1240
1241        public PrivilegedOperation(int operation, Object JavaDoc[] params) {
1242            this.operation = operation;
1243            this.params = params;
1244        }
1245
1246        public Object JavaDoc run() throws Exception JavaDoc {
1247            return doOperation(operation, params);
1248        }
1249
1250        private int operation;
1251        private Object JavaDoc[] params;
1252    }
1253
1254    //------------------------------------------------------------------------
1255
// private classes
1256
//------------------------------------------------------------------------
1257
private class RMIServerCommunicatorAdmin extends ServerCommunicatorAdmin {
1258    public RMIServerCommunicatorAdmin(long timeout) {
1259        super(timeout);
1260    }
1261
1262    protected void doStop() {
1263        try {
1264        close();
1265        } catch (IOException JavaDoc ie) {
1266        logger.warning("RMIServerCommunicatorAdmin-doStop",
1267                   "Failed to close: " + ie);
1268        logger.debug("RMIServerCommunicatorAdmin-doStop",ie);
1269        }
1270    }
1271
1272    }
1273
1274
1275    //------------------------------------------------------------------------
1276
// private methods
1277
//------------------------------------------------------------------------
1278

1279    private ClassLoaderRepository JavaDoc getClassLoaderRepository() {
1280        return (ClassLoaderRepository JavaDoc)
1281            AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
1282                    public Object JavaDoc run() {
1283                        return mbeanServer.getClassLoaderRepository();
1284                    }
1285                });
1286    }
1287
1288    private ClassLoader JavaDoc getClassLoader(final ObjectName JavaDoc name)
1289        throws InstanceNotFoundException JavaDoc {
1290        try {
1291            return (ClassLoader JavaDoc)
1292                AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
1293                        public Object JavaDoc run() throws InstanceNotFoundException JavaDoc {
1294                            return mbeanServer.getClassLoader(name);
1295                        }
1296                    });
1297        } catch (PrivilegedActionException JavaDoc pe) {
1298            throw (InstanceNotFoundException JavaDoc) extractException(pe);
1299        }
1300    }
1301
1302    private ClassLoader JavaDoc getClassLoaderFor(final ObjectName JavaDoc name)
1303        throws InstanceNotFoundException JavaDoc {
1304        try {
1305            return (ClassLoader JavaDoc)
1306                AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
1307                        public Object JavaDoc run() throws InstanceNotFoundException JavaDoc {
1308                            return mbeanServer.getClassLoaderFor(name);
1309                        }
1310                    });
1311        } catch (PrivilegedActionException JavaDoc pe) {
1312            throw (InstanceNotFoundException JavaDoc) extractException(pe);
1313        }
1314    }
1315
1316    private Object JavaDoc doPrivilegedOperation(final int operation,
1317                                         final Object JavaDoc[] params,
1318                                         final Subject JavaDoc delegationSubject)
1319        throws PrivilegedActionException JavaDoc, IOException JavaDoc {
1320
1321    serverCommunicatorAdmin.reqIncoming();
1322    try {
1323
1324        final AccessControlContext JavaDoc reqACC;
1325        if (delegationSubject == null)
1326        reqACC = acc;
1327        else {
1328        if (subject == null) {
1329            final String JavaDoc msg =
1330            "Subject delegation cannot be enabled unless " +
1331            "an authenticated subject is put in place";
1332            throw new SecurityException JavaDoc(msg);
1333        }
1334        reqACC =
1335            subjectDelegator.delegatedContext(acc,
1336                              delegationSubject);
1337        }
1338
1339        PrivilegedOperation op =
1340        new PrivilegedOperation(operation, params);
1341        if (reqACC == null) {
1342        try {
1343            return op.run();
1344        } catch (Exception JavaDoc e) {
1345            if (e instanceof RuntimeException JavaDoc)
1346            throw (RuntimeException JavaDoc) e;
1347            throw new PrivilegedActionException JavaDoc(e);
1348        }
1349        } else {
1350        return AccessController.doPrivileged(op, reqACC);
1351        }
1352    } catch (Error JavaDoc e) {
1353        throw new JMXServerErrorException JavaDoc(e.toString(),e);
1354    } finally {
1355        serverCommunicatorAdmin.rspOutgoing();
1356    }
1357    }
1358
1359    private Object JavaDoc doOperation(int operation, Object JavaDoc[] params)
1360        throws Exception JavaDoc {
1361
1362    switch (operation) {
1363
1364    case CREATE_MBEAN:
1365        return mbeanServer.createMBean((String JavaDoc)params[0],
1366                       (ObjectName JavaDoc)params[1]);
1367
1368    case CREATE_MBEAN_LOADER:
1369        return mbeanServer.createMBean((String JavaDoc)params[0],
1370                       (ObjectName JavaDoc)params[1],
1371                       (ObjectName JavaDoc)params[2]);
1372
1373    case CREATE_MBEAN_PARAMS:
1374        return mbeanServer.createMBean((String JavaDoc)params[0],
1375                       (ObjectName JavaDoc)params[1],
1376                       (Object JavaDoc[])params[2],
1377                       (String JavaDoc[])params[3]);
1378
1379    case CREATE_MBEAN_LOADER_PARAMS:
1380        return mbeanServer.createMBean((String JavaDoc)params[0],
1381                       (ObjectName JavaDoc)params[1],
1382                       (ObjectName JavaDoc)params[2],
1383                       (Object JavaDoc[])params[3],
1384                       (String JavaDoc[])params[4]);
1385
1386    case GET_ATTRIBUTE:
1387        return mbeanServer.getAttribute((ObjectName JavaDoc)params[0],
1388                        (String JavaDoc)params[1]);
1389
1390    case GET_ATTRIBUTES:
1391        return mbeanServer.getAttributes((ObjectName JavaDoc)params[0],
1392                         (String JavaDoc[])params[1]);
1393
1394    case GET_DEFAULT_DOMAIN:
1395        return mbeanServer.getDefaultDomain();
1396        
1397    case GET_DOMAINS:
1398        return mbeanServer.getDomains();
1399        
1400    case GET_MBEAN_COUNT:
1401        return mbeanServer.getMBeanCount();
1402
1403    case GET_MBEAN_INFO:
1404        return mbeanServer.getMBeanInfo((ObjectName JavaDoc)params[0]);
1405        
1406    case GET_OBJECT_INSTANCE:
1407        return mbeanServer.getObjectInstance((ObjectName JavaDoc)params[0]);
1408        
1409    case INVOKE:
1410        return mbeanServer.invoke((ObjectName JavaDoc)params[0],
1411                      (String JavaDoc)params[1],
1412                      (Object JavaDoc[])params[2],
1413                      (String JavaDoc[])params[3]);
1414
1415    case IS_INSTANCE_OF:
1416        return mbeanServer.isInstanceOf((ObjectName JavaDoc)params[0],
1417                        (String JavaDoc)params[1])
1418        ? Boolean.TRUE : Boolean.FALSE;
1419
1420    case IS_REGISTERED:
1421        return mbeanServer.isRegistered((ObjectName JavaDoc)params[0])
1422        ? Boolean.TRUE : Boolean.FALSE;
1423
1424    case QUERY_MBEANS:
1425        return mbeanServer.queryMBeans((ObjectName JavaDoc)params[0],
1426                       (QueryExp JavaDoc)params[1]);
1427
1428    case QUERY_NAMES:
1429        return mbeanServer.queryNames((ObjectName JavaDoc)params[0],
1430                      (QueryExp JavaDoc)params[1]);
1431
1432    case SET_ATTRIBUTE:
1433        mbeanServer.setAttribute((ObjectName JavaDoc)params[0],
1434                     (Attribute JavaDoc)params[1]);
1435        return null;
1436        
1437    case SET_ATTRIBUTES:
1438        return mbeanServer.setAttributes((ObjectName JavaDoc)params[0],
1439                                             (AttributeList JavaDoc)params[1]);
1440
1441    case UNREGISTER_MBEAN:
1442        mbeanServer.unregisterMBean((ObjectName JavaDoc)params[0]);
1443        return null;
1444
1445    case ADD_NOTIFICATION_LISTENERS:
1446        return getServerNotifFwd().addNotificationListener(
1447                        (ObjectName JavaDoc)params[0],
1448                        (NotificationFilter JavaDoc)params[1]);
1449        
1450    case ADD_NOTIFICATION_LISTENER_OBJECTNAME:
1451        mbeanServer.addNotificationListener((ObjectName JavaDoc)params[0],
1452                                                (ObjectName JavaDoc)params[1],
1453                                                (NotificationFilter JavaDoc)params[2],
1454                                                (Object JavaDoc)params[3]);
1455        return null;
1456        
1457    case REMOVE_NOTIFICATION_LISTENER:
1458        getServerNotifFwd().removeNotificationListener(
1459                                                   (ObjectName JavaDoc)params[0],
1460                                                   (Integer JavaDoc[])params[1]);
1461        return null;
1462
1463    case REMOVE_NOTIFICATION_LISTENER_OBJECTNAME:
1464        mbeanServer.removeNotificationListener((ObjectName JavaDoc)params[0],
1465                                                   (ObjectName JavaDoc)params[1]);
1466        return null;
1467
1468    case REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK:
1469        mbeanServer.removeNotificationListener(
1470                                          (ObjectName JavaDoc)params[0],
1471                                          (ObjectName JavaDoc)params[1],
1472                                          (NotificationFilter JavaDoc)params[2],
1473                                          (Object JavaDoc)params[3]);
1474        return null;
1475
1476    default:
1477        throw new IllegalArgumentException JavaDoc("Invalid operation");
1478    }
1479    }
1480
1481    /*
1482       Parameters to certain MBeanServer operations are passed
1483       remotely wrapped inside a MarshalledObject, so that they can be
1484       unwrapped with the right class loader. This loader is
1485       typically the target MBean's class loader.
1486       MarshalledObject.get() uses the context class loader to load
1487       classes as it deserializes, which is what we want. However,
1488       before consulting the context class loader, it consults the
1489       calling class's loader, if that's not null. So, in the
1490       standalone version of javax.management.remote, if the class
1491       you're looking for is known to RMIConnectionImpl's class loader
1492       (typically the system class loader) then that loader will load
1493       it. This contradicts the class-loading semantics defined in JSR
1494       160, and can lead to problems if the same class name is known
1495       to RMIConnectionImpl's class loader and to the class loader of
1496       the target MBean in an invoke, setAttribute, or createMBean
1497       operation. If it is deserialized by the former, it can't be
1498       passed to the MBean, which expects the latter.
1499
1500       We therefore call MarshalledObject.get() from within a class
1501       that is loaded by a NoCallStackClassLoader. This loader
1502       doesn't know any other classes, so cannot load any that it is
1503       not supposed to.
1504
1505       This is not needed in J2SE 5, where javax.management.remote
1506       is loaded by the bootstrap class loader.
1507
1508       The byteCodeString below encodes the following Java class,
1509       compiled with "javac -g:none" on J2SE 1.4.2.
1510
1511    package com.sun.jmx.remote.internal;
1512
1513    import java.io.IOException;
1514    import java.rmi.MarshalledObject;
1515
1516    public class MOGet implements Unmarshal {
1517        public Object get(MarshalledObject mo)
1518            throws IOException, ClassNotFoundException {
1519        return mo.get();
1520        }
1521    }
1522     
1523     */

1524
1525    private static final String JavaDoc unmarshalClassName =
1526    "com.sun.jmx.remote.internal.MOGet";
1527
1528    private static boolean bootstrapLoaded =
1529    (RMIConnectionImpl JavaDoc.class.getClassLoader() ==
1530     Object JavaDoc.class.getClassLoader());
1531
1532    private static final Unmarshal unmarshal;
1533    static {
1534    final String JavaDoc byteCodeString =
1535        "\312\376\272\276\0\0\0.\0\30\12\0\4\0\16\12\0\17\0\20\7\0\21\7\0"+
1536        "\22\7\0\23\1\0\6<init>\1\0\3()V\1\0\4Code\1\0\3get\1\0/(Ljava/r"+
1537        "mi/MarshalledObject;)Ljava/lang/Object;\1\0\12Exceptions\7\0\24"+
1538        "\7\0\25\14\0\6\0\7\7\0\26\14\0\11\0\27\1\0!com/sun/jmx/remote/i"+
1539        "nternal/MOGet\1\0\20java/lang/Object\1\0%com/sun/jmx/remote/int"+
1540        "ernal/Unmarshal\1\0\23java/io/IOException\1\0\40java/lang/Class"+
1541        "NotFoundException\1\0\31java/rmi/MarshalledObject\1\0\24()Ljava"+
1542        "/lang/Object;\0!\0\3\0\4\0\1\0\5\0\0\0\2\0\1\0\6\0\7\0\1\0\10\0"+
1543        "\0\0\21\0\1\0\1\0\0\0\5*\267\0\1\261\0\0\0\0\0\1\0\11\0\12\0\2\0"+
1544        "\10\0\0\0\21\0\1\0\2\0\0\0\5+\266\0\2\260\0\0\0\0\0\13\0\0\0\6\0"+
1545        "\2\0\14\0\15\0\0";
1546    if (bootstrapLoaded)
1547        unmarshal = null;
1548    else {
1549        final byte[] byteCode =
1550        NoCallStackClassLoader.stringToBytes(byteCodeString);
1551        final String JavaDoc[] otherClassNames = {
1552        Unmarshal.class.getName()
1553        };
1554        final Class JavaDoc thisClass = RMIConnectionImpl JavaDoc.class;
1555        final ClassLoader JavaDoc thisClassLoader = thisClass.getClassLoader();
1556        final PrivilegedExceptionAction JavaDoc action =
1557        new PrivilegedExceptionAction JavaDoc() {
1558            public Object JavaDoc run() throws Exception JavaDoc {
1559            final ProtectionDomain JavaDoc thisProtectionDomain =
1560                thisClass.getProtectionDomain();
1561            ClassLoader JavaDoc cl =
1562                new NoCallStackClassLoader JavaDoc(unmarshalClassName,
1563                               byteCode,
1564                               otherClassNames,
1565                               thisClassLoader,
1566                               thisProtectionDomain);
1567            Class JavaDoc c = cl.loadClass(unmarshalClassName);
1568            return c.newInstance();
1569            }
1570        };
1571        try {
1572        unmarshal = (Unmarshal) AccessController.doPrivileged(action);
1573        } catch (PrivilegedActionException JavaDoc e) {
1574        Error JavaDoc error = new Error JavaDoc("Internal error: " + e);
1575        EnvHelp.initCause(error, e);
1576        throw error;
1577        }
1578    }
1579    }
1580
1581    private static Object JavaDoc unwrap(final MarshalledObject JavaDoc mo,
1582                 final ClassLoader JavaDoc cl)
1583        throws IOException JavaDoc {
1584        if (mo == null) {
1585            return null;
1586        }
1587        try {
1588            return AccessController.doPrivileged(
1589        new PrivilegedExceptionAction JavaDoc() {
1590                    public Object JavaDoc run()
1591                throws IOException JavaDoc {
1592                        final ClassLoader JavaDoc old =
1593                            Thread.currentThread().getContextClassLoader();
1594                        Thread.currentThread().setContextClassLoader(cl);
1595                        try {
1596                if (bootstrapLoaded)
1597                return mo.get();
1598                else
1599                return unmarshal.get(mo);
1600            } catch (ClassNotFoundException JavaDoc cnfe) {
1601                throw new UnmarshalException JavaDoc(cnfe.toString(), cnfe);
1602                        } finally {
1603                            Thread.currentThread().setContextClassLoader(old);
1604                        }
1605                    }
1606                });
1607        } catch (PrivilegedActionException JavaDoc pe) {
1608            Exception JavaDoc e = extractException(pe);
1609            if (e instanceof IOException JavaDoc) {
1610                throw (IOException JavaDoc) e;
1611            }
1612            if (e instanceof ClassNotFoundException JavaDoc) {
1613        throw new UnmarshalException JavaDoc(e.toString(), e);
1614            }
1615        logger.warning("unwrap", "Failed to unmarshall object: " + e);
1616        logger.debug("unwrap", e);
1617        }
1618        return null;
1619    }
1620
1621    private static Object JavaDoc unwrap(final MarshalledObject JavaDoc mo,
1622                 final ClassLoader JavaDoc cl1,
1623                 final ClassLoader JavaDoc cl2)
1624        throws IOException JavaDoc {
1625        if (mo == null) {
1626            return null;
1627        }
1628        try {
1629            return AccessController.doPrivileged(
1630                   new PrivilegedExceptionAction JavaDoc() {
1631                       public Object JavaDoc run()
1632                           throws IOException JavaDoc {
1633                           return unwrap(mo, new OrderClassLoaders(cl1, cl2));
1634                       }
1635                   });
1636        } catch (PrivilegedActionException JavaDoc pe) {
1637            Exception JavaDoc e = extractException(pe);
1638            if (e instanceof IOException JavaDoc) {
1639                throw (IOException JavaDoc) e;
1640            }
1641            if (e instanceof ClassNotFoundException JavaDoc) {
1642                throw new UnmarshalException JavaDoc(e.toString(), e);
1643            }
1644        logger.warning("unwrap", "Failed to unmarshall object: " + e);
1645        logger.debug("unwrap", e);
1646        }
1647        return null;
1648    }
1649
1650    /**
1651     * Construct a new IOException with a nested exception.
1652     * The nested exception is set only if JDK >= 1.4
1653     */

1654    private static IOException JavaDoc newIOException(String JavaDoc message,
1655                                              Throwable JavaDoc cause) {
1656        final IOException JavaDoc x = new IOException JavaDoc(message);
1657        return (IOException JavaDoc) EnvHelp.initCause(x,cause);
1658    }
1659
1660    /**
1661     * Iterate until we extract the real exception
1662     * from a stack of PrivilegedActionExceptions.
1663     */

1664    private static Exception JavaDoc extractException(Exception JavaDoc e) {
1665        while (e instanceof PrivilegedActionException JavaDoc) {
1666            e = ((PrivilegedActionException JavaDoc)e).getException();
1667        }
1668        return e;
1669    }
1670
1671    private static final Object JavaDoc[] NO_OBJECTS = new Object JavaDoc[0];
1672    private static final String JavaDoc[] NO_STRINGS = new String JavaDoc[0];
1673
1674    /*
1675     * The JMX spec doesn't explicitly say that a null Object[] or
1676     * String[] in e.g. MBeanServer.invoke is equivalent to an empty
1677     * array, but the RI behaves that way. In the interests of
1678     * maximal interoperability, we make it so even when we're
1679     * connected to some other JMX implementation that might not do
1680     * that. This should be clarified in the next version of JMX.
1681     */

1682    private static Object JavaDoc[] nullIsEmpty(Object JavaDoc[] array) {
1683    return (array == null) ? NO_OBJECTS : array;
1684    }
1685
1686    private static String JavaDoc[] nullIsEmpty(String JavaDoc[] array) {
1687    return (array == null) ? NO_STRINGS : array;
1688    }
1689
1690    /*
1691     * Similarly, the JMX spec says for some but not all methods in
1692     * MBeanServer that take an ObjectName target, that if it's null
1693     * you get this exception. We specify it for all of them, and
1694     * make it so for the ones where it's not specified in JMX even if
1695     * the JMX implementation doesn't do so.
1696     */

1697    private static void checkNonNull(String JavaDoc what, Object JavaDoc x) {
1698    if (x == null) {
1699        RuntimeException JavaDoc wrapped =
1700        new IllegalArgumentException JavaDoc(what + " must not be null");
1701        throw new RuntimeOperationsException JavaDoc(wrapped);
1702    }
1703    }
1704
1705    //------------------------------------------------------------------------
1706
// private variables
1707
//------------------------------------------------------------------------
1708

1709    private final Subject JavaDoc subject;
1710
1711    private final SubjectDelegator subjectDelegator;
1712
1713    private final AccessControlContext JavaDoc acc;
1714
1715    private final RMIServerImpl JavaDoc rmiServer;
1716
1717    private final MBeanServer JavaDoc mbeanServer;
1718
1719    private final ClassLoader JavaDoc defaultClassLoader;
1720
1721    private final ClassLoaderWithRepository classLoaderWithRepository;
1722    
1723    private boolean terminated = false;
1724    
1725    private final String JavaDoc connectionId;
1726
1727    private final ServerCommunicatorAdmin serverCommunicatorAdmin;
1728    // Method IDs for doOperation
1729
//---------------------------
1730

1731    private final static int
1732        ADD_NOTIFICATION_LISTENERS = 1;
1733    private final static int
1734        ADD_NOTIFICATION_LISTENER_OBJECTNAME = 2;
1735    private final static int
1736        CREATE_MBEAN = 3;
1737    private final static int
1738        CREATE_MBEAN_PARAMS = 4;
1739    private final static int
1740        CREATE_MBEAN_LOADER = 5;
1741    private final static int
1742        CREATE_MBEAN_LOADER_PARAMS = 6;
1743    private final static int
1744        GET_ATTRIBUTE = 7;
1745    private final static int
1746        GET_ATTRIBUTES = 8;
1747    private final static int
1748        GET_DEFAULT_DOMAIN = 9;
1749    private final static int
1750        GET_DOMAINS = 10;
1751    private final static int
1752        GET_MBEAN_COUNT = 11;
1753    private final static int
1754        GET_MBEAN_INFO = 12;
1755    private final static int
1756        GET_OBJECT_INSTANCE = 13;
1757    private final static int
1758        INVOKE = 14;
1759    private final static int
1760        IS_INSTANCE_OF = 15;
1761    private final static int
1762        IS_REGISTERED = 16;
1763    private final static int
1764        QUERY_MBEANS = 17;
1765    private final static int
1766        QUERY_NAMES = 18;
1767    private final static int
1768        REMOVE_NOTIFICATION_LISTENER = 19;
1769    private final static int
1770        REMOVE_NOTIFICATION_LISTENER_FILTER_HANDBACK = 20;
1771    private final static int
1772        REMOVE_NOTIFICATION_LISTENER_OBJECTNAME = 21;
1773    private final static int
1774        REMOVE_NOTIFICATION_LISTENER_OBJECTNAME_FILTER_HANDBACK = 22;
1775    private final static int
1776        SET_ATTRIBUTE = 23;
1777    private final static int
1778        SET_ATTRIBUTES = 24;
1779    private final static int
1780        UNREGISTER_MBEAN = 25;
1781
1782    // SERVER NOTIFICATION
1783
//--------------------
1784
private ServerNotifForwarder serverNotifForwarder;
1785    private Map JavaDoc env;
1786    
1787    // TRACES & DEBUG
1788
//---------------
1789

1790    private static String JavaDoc objects(final Object JavaDoc[] objs) {
1791        if (objs == null)
1792            return "null";
1793        else
1794            return Arrays.asList(objs).toString();
1795    }
1796
1797    private static String JavaDoc strings(final String JavaDoc[] strs) {
1798    return objects(strs);
1799    }
1800
1801    private static final ClassLogger logger =
1802    new ClassLogger("javax.management.remote.rmi", "RMIConnectionImpl");
1803}
1804
Popular Tags