KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Proxy JavaDoc;
25
26 import javax.management.DynamicMBean JavaDoc;
27 import javax.management.InstanceAlreadyExistsException JavaDoc;
28 import javax.management.MBeanException JavaDoc;
29 import javax.management.MBeanRegistrationException JavaDoc;
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.MBeanServerFactory JavaDoc;
32 import javax.management.NotCompliantMBeanException JavaDoc;
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35
36 /**
37  *
38  * @see java.lang.reflect.Proxy
39  *
40  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
41  * @version $Revision: 37459 $
42  *
43  */

44 public class MBeanProxy
45 {
46
47    // Static --------------------------------------------------------
48

49    /**
50     * Creates a proxy to an MBean in the given MBean server.
51     *
52     * @param intrface the interface this proxy implements
53     * @param name object name of the MBean this proxy connects to
54     * @param agentID agent ID of the MBean server this proxy connects to
55     *
56     * @return proxy instance
57     *
58     * @throws MBeanProxyCreationException if the proxy could not be created
59     */

60    public static Object JavaDoc get(Class JavaDoc intrface, ObjectName JavaDoc name, String JavaDoc agentID) throws MBeanProxyCreationException
61    {
62       return get(intrface, name, (MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(agentID).get(0));
63    }
64
65    /**
66     * Creates a proxy to an MBean in the given MBean server.
67     *
68     * @param intrface the interface this proxy implements
69     * @param name object name of the MBean this proxy connects to
70     * @param server MBean server this proxy connects to
71     *
72     * @return proxy instance
73     *
74     * @throws MBeanProxyCreationException if the proxy could not be created
75     */

76    public static Object JavaDoc get(Class JavaDoc intrface, ObjectName JavaDoc name, MBeanServer JavaDoc server) throws MBeanProxyCreationException
77    {
78       return get(new Class JavaDoc[] { intrface, ProxyContext.class, DynamicMBean JavaDoc.class }, name, server);
79    }
80
81    /**
82     */

83    public static Object JavaDoc get(ObjectName JavaDoc name, MBeanServer JavaDoc server) throws MBeanProxyCreationException
84    {
85       return get(new Class JavaDoc[] { ProxyContext.class, DynamicMBean JavaDoc.class }, name, server);
86    }
87
88    private static Object JavaDoc get(Class JavaDoc[] interfaces, ObjectName JavaDoc name, MBeanServer JavaDoc server) throws MBeanProxyCreationException
89    {
90       return Proxy.newProxyInstance(
91             Thread.currentThread().getContextClassLoader(),
92             interfaces, new JMXInvocationHandler(server, name)
93       );
94    }
95    
96    /**
97     * Convenience method for registering an MBean and retrieving a proxy for it.
98     *
99     * @param instance MBean instance to be registered
100     * @param intrface the interface this proxy implements
101     * @param name object name of the MBean
102     * @param agentID agent ID of the MBean server this proxy connects to
103     *
104     * @return proxy instance
105     *
106     * @throws MBeanProxyCreationException if the proxy could not be created
107     */

108    public static Object JavaDoc create(Class JavaDoc instance, Class JavaDoc intrface, ObjectName JavaDoc name, String JavaDoc agentID) throws MBeanProxyCreationException
109    {
110       return create(instance, intrface, name, (MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(agentID).get(0));
111    }
112    
113    /**
114     * Convenience method for registering an MBean and retrieving a proxy for it.
115     *
116     * @param instance MBean instance to be registered
117     * @param intrface the interface this proxy implements
118     * @param name object name of the MBean
119     * @param server MBean server this proxy connects to
120     *
121     * @throws MBeanProxyCreationException if the proxy could not be created
122     */

123    public static Object JavaDoc create(Class JavaDoc instance, Class JavaDoc intrface, ObjectName JavaDoc name, MBeanServer JavaDoc server) throws MBeanProxyCreationException
124    {
125       try
126       {
127          server.createMBean(instance.getName(), name);
128          return get(intrface, name, server);
129       }
130       catch (ReflectionException JavaDoc e) {
131          throw new MBeanProxyCreationException("Creating the MBean failed: " + e.toString());
132       }
133       catch (InstanceAlreadyExistsException JavaDoc e) {
134          throw new MBeanProxyCreationException("Instance already exists: " + name);
135       }
136       catch (MBeanRegistrationException JavaDoc e) {
137          throw new MBeanProxyCreationException("Error registering the MBean to the server: " + e.toString());
138       }
139       catch (MBeanException JavaDoc e) {
140          throw new MBeanProxyCreationException(e.toString());
141       }
142       catch (NotCompliantMBeanException JavaDoc e) {
143          throw new MBeanProxyCreationException("Not a compliant MBean " + instance.getClass().getName() + ": " + e.toString());
144       }
145    }
146    
147 }
148
149
150
151
Popular Tags