KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > adventnet > jmx > MBeanServerImpl


1 /**
2 * The XMOJO Project 5
3 * Copyright © 2003 XMOJO.org. All rights reserved.
4
5 * NO WARRANTY
6
7 * BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
8 * THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
9 * OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
10 * PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
11 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
13 * TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE
14 * LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
15 * REPAIR OR CORRECTION.
16
17 * IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL
18 * ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
19 * THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
20 * GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
21 * USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF
22 * DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
23 * PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE),
24 * EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGES.
26 **/

27
28 package com.adventnet.jmx;
29
30 //----------------------Importing the Java Packages --------------------------//
31
import java.lang.reflect.Constructor JavaDoc;
32 import java.lang.reflect.Modifier JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.HashSet JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 //----------------------Importing the JMX Packages --------------------------//
44
import javax.management.Attribute JavaDoc;
45 import javax.management.AttributeList JavaDoc;
46 import javax.management.DynamicMBean JavaDoc;
47 import javax.management.ObjectName JavaDoc;
48 import javax.management.ObjectInstance JavaDoc;
49 import javax.management.MBeanServer JavaDoc;
50 import javax.management.MBeanServerDelegate JavaDoc;
51 import javax.management.MBeanServerNotification JavaDoc;
52 import javax.management.MBeanRegistration JavaDoc;
53 import javax.management.MBeanInfo JavaDoc;
54 import javax.management.Notification JavaDoc;
55 import javax.management.NotificationListener JavaDoc;
56 import javax.management.NotificationFilter JavaDoc;
57 import javax.management.NotificationBroadcaster JavaDoc;
58 import javax.management.QueryExp JavaDoc;
59 import javax.management.AttributeNotFoundException JavaDoc;
60 import javax.management.ListenerNotFoundException JavaDoc;
61 import javax.management.InstanceNotFoundException JavaDoc;
62 import javax.management.InstanceAlreadyExistsException JavaDoc;
63 import javax.management.InvalidAttributeValueException JavaDoc;
64 import javax.management.IntrospectionException JavaDoc;
65 import javax.management.JMRuntimeException JavaDoc;
66 import javax.management.MBeanException JavaDoc;
67 import javax.management.MBeanRegistrationException JavaDoc;
68 import javax.management.NotCompliantMBeanException JavaDoc;
69 import javax.management.OperationsException JavaDoc;
70 import javax.management.ReflectionException JavaDoc;
71 import javax.management.RuntimeErrorException JavaDoc;
72 import javax.management.RuntimeMBeanException JavaDoc;
73 import javax.management.RuntimeOperationsException JavaDoc;
74
75 import com.adventnet.agent.logging.Log;
76 import com.adventnet.agent.logging.LogFactory;
77 import com.adventnet.jmx.utils.JmxUtilities;
78
79 /**
80  * This is the implementation class for MBean manipulation on the agent side.
81  * It contains the necessary methods for the creation, registration,
82  * and deletion of MBeans as well as the access methods for registered
83  * MBeans. This is the core component of the JMX infrastructure.
84  * @version C_VERSION
85  */

