KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > remoting > support > RemoteExporter


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
27  * Abstract base class for classes that export a remote service.
28  * Provides "service" and "serviceInterface" bean properties.
29  *
30  * <p>Note that the service interface being used will show some signs of
31  * remotability, like the granularity of method calls that it offers.
32  * Furthermore, it has to have serializable arguments etc.
33  *
34  * @author Juergen Hoeller
35  * @since 26.12.2003
36  */

37 public abstract class RemoteExporter implements BeanClassLoaderAware {
38
39     /** Logger available to subclasses */
40     protected final Log logger = LogFactory.getLog(getClass());
41
42     private Object JavaDoc service;
43
44     private Class JavaDoc serviceInterface;
45
46     private boolean registerTraceInterceptor = true;
47
48     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
49
50
51     /**
52      * Set the service to export.
53      * Typically populated via a bean reference.
54      */

55     public void setService(Object JavaDoc service) {
56         this.service = service;
57     }
58
59     /**
60      * Return the service to export.
61      */

62     public Object JavaDoc getService() {
63         return this.service;
64     }
65
66     /**
67      * Set the interface of the service to export.
68      * The interface must be suitable for the particular service and remoting strategy.
69      */

70     public void setServiceInterface(Class JavaDoc serviceInterface) {
71         if (serviceInterface != null && !serviceInterface.isInterface()) {
72             throw new IllegalArgumentException JavaDoc("'serviceInterface' must be an interface");
73         }
74         this.serviceInterface = serviceInterface;
75     }
76
77     /**
78      * Return the interface of the service to export.
79      */

80     public Class JavaDoc getServiceInterface() {
81         return this.serviceInterface;
82     }
83
84     /**
85      * Set whether to register a RemoteInvocationTraceInterceptor for exported
86      * services. Only applied when a subclass uses <code>getProxyForService</code>
87      * for creating the proxy to expose.
88      * <p>Default is "true". RemoteInvocationTraceInterceptor's most important value
89      * is that it logs exception stacktraces on the server, before propagating
90      * an exception to the client.
91      * @see #getProxyForService
92      * @see RemoteInvocationTraceInterceptor
93      */

94     public void setRegisterTraceInterceptor(boolean registerTraceInterceptor) {
95         this.registerTraceInterceptor = registerTraceInterceptor;
96     }
97
98     /**
99      * Return whether to register a RemoteInvocationTraceInterceptor for
100      * exported services.
101      */

102     protected boolean isRegisterTraceInterceptor() {
103         return this.registerTraceInterceptor;
104     }
105
106     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
107         this.beanClassLoader = classLoader;
108     }
109
110
111     /**
112      * Check whether the service reference has been set.
113      * @see #setService
114      */

115     protected void checkService() throws IllegalArgumentException JavaDoc {
116         if (this.service == null) {
117             throw new IllegalArgumentException JavaDoc("Property 'service' is required");
118         }
119     }
120
121     /**
122      * Check whether a service reference has been set,
123      * and whether it matches the specified service.
124      * @see #setServiceInterface
125      * @see #setService
126      */

127     protected void checkServiceInterface() throws IllegalArgumentException JavaDoc {
128         if (this.serviceInterface == null) {
129             throw new IllegalArgumentException JavaDoc("Property 'serviceInterface' is required");
130         }
131         if (!this.serviceInterface.isInstance(this.service)) {
132             throw new IllegalArgumentException JavaDoc(
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     /**
140      * Get a proxy for the given service object, implementing the specified
141      * service interface.
142      * <p>Used to export a proxy that does not expose any internals but just
143      * a specific interface intended for remote access. Furthermore, a
144      * {@link RemoteInvocationTraceInterceptor} will be registered (by default).
145      * @return the proxy
146      * @see #setServiceInterface
147      * @see #setRegisterTraceInterceptor
148      * @see RemoteInvocationTraceInterceptor
149      */

150     protected Object JavaDoc 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     /**
163      * Return a short name for this exporter.
164      * Used for tracing of remote invocations.
165      * <p>Default is the unqualified class name (without package).
166      * Can be overridden in subclasses.
167      * @see #getProxyForService
168      * @see RemoteInvocationTraceInterceptor
169      * @see org.springframework.util.ClassUtils#getShortName
170      */

171     protected String JavaDoc getExporterName() {
172         return ClassUtils.getShortName(getClass());
173     }
174
175 }
176
Popular Tags