001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import java.lang.reflect.Proxy;
025:
026: import javax.management.DynamicMBean;
027: import javax.management.InstanceAlreadyExistsException;
028: import javax.management.MBeanException;
029: import javax.management.MBeanRegistrationException;
030: import javax.management.MBeanServer;
031: import javax.management.MBeanServerFactory;
032: import javax.management.NotCompliantMBeanException;
033: import javax.management.ObjectName;
034: import javax.management.ReflectionException;
035:
036: /**
037: *
038: * @see java.lang.reflect.Proxy
039: *
040: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
041: * @version $Revision: 57200 $
042: *
043: */
044: public class MBeanProxy {
045:
046: // Static --------------------------------------------------------
047:
048: /**
049: * Creates a proxy to an MBean in the given MBean server.
050: *
051: * @param intrface the interface this proxy implements
052: * @param name object name of the MBean this proxy connects to
053: * @param agentID agent ID of the MBean server this proxy connects to
054: *
055: * @return proxy instance
056: *
057: * @throws MBeanProxyCreationException if the proxy could not be created
058: */
059: public static Object get(Class intrface, ObjectName name,
060: String agentID) throws MBeanProxyCreationException {
061: return get(intrface, name, (MBeanServer) MBeanServerFactory
062: .findMBeanServer(agentID).get(0));
063: }
064:
065: /**
066: * Creates a proxy to an MBean in the given MBean server.
067: *
068: * @param intrface the interface this proxy implements
069: * @param name object name of the MBean this proxy connects to
070: * @param server MBean server this proxy connects to
071: *
072: * @return proxy instance
073: *
074: * @throws MBeanProxyCreationException if the proxy could not be created
075: */
076: public static Object get(Class intrface, ObjectName name,
077: MBeanServer server) throws MBeanProxyCreationException {
078: return get(new Class[] { intrface, ProxyContext.class,
079: DynamicMBean.class }, name, server);
080: }
081:
082: /**
083: */
084: public static Object get(ObjectName name, MBeanServer server)
085: throws MBeanProxyCreationException {
086: return get(
087: new Class[] { ProxyContext.class, DynamicMBean.class },
088: name, server);
089: }
090:
091: private static Object get(Class[] interfaces, ObjectName name,
092: MBeanServer server) throws MBeanProxyCreationException {
093: return Proxy.newProxyInstance(Thread.currentThread()
094: .getContextClassLoader(), interfaces,
095: new JMXInvocationHandler(server, name));
096: }
097:
098: /**
099: * Convenience method for registering an MBean and retrieving a proxy for it.
100: *
101: * @param instance MBean instance to be registered
102: * @param intrface the interface this proxy implements
103: * @param name object name of the MBean
104: * @param agentID agent ID of the MBean server this proxy connects to
105: *
106: * @return proxy instance
107: *
108: * @throws MBeanProxyCreationException if the proxy could not be created
109: */
110: public static Object create(Class instance, Class intrface,
111: ObjectName name, String agentID)
112: throws MBeanProxyCreationException {
113: return create(instance, intrface, name,
114: (MBeanServer) MBeanServerFactory.findMBeanServer(
115: agentID).get(0));
116: }
117:
118: /**
119: * Convenience method for registering an MBean and retrieving a proxy for it.
120: *
121: * @param instance MBean instance to be registered
122: * @param intrface the interface this proxy implements
123: * @param name object name of the MBean
124: * @param server MBean server this proxy connects to
125: *
126: * @throws MBeanProxyCreationException if the proxy could not be created
127: */
128: public static Object create(Class instance, Class intrface,
129: ObjectName name, MBeanServer server)
130: throws MBeanProxyCreationException {
131: try {
132: server.createMBean(instance.getName(), name);
133: return get(intrface, name, server);
134: } catch (ReflectionException e) {
135: throw new MBeanProxyCreationException(
136: "Creating the MBean failed: " + e.toString());
137: } catch (InstanceAlreadyExistsException e) {
138: throw new MBeanProxyCreationException(
139: "Instance already exists: " + name);
140: } catch (MBeanRegistrationException e) {
141: throw new MBeanProxyCreationException(
142: "Error registering the MBean to the server: "
143: + e.toString());
144: } catch (MBeanException e) {
145: throw new MBeanProxyCreationException(e.toString());
146: } catch (NotCompliantMBeanException e) {
147: throw new MBeanProxyCreationException(
148: "Not a compliant MBean "
149: + instance.getClass().getName() + ": "
150: + e.toString());
151: }
152: }
153:
154: }
|