View Javadoc

1   package net.sf.crispy.server;
2   
3   import java.lang.reflect.Method;
4   
5   import net.sf.crispy.ExceptionWrapper;
6   import net.sf.crispy.InterceptorHandler;
7   import net.sf.crispy.proxy.DynamicJdkProxy;
8   import net.sf.crispy.proxy.DynamicProxy;
9   import net.sf.crispy.proxy.InvocationHandler;
10  
11  /**
12   * The default implementation of the <code>ServiceEndpoint</code>.
13   * 
14   * @author Linke
15   * 
16   * @since 1.1.0 
17   *
18   */
19  public class ServiceEndpointImpl implements ServiceEndpoint {
20  
21  	private InterceptorHandlerCreator interceptorHandlerCreator = null;
22  	
23  	public void setInterceptorHandlerCreator(InterceptorHandlerCreator pvCreator) {
24  		interceptorHandlerCreator = pvCreator;
25  	}
26  
27  	public InterceptorHandlerCreator getInterceptorHandlerCreator() {
28  		return interceptorHandlerCreator;
29  	}
30  
31  	/** 
32  	 * If don't set a <code>InterceptorHandlerCreator</code>, than create a new <code>InterceptorHandler</code> instance.
33  	 * 
34  	 * @return By every call create a new <code>InterceptorHandler</code> instance.
35  	 */
36  	public final InterceptorHandler createNewInterceptorHandlerInstance() {
37  		if (this.getInterceptorHandlerCreator() != null) {
38  			return getInterceptorHandlerCreator().createNewInterceptorHandlerInstance();
39  		} else {
40  			return new InterceptorHandler();
41  		}
42  	}
43  
44  	public final Object doInvoke(Object pvServiceImpl, Method pvMethod, Object[] pvArgs, InterceptorHandler pvInterceptorHandler) {
45  		Object lvResult = null;
46  		
47  		InterceptorHandler lvInterceptorHandler = pvInterceptorHandler;
48  		if (lvInterceptorHandler == null) {
49  			lvInterceptorHandler = createNewInterceptorHandlerInstance();
50  		}
51  		
52  		DynamicProxy dynamicProxy = new DynamicJdkProxy();
53  		dynamicProxy.setProxyClass(pvServiceImpl.getClass());
54  		dynamicProxy.setProxyObject(pvServiceImpl);
55  		
56  		try {
57  			lvResult = InvocationHandler.doInvoke(dynamicProxy, null, pvMethod, pvArgs, lvInterceptorHandler);
58  		} catch (Exception e) {
59  			lvResult = ExceptionWrapper.isThrowableThanHandleThrowable(e);
60  		}
61  		return lvResult;
62  	}
63  
64  }