1 16 17 package org.springframework.remoting.support; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 22 import org.springframework.aop.framework.ProxyFactory; 23 import org.springframework.beans.factory.BeanClassLoaderAware; 24 import org.springframework.util.ClassUtils; 25 26 37 public abstract class RemoteExporter implements BeanClassLoaderAware { 38 39 40 protected final Log logger = LogFactory.getLog(getClass()); 41 42 private Object service; 43 44 private Class serviceInterface; 45 46 private boolean registerTraceInterceptor = true; 47 48 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 49 50 51 55 public void setService(Object service) { 56 this.service = service; 57 } 58 59 62 public Object getService() { 63 return this.service; 64 } 65 66 70 public void setServiceInterface(Class serviceInterface) { 71 if (serviceInterface != null && !serviceInterface.isInterface()) { 72 throw new IllegalArgumentException ("'serviceInterface' must be an interface"); 73 } 74 this.serviceInterface = serviceInterface; 75 } 76 77 80 public Class getServiceInterface() { 81 return this.serviceInterface; 82 } 83 84 94 public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) { 95 this.registerTraceInterceptor = registerTraceInterceptor; 96 } 97 98 102 protected boolean isRegisterTraceInterceptor() { 103 return this.registerTraceInterceptor; 104 } 105 106 public void setBeanClassLoader(ClassLoader classLoader) { 107 this.beanClassLoader = classLoader; 108 } 109 110 111 115 protected void checkService() throws IllegalArgumentException { 116 if (this.service == null) { 117 throw new IllegalArgumentException ("Property 'service' is required"); 118 } 119 } 120 121 127 protected void checkServiceInterface() throws IllegalArgumentException { 128 if (this.serviceInterface == null) { 129 throw new IllegalArgumentException ("Property 'serviceInterface' is required"); 130 } 131 if (!this.serviceInterface.isInstance(this.service)) { 132 throw new IllegalArgumentException ( 133 "Service interface [" + this.serviceInterface.getName() + 134 "] needs to be implemented by service [" + this.service + 135 "] of class [" + this.service.getClass().getName() + "]"); 136 } 137 } 138 139 150 protected Object getProxyForService() { 151 checkService(); 152 checkServiceInterface(); 153 ProxyFactory proxyFactory = new ProxyFactory(); 154 proxyFactory.addInterface(getServiceInterface()); 155 if (isRegisterTraceInterceptor()) { 156 proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName())); 157 } 158 proxyFactory.setTarget(getService()); 159 return proxyFactory.getProxy(this.beanClassLoader); 160 } 161 162 171 protected String getExporterName() { 172 return ClassUtils.getShortName(getClass()); 173 } 174 175 } 176 | Popular Tags |