86
87 public class MBeanServerImpl implements MBeanServer JavaDoc
88 {
89     //-----------------------Variable Declaration-----------------------//
90

91     /** The default domain of the MBeanServer **/
92     private String JavaDoc defaultDomain = new String JavaDoc("DefaultDomain");
93
94     /** The static list which maintains the MBeanServer instances **/
95     private static ArrayList JavaDoc servers = new ArrayList JavaDoc();
96
97     /**
98      * ServerTable maintains all the MBeans registered with the JMX agent
99      * key = ObjectName(mbean objectname) ; value = Object(mbean)
100      */

101     private Hashtable JavaDoc serverTable = null;
102
103     /** The delegate object for the MBeanServer **/
104     private MBeanServerDelegate JavaDoc delegate = null;
105
106     /**
107      * The table that maintains the Loader reference for each of the MBeans
108      * Key = String(mbeans name); value = Object(loader)
109      */

110     private Hashtable JavaDoc loaderTable = null;
111
112     /** The Notification seqenceNumber for the MBeanServerDelegate **/
113     private long sequenceNumber = 1;
114
115     private Log log=null;
116
117     /**
118      * Default Constructor
119      */

120     public MBeanServerImpl()
121     {
122         this("defaultdomain");
123
124         try
125         {
126             createLogger();
127         }
128         catch(Exception JavaDoc e)
129         {
130             System.out.println("FATAL:Log Creation Failed");
131         }
132     }
133
134     /** Creates an MBeanServer with the specified default domain name.**/
135     public MBeanServerImpl(String JavaDoc defaultDomain)
136     {
137         try
138         {
139             createLogger();
140         }
141         catch(Exception JavaDoc e)
142         {
143             System.out.println("FATAL:Log Creation Failed");
144         }
145
146         //PERF
147
//< 28-11-2001 serverTable is going to store all the MBeans. The default hashtable allocates space only for 10 objects. Practical applications must be having 100s of MBeans !!! -- Balaji>
148
serverTable = new Hashtable JavaDoc();
149         loaderTable = new Hashtable JavaDoc();
150
151         delegate = new MBeanServerDelegate JavaDoc();
152
153         try
154         {
155             registerMBean(delegate, new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"));
156         }
157         catch(Exception JavaDoc e)
158         {
159             //TODO
160
log.error("MBeanServerDelegate Registration Failed",e);
161             //e.printStackTrace();
162
}
163
164         //TODO
165
//< 28-11-2001 IS this really needed ??? It will keep on populating the loaders in the MBeanServer with objects of same type.--Bala >
166
//DefaultLoaderRepositoryExt.addLoader( new DefaultClassLoader());
167
DefaultLoaderRepositoryExt.addLoader(Thread.currentThread().getContextClassLoader());
168
169         this.defaultDomain = defaultDomain;
170     }
171
172     /**
173      * Checks whether an MBean, identified by its object name, is already
174      * registered with the MBeanServer.
175      *
176      * @param name The object name of the MBean to be checked.
177      *
178      * @return true if the MBean is already controlled by the MBeanServer,
179      * false otherwise.
180      */

181     public boolean isRegistered(ObjectName JavaDoc name)
182     {
183         return serverTable.containsKey(name);
184     }
185
186     /**
187      * Returns true if the MBean specified is an instance of the specified
188      * class, false otherwise.
189      * @param name The object name of the MBean to be checked.
190      * @param className The name of the class.
191      * @return true if the MBean is the instance of the specified class, false otherwise.
192      * @exception InstanceNotFoundException The MBean specified is not registered in the MBean server.
193      */

194     public boolean isInstanceOf(ObjectName JavaDoc name, String JavaDoc className)
195                     throws InstanceNotFoundException JavaDoc
196     {
197         Object JavaDoc obj = serverTable.get(name);
198         Class JavaDoc clazz = null;
199
200         if(obj == null)
201                 throw new InstanceNotFoundException JavaDoc();
202
203         if(obj instanceof javax.management.modelmbean.RequiredModelMBean JavaDoc)
204         {
205             try
206             {
207                 MBeanInfo JavaDoc info = this.getMBeanInfo(name);
208
209                 if(info.getClassName().equals(className))
210                     return true;
211
212                 clazz = Thread.currentThread().getContextClassLoader()
213                                               .loadClass(className);
214                 if(clazz.isInstance(obj))
215                     return true;
216                 else
217                     return false;
218             }
219             catch(Exception JavaDoc e1)
220             {
221                 //TODO
222
log.warn("RMM loading failed",e1);
223                 return false;
224             }
225         }
226         else if(obj instanceof com.adventnet.jmx.DefaultDynamicMBean)
227         {
228             Object JavaDoc toCheck = ((DefaultDynamicMBean)(obj)).getStandardMBeanObject();
229             clazz = null;
230
231             try
232             {
233                 clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
234
235                 if(clazz != null && clazz.isInstance(toCheck))
236                 {
237                     return true;
238                 }
239                 else
240                 {
241                     return false;
242                 }
243             }
244             catch(Exception JavaDoc e1)
245             {
246                 log.warn("Failed to loadClass"+toCheck.getClass().getName(),e1);
247                 //e1.printStackTrace();
248
return false;
249             }
250         }
251         else
252         {
253             clazz = null;
254
255             try
256             {
257                 clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
258             }
259             catch(Exception JavaDoc e)
260             {
261                 log.warn("isInstanceOf() : Failed to loadClass "+className);
262                 return false;
263             }
264             if(clazz != null && clazz.isInstance(obj))
265             {
266                 return true;
267             }
268             else
269             {
270                 return false;
271             }
272         }
273     }
274
275     /**
276      * Returns the default domain used for the MBean naming.
277      */

278     public String JavaDoc getDefaultDomain()
279     {
280         return defaultDomain;
281     }
282
283     /**
284      * Gets MBeans controlled by the MBeanServer. This method allows any of
285      * the following to be obtained: All MBeans, a set of MBeans specified by
286      * pattern matching on the ObjectName and/or a Query expression, a specific
287      * MBean. When the object name is null or empty, all objects are to be
288      * selected (and filtered if a query is specified). It returns the set of
289      * ObjectInstance objects (containing the ObjectName and the Java Class
290      * name) for the selected MBeans.
291      *
292      * @param name The object name pattern identifying the MBeans to be
293      * retrieved. If null orempty all the MBeans registered
294      * will be retrieved.
295      *
296      * @param query The query expression to be applied for selecting MBeans.
297      *
298      * @return A set containing the ObjectInstance objects for the selected MBeans.
299      * If no MBean satisfies the query an empty list is returned.
300      */

301     public Set JavaDoc queryMBeans(ObjectName JavaDoc name, QueryExp JavaDoc query)
302     {
303         return queryFromTable(name, query, false);
304     }
305
306     /**
307      * All the pattern matching stuff with ObjectName is done here
308      */

309     private Set JavaDoc queryFromTable(ObjectName JavaDoc name, QueryExp JavaDoc query,
310                                                             boolean queryName)
311     {
312         boolean wildCard = false;
313
314         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
315
316         HashSet JavaDoc querySet = new HashSet JavaDoc();
317
318         try
319         {
320             if(name == null)
321                 name = new ObjectName JavaDoc("*:*");
322         }
323         catch(Exception JavaDoc e1)
324         {
325             //log.warn("queryFromTable");
326
return querySet;
327         }
328
329         if(name != null && name.toString().length() != 0)
330         {
331             String JavaDoc strPattern = name.toString();
332
333             if((strPattern.indexOf("*") != -1) || (strPattern.indexOf("?") != -1))
334             {
335                 wildCard = true;
336             }
337
338             int length = strPattern.length();
339             for(int i=0;i<length;i++)
340             {
341                 char ch = strPattern.charAt(i) ;
342
343                 /*if(ch == '*' || ch == '?')
344                         buff.append("$");
345                 */

346
347                 buff.append(ch);
348             }
349         }
350
351         String JavaDoc sname = buff.toString();
352
353         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(sname, ":");
354
355         if(st.countTokens() != 2)
356             return querySet;
357
358         String JavaDoc dom = st.nextToken();
359         String JavaDoc des = st.nextToken();
360
361         if(dom == null || des == null)
362             return querySet;
363
364         for(Enumeration JavaDoc e = serverTable.keys(); e.hasMoreElements();)
365         {
366             ObjectName JavaDoc obj = (ObjectName JavaDoc)e.nextElement();
367             if(name == null || (name != null && name.toString().length() == 0) )
368             {
369                 if(queryName)
370                 {
371                     Object JavaDoc mbean = serverTable.get(obj);
372                     try
373                     {
374                         if(query == null)
375                             querySet.add(obj);
376                         else
377                         {
378                             query.setMBeanServer(this);
379                             if(query.apply(obj))
380                                     querySet.add(obj);
381                         }
382                     }
383                     catch(Exception JavaDoc e1)
384                     {
385                         if(!(e1 instanceof NullPointerException JavaDoc))
386                             log.warn("Null Pointer Exception",e1);
387                     }
388                 }
389                 else
390                 {
391                     Object JavaDoc mbean = serverTable.get(obj);
392                     String JavaDoc classname = null;
393
394                     try
395                     {
396                         classname = ((DynamicMBean JavaDoc)mbean).getMBeanInfo().getClassName();
397                     }
398                     catch(Exception JavaDoc ex)
399                     {
400                     }
401
402                     try
403                     {
404                         if(query == null)
405                             querySet.add(new ObjectInstance JavaDoc(obj, classname));
406                         else
407                         {
408                             query.setMBeanServer(this);
409                             if(query.apply(obj))
410                                     querySet.add(new ObjectInstance JavaDoc(obj, classname));
411                         }
412                     }
413                     catch(Exception JavaDoc e2)
414                     {
415                         if(!(e2 instanceof NullPointerException JavaDoc))
416                             log.warn("NullPointer Exception",e2);
417                     }
418                 }
419             }
420             else //if(obj.toString().indexOf(name.toString()) != -1)
421
{
422                 String JavaDoc sobj = obj.toString();
423
424                 StringTokenizer JavaDoc st1 = new StringTokenizer JavaDoc(sobj, ":");
425
426                 String JavaDoc lobj = st1.nextToken();
427                 String JavaDoc robj = st1.nextToken();
428
429                 boolean retVal1 = false;
430                 boolean retVal2 = false;
431
432                 retVal1 = JmxUtilities.checkPattern(lobj, dom, wildCard );
433
434                 if(retVal1)
435                     retVal2 = JmxUtilities.checkPattern(robj, des, wildCard );
436
437                 if(retVal2)
438                 {
439                     if(queryName)
440                     {
441                         Object JavaDoc mbean = serverTable.get(obj);
442
443                         try
444                         {
445                             if(query == null)
446                             {
447                                 querySet.add(obj);
448                             }
449                             else
450                             {
451                                 query.setMBeanServer(this);
452                                 if(query.apply(obj))
453                                     querySet.add(obj);
454                             }
455                         }
456                         catch(Exception JavaDoc e3)
457                         {
458                             if(!(e3 instanceof NullPointerException JavaDoc))
459                                 log.warn("Null Pointer Exception",e3);
460                         }
461                     }
462                     else
463                     {
464                         Object JavaDoc mbean = serverTable.get(obj);
465                         String JavaDoc classname = null;
466
467                         try
468                         {
469                             classname = ((DynamicMBean JavaDoc)mbean).getMBeanInfo().getClassName();
470                         }
471                         catch(Exception JavaDoc ex)
472                         {
473                         }
474
475                         try
476                         {
477                             if(query == null)
478                                 querySet.add(new ObjectInstance JavaDoc(obj, classname));
479                             else
480                             {
481                                 query.setMBeanServer(this);
482                                 if(query.apply(obj))
483                                     querySet.add(new ObjectInstance JavaDoc(obj, classname));
484                             }
485                         }
486                         catch(Exception JavaDoc e4)
487                         {
488                             if(!(e4 instanceof NullPointerException JavaDoc))
489                                 log.warn("Null Pointer Exception",e4);
490                         }
491                     }
492                 }
493             }
494         }
495
496         return querySet;
497     }
498
499     /**
500      * Gets the names of MBeans controlled by the MBeanServer.
501      * This method enables any of the following to be obtained:
502      * The names of all MBeans, the names of a set of MBeans specified by
503      * pattern matching on the ObjectName and/or a Query expression, a specific
504      * MBean name(equivalent to testing whether an MBean is registered). When
505      * the object name is null or empty, all objects are to be selected (and
506      * filtered if a query is specified). It returns the set of ObjectNames for
507      * the MBeans selected.
508      *
509      * @param name The object name pattern identifying the MBean names
510      * to be retrieved. If null or empty, the name of all
511      * registered MBeans will be retrieved.
512      *
513      * @param query The query expression to be applied for selecting MBeans.
514      *
515      * @return A set containing the ObjectNames for the MBeans selected.
516      * If no MBean satisfies the query an empty list is returned.
517      */

518     public Set JavaDoc queryNames(ObjectName JavaDoc name, QueryExp JavaDoc query)
519     {
520         return queryFromTable(name, query, true);
521     }
522
523     /**
524      * Registers a pre-existing object as an MBean with the MBeanServer.
525      * If the object name given is null, the MBean may automatically
526      * provide its own name by implementing the MBeanRegistration interface.
527      * The call returns the MBean name.
528      *
529      * @param object The Java Bean to be registered as an MBean.
530      *
531      * @param name The object name of the MBean. May be null.
532      *
533      * @return The ObjectInstance for the MBean that has been registered.
534      *
535      * @exception javax.management.InstanceAlreadyExistsException The MBean
536      * is already under the control of the MBeanServer.
537      *
538      * @exception javax.management.MBeanRegistrationException The preRegister
539      * (MBeanRegistration interface) method of the MBean has
540      * thrown an exception. The MBean will not be registered.
541      *
542      * @exception javax.management.NotCompliantMBeanException This object is
543      * not an JMX compliant MBean
544      */

545     public ObjectInstance JavaDoc registerMBean(Object JavaDoc object, ObjectName JavaDoc name)
546                                 throws InstanceAlreadyExistsException JavaDoc,
547                                         MBeanRegistrationException JavaDoc,
548                                         NotCompliantMBeanException JavaDoc
549     {
550         try
551         {
552             //Hyther:TCK - when user gives DefaultDomain will be
553
//overidden by server.defaultdomain at any cost
554
ObjectName JavaDoc temp = name;
555
556             try
557             {
558                 if(name.getDomain().equals("DefaultDomain") || name.getDomain().equals(""))
559                 {
560                     name = new ObjectName JavaDoc(defaultDomain+":"+name.getKeyPropertyListString());
561                 }
562             }
563             catch(Exception JavaDoc ee)
564             {
565                 name = temp;
566             }
567
568             if(object == null)
569                 throw new IllegalArgumentException JavaDoc();
570
571             if(object instanceof DynamicMBean JavaDoc )
572             {
573                 try
574                 {
575                     MBeanInfo JavaDoc mbi = ((DynamicMBean JavaDoc)(object)).getMBeanInfo();
576                     log.trace("registerMBean:object instanceof RMM");
577                     if(mbi==null)
578                     {
579                         log.debug("MBeanInfo is Null");
580                         throw new NotCompliantMBeanException JavaDoc("Exception occured while invoking the getMBeanInfo() of DynamicMBean");
581                     }
582
583                     if(!isValidMBeanInfo(mbi))
584                     {
585                         throw new NotCompliantMBeanException JavaDoc("MBeanInfo is not proper ");
586                     }
587                 }
588                 catch(Exception JavaDoc ee)
589                 {
590                     throw new NotCompliantMBeanException JavaDoc("Exception occured while invoking the getMBeanInfo() of DynamicMBean");
591                 }
592             }
593
594             //calling the preRegister before registering the MBean
595
boolean isStandard = false;
596             Class JavaDoc className = object.getClass();
597             String JavaDoc fileName = className.getName();
598             Class JavaDoc[] interfaces = className.getInterfaces();
599             for(int i=0; i<interfaces.length; i++)
600             {
601                 if(interfaces[i].getName().equals(fileName+"MBean"))
602                 {
603                     isStandard = true;
604                     break;
605                 }
606             }
607
608             //if(interfaces.length == 0){
609
if(!isStandard)
610             {
611                 Class JavaDoc superClass = className;
612                 //for(;superClass != null;superClass = superClass.getSuperclass()){
613
while(superClass != null)
614                 {
615                     superClass = superClass.getSuperclass();
616                     if(superClass == null)
617                     {
618                         break;
619                     }
620
621                     String JavaDoc superClassName = superClass.getName();
622                     interfaces = superClass.getInterfaces();
623
624                     for(int i=0; i<interfaces.length; i++)
625                     {
626                         if(interfaces[i].getName().equals(superClassName+"MBean"))
627                         {
628                             isStandard = true;
629                             break;
630                         }
631                     }
632                 }
633             }
634
635             if(object instanceof MBeanRegistration JavaDoc)
636             {
637                 log.trace("registerMBean:object instanceOf MBeanRegistration");
638                 ObjectName JavaDoc objName = null;
639
640                 try
641                 {
642                     objName = ((MBeanRegistration JavaDoc)object).preRegister(this,name);
643                 }
644                 catch(RuntimeException JavaDoc re)
645                 {
646                     throw new RuntimeMBeanException JavaDoc(re);
647                 }
648                 catch(Exception JavaDoc e)
649                 {
650                     log.warn("MBeanRegistration Failed",e);
651                     if(e instanceof MBeanRegistrationException JavaDoc)
652                     {
653                         throw (MBeanRegistrationException JavaDoc)e;
654                     }
655
656                     throw new MBeanRegistrationException JavaDoc(e,e.getMessage());
657                 }
658
659                 name = objName;
660             }
661
662             if(name == null)
663             {
664                 try
665                 {
666                     if(object instanceof MBeanRegistration JavaDoc)
667                         ((MBeanRegistration JavaDoc)object).postRegister(new Boolean JavaDoc(false));
668                 }
669                 catch(RuntimeException JavaDoc re)
670                 {
671                     throw new RuntimeMBeanException JavaDoc(re);
672                 }
673                 catch(Exception JavaDoc e)
674                 {
675                     throw new MBeanRegistrationException JavaDoc(e,e.getMessage());
676                 }
677
678                 throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Object name is null"));
679             }
680
681             if(serverTable.get(name) != null)
682             {
683                 try
684                 {
685                     if(object instanceof MBeanRegistration JavaDoc)
686                         ((MBeanRegistration JavaDoc)object).postRegister(new Boolean JavaDoc(false));
687
688                     throw new InstanceAlreadyExistsException JavaDoc();
689                 }
690                 catch(Exception JavaDoc e)
691                 {
692                     /** This required to handle even if postREgiter() throws any Exception */
693                     if(e instanceof RuntimeException JavaDoc)
694                     {
695                         throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc)e);
696                     }
697                     else if(e instanceof InstanceAlreadyExistsException JavaDoc)
698                     {
699                         throw (InstanceAlreadyExistsException JavaDoc)e;
700                     }
701                     else
702                     {
703                         throw new MBeanRegistrationException JavaDoc(e);
704                     }
705                 }
706             }
707
708             /** No Other MBeans except MBeanServerDelegate can be registered with the domain JMImplementation */
709             if(!(name.toString().equals("JMImplementation:type=MBeanServerDelegate")))
710             {
711                 if(name.getDomain().equals("JMImplementation"))
712                 {
713                     throw new JMRuntimeException JavaDoc();
714                 }
715             }
716
717             String JavaDoc oname = object.getClass().getName();
718             //ClassLoader l = object.getClass().getClassLoader();
719
ClassLoader JavaDoc l = getClassLoader(object);
720             testForCompliance(oname,l);
721             //if(! (object instanceof DynamicMBean) )
722
if(isStandard)
723             {
724                 log.trace("Object Not an instance of DynamicMBean");
725                 try
726                 {
727                     object = new DefaultDynamicMBean(object);
728                 }
729                 catch(Exception JavaDoc e)
730                 {
731                     try
732                     {
733                         if(object instanceof MBeanRegistration JavaDoc)
734                             ((MBeanRegistration JavaDoc)object).postRegister(new Boolean JavaDoc(false));
735                     }
736                     catch(Exception JavaDoc poe)
737                     {
738                         throw new MBeanRegistrationException JavaDoc(poe,"Exception occured in postRegister");
739                     }
740
741                     throw new NotCompliantMBeanException JavaDoc();
742                 }
743             }
744
745             if(name.isPattern())
746             {
747                 try
748                 {
749                     if(object instanceof MBeanRegistration JavaDoc)
750                         ((MBeanRegistration JavaDoc)object).postRegister(new Boolean JavaDoc(false));
751                 }
752                 catch(RuntimeException JavaDoc re)
753                 {
754                     throw new RuntimeMBeanException JavaDoc(re);
755                 }
756                 catch(Exception JavaDoc poe)
757                 {
758                     throw new MBeanRegistrationException JavaDoc(poe,"Exception occured in postRegister");
759                 }
760
761                 throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("ObjectName pattern fails"));
762             }
763
764             serverTable.put(name, object);
765             loaderTable.put(oname, l);
766
767             try
768             {
769                 if(object instanceof MBeanRegistration JavaDoc)
770                     ((MBeanRegistration JavaDoc)object).postRegister(new Boolean JavaDoc(true));
771             }
772             catch(RuntimeException JavaDoc re)
773             {
774                 throw new RuntimeMBeanException JavaDoc(re);
775             }
776             catch(Exception JavaDoc poe)
777             {
778                 throw new MBeanRegistrationException JavaDoc(poe,"Exception occured in postRegister");
779             }
780             catch(Throwable JavaDoc th)
781             {
782                 if(th instanceof Error JavaDoc)
783                 {
784                     throw new RuntimeErrorException JavaDoc((Error JavaDoc)th);
785                 }
786                 Exception JavaDoc e = (Exception JavaDoc)th;
787                 throw new MBeanRegistrationException JavaDoc(e,"Exception occured in postRegister");
788             }
789
790             if(object instanceof ClassLoader JavaDoc)
791                 DefaultLoaderRepositoryExt.addLoader((ClassLoader JavaDoc)object);
792
793             if(object instanceof DefaultDynamicMBean &&
794                     ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
795             {
796                 DefaultLoaderRepositoryExt.addLoader((ClassLoader JavaDoc)((DefaultDynamicMBean)object).getStandardMBeanObject());
797             }
798
799             //send Registration Notification
800
Notification JavaDoc notif = new MBeanServerNotification JavaDoc(
801                             MBeanServerNotification.REGISTRATION_NOTIFICATION,
802                             delegate.getClass().getName(),
803                             sequenceNumber++,
804                             name);
805
806             notif.setTimeStamp(System.currentTimeMillis());
807             delegate.sendNotification(notif);
808
809             if(object instanceof DefaultDynamicMBean)
810             {
811                 return new ObjectInstance JavaDoc(name, ((DefaultDynamicMBean)object).getStandardMBeanObject().getClass().getName());
812             }
813             else
814             {
815                 return new ObjectInstance JavaDoc(name, object.getClass().getName());
816             }
817         }
818         catch(IllegalArgumentException JavaDoc ie)
819         {
820             throw new RuntimeOperationsException JavaDoc(ie);
821         }
822     }
823
824     /**
825      * De-registers an MBean from the MBeanServer. The MBean is identified by
826      * its object name. Once the method has been invoked, the MBean may no
827      * longer be accessed by its object name.
828      *
829      * @param name The object name of the MBean to be de-registered.
830      *
831      * @exception javax.management.InstanceNotFoundException The
832      * specified MBean is not registered in the MBeanServer.
833      *
834      * @exception javax.management.MBeanRegistrationException The
835      * preDeregister (MBeanRegistration interface) method of the
836      * MBean has thrown an exception.
837      **/

