KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > rmi > RMIRemoteMBeanProxy


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.jmx.adaptor.rmi;
23
24 import java.io.Serializable JavaDoc;
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28
29
30 /**
31  * A factory for producing MBean proxies that run on a distant node and access
32  * the server through RMI. Most of the code comes from MBeanProxy.
33  *
34  * @version <tt>$Revision: 37459 $</tt>
35  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
36  */

37 public class RMIRemoteMBeanProxy
38    implements Serializable JavaDoc, InvocationHandler JavaDoc
39 {
40    /** The server to proxy invoke calls to. */
41    private final RMIAdaptor remoteServer;
42
43    /** The name of the object to invoke. */
44    private final ObjectName JavaDoc name;
45   
46    /**
47     * Construct a MBeanProxy.
48     */

49
50    RMIRemoteMBeanProxy (final ObjectName JavaDoc name, final javax.management.MBeanServer JavaDoc server) throws Exception JavaDoc
51    {
52       this.name = name;
53       this.remoteServer = getRmiAdaptor ();
54    }
55
56    /** Used when args is null. */
57    private static final Object JavaDoc EMPTY_ARGS[] = {};
58
59    /**
60     * Invoke the configured MBean via the target MBeanServer and decode
61     * any resulting JMX exceptions that are thrown.
62     */

63    public Object JavaDoc invoke (final Object JavaDoc proxy, final java.lang.reflect.Method JavaDoc method, final Object JavaDoc[] args) throws Throwable JavaDoc
64    {
65       String JavaDoc methodName = method.getName();
66
67       // Get attribute
68
if (methodName.startsWith("get") && args == null)
69       {
70          String JavaDoc attrName = methodName.substring(3);
71          return remoteServer.getAttribute(name, attrName);
72       }
73
74       // Is attribute
75
else if (methodName.startsWith("is") && args == null)
76       {
77          String JavaDoc attrName = methodName.substring(2);
78          return remoteServer.getAttribute(name, attrName);
79       }
80
81       // Set attribute
82
else if (methodName.startsWith("set") && args != null && args.length == 1)
83       {
84          String JavaDoc attrName = methodName.substring(3);
85          remoteServer.setAttribute(name, new javax.management.Attribute JavaDoc(attrName, args[0]));
86          return null;
87       }
88
89       // Operation
90

91       // convert the parameter types to strings for JMX
92
Class JavaDoc[] types = method.getParameterTypes();
93       String JavaDoc[] sig = new String JavaDoc[types.length];
94       for (int i = 0; i < types.length; i++) {
95          sig[i] = types[i].getName();
96       }
97
98       // invoke the server and decode JMX exceptions
99
return remoteServer.invoke(name, methodName, args == null ? EMPTY_ARGS : args, sig);
100    }
101    
102    protected RMIAdaptor getRmiAdaptor () throws Exception JavaDoc
103    {
104       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
105       return (RMIAdaptor) ctx.lookup("jmx/invoker/RMIAdaptor");
106    }
107
108
109    ///////////////////////////////////////////////////////////////////////////
110
// MBeanProxyInstance //
111
///////////////////////////////////////////////////////////////////////////
112

113    public final ObjectName JavaDoc getMBeanProxyObjectName()
114    {
115       return name;
116    }
117
118    public final RMIAdaptor getMBeanProxyRMIAdaptor()
119    {
120       return remoteServer;
121    }
122
123
124    ///////////////////////////////////////////////////////////////////////////
125
// Factory Methods //
126
///////////////////////////////////////////////////////////////////////////
127

128    /**
129     * Create an MBean proxy.
130     *
131     * @param intf The interface which the proxy will implement.
132     * @param name A string used to construct the ObjectName of the
133     * MBean to proxy to.
134     * @param server The MBeanServer that contains the MBean to proxy to.
135     * @return A MBean proxy.
136     *
137     * @throws Exception Invalid object name.
138     */

139    public static Object JavaDoc create (final Class JavaDoc intf, final String JavaDoc name, final javax.management.MBeanServer JavaDoc server) throws Exception JavaDoc
140    {
141       return create(intf, new ObjectName JavaDoc(name), server);
142    }
143    
144    /**
145     * Create an MBean proxy.
146     *
147     * @param intf The interface which the proxy will implement.
148     * @param name The name of the MBean to proxy invocations to.
149     * @param server The MBeanServer that contains the MBean to proxy to.
150     * @return A MBean proxy.
151     */

152    public static Object JavaDoc create (final Class JavaDoc intf, final ObjectName JavaDoc name, final javax.management.MBeanServer JavaDoc server) throws Exception JavaDoc
153    {
154       return java.lang.reflect.Proxy.newProxyInstance(Thread.currentThread ().getContextClassLoader (),
155                                     new Class JavaDoc[] { intf },
156                                     new RMIRemoteMBeanProxy(name, server));
157    }
158 }
159
Popular Tags