001/* 002 * Copyright 2002-2017 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 * https://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 017package org.springframework.jmx.access; 018 019import org.springframework.aop.framework.ProxyFactory; 020import org.springframework.beans.factory.BeanClassLoaderAware; 021import org.springframework.beans.factory.FactoryBean; 022import org.springframework.beans.factory.InitializingBean; 023import org.springframework.jmx.MBeanServerNotFoundException; 024import org.springframework.lang.Nullable; 025import org.springframework.util.ClassUtils; 026 027/** 028 * Creates a proxy to a managed resource running either locally or remotely. 029 * The "proxyInterface" property defines the interface that the generated 030 * proxy is supposed to implement. This interface should define methods and 031 * properties that correspond to operations and attributes in the management 032 * interface of the resource you wish to proxy. 033 * 034 * <p>There is no need for the managed resource to implement the proxy interface, 035 * although you may find it convenient to do. It is not required that every 036 * operation and attribute in the management interface is matched by a 037 * corresponding property or method in the proxy interface. 038 * 039 * <p>Attempting to invoke or access any method or property on the proxy 040 * interface that does not correspond to the management interface will lead 041 * to an {@code InvalidInvocationException}. 042 * 043 * @author Rob Harrop 044 * @author Juergen Hoeller 045 * @since 1.2 046 * @see MBeanClientInterceptor 047 * @see InvalidInvocationException 048 */ 049public class MBeanProxyFactoryBean extends MBeanClientInterceptor 050 implements FactoryBean<Object>, BeanClassLoaderAware, InitializingBean { 051 052 @Nullable 053 private Class<?> proxyInterface; 054 055 @Nullable 056 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 057 058 @Nullable 059 private Object mbeanProxy; 060 061 062 /** 063 * Set the interface that the generated proxy will implement. 064 * <p>This will usually be a management interface that matches the target MBean, 065 * exposing bean property setters and getters for MBean attributes and 066 * conventional Java methods for MBean operations. 067 * @see #setObjectName 068 */ 069 public void setProxyInterface(Class<?> proxyInterface) { 070 this.proxyInterface = proxyInterface; 071 } 072 073 @Override 074 public void setBeanClassLoader(ClassLoader classLoader) { 075 this.beanClassLoader = classLoader; 076 } 077 078 /** 079 * Checks that the {@code proxyInterface} has been specified and then 080 * generates the proxy for the target MBean. 081 */ 082 @Override 083 public void afterPropertiesSet() throws MBeanServerNotFoundException, MBeanInfoRetrievalException { 084 super.afterPropertiesSet(); 085 086 if (this.proxyInterface == null) { 087 this.proxyInterface = getManagementInterface(); 088 if (this.proxyInterface == null) { 089 throw new IllegalArgumentException("Property 'proxyInterface' or 'managementInterface' is required"); 090 } 091 } 092 else { 093 if (getManagementInterface() == null) { 094 setManagementInterface(this.proxyInterface); 095 } 096 } 097 this.mbeanProxy = new ProxyFactory(this.proxyInterface, this).getProxy(this.beanClassLoader); 098 } 099 100 101 @Override 102 @Nullable 103 public Object getObject() { 104 return this.mbeanProxy; 105 } 106 107 @Override 108 public Class<?> getObjectType() { 109 return this.proxyInterface; 110 } 111 112 @Override 113 public boolean isSingleton() { 114 return true; 115 } 116 117}