838     public void unregisterMBean(ObjectName JavaDoc name)
839             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
840     {
841         log.trace("Unregistering MBean with object name " + name);
842
843         if(name == null)
844             throw new RuntimeOperationsException JavaDoc(
845                 new IllegalArgumentException JavaDoc("ObjectName cannot be null!!!"));
846
847         Object JavaDoc object = null;
848
849         if((object = serverTable.get(name)) == null)
850             throw new InstanceNotFoundException JavaDoc();
851
852         if(object instanceof MBeanRegistration JavaDoc)
853         {
854             try
855             {
856                 ((MBeanRegistration JavaDoc)object).preDeregister();
857             }
858             catch(RuntimeException JavaDoc re)
859             {
860                 throw new RuntimeMBeanException JavaDoc(re);
861             }
862             catch(Exception JavaDoc e)
863             {
864                 log.warn("Exception while unregistering the MBean ");
865                 if(e instanceof MBeanRegistrationException JavaDoc)
866                 {
867                     throw (MBeanRegistrationException JavaDoc)e;
868                 }
869
870                 throw new MBeanRegistrationException JavaDoc(e);
871             }
872         }
873
874         if(name.toString().equals("JMImplementation:type=MBeanServerDelegate"))
875             throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
876                             "MBeanServerDelegate Cannot be unregistered"));
877
878         Object JavaDoc o =serverTable.get(name);
879         serverTable.remove(name);
880         loaderTable.remove(name);
881
882         if(o instanceof DefaultDynamicMBean)
883         {
884             if(((DefaultDynamicMBean)o).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
885             {
886                 DefaultLoaderRepositoryExt.removeLoader((ClassLoader JavaDoc)(((DefaultDynamicMBean)o).getStandardMBeanObject()));
887             }
888         }
889
890         if(o instanceof ClassLoader JavaDoc)
891         {
892             DefaultLoaderRepositoryExt.removeLoader((ClassLoader JavaDoc)o);
893         }
894
895         if(object instanceof MBeanRegistration JavaDoc)
896         {
897             try
898             {
899                 ((MBeanRegistration JavaDoc)object).postDeregister();
900             }
901             catch(Exception JavaDoc e)
902             {
903                 if( e instanceof RuntimeException JavaDoc)
904                 {
905                     RuntimeException JavaDoc rt = (RuntimeException JavaDoc)e;
906                     throw new RuntimeMBeanException JavaDoc(rt);
907                 }
908             }
909             catch(Error JavaDoc e)
910             {
911                  throw new RuntimeErrorException JavaDoc(e);
912             }
913         }
914
915         //send registration Notification...
916
Notification JavaDoc notif = new MBeanServerNotification JavaDoc(
917                             MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
918                             delegate.getClass().getName(),
919                             sequenceNumber++,
920                             name);
921
922         delegate.sendNotification(notif);
923     }
924
925     /**
926      * Gets the value of a specific attribute of a named MBean.
927      * The MBean is identified by its object name.
928      *
929      * @param name The object name of the MBean from which the
930      * attribute is to be retrieved.
931      *
932      * @param attribute A String specifying the name of the
933      * attribute to be retrieved.
934      *
935      * @return The value of the retrieved attribute.
936      *
937      * @exception javax.management.AttributeNotFoundException The
938      * specified attribute is not accessible in the MBean.
939      *
940      * @exception javax.management.MBeanException Wraps an exception
941      * thrown by the MBean's getter.
942      *
943      * @exception javax.management.InstanceNotFoundException The
944      * specified MBean is not registered in the MBeanServer.
945      *
946      * @exception javax.management.ReflectionException Wraps an
947      * java.lang.Exception thrown while trying to invoke the setter.
948      **/

949     public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute)
950                         throws MBeanException JavaDoc,
951                                AttributeNotFoundException JavaDoc,
952                                InstanceNotFoundException JavaDoc,
953                                ReflectionException JavaDoc
954     {
955         log.trace("getAttribute - getting value for attribute " + attribute
956                                 + " of MBean with ObjectName " + name);
957
958         if(name == null || attribute == null)
959             throw new RuntimeOperationsException JavaDoc(
960                     new IllegalArgumentException JavaDoc("Null Values not possible"));
961
962         Object JavaDoc object = null;
963         if((object = serverTable.get(name)) == null)
964                 throw new InstanceNotFoundException JavaDoc();
965
966         if(object instanceof DynamicMBean JavaDoc)
967         {
968             Object JavaDoc toRet = null;
969
970             synchronized(object)
971             {
972                 try
973                 {
974                     toRet = ((DynamicMBean JavaDoc)object).getAttribute(attribute);
975                 }
976                 catch(AttributeNotFoundException JavaDoc ae)
977                 {
978                     throw ae;
979                 }
980                 catch(ReflectionException JavaDoc re)
981                 {
982                     Exception JavaDoc e = re.getTargetException();
983                     if(e instanceof InvocationTargetException JavaDoc)
984                     {
985                         InvocationTargetException JavaDoc it = (InvocationTargetException JavaDoc)e;
986                         Exception JavaDoc ee = (Exception JavaDoc)it.getTargetException();
987                         if(ee instanceof RuntimeException JavaDoc)
988                         {
989                             RuntimeException JavaDoc rt = (RuntimeException JavaDoc)ee;
990                             throw new RuntimeMBeanException JavaDoc(rt);
991                         }
992                         else
993                         {
994                             throw new MBeanException JavaDoc(ee);
995                         }
996                     }
997
998                     throw new MBeanException JavaDoc(e);
999                 }
1000                catch(Exception JavaDoc e)
1001                {
1002                        if(e instanceof RuntimeErrorException JavaDoc)
1003                        {
1004                            RuntimeErrorException JavaDoc rt = (RuntimeErrorException JavaDoc)e;
1005                            throw rt;
1006                        }
1007                        throw new MBeanException JavaDoc(e);
1008                }
1009            }
1010
1011            return toRet;
1012        }
1013
1014        return null;
1015    }
1016
1017    /**
1018     * Enables the values of several attributes of a named MBean.
1019     * The MBean is identified by its object name.
1020     *
1021     * @param name The object name of the MBean from which the
1022     * attributes are to be retrieved.
1023     *
1024     * @param attributes A list of the attributes to be retrieved.
1025     *
1026     * @return The list of the retrieved attributes.
1027     *
1028     * @exception javax.management.InstanceNotFoundException The
1029     * specified MBean is not registered in the MBeanServer.
1030     **/

1031    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes)
1032                        throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
1033    {
1034        log.trace("getAttributes entered");
1035
1036        int length = 0;
1037
1038        if(name == null || attributes == null)
1039            throw new RuntimeOperationsException JavaDoc(
1040                new IllegalArgumentException JavaDoc("Null values not possible"));
1041
1042        Object JavaDoc object = null;
1043
1044        if((object = serverTable.get(name)) == null)
1045            throw new InstanceNotFoundException JavaDoc();
1046
1047        length = attributes.length;
1048
1049        for(int i=0 ; i<length ; i++)
1050        {
1051            if (attributes[i] == null)
1052            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Null values not possible"));
1053        }
1054
1055
1056        if(object instanceof DynamicMBean JavaDoc)
1057        {
1058            synchronized(object)
1059            {
1060                try
1061                {
1062                      return ((DynamicMBean JavaDoc)object).getAttributes(attributes);
1063                }
1064                catch(Exception JavaDoc ex)
1065                {
1066                    new ReflectionException JavaDoc(ex);
1067                }
1068            }
1069        }
1070
1071        return null;
1072    }
1073
1074    /**
1075     * Invokes an action on an MBean.
1076     *
1077     * @param name The object name of the MBean on which the method is to be invoked.
1078     *
1079     * @param actionName The name of the action to be invoked.
1080     *
1081     * @param params An array containing the parameters to be set when the action is invoked
1082     *
1083     * @param signature An array containing the signature of the action.
1084     * The class objects will be loaded using the same class loader
1085     * as the one used for loading the MBean on which the action was invoked.
1086     *
1087     * @return The object returned by the action, which represents the result
1088     * of invoking the action on the specified MBean.
1089     *
1090     * @exception javax.management.InstanceNotFoundException The
1091     * specified MBean is not registered in the MBeanServer.
1092     *
1093     * @exception javax.management.MBeanException Wraps an exception
1094     * thrown by the MBean's invoked method.
1095     *
1096     * @exception javax.management.ReflectionException Wraps an
1097     * java.lang.Exception thrown while trying to invoke the method.
1098     **/

