001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.jmx.access;
018:
019: import org.springframework.aop.framework.ProxyFactory;
020: import org.springframework.beans.factory.BeanClassLoaderAware;
021: import org.springframework.beans.factory.FactoryBean;
022: import org.springframework.beans.factory.InitializingBean;
023: import org.springframework.jmx.MBeanServerNotFoundException;
024: import org.springframework.util.ClassUtils;
025:
026: /**
027: * Creates a proxy to a managed resource running either locally or remotely.
028: * The "proxyInterface" property defines the interface that the generated
029: * proxy is supposed to implement. This interface should define methods and
030: * properties that correspond to operations and attributes in the management
031: * interface of the resource you wish to proxy.
032: *
033: * <p>There is no need for the managed resource to implement the proxy interface,
034: * although you may find it convenient to do. It is not required that every
035: * operation and attribute in the management interface is matched by a
036: * corresponding property or method in the proxy interface.
037: *
038: * <p>Attempting to invoke or access any method or property on the proxy
039: * interface that does not correspond to the management interface will lead
040: * to an <code>InvalidInvocationException</code>.
041: *
042: * <p>Requires JMX 1.2's <code>MBeanServerConnection</code> feature.
043: * As a consequence, this class will not work on JMX 1.0.
044: *
045: * @author Rob Harrop
046: * @author Juergen Hoeller
047: * @since 1.2
048: * @see MBeanClientInterceptor
049: * @see InvalidInvocationException
050: */
051: public class MBeanProxyFactoryBean extends MBeanClientInterceptor
052: implements FactoryBean, BeanClassLoaderAware, InitializingBean {
053:
054: private Class proxyInterface;
055:
056: private ClassLoader beanClassLoader = ClassUtils
057: .getDefaultClassLoader();
058:
059: private Object mbeanProxy;
060:
061: /**
062: * Set the interface that the generated proxy will implement.
063: * <p>This will usually be a management interface that matches the target MBean,
064: * exposing bean property setters and getters for MBean attributes and
065: * conventional Java methods for MBean operations.
066: * @see #setObjectName
067: */
068: public void setProxyInterface(Class managementInterface) {
069: this .proxyInterface = managementInterface;
070: }
071:
072: public void setBeanClassLoader(ClassLoader classLoader) {
073: this .beanClassLoader = classLoader;
074: }
075:
076: /**
077: * Checks that the <code>proxyInterface</code> has been specified and then
078: * generates the proxy for the target MBean.
079: */
080: public void afterPropertiesSet()
081: throws MBeanServerNotFoundException,
082: MBeanInfoRetrievalException {
083: super .afterPropertiesSet();
084:
085: if (this .proxyInterface == null) {
086: throw new IllegalArgumentException(
087: "Property 'proxyInterface' is required");
088: }
089: this .mbeanProxy = new ProxyFactory(this .proxyInterface, this )
090: .getProxy(this .beanClassLoader);
091: }
092:
093: public Object getObject() {
094: return this .mbeanProxy;
095: }
096:
097: public Class getObjectType() {
098: return this .proxyInterface;
099: }
100:
101: public boolean isSingleton() {
102: return true;
103: }
104:
105: }
|