KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > MBeanTyper


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mx.util;
23
24 import javax.management.MBeanServer JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26 import java.lang.reflect.Proxy JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * MBeanTyper is a helper class that creates a typed-object from an MBean ObjectName and a main
32  * interface class that the MBean implements. You can then use the returned object (casted to the appropriate
33  * main interface class) with the correct typed signatures instead of <tt>mbeanserver.invoke(objectname,<sig>,etc.)</tt>.
34  * <P>
35  * Example usage: <BR>
36  * <code><tt>
37  * MyInterfaceMBean mbean=(MyInterfaceMBean)MBeanTyper.typeMBean(server,new ObjectName(":type=MyBean"),MyInterfaceMBean.class);
38  * mbean.foobar();
39  * </tt></code> <P>
40  *
41  * To turn debug on for this package, set the System property <tt>vocalos.jmx.mbeantyper.debug</tt> to true.
42  *
43  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
44  */

45 public class MBeanTyper
46 {
47     static final boolean DEBUG = Boolean.getBoolean("jboss.jmx.debug");
48
49     /**
50      * create a typed object from an mbean
51      */

52     public static final Object JavaDoc typeMBean(MBeanServer JavaDoc server, ObjectName JavaDoc mbean, Class JavaDoc mainInterface)
53             throws Exception JavaDoc
54     {
55         List JavaDoc interfaces = new ArrayList JavaDoc();
56         if (mainInterface.isInterface())
57         {
58             interfaces.add(mainInterface);
59         }
60         addInterfaces(mainInterface.getInterfaces(), interfaces);
61         Class JavaDoc cl[] = (Class JavaDoc[]) interfaces.toArray(new Class JavaDoc[interfaces.size()]);
62         if (DEBUG)
63         {
64             System.err.println("typeMean->server=" + server + ",mbean=" + mbean + ",mainInterface=" + mainInterface);
65             for (int c = 0; c < cl.length; c++)
66             {
67                 System.err.println(" :" + cl[c]);
68             }
69         }
70
71         return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), cl, new MBeanTyperInvoker(server, mbean));
72     }
73
74     private static final void addInterfaces(Class JavaDoc cl[], List JavaDoc list)
75     {
76         if (cl == null) return;
77         for (int c = 0; c < cl.length; c++)
78         {
79             list.add(cl[c]);
80             addInterfaces(cl[c].getInterfaces(), list);
81         }
82     }
83 }
84
Popular Tags