1099    public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc actionName,
1100                         Object JavaDoc[] params, String JavaDoc[] signature)
1101                throws InstanceNotFoundException JavaDoc,
1102                        MBeanException JavaDoc,
1103                        ReflectionException JavaDoc
1104    {
1105        Object JavaDoc object = null;
1106
1107        if((object = serverTable.get(name)) == null)
1108        {
1109            throw new InstanceNotFoundException JavaDoc();
1110        }
1111
1112        if(object instanceof DynamicMBean JavaDoc)
1113        {
1114            synchronized(object)
1115            {
1116                Object JavaDoc retObj = null;
1117                if( object instanceof DefaultDynamicMBean)
1118                {
1119                    try
1120                    {
1121                        retObj = ((DynamicMBean JavaDoc)object).invoke(actionName, params, signature);
1122                    }
1123                    catch(RuntimeOperationsException JavaDoc roe)
1124                    {
1125                        throw roe;
1126                    }
1127                    catch(RuntimeException JavaDoc re)
1128                    {
1129                        RuntimeException JavaDoc rt = (RuntimeException JavaDoc)re;
1130                        if(rt instanceof RuntimeOperationsException JavaDoc)
1131                        {
1132                            throw (RuntimeOperationsException JavaDoc)rt;
1133                        }
1134
1135                        if(rt instanceof RuntimeErrorException JavaDoc)
1136                        {
1137                            throw (RuntimeErrorException JavaDoc)rt;
1138                        }
1139
1140                        if(rt instanceof RuntimeMBeanException JavaDoc)
1141                        {
1142                            throw rt;
1143                        }
1144
1145                        throw new RuntimeMBeanException JavaDoc(rt);
1146                    }
1147                    catch(ReflectionException JavaDoc re)
1148                    {
1149                        throw re;
1150                    }
1151                    catch(MBeanException JavaDoc me)
1152                    {
1153                        Exception JavaDoc e = me.getTargetException();
1154
1155                        if(e instanceof RuntimeException JavaDoc)
1156                        {
1157                            RuntimeException JavaDoc rt = (RuntimeException JavaDoc)e;
1158                            throw new RuntimeMBeanException JavaDoc(rt);
1159                        }
1160
1161                        throw me;
1162                    }
1163
1164                }
1165                else
1166                {
1167                    try
1168                    {
1169                        retObj= ((DynamicMBean JavaDoc)object).invoke(actionName, params, signature);
1170                    }
1171                    catch(ReflectionException JavaDoc re)
1172                    {
1173                        Exception JavaDoc e = re.getTargetException();
1174                        if(e instanceof MBeanException JavaDoc)
1175                        {
1176                            MBeanException JavaDoc mb = (MBeanException JavaDoc)e;
1177                            throw mb;
1178                        }
1179                        else
1180                        {
1181                            throw re;
1182                        }
1183                    }
1184                }
1185
1186                return retObj;
1187            }
1188        }
1189
1190        return null;
1191    }
1192
1193    /**
1194     * Sets the value of a specific attribute of a named MBean.
1195     * The MBean is identified by its object name.
1196     *
1197     * @param name The name of the MBean within which the attribute is to be set.
1198     *
1199     * @param attribute The identification of the attribute to
1200     * be set and the value it is to be set to.
1201     *
1202     * @return The value of the attribute that has been set.
1203     *
1204     * @exception javax.management.InstanceNotFoundException The
1205     * specified MBean is not registered in the MBeanServer.
1206     *
1207     * @exception javax.management.AttributeNotFoundException The
1208     * specified attribute is not accessible in the MBean.
1209     *
1210     * @exception javax.management.InvalidAttributeValueException The
1211     * specified value for the attribute is not valid.
1212     *
1213     * @exception javax.management.MBeanException Wraps an exception
1214     * thrown by the MBean's setter.
1215     *
1216     * @exception javax.management.ReflectionException Wraps an
1217     * java.lang.Exception thrown while trying to invoke the setter.
1218     **/

1219    public void setAttribute(ObjectName JavaDoc name, Attribute JavaDoc attribute)
1220                      throws InstanceNotFoundException JavaDoc,
1221                             AttributeNotFoundException JavaDoc,
1222                             InvalidAttributeValueException JavaDoc,
1223                             MBeanException JavaDoc,
1224                             ReflectionException JavaDoc
1225    {
1226        log.trace("setAttribute - setting attribute");
1227
1228        if(name == null || attribute == null)
1229            throw new RuntimeOperationsException JavaDoc(
1230                new IllegalArgumentException JavaDoc("Null Values not possible"));
1231
1232        Object JavaDoc object = null;
1233        if((object = serverTable.get(name)) == null)
1234            throw new InstanceNotFoundException JavaDoc("No Mbean " + name + "present in the JMXServer");
1235
1236        synchronized(object)
1237        {
1238            ((DynamicMBean JavaDoc)object).setAttribute(attribute);
1239        }
1240    }
1241
1242    /**
1243     * Sets the values of several attributes of a named MBean.
1244     * The MBean is identified by its object name.
1245     *
1246     * @param name The object name of the MBean within which the attributes are to be set.
1247     *
1248     * @param attributes A list of attributes: The identification of the
1249     * attributes to be set and the values they are to be set to.
1250     *
1251     * @return The list of attributes that were set, with their new values.
1252     *
1253     * @exception javax.management.InstanceNotFoundException The specified
1254     * MBean is not registered in the MBeanServer.
1255     */

1256    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name, AttributeList JavaDoc attributes)
1257            throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
1258    {
1259        log.trace("setAttributes called for MBean with ObjectName " + name);
1260
1261        if(name == null || attributes == null)
1262                throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Either ObjectName / attributes is null"));
1263
1264        Object JavaDoc object = null;
1265
1266        if((object = serverTable.get(name)) == null)
1267            throw new InstanceNotFoundException JavaDoc();
1268
1269        if(object instanceof DynamicMBean JavaDoc)
1270        {
1271            synchronized(object)
1272            {
1273                return ((DynamicMBean JavaDoc)object).setAttributes(attributes);
1274            }
1275        }
1276
1277        return null;
1278    }
1279
1280    /**
1281     * This method discovers the attributes and operations that an MBean exposes
1282     * for management. When flatten is false, inherited attributes are not returned.
1283     *
1284     * @param name The name of the MBean to analyze
1285     *
1286     * @return An instance of MBeanInfo allowing to retrieve all attributes
1287     * and operations of this MBean.
1288     *
1289     * @exception java.beans.IntrospectionException An exception
1290     * occurs during introspection.
1291     *
1292     * @exception javax.management.InstanceNotFoundException The specified
1293     * MBean is not found.
1294     **/

1295    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name)
1296                           throws InstanceNotFoundException JavaDoc,
1297                                  IntrospectionException JavaDoc,
1298                                  ReflectionException JavaDoc
1299    {
1300        log.trace("getting MBeanInfo for MBean with ObjectName " + name);
1301
1302        Object JavaDoc object = null;
1303
1304        if((object = serverTable.get(name)) == null)
1305            throw new InstanceNotFoundException JavaDoc();
1306
1307        if(object instanceof DynamicMBean JavaDoc)
1308        {
1309            synchronized(object)
1310            {
1311                MBeanInfo JavaDoc info = null;
1312                try
1313                {
1314                    info = ((DynamicMBean JavaDoc)object).getMBeanInfo();
1315                    if(info == null)
1316                    {
1317                        throw new JMRuntimeException JavaDoc();
1318                    }
1319                }
1320                catch(Exception JavaDoc e)
1321                {
1322                    if(e instanceof JMRuntimeException JavaDoc)
1323                    {
1324                        throw (JMRuntimeException JavaDoc)e;
1325                    }
1326                    if(e instanceof RuntimeException JavaDoc)
1327                    {
1328                        RuntimeException JavaDoc rt = (RuntimeException JavaDoc)e;
1329                        throw new RuntimeMBeanException JavaDoc(rt);
1330                    }
1331                }
1332
1333                return info;
1334            }
1335        }
1336
1337        return null;
1338    }
1339
1340    /**
1341     * Gets the ObjectInstance for a given MBean registered with the MBean server.
1342     *
1343     * @param name The object name of the MBean.
1344     *
1345     * @return The ObjectInstance associated to the MBean specified by name.
1346     *
1347     * @exception javax.management.InstanceNotFoundException The MBean specified
1348     * is not registered in the MBean server.
1349     */

1350    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc name)
1351                                    throws InstanceNotFoundException JavaDoc
1352    {
1353        if (name == null)
1354            throw new InstanceNotFoundException JavaDoc("The specified ObjectName is null");
1355
1356        if( ! isRegistered(name) )
1357            throw new InstanceNotFoundException JavaDoc(name.toString());
1358
1359        Object JavaDoc mbean = serverTable.get(name);
1360        String JavaDoc classname = null;
1361
1362        try
1363        {
1364            if(mbean instanceof javax.management.modelmbean.ModelMBean JavaDoc)
1365                classname = "javax.management.ModelMBean";
1366            else
1367                classname = ((DynamicMBean JavaDoc)mbean).getMBeanInfo().getClassName();
1368        }
1369        catch(Exception JavaDoc ex)
1370        {
1371        }
1372
1373        try
1374        {
1375            return new ObjectInstance JavaDoc(name, classname);
1376        }
1377        catch(Exception JavaDoc ee)
1378        {
1379            log.error("ObjectInstance Creation failed",ee);
1380            return null;
1381        }
1382    }
1383
1384    /**
1385     * Enables a couple (listener,handback) for a registered MBean to be added.
1386     * @param name The name of the MBean on which the listener should be added.
1387     * @param listener The listener object which will handles notifications
1388     * emitted by the registered MBean.
1389     * @param filter The filter object. If not specified, no filtering will be
1390     * performed before handling notifications.
1391     * @param handback The context to be sent to the listener when a notification
1392     * is emitted.
1393     * @exception javax.management.InstanceNotFoundException The MBean name
1394     * doesn't correspond to a registered MBean.
1395     * @exception java.lang.IllegalArgumentException The object with name "name"
1396     * doesn't implements NotificationBroadcaster
1397     * @exception javax.management.InstanceAlreadyExistsException The couple
1398     * (listener,handback) is already registered in the MBean.
1399     **/

1400    public void addNotificationListener(ObjectName JavaDoc name,
1401                                  NotificationListener JavaDoc listener,
1402                                  NotificationFilter JavaDoc filter,
1403                                  Object JavaDoc handback)
1404                           throws InstanceNotFoundException JavaDoc
1405    {
1406        Object JavaDoc object = null;
1407
1408        if(name == null)
1409        {
1410            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1411                                            "ObjectName cannot be null!!!"));
1412        }
1413
1414        object = serverTable.get(name);
1415
1416        if(object == null)
1417        {
1418            throw new InstanceNotFoundException JavaDoc("No object with the name " +
1419                                        name + " present in the JMXServer");
1420        }
1421
1422        if( !(object instanceof NotificationBroadcaster JavaDoc))
1423        {
1424            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1425                "MBean " + name + " does not implement NotificationBroadCaster"));
1426        }
1427
1428        if(listener == null)
1429        {
1430            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1431                                        "Null Listener is being passed !!!"));
1432        }
1433
1434        synchronized(object)
1435        {
1436            ((NotificationBroadcaster JavaDoc) object)
1437                        .addNotificationListener(listener, filter, handback);
1438        }
1439    }
1440
1441    /**
1442     * Enables a couple (listener,handback) for a registered MBean to be added.
1443     *
1444     * @param name The name of the MBean on which the listener should be added.
1445     *
1446     * @param listener The listener name which will handles notifications
1447     * emitted by the registered MBean.
1448     *
1449     * @param filter The filter object. If not specified, no filtering will be
1450     * performed before handling notifications.
1451     *
1452     * @param handback The context to be sent to the listener when a
1453     * notification is emitted.
1454     *
1455     * @exception javax.management.InstanceNotFoundException The MBean name
1456     * doesn't correspond to a registered MBean.
1457     *
1458     * @exception java.lang.IllegalArgumentException The object with name "name"
1459     * doesn't implements NotificationBroadcaster
1460     *
1461     * @exception javax.management.InstanceAlreadyExistsException The couple
1462     * (listener,handback) is already registered in the MBean.
1463     **/

