View Javadoc

1   package net.sf.crispy.impl.jboss;
2   
3   import java.util.Hashtable;
4   import java.util.Map;
5   import java.util.Vector;
6   
7   import javax.management.MBeanServer;
8   
9   import net.sf.crispy.InvocationException;
10  import net.sf.crispy.util.Converter;
11  import net.sf.crispy.util.Invoker;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.jboss.remoting.InvocationRequest;
16  import org.jboss.remoting.ServerInvocationHandler;
17  import org.jboss.remoting.ServerInvoker;
18  import org.jboss.remoting.callback.InvokerCallbackHandler;
19  
20  /**
21   * Handle client request.
22   * 
23   * @author Linke
24   *
25   */
26  public class JBossRemotingInvocationHandler implements ServerInvocationHandler {
27  
28  	protected static final Log log = LogFactory.getLog (JBossRemotingInvocationHandler.class);
29  	private Map services = new Hashtable();
30  	
31  	public JBossRemotingInvocationHandler() {
32  //		addService(Echo.class.getName(), new EchoImpl());
33  //		addService(Calculator.class.getName(), new CalculatorImpl());
34  
35  	}
36  	
37  	public void addService (String pvServiceInterface, Object pvServiceImpl) {
38  		services.put(pvServiceInterface, pvServiceImpl);
39  	}
40  	public Object getService (String pvServiceInterface) {
41  		return services.get(pvServiceInterface);
42  	}
43  	public Object removeService(String pvServiceInterface) {
44  		return services.remove(pvServiceInterface);
45  	}
46  	public int serviceSize() {
47  		return services.size();
48  	}
49  
50  	public Object invoke(InvocationRequest pvInvocation) throws Throwable {
51  		try {		
52  	    	String lvClass = (String) pvInvocation.getRequestPayload().keySet().iterator().next();
53  	    	String lvMethod = (String) pvInvocation.getRequestPayload().get(lvClass);
54  	    	if (log.isDebugEnabled()) { log.debug("Invoke service: " + lvClass + " with method: " + lvMethod); }
55  	    	Object lvObject = getService(lvClass);
56  	    	if (lvObject == null) { throw new InvocationException("For the class: " + lvClass + " can't find a register service!"); }
57  	    	Object lvResult = Invoker.invoke(lvObject, lvMethod, (Vector) pvInvocation.getParameter(), false, new Converter());
58  	    	if (log.isDebugEnabled()) { log.debug("Result invoke service " + lvClass + ": " + lvResult); }
59  	    	
60  	//    	System.out.println("--- " + lvClass + "--" + lvMethod + "--" + lvResult + "--" + pvInvocation.getParameter());
61  	    	
62  			return lvResult;
63  		} catch (Exception e) {
64  			return e;
65  		}
66  	}
67  
68  	
69  	
70  	public void setMBeanServer(MBeanServer pvArg0) {
71  		// make nothing
72  	}
73  
74  	public void setInvoker(ServerInvoker pvArg0) {
75  		// make nothing
76  	}
77  
78  
79  	public void addListener(InvokerCallbackHandler pvArg0) {
80  		// make nothing	
81  	}
82  
83  	public void removeListener(InvokerCallbackHandler pvArg0) {
84  		// make nothing
85  	}
86  
87  }