View Javadoc

1   /*
2    * Created on 19.04.2005
3    *
4    */
5   package net.sf.crispy.impl;
6   
7   import java.lang.reflect.Method;
8   import java.util.Vector;
9   
10  import net.sf.crispy.ExceptionWrapper;
11  import net.sf.crispy.Executor;
12  import net.sf.crispy.InvocationException;
13  import net.sf.crispy.InvocationStrategy;
14  import net.sf.crispy.strategy.NameSpacePlusMethodInvocationStrategy;
15  import net.sf.crispy.util.Invoker;
16  import net.sf.crispy.util.Util;
17  
18  import org.apache.xmlrpc.XmlRpcClient;
19  import org.apache.xmlrpc.XmlRpcException;
20  
21  /**
22   * Remote-Call for XML-RPC.
23   * 
24   * @author Linke
25   *
26   */
27  public class XmlRpcExecutor extends Executor {
28      
29      public final static String DEFAULT_URL_AND_PORT = "http://localhost:9090";
30      
31      private XmlRpcClient client = null;
32  	    
33      
34      public String getDefaultUrlAndPort() { return DEFAULT_URL_AND_PORT; }
35      
36      public InvocationStrategy getDefaultInvocationStrategy() { return new NameSpacePlusMethodInvocationStrategy(); }
37      
38      public boolean withConverter() { return true; }
39          
40      /**
41       * @see net.sf.crispy.Executor#execute(java.lang.Class, java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
42       */
43      public Object execute(Class pvProxyClass, Object pvProxy, Method pvMethod, Object[] pvArgs) throws Exception {
44      	// Create xmlRpcClient
45      	if (client == null) {
46      		client = new XmlRpcClient(getUrlAndPort());
47              if (log.isDebugEnabled()) { log.debug("Create XmlRpcClient: " + client + " - URL: " + getUrlAndPort()); }
48          }
49                  
50          String lvStrService = getInvocationStrategy().toString();
51          if (log.isDebugEnabled()) { log.debug("Service: " + lvStrService + ": " + pvArgs); }
52          Vector param = Invoker.array2Vector(pvArgs);
53          Object ret = null;
54          try {
55          	ret = client.execute(lvStrService, param);	
56          	ret = ExceptionWrapper.isResultExceptionThanThrowIt(ret);
57          } catch (XmlRpcException e) {
58          	Throwable t = Util.findDeepestThrowable(e);
59          	throw new InvocationException("Error by execute service: " + lvStrService + " -- args: " + param + " -- " + t.getMessage(), t);
60  		} 
61          
62          return ret;
63      }
64  }