1464    public void addNotificationListener(ObjectName JavaDoc name,
1465                                  ObjectName JavaDoc listener,
1466                                  NotificationFilter JavaDoc filter,
1467                                  Object JavaDoc handback)
1468                           throws InstanceNotFoundException JavaDoc
1469    {
1470        Object JavaDoc object = null;
1471
1472        if(name == null)
1473        {
1474            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1475                                            "ObjectName cannot be null!!!"));
1476        }
1477
1478        object = serverTable.get(name);
1479
1480        if(object == null)
1481        {
1482            throw new InstanceNotFoundException JavaDoc("No object with the name " +
1483                                            name + " present in the JMXServer");
1484        }
1485
1486        if( !(object instanceof NotificationBroadcaster JavaDoc))
1487        {
1488            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1489                "MBean " + name + " does not implement NotificationBroadCaster"));
1490        }
1491
1492        Object JavaDoc lobject = null;
1493        lobject = serverTable.get(listener);
1494
1495        if(lobject == null)
1496        {
1497            throw new InstanceNotFoundException JavaDoc("No listener with the name "
1498                                + listener + " present in the JMXServer!!!");
1499        }
1500
1501        if(lobject instanceof DefaultDynamicMBean)
1502        {
1503            if(!(((DefaultDynamicMBean)(lobject)).getStandardMBeanObject() instanceof NotificationListener JavaDoc))
1504            {
1505                throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1506                    "MBean " + name + " does not implement NotificationListener"));
1507            }
1508        }
1509
1510        if( !(lobject instanceof NotificationListener JavaDoc))
1511        {
1512            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1513                "MBean " + name + " does not implement NotificationListener"));
1514        }
1515
1516        synchronized(object)
1517        {
1518            ((NotificationBroadcaster JavaDoc)object).addNotificationListener( (NotificationListener JavaDoc)lobject,filter,handback);
1519        }
1520    }
1521
1522    /**
1523     * Instantiates and registers a MBean with the MBeanServer.
1524     * The MBean server will use the default loader repository to load the
1525     * class of the MBean. An object name is associated to the MBean. If the
1526     * object name given is null, the MBean can automatically provide its own
1527     * name by implementing the MBeanRegistration interface. The call returns a
1528     * reference to the new instance and its object name.
1529     *
1530     * @param className The class name of the MBean to be instantiated.
1531     *
1532     * @param name The object name of the MBean. May be null.
1533     *
1534     * @return An ObjectInstance, containing the ObjectName and the Java class
1535     * name of the newly instantiated MBean.
1536     *
1537     * @exception javax.management.ReflectionException Wraps Wraps a
1538     * ClassNotFoundException or a java.lang.Exception that
1539     * occured trying to invoke the MBean's constructor.
1540     *
1541     * @exception javax.management.InstanceAlreadyExistsException The MBean is
1542     * already under the control of the MBeanServer.
1543     *
1544     * @exception javax.management.MBeanRegistrationException The preRegister
1545     * (MBeanRegistration interface) method of the MBean has
1546     * thrown an exception. The MBean will not be registered.
1547     *
1548     * @exception javax.management.MBeanException The constructor of the
1549     * MBean has thrown an exception
1550     *
1551     * @exception javax.management.NotCompliantMBeanException This class is
1552     * not an JMX compliant MBean
1553     **/

1554    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name)
1555                               throws ReflectionException JavaDoc,
1556                                      InstanceAlreadyExistsException JavaDoc,
1557                                      MBeanRegistrationException JavaDoc,
1558                                      MBeanException JavaDoc,
1559                                      NotCompliantMBeanException JavaDoc
1560    {
1561        log.trace("createMBean called with className and ObjectName");
1562
1563        Object JavaDoc obj = null;
1564
1565        if(className == null)
1566            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1567                                "ClassName or ObjectName cannot be null"));
1568
1569        Class JavaDoc class_name = null;
1570
1571        try
1572        {
1573            class_name = Thread.currentThread().getContextClassLoader().loadClass(className);
1574        }
1575        catch(ClassNotFoundException JavaDoc cne)
1576        {
1577            try
1578            {
1579                class_name = DefaultLoaderRepositoryExt.loadClass(className);
1580            }
1581            catch(ClassNotFoundException JavaDoc cnee)
1582            {
1583                throw new ReflectionException JavaDoc(cnee, "The MBean class could not be loaded by the default loader repository");
1584            }
1585        }
1586
1587        try
1588        {
1589            obj = class_name.newInstance();
1590        }
1591        catch(InstantiationException JavaDoc ite)
1592        {
1593            if(Modifier.isAbstract(class_name.getModifiers()))
1594            {
1595                throw new NotCompliantMBeanException JavaDoc();
1596            }
1597            try
1598            {
1599                Constructor JavaDoc cons = class_name.getConstructor(new Class JavaDoc[0]);
1600            }
1601            catch(NoSuchMethodException JavaDoc e)
1602            {
1603                throw new ReflectionException JavaDoc(e);
1604            }
1605
1606            throw new MBeanException JavaDoc(ite,"Exception occured while invoking the constructor of the MBean!!!");
1607        }
1608        catch(IllegalAccessException JavaDoc iae)
1609        {
1610            throw new MBeanException JavaDoc(iae,"Exception occured while invoking the constructor of the MBean!!!");
1611        }
1612        catch(Exception JavaDoc e)
1613        {
1614            if(e instanceof NotCompliantMBeanException JavaDoc)
1615            {
1616                throw (NotCompliantMBeanException JavaDoc)e;
1617            }
1618
1619            throw new MBeanException JavaDoc(e, "General Exception occured. It might" +
1620                            " be runtime errors while invoking the constructor");
1621        }
1622
1623        return registerMBean(obj, name);
1624    }
1625
1626    /**
1627     * Instantiates and registers a MBean with the MBeanServer.
1628     * The MBean server will use the default loader repository to load the
1629     * class of the MBean. An object name is associated to the MBean. If the
1630     * object name given is null, the MBean can automatically provide its own
1631     * name by implementing the MBeanRegistration interface. The call returns
1632     * a reference to the new instance and its object name.
1633     *
1634     * @param className The class name of the MBean to be instantiated.
1635     *
1636     * @param name The object name of the MBean. May be null.
1637     *
1638     * @param params An array containing the parameters of the constructor
1639     * to be invoked.
1640     *
1641     * @param signature An array containing the signature of the constructor
1642     * to be invoked.
1643     *
1644     * @return An ObjectInstance, containing the ObjectName and the Java class
1645     * name of the newly instantiated MBean.
1646     *
1647     * @exception javax.management.ReflectionException Wraps Wraps a
1648     * ClassNotFoundException or a java.lang.Exception that
1649     * occured trying to invoke the MBean's constructor.
1650     *
1651     * @exception javax.management.InstanceAlreadyExistsException The MBean is
1652     * already under the control of the MBeanServer.
1653     *
1654     * @exception javax.management.MBeanRegistrationException The preRegister
1655     * (MBeanRegistration interface) method of the MBean has
1656     * thrown an exception. The MBean will not be registered.
1657     *
1658     * @exception javax.management.MBeanException The constructor of the MBean
1659     * has thrown an exception
1660     **/

1661    public ObjectInstance JavaDoc createMBean(String JavaDoc className,
1662                                      ObjectName JavaDoc name,
1663                                      Object JavaDoc[] params,
1664                                      String JavaDoc[] signature)
1665                             throws ReflectionException JavaDoc,
1666                                    InstanceAlreadyExistsException JavaDoc,
1667                                    MBeanRegistrationException JavaDoc,
1668                                    MBeanException JavaDoc,
1669                                    NotCompliantMBeanException JavaDoc
1670    {
1671        log.trace("createMBean called with className, ObjectName, " +
1672                    "parameters as Object array and signature as String array");
1673
1674        Object JavaDoc obj = null;
1675
1676        if(className == null)
1677        {
1678            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1679                                "ClassName or ObjectName cannot be null"));
1680        }
1681
1682        Class JavaDoc class_name = null;
1683
1684        try
1685        {
1686            class_name = DefaultLoaderRepositoryExt.loadClass(className);
1687
1688            if(params == null || signature == null)
1689            {
1690                return createMBean(className,name);
1691            }
1692
1693            Class JavaDoc[] sig = new Class JavaDoc[signature.length];
1694
1695            for(int i = 0;i<signature.length;i++)
1696            {
1697                if((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
1698                {
1699                    try
1700                    {
1701                        sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
1702                    }
1703                    catch(ClassNotFoundException JavaDoc ee)
1704                    {
1705                        throw new ReflectionException JavaDoc(ee, "The MBean class " +
1706                            "could not be loaded by the default loader repository");
1707                    }
1708
1709                    try
1710                    {
1711                        if(obj instanceof DefaultDynamicMBean &&
1712                            ((DefaultDynamicMBean)obj).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
1713                        {
1714                            sig[i] = ((ClassLoader JavaDoc)((DefaultDynamicMBean)obj).getStandardMBeanObject()).loadClass(signature[i]);
1715                        }
1716                    }
1717                    catch(ClassNotFoundException JavaDoc ce)
1718                    {
1719                        try
1720                        {
1721                            sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
1722                        }
1723                        catch(ClassNotFoundException JavaDoc ee)
1724                        {
1725                            throw new ReflectionException JavaDoc(ee, "The MBean class could not be loaded by the default loader repository");
1726                        }
1727                    }
1728
1729                }
1730            }
1731
1732            Constructor JavaDoc constr = null;
1733
1734            constr = class_name.getConstructor(sig);
1735            obj = constr.newInstance(params);
1736        }
1737        catch(ClassNotFoundException JavaDoc cne)
1738        {
1739            throw new ReflectionException JavaDoc(cne, "The MBean class could not be loaded by the default loader repository");
1740        }
1741        catch(NoSuchMethodException JavaDoc ne)
1742        {
1743            throw new ReflectionException JavaDoc(ne);
1744        }
1745        catch(IllegalArgumentException JavaDoc iae)
1746        {
1747            throw new ReflectionException JavaDoc(iae);
1748        }
1749        catch(InstantiationException JavaDoc ie)
1750        {
1751            try
1752            {
1753                Class JavaDoc[] classParams = new Class JavaDoc[params.length];
1754
1755                for(int i=0; i<params.length; i++)
1756                {
1757                    classParams[i] = params[i].getClass();
1758                }
1759
1760                Constructor JavaDoc cons = class_name.getConstructor(classParams);
1761            }
1762            catch(NoSuchMethodException JavaDoc ne)
1763            {
1764                throw new ReflectionException JavaDoc(ne);
1765            }
1766
1767            throw new MBeanException JavaDoc(ie);
1768        }
1769        catch(Exception JavaDoc e)
1770        {
1771            if(e instanceof ReflectionException JavaDoc)
1772            {
1773                throw (ReflectionException JavaDoc)e;
1774            }
1775
1776            throw new MBeanException JavaDoc(e, "Exception occured while calling the constructor of the MBean!!!");
1777        }
1778
1779        return registerMBean(obj, name);
1780    }
1781
1782    /**
1783     * Instantiates and registers a MBean with the MBeanServer.
1784     * The class loader to be used is identified by its object name. An object
1785     * name is associated to the MBean. If the object name of the loader is null,
1786     * the system ClassLoader will be used.If the MBean's object name given is
1787     * null, the MBean can automatically provide its own name by implementing
1788     * the MBeanRegistration interface. The call returns a reference to the new
1789     * instance and its object name.
1790     *
1791     * @param className The class name of the MBean to be instantiated.
1792     *
1793     * @param name The object name of the MBean. May be null.
1794     *
1795     * @param loaderName The object name of the class loader to be used.
1796     *
1797     * @return An ObjectInstance, containing the ObjectName and the Java class
1798     * name of the newly instantiated MBean.
1799     *
1800     * @exception javax.management.InstanceAlreadyExistsException The MBean is
1801     * already under the control of the MBeanServer.
1802     *
1803     * @exception javax.management.MBeanRegistrationException The preRegister
1804     * (MBeanRegistration interface) method of the MBean has
1805     * thrown an exception. The MBean will not be registered.
1806     *
1807     * @exception javax.management.MBeanException The constructor of the MBean
1808     * has thrown an exception
1809     *
1810     * @exception javax.management.NotCompliantMBeanException This class is not
1811     * an JMX compliant MBean
1812     *
1813     * @exception javax.management.InstanceNotFoundException The specified class
1814     * loader is not registered in the MBeanServer.
1815     **/

1816    public ObjectInstance JavaDoc createMBean(String JavaDoc className,
1817                                    ObjectName JavaDoc name,
1818                                    ObjectName JavaDoc loaderName)
1819                             throws ReflectionException JavaDoc,
1820                                    InstanceAlreadyExistsException JavaDoc,
1821                                    MBeanRegistrationException JavaDoc,
1822                                    MBeanException JavaDoc,
1823                                    NotCompliantMBeanException JavaDoc,
1824                                    InstanceNotFoundException JavaDoc
1825    {
1826        log.trace("createMBean called with className, ObjectName and " +
1827                                                "loaderName as ObjectName");
1828
1829        Object JavaDoc object = null;
1830        Class JavaDoc clazz = null;
1831        Object JavaDoc obj = null;
1832
1833        if(className == null )
1834        {
1835            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1836                                "ClassName or ObjectName cannot be null"));
1837        }
1838
1839        try
1840        {
1841            if(loaderName == null)
1842            {
1843                //clazz = this.getClass().getClassLoader().loadClass(className);
1844
clazz = getClassLoader(this).loadClass(className);
1845            }
1846            else if((object = serverTable.get(loaderName)) == null)
1847            {
1848                //clazz = this.getClass().getClassLoader().loadClass(className);
1849
//clazz = getClassLoader(this).loadClass(className);
1850
throw new InstanceNotFoundException JavaDoc(loaderName.toString());
1851            }
1852            else if(object instanceof DefaultDynamicMBean &&
1853                ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
1854            {
1855                clazz = ((ClassLoader JavaDoc)((DefaultDynamicMBean)object).getStandardMBeanObject()).loadClass(className);
1856            }
1857            else if( !(object instanceof ClassLoader JavaDoc))
1858            {
1859                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader"));
1860            }
1861            else
1862            {
1863                clazz = ((ClassLoader JavaDoc)object).loadClass(className);
1864            }
1865
1866            obj = clazz.newInstance();
1867        }
1868        catch(ClassNotFoundException JavaDoc cne)
1869        {
1870            throw new ReflectionException JavaDoc(cne, "The MBean class could not be loaded by the available class Loaders");
1871        }
1872        catch(InstantiationException JavaDoc ite)
1873        {
1874            try
1875            {
1876                Constructor JavaDoc cons = clazz.getConstructor(new Class JavaDoc[0]);
1877            }
1878            catch(NoSuchMethodException JavaDoc ne)
1879            {
1880                throw new ReflectionException JavaDoc(ne);
1881            }
1882
1883            throw new MBeanException JavaDoc(ite);
1884        }
1885        catch(IllegalAccessException JavaDoc iae)
1886        {
1887            throw new MBeanException JavaDoc(iae,"Exception occured while invoking the constructor of the MBean!!!");
1888        }
1889        catch(Exception JavaDoc e)
1890        {
1891            throw new MBeanException JavaDoc(e,"General Exception occured .It might be runtime errors while invoking the constructor");
1892        }
1893
1894        return registerMBean(obj, name);
1895    }
1896
1897    /**
1898     * Instantiates and registers a MBean with the MBeanServer. The class
1899     * loader to be used is identified by its object name. An object name
1900     * is associated to the MBean. If the object name of the loader is not
1901     * specified, the system ClassLoader will be used.If the MBean object
1902     * name given is null, the MBean can automatically provide its own name
1903     * by implementing the MBeanRegistration interface. The call returns a
1904     * reference to the new instance and its object name.
1905     *
1906     * @param className The class name of the MBean to be instantiated.
1907     *
1908     * @param name The object name of the MBean. May be null.
1909     *
1910     * @param params An array containing the parameters of the constructor
1911     * to be invoked.
1912     *
1913     * @param signature An array containing the signature of the constructor
1914     * to be invoked.
1915     *
1916     * @param loaderName The object name of the class loader to be used.
1917     *
1918     * @return An ObjectInstance, containing the ObjectName and the Java class
1919     * name of the newly instantiated MBean.
1920     *
1921     * @exception javax.management.ReflectionException Wraps Wraps a
1922     * ClassNotFoundException or a java.lang.Exception that
1923     * occured trying to invoke the MBean's constructor.
1924     *
1925     * @exception javax.management.InstanceAlreadyExistsException The MBean is
1926     * already under the control of the MBeanServer.
1927     *
1928     * @exception javax.management.MBeanRegistrationException The preRegister
1929     * (MBeanRegistration interface) method of the MBean has thrown
1930     * an exception. The MBean will not be registered.
1931     *
1932     * @exception javax.management.MBeanException The constructor of the MBean
1933     * has thrown an exception
1934     **/

1935    public ObjectInstance JavaDoc createMBean(String JavaDoc className,
1936                                    ObjectName JavaDoc name,
1937                                    ObjectName JavaDoc loaderName,
1938                                    Object JavaDoc[] params,
1939                                    String JavaDoc[] signature)
1940                             throws ReflectionException JavaDoc,
1941                                    InstanceAlreadyExistsException JavaDoc,
1942                                    MBeanRegistrationException JavaDoc,
1943                                    MBeanException JavaDoc,
1944                                    RuntimeMBeanException JavaDoc,
1945                                    NotCompliantMBeanException JavaDoc,
1946                                    InstanceNotFoundException JavaDoc
1947    {
1948        log.trace("createMBean called with className, ObjectName, loaderName as " +
1949            "ObjectName, parameters as Object array and signature as String array");
1950
1951        Object JavaDoc object = null;
1952        Class JavaDoc clazz = null;
1953        Object JavaDoc obj = null;
1954
1955        if(className == null )
1956        {
1957            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
1958                                    "ClassName or ObjectName cannot be null"));
1959        }
1960
1961        try
1962        {
1963            if(loaderName == null)
1964            {
1965                //clazz = this.getClass().getClassLoader().loadClass(className);
1966
clazz = getClassLoader(this).loadClass(className);
1967            }
1968            else if((object = serverTable.get(loaderName)) == null)
1969            {
1970                //clazz = this.getClass().getClassLoader().loadClass(className);
1971
//clazz = getClassLoader(this).loadClass(className);
1972
throw new InstanceNotFoundException JavaDoc(loaderName.toString());
1973            }
1974            else if(object instanceof DefaultDynamicMBean &&
1975                ((DefaultDynamicMBean)object).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
1976            {
1977                clazz = ((ClassLoader JavaDoc)((DefaultDynamicMBean)object).getStandardMBeanObject()).loadClass(className);
1978            }
1979            else if( !(object instanceof ClassLoader JavaDoc))
1980            {
1981                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader "));
1982            }
1983            else
1984            {
1985                clazz = ((ClassLoader JavaDoc)object).loadClass(className);
1986            }
1987        }
1988        catch(ClassNotFoundException JavaDoc cne)
1989        {
1990            throw new ReflectionException JavaDoc(cne, "The MBean class could not be " +
1991                                    "loaded by the available class Loaders");
1992        }
1993
1994        if(params == null || signature == null)
1995        {
1996            return createMBean(className,name,loaderName);
1997        }
1998
1999        try
2000        {
2001            Class JavaDoc[] sig = new Class JavaDoc[signature.length];
2002            for(int i = 0;i<signature.length;i++)
2003            {
2004                try
2005                {
2006                    if((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
2007                        sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
2008                }
2009                catch(ClassNotFoundException JavaDoc eee)
2010                {
2011                    try
2012                    {
2013                        DefaultLoaderRepositoryExt.loadClass(signature[i]);
2014                    }
2015                    catch(ClassNotFoundException JavaDoc ee)
2016                    {
2017                        throw new ReflectionException JavaDoc(ee, "The MBean class could not be loaded by the default loader repository");
2018                    }
2019                }
2020            }
2021
2022            Constructor JavaDoc constr = null;
2023            constr = clazz.getConstructor(sig);
2024            obj = constr.newInstance(params);
2025        }
2026        catch(InstantiationException JavaDoc ie)
2027        {
2028            try
2029            {
2030                Class JavaDoc[] classParams = new Class JavaDoc[params.length];
2031                for(int i=0; i<params.length; i++){
2032                        classParams[i] = params[i].getClass();
2033                }
2034                Constructor JavaDoc cons = clazz.getConstructor(classParams);
2035            }
2036            catch(NoSuchMethodException JavaDoc ne)
2037            {
2038                throw new ReflectionException JavaDoc(ne);
2039            }
2040        }
2041        catch(NoSuchMethodException JavaDoc nsme)
2042        {
2043            try
2044            {
2045                Class JavaDoc[] classParams = new Class JavaDoc[params.length];
2046                for(int i=0; i<params.length; i++)
2047                {
2048                    classParams[i] = params[i].getClass();
2049                }
2050
2051                Constructor JavaDoc cons = clazz.getConstructor(classParams);
2052                obj = cons.newInstance(params);
2053            }
2054            catch(NoSuchMethodException JavaDoc ne)
2055            {
2056                throw new ReflectionException JavaDoc(ne);
2057            }
2058            catch(Exception JavaDoc ene)
2059            {
2060                throw new MBeanException JavaDoc(ene);
2061            }
2062        }
2063        catch(IllegalAccessException JavaDoc iae)
2064        {
2065            throw new MBeanException JavaDoc(iae,"Exception occured while invoking the constructor of the MBean!!!");
2066        }
2067        catch(Exception JavaDoc e)
2068        {
2069            if(e instanceof ReflectionException JavaDoc)
2070            {
2071                throw (ReflectionException JavaDoc)e;
2072            }
2073
2074            if(e instanceof RuntimeException JavaDoc)
2075            {
2076                throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc)e);
2077            }
2078
2079            if(e instanceof MBeanException JavaDoc)
2080            {
2081                throw (MBeanException JavaDoc)e;
2082            }
2083
2084            if(e instanceof InvocationTargetException JavaDoc)
2085            {
2086                Exception JavaDoc ee = (Exception JavaDoc)((InvocationTargetException JavaDoc)e).getTargetException();
2087                if(ee instanceof RuntimeException JavaDoc)
2088                {
2089                    throw new RuntimeMBeanException JavaDoc((RuntimeException JavaDoc)ee);
2090                }
2091                else
2092                {
2093                    throw new MBeanException JavaDoc(ee);
2094                }
2095            }
2096
2097            throw new ReflectionException JavaDoc(e,"General Exception occured. " +
2098                    "It might be runtime errors while invoking the constructor");
2099        }
2100
2101        return registerMBean(obj, name);
2102    }
2103
2104    /**
2105     * De-serializes a byte array in the context of the class loader of an MBean.
2106     *
2107     * @param name The name of the MBean whose class loader should be used
2108     * for the de-serialization.
2109     *
2110     * @param data The byte array to be de-sererialized.
2111     *
2112     * @return The de-serialized object stream.
2113     *
2114     * @exception javax.management.InstanceNotFoundException The MBean
2115     * specified is not found.
2116     *
2117     * @exception javax.management.OperationsException Any of the usual
2118     * Input/Output related exceptions.
2119     */

2120    public java.io.ObjectInputStream JavaDoc deserialize(ObjectName JavaDoc name, byte[] data)
2121                                    throws InstanceNotFoundException JavaDoc,
2122                                           OperationsException JavaDoc
2123    {
2124        Object JavaDoc obj = serverTable.get(name);
2125        if(obj == null)
2126            throw new InstanceNotFoundException JavaDoc(name.toString());
2127
2128        try
2129        {
2130            return deserialize(obj.getClass().getName(), data);
2131        }
2132        catch(ReflectionException JavaDoc re)
2133        {
2134            throw new OperationsException JavaDoc("Exception while de-serializing the byte array data");
2135        }
2136    }
2137
2138    /**
2139     * De-serializes a byte array in the context of a given MBean class loader.
2140     * The class loader is the one that loaded the class with name "className".
2141     *
2142     * @param name The name of the class whose class loader should be used
2143     * for the de-serialization.
2144     *
2145     * @param data The byte array to be de-sererialized.
2146     *
2147     * @return The de-serialized object stream.
2148     *
2149     * @exception javax.management.OperationsException Any of the usual
2150     * Input/Output related exceptions.
2151     *
2152     * @exception javax.management.ReflectionException The specified class could
2153     * not be loaded by the default loader repository
2154     */

2155    public java.io.ObjectInputStream JavaDoc deserialize(String JavaDoc className, byte[] data)
2156                                throws OperationsException JavaDoc,ReflectionException JavaDoc
2157    {
2158        return deserialize(className, null, data);
2159    }
2160
2161    /**
2162     * De-serializes a byte array in the context of a given MBean class loader.
2163     * The class loader is the one that loaded the class with name "className".
2164     * The name of the class loader to be used for loading the specified class
2165     * is specified. If null, the MBean Server's class loader will be used.
2166     *
2167     * @param name The name of the class whose class loader should be used
2168     * for the de-serialization.
2169     *
2170     * @param data The byte array to be de-sererialized.
2171     *
2172     * @param loaderName The name of the class loader to be used for loading
2173     * the specified class. If null, the MBean Server's
2174     * class loader will be used.
2175     *
2176     * @return The de-serialized object stream.
2177     *
2178     * @exception javax.management.InstanceNotFoundException The specified
2179     * class loader MBean is not found.
2180     *
2181     * @exception javax.management.OperationsException Any of the usual
2182     * Input/Output related exceptions.
2183     *
2184     * @exception javax.management.ReflectionException The specified class
2185     * could not be loaded by the specified class loader.
2186     */

2187    public java.io.ObjectInputStream JavaDoc deserialize(String JavaDoc className,
2188                                             ObjectName JavaDoc loaderName,
2189                                             byte[] data)
2190                                      throws InstanceNotFoundException JavaDoc,
2191                                             OperationsException JavaDoc,
2192                                             ReflectionException JavaDoc
2193    {
2194        Class JavaDoc clazz = null;
2195
2196        if(data == null || data.length == 0)
2197            throw new RuntimeOperationsException JavaDoc(
2198                    new IllegalArgumentException JavaDoc(), "invalid data");
2199
2200        if(className == null)
2201            throw new RuntimeOperationsException JavaDoc(
2202                    new IllegalArgumentException JavaDoc(), "invalid className");
2203
2204        if(loaderName != null)
2205        {
2206            Object JavaDoc obj = serverTable.get(loaderName);
2207            try
2208            {
2209                if(obj == null)
2210                {
2211                    throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Invalid classloader "+loaderName));
2212                }
2213                else if(obj instanceof DefaultDynamicMBean &&
2214                    ((DefaultDynamicMBean)obj).getStandardMBeanObject() instanceof ClassLoader JavaDoc)
2215                {
2216                    clazz = ((ClassLoader JavaDoc)((DefaultDynamicMBean)obj).getStandardMBeanObject()).loadClass(className);
2217                }
2218                else if(!(obj instanceof ClassLoader JavaDoc))
2219                {
2220                    throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("Invalid classloader "+loaderName));
2221                }
2222                else
2223                {
2224                    clazz = ((ClassLoader JavaDoc)obj).loadClass(className);
2225                }
2226            }
2227            catch(ClassNotFoundException JavaDoc cnfe)
2228            {
2229                throw new ReflectionException JavaDoc(cnfe, "Loading failed by the loader " + loaderName);
2230            }
2231        }
2232        else
2233        {
2234            try
2235            {
2236                //clazz = this.getClass().getClassLoader().loadClass(className);
2237
clazz = getClassLoader(this).loadClass(className);
2238            }
2239            catch(ClassNotFoundException JavaDoc cnfe)
2240            {
2241                throw new ReflectionException JavaDoc(cnfe, className + " MBean could not be loaded");
2242            }
2243        }
2244
2245        ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(data);
2246
2247        ObjectInputStreamSupport oiss = null;
2248        try
2249        {
2250            //oiss = new ObjectInputStreamSupport(bis, clazz.getClassLoader());
2251
oiss = new ObjectInputStreamSupport(bis, getClassLoader(clazz));
2252        }
2253        catch(Exception JavaDoc e)
2254        {
2255            throw new OperationsException JavaDoc("Exception while de-serializing the byte array data");
2256        }
2257
2258        return oiss;
2259    }
2260
2261    /**
2262     * Returns the number of MBeans controlled by the MBeanServer.
2263     **/

2264    public Integer JavaDoc getMBeanCount()
2265    {
2266        return new Integer JavaDoc(serverTable.size());
2267    }
2268
2269    /**
2270     * This method de-serializes an object in the context of a given MBean
2271     * class loader.
2272     *
2273     * @param name The name of the MBean which defines the class loader
2274     *
2275     * @param entityBody The byte array to be de-sererialized.
2276     *
2277     * @return The de-serializaed object. Null can be returned if entityBody
2278     * is null or has no element(length to zero).
2279     *
2280     * @exception javax.management.InstanceNotFoundException The specified
2281     * MBean is not found.
2282     *
2283     * @exception java.lang.ClassNotFoundException The object which has to be
2284     * deserialized is not found the context the the MBean class loader
2285     *
2286     * @exception java.io.OptionalDataException Primitive data was found in
2287     * the stream instead of objects.
2288     *
2289     * @exception java.io.IOException Any of the usual Input/Output related
2290     * exceptions.
2291     **/

2292    public Object JavaDoc getObjectInClassLoader(ObjectName JavaDoc name, byte[] entityBody)
2293                                  throws InstanceNotFoundException JavaDoc,
2294                                         ClassNotFoundException JavaDoc,
2295                                         java.io.OptionalDataException JavaDoc,
2296                                         java.io.IOException JavaDoc
2297                                                                                      //throws Exception
2298
{
2299        Object JavaDoc object = null;
2300        Class JavaDoc clazz = null;
2301
2302        if(entityBody == null)
2303            throw new IOException JavaDoc("entityBody is null");
2304
2305        if(entityBody.length == 0)
2306            throw new IOException JavaDoc("entityBody length is zero");
2307
2308        return null;
2309    }
2310
2311    /**
2312     * Instantiates an object using the list of all class loaders registered
2313     * in the MBeanServer. It returns a reference to the newly created object.
2314     *
2315     * @param className The class name of the object to be instantiated.
2316     *
2317     * @return The newly instantiated object.
2318     *
2319     * @exception javax.management.ReflectionException Wraps a
2320     * ClassNotFoundException or the java.lang.Exception that
2321     * occured trying to invoke the object's constructor.
2322     *
2323     * @exception javax.management.MBeanException The constructor of the object
2324     * has thrown an exception
2325     **/

2326    public Object JavaDoc instantiate(String JavaDoc className)
2327                           throws ReflectionException JavaDoc, MBeanException JavaDoc
2328    {
2329        if(className == null)
2330            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
2331                                    "className cannot be null"));
2332
2333        Class JavaDoc class_name = null;
2334
2335        try
2336        {
2337            class_name = DefaultLoaderRepositoryExt.loadClass(className);
2338            return class_name.newInstance();
2339        }
2340        catch(ClassNotFoundException JavaDoc e)
2341        {
2342            throw new ReflectionException JavaDoc(e);
2343        }
2344        catch(IllegalAccessException JavaDoc ie)
2345        {
2346            throw new ReflectionException JavaDoc(ie);
2347        }
2348        catch(InstantiationException JavaDoc ie)
2349        {
2350            try
2351            {
2352                Constructor JavaDoc cons = class_name.getConstructor(new Class JavaDoc[0]);
2353            }
2354            catch(NoSuchMethodException JavaDoc e)
2355            {
2356                throw new ReflectionException JavaDoc(e);
2357            }
2358
2359            throw new MBeanException JavaDoc(ie);
2360        }
2361        catch(Exception JavaDoc e1)
2362        {
2363            if(e1 instanceof ReflectionException JavaDoc)
2364            {
2365                throw (ReflectionException JavaDoc)e1;
2366            }
2367
2368            throw new MBeanException JavaDoc(e1);
2369        }
2370    }
2371
2372    /**
2373     * Instantiates an object using the list of all class loaders registered
2374     * in the MBeanServer. The call returns a reference to the newly created
2375     * object.
2376     *
2377     * @param className The class name of the object to be instantiated.
2378     *
2379     * @param params An array containing the parameters of the constructor
2380     * to be invoked.
2381     *
2382     * @param signature An array containing the signature of the constructor
2383     * to be invoked.
2384     *
2385     * @return The newly instantiated object.
2386     *
2387     * @exception javax.management.ReflectionException Wraps a
2388     * ClassNotFoundException or the java.lang.Exception that
2389     * occured trying to invoke the object's constructor.
2390     *
2391     * @exception javax.management.MBeanException The constructor of the object
2392     * has thrown an exception
2393     **/

2394    public Object JavaDoc instantiate(String JavaDoc className, Object JavaDoc[] params, String JavaDoc[] signature)
2395                                    throws ReflectionException JavaDoc, MBeanException JavaDoc
2396    {
2397        if(className == null)
2398            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("className cannot be null"));
2399
2400        try
2401        {
2402            Class JavaDoc class_name = DefaultLoaderRepositoryExt.loadClass(className);
2403
2404            if(params == null || signature == null)
2405            {
2406                return instantiate(className);
2407            }
2408
2409            Class JavaDoc[] sig = new Class JavaDoc[signature.length];
2410            for(int i = 0;i<signature.length;i++)
2411            {
2412                if((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
2413                    sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
2414            }
2415
2416            Constructor JavaDoc constr = class_name.getConstructor(sig);
2417
2418            return constr.newInstance(params);
2419        }
2420        catch(ClassNotFoundException JavaDoc e)
2421        {
2422            throw new ReflectionException JavaDoc(e);
2423        }
2424        catch(IllegalArgumentException JavaDoc ie)
2425        {
2426            throw new ReflectionException JavaDoc(ie);
2427        }
2428        catch(NoSuchMethodException JavaDoc ne)
2429        {
2430            throw new ReflectionException JavaDoc(ne);
2431        }
2432        catch(Exception JavaDoc e1)
2433        {
2434            throw new MBeanException JavaDoc(e1);
2435        }
2436    }
2437
2438    /**
2439     * Instantiates an object using the class Loader specified by its ObjectName.
2440     * If the loader name is null, the system ClassLoader will be used. It
2441     * returns a reference to the new created object.
2442     *
2443     * @param className The class name of the MBean to be instantiated.
2444     *
2445     * @param loaderName The object name of the class loader to be used.
2446     *
2447     * @return The newly instantiated object.
2448     *
2449     * @exception javax.management.ReflectionException Wraps a
2450     * ClassNotFoundException or the java.lang.Exception
2451     * that occured trying to invoke the object's constructor.
2452     *
2453     * @exception javax.management.MBeanException The constructor of the
2454     * object has thrown an exception.
2455     *
2456     * @exception javax.management.InstanceNotFoundException The specified
2457     * class loader is not registered in the MBeanServer.
2458     **/

2459    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName)
2460                           throws ReflectionException JavaDoc,
2461                                  MBeanException JavaDoc,
2462                                  InstanceNotFoundException JavaDoc
2463    {
2464        if(className == null )
2465                throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("className cannot be null"));
2466
2467        Object JavaDoc object = null;
2468        Object JavaDoc loaderObject = null;
2469        Class JavaDoc clazz = null;
2470
2471        try
2472        {
2473            object = serverTable.get(loaderName);
2474            if( (object instanceof DefaultDynamicMBean))
2475            {
2476                loaderObject = ((DefaultDynamicMBean)(object)).getStandardMBeanObject();
2477
2478            }
2479            else if( ! (object instanceof ClassLoader JavaDoc))
2480            {
2481                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader "));
2482            }
2483
2484            if(loaderObject instanceof ClassLoader JavaDoc)
2485            {
2486                clazz = ((ClassLoader JavaDoc)loaderObject).loadClass(className);
2487            }
2488            else
2489            {
2490                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader "));
2491            }
2492        }
2493        catch(ClassNotFoundException JavaDoc cnfe)
2494        {
2495            throw new ReflectionException JavaDoc(cnfe);
2496        }
2497        catch(Exception JavaDoc e)
2498        {
2499            try
2500            {
2501                clazz = getClassLoader(this).loadClass(className);
2502            }
2503            catch(ClassNotFoundException JavaDoc cnfe)
2504            {
2505                throw new ReflectionException JavaDoc(cnfe);
2506            }
2507        }
2508
2509        try
2510        {
2511            return clazz.newInstance();
2512        }
2513        catch(IllegalAccessException JavaDoc ie)
2514        {
2515            throw new ReflectionException JavaDoc(ie);
2516        }
2517        catch(Exception JavaDoc e1)
2518        {
2519            throw new MBeanException JavaDoc(e1,"Exception occured while invoking the constructor of the MBean");
2520        }
2521    }
2522
2523    /**
2524     * Instantiates an object. The class loader to be used is identified by its
2525     * object name. If the object name of the loader is null, the system ClassLoader
2526     * will be used. The call returns a reference to the newly created object.
2527     *
2528     * @param className The class name of the object to be instantiated.
2529     *
2530     * @param params An array containing the parameters of the constructor
2531     * to be invoked.
2532     *
2533     * @param signature An array containing the signature of the constructor
2534     * to be invoked.
2535     *
2536     * @param loaderName The object name of the class loader to be used.
2537     *
2538     * @return The newly instantiated object.
2539     *
2540     * @exception javax.management.ReflectionException Wraps a
2541     * ClassNotFoundException or the java.lang.Exception that
2542     * occured trying to invoke the object's constructor.
2543     *
2544     * @exception javax.management.MBeanException The constructor of the object has
2545     * thrown an exception
2546     *
2547     * @exception javax.management.InstanceNotFoundException The specified class
2548     * loader is not registered in the MBeanServer.
2549     **/

2550    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName,
2551                              Object JavaDoc[] params, String JavaDoc[] signature)
2552                       throws ReflectionException JavaDoc,
2553                              MBeanException JavaDoc,
2554                              InstanceNotFoundException JavaDoc
2555    {
2556        if(className == null )
2557            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("className cannot be null"));
2558
2559        Object JavaDoc object = null;
2560        Class JavaDoc clazz = null;
2561        Object JavaDoc loaderObject = null;
2562
2563        try
2564        {
2565            object = serverTable.get(loaderName);
2566
2567            if( (object instanceof DefaultDynamicMBean))
2568            {
2569                loaderObject = ((DefaultDynamicMBean)(object)).getStandardMBeanObject();
2570            }
2571            else if( ! (object instanceof ClassLoader JavaDoc))
2572            {
2573                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader "));
2574            }
2575
2576            if(loaderObject instanceof ClassLoader JavaDoc)
2577            {
2578                clazz = ((ClassLoader JavaDoc)loaderObject).loadClass(className);
2579            }
2580            else
2581            {
2582                throw new MBeanException JavaDoc(new Exception JavaDoc("Invalid ClassLoader "));
2583            }
2584        }
2585        catch(ClassNotFoundException JavaDoc cnfe)
2586        {
2587            throw new ReflectionException JavaDoc(cnfe);
2588        }
2589        catch(Exception JavaDoc e)
2590        {
2591            try
2592            {
2593                //clazz = this.getClass().getClassLoader().loadClass(className);
2594
clazz = getClassLoader(this).loadClass(className);
2595            }
2596            catch(ClassNotFoundException JavaDoc cnfe)
2597            {
2598                throw new ReflectionException JavaDoc(cnfe);
2599            }
2600        }
2601
2602        if(params == null || signature == null)
2603        {
2604            return instantiate(className,loaderName);
2605        }
2606
2607        try
2608        {
2609            Class JavaDoc[] sig = new Class JavaDoc[signature.length];
2610
2611            for(int i = 0;i<signature.length;i++)
2612            {
2613                if((sig[i] = getThePrimitiveClassObject(signature[i])) == null)
2614                sig[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]);
2615            }
2616
2617            Constructor JavaDoc constr = clazz.getConstructor(sig);
2618            return constr.newInstance(params);
2619        }
2620        catch(ClassNotFoundException JavaDoc cnfe)
2621        {
2622            throw new ReflectionException JavaDoc(cnfe);
2623        }
2624        catch(IllegalAccessException JavaDoc ie){
2625            throw new ReflectionException JavaDoc(ie);
2626        }
2627        catch(Exception JavaDoc e1)
2628        {
2629            throw new MBeanException JavaDoc(e1);
2630        }
2631    }
2632
2633    /**
2634     * This method checks if an object has been loaded in the context of a
2635     * MBean class loader.
2636     *
2637     * @param name The name of the MBean which defines the class loader
2638     *
2639     * @param object The object to check.
2640     *
2641     * @return True if the object as been loaded in the context of the MBean
2642     * class loader ; false if not. of this MBean.
2643     *
2644     * @exception javax.management.InstanceNotFoundException The specified
2645     * MBean is not found.
2646     **/

2647    public boolean isInSameClassLoader(ObjectName JavaDoc name, Object JavaDoc object)
2648                                throws InstanceNotFoundException JavaDoc
2649    {
2650        return false;
2651    }
2652
2653    /**
2654     * @exception java.lang.IllegalArgumentException The object with name "name"
2655     * doesn't implements NotificationBroadcaster
2656     **/

2657    public void removeNotificationListener(ObjectName JavaDoc name,
2658                                         NotificationListener JavaDoc listener)
2659                                  throws InstanceNotFoundException JavaDoc,
2660                                         ListenerNotFoundException JavaDoc
2661    {
2662        Object JavaDoc object = null;
2663
2664        if(name == null)
2665        {
2666            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
2667                                        "ObjectName cannot be null!!!"));
2668        }
2669
2670        object = serverTable.get(name);
2671
2672        if(object == null)
2673        {
2674            throw new InstanceNotFoundException JavaDoc("No MBean with the Object Name "+listener+ " registered with MBeanServer");
2675        }
2676
2677        if( !(object instanceof NotificationBroadcaster JavaDoc))
2678        {
2679            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("The MBean"+name+" does not implement NotificationBroadCaster"));
2680        }
2681
2682        synchronized(object)
2683        {
2684            ((NotificationBroadcaster JavaDoc)object).removeNotificationListener(listener);
2685        }
2686    }
2687
2688    /**
2689     * Enables a listener for an MBean to be removed.
2690     *
2691     * @param name The name of the MBean on which the listener should be removed
2692     *
2693     * @param listener The listener name which will handles notifications
2694     * emitted by the registered MBean. This method will remove
2695     * all information related to this listener.
2696     *
2697     * @exception javax.management.InstanceNotFoundException The MBean name or
2698     * the listener name doesn't correspond to a registered MBean
2699     *
2700     * @exception javax.management.ListenerNotFoundException The couple
2701     * (listener,handback) is not registered in the MBean. The
2702     * exception message contains either "listener", "handback" or
2703     * the object name depending on which object cannot be found.
2704     *
2705     * @exception java.lang.IllegalArgumentException The object with name "name"
2706     * doesn't implements NotificationBroadcaster or the object
2707     * with name "listener" doesn't implements NotificationListener.
2708     * The faulty ObjectName string representation can be obtained
2709     * by means of the getMessage() method of the
2710     * IllegalArgumentException object.
2711     **/

2712    public void removeNotificationListener(ObjectName JavaDoc name, ObjectName JavaDoc listener)
2713                    throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
2714    {
2715        Object JavaDoc object = null;
2716
2717        if(name == null)
2718        {
2719            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc(
2720                                            "ObjectName cannot be null!!!"));
2721        }
2722        object = serverTable.get(name);
2723
2724        if(object == null)
2725        {
2726            throw new InstanceNotFoundException JavaDoc("No MBean with the Object Name "+listener+ " registered with MBeanServer");
2727        }
2728
2729        Object JavaDoc lobject = null;
2730        lobject = serverTable.get(listener);
2731
2732        if(lobject == null)
2733        {
2734            throw new InstanceNotFoundException JavaDoc("No listener with the name "+listener+ " present in the JMXServer!!!");
2735        }
2736
2737        if( !(lobject instanceof NotificationListener JavaDoc))
2738        {
2739            throw new RuntimeOperationsException JavaDoc(new IllegalArgumentException JavaDoc("The MBean"+name+" does not implement NotificationListener"));
2740        }
2741
2742        synchronized(object)
2743        {
2744            ((NotificationBroadcaster JavaDoc)object).removeNotificationListener( (NotificationListener JavaDoc)lobject);
2745        }
2746    }
2747
2748    /**
2749     * Returns the table that has the loader references os the mbeans.
2750     * Used by the package access class.
2751     */

2752    public Hashtable JavaDoc getLoaderTable()
2753    {
2754        return loaderTable;
2755    }
2756
2757    //---------------------------- Private methods --------------------------//
2758

2759    private Class JavaDoc getThePrimitiveClassObject(String JavaDoc str)
2760    {
2761        if(str.indexOf("boolean") != -1)
2762            return Boolean.TYPE;
2763        else if(str.indexOf("int") != -1)
2764            return Integer.TYPE;
2765        else if(str.indexOf("double") != -1)
2766            return Double.TYPE;
2767        else if(str.indexOf("float") != -1)
2768            return Float.TYPE;
2769        else if(str.indexOf("short") != -1)
2770            return Short.TYPE;
2771        else if(str.indexOf("long") != -1)
2772            return Long.TYPE;
2773        else if(str.indexOf("byte") != -1)
2774            return Byte.TYPE;
2775        else if(str.indexOf("char") != -1)
2776            return Character.TYPE;
2777        else
2778            return null;
2779    }
2780
2781    private void testForCompliance(String JavaDoc className,ClassLoader JavaDoc l)
2782                    throws NotCompliantMBeanException JavaDoc
2783    {
2784        Class JavaDoc cl = null;
2785
2786        try
2787        {
2788            cl = Class.forName(className,true,l);
2789            int i = cl.getModifiers();
2790            if(Modifier.isAbstract(i))
2791                throw new NotCompliantMBeanException JavaDoc("The MBean cannot be a abstract class");
2792
2793            java.lang.reflect.Constructor JavaDoc constructors[] = cl.getConstructors();
2794
2795            if(constructors.length == 0)
2796                throw new NotCompliantMBeanException JavaDoc("The MBean does not have a public constructor");
2797        }
2798        catch(ClassNotFoundException JavaDoc ee)
2799        {
2800            log.warn("ClassNotFoundException",ee);
2801        }
2802
2803        Class JavaDoc superClass = null;
2804        superClass = cl;
2805        Class JavaDoc class1 = null;
2806        Class JavaDoc class2 = null;
2807        boolean flag = false;
2808        for(;superClass != null;superClass = superClass.getSuperclass())
2809        {
2810            Class JavaDoc[] interfaces = superClass.getInterfaces();
2811            boolean checkFlag = false;
2812            for(int j=0;j<interfaces.length;j++)
2813            {
2814                Class JavaDoc tempClass = checkForMBeanInterface(interfaces[j],superClass.getName());
2815                if(tempClass != null)
2816                {
2817                    flag = true;
2818                    class1 = tempClass;
2819                    class2 = superClass;
2820                }
2821
2822                if(checkForDynamicMBeanInterface(interfaces[j]))
2823                {
2824                    flag = true;
2825                    checkFlag = true;
2826                }
2827
2828                if(class1 != null && superClass.equals(class2) && checkFlag)
2829                    throw new NotCompliantMBeanException JavaDoc(className + " implements javax.management.DynamicMBean and MBean interface");
2830            }
2831            if(flag)
2832            break;
2833        }
2834
2835        if(!flag)
2836            throw new NotCompliantMBeanException JavaDoc(className + " does not implement the " + className+"MBean" + " interface or the DynamicMBean interface");
2837    }
2838
2839    private Class JavaDoc checkForMBeanInterface(Class JavaDoc class1, String JavaDoc s)
2840    {
2841        if(class1.getName().compareTo(s + "MBean") == 0)
2842            return class1;
2843
2844        Class JavaDoc aclass[] = class1.getInterfaces();
2845        for(int i = 0; i < aclass.length; i++)
2846        {
2847            if(aclass[i].getName().compareTo(s + "MBean") == 0)
2848                return aclass[i];
2849        }
2850
2851        return null;
2852    }
2853
2854
2855    private boolean checkForDynamicMBeanInterface(Class JavaDoc class1)
2856    {
2857        if(class1.getName().compareTo("javax.management.DynamicMBean") == 0)
2858            return true;
2859        Class JavaDoc aclass[] = class1.getInterfaces();
2860        for(int i = 0; i < aclass.length; i++)
2861            if(aclass[i].getName().compareTo("javax.management.DynamicMBean") == 0)
2862                return true;
2863
2864        return false;
2865    }
2866
2867    private boolean isValidMBeanInfo(MBeanInfo JavaDoc mbInfo)
2868    {
2869        if(mbInfo.getClassName() == null)
2870        {
2871            return false;
2872        }
2873
2874        /*
2875        MBeanConstructorInfo[] consInfo = mbInfo.getConstructors();
2876        if(consInfo == null){
2877                return false;
2878        }
2879        else if(consInfo.length == 0){
2880                return false;
2881        }*/

2882
2883        return true;
2884    }
2885
2886    private ClassLoader JavaDoc getClassLoader(Object JavaDoc o)
2887    {
2888        return getClassLoader(o.getClass());
2889    }
2890
2891    private ClassLoader JavaDoc getClassLoader(Class JavaDoc clazz)
2892    {
2893        ClassLoader JavaDoc cl = clazz.getClassLoader();
2894        if(cl == null)
2895        {
2896            cl = Thread.currentThread().getContextClassLoader();
2897        }
2898        return cl;
2899    }
2900
2901    private void createLogger()
2902    {
2903        try
2904        {
2905            log = LogFactory.getInstance("JMX");
2906        }
2907        catch(Exception JavaDoc e)
2908        {
2909            System.out.println("FATAL:Log Creation Failed");
2910        }
2911    }
2912}
Popular Tags