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 proxyInterface) {
069: this .proxyInterface = proxyInterface;
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: this .proxyInterface = getManagementInterface();
087: if (this .proxyInterface == null) {
088: throw new IllegalArgumentException(
089: "Property 'proxyInterface' or 'managementInterface' is required");
090: }
091: } else {
092: if (getManagementInterface() == null) {
093: setManagementInterface(this .proxyInterface);
094: }
095: }
096: this .mbeanProxy = new ProxyFactory(this .proxyInterface, this )
097: .getProxy(this .beanClassLoader);
098: }
099:
100: public Object getObject() {
101: return this .mbeanProxy;
102: }
103:
104: public Class getObjectType() {
105: return this .proxyInterface;
106: }
107:
108: public boolean isSingleton() {
109: return true;
110: }
111:
112: }
|