001: package net.sf.crispy.impl.jboss;
002:
003: import java.util.Hashtable;
004: import java.util.Map;
005: import java.util.Vector;
006:
007: import javax.management.MBeanServer;
008:
009: import net.sf.crispy.InvocationException;
010: import net.sf.crispy.util.Converter;
011: import net.sf.crispy.util.Invoker;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015: import org.jboss.remoting.InvocationRequest;
016: import org.jboss.remoting.ServerInvocationHandler;
017: import org.jboss.remoting.ServerInvoker;
018: import org.jboss.remoting.callback.InvokerCallbackHandler;
019:
020: /**
021: * Handle client request.
022: *
023: * @author Linke
024: *
025: */
026: public class JBossRemotingInvocationHandler implements
027: ServerInvocationHandler {
028:
029: protected static final Log log = LogFactory
030: .getLog(JBossRemotingInvocationHandler.class);
031: private Map services = new Hashtable();
032:
033: public JBossRemotingInvocationHandler() {
034: // addService(Echo.class.getName(), new EchoImpl());
035: // addService(Calculator.class.getName(), new CalculatorImpl());
036:
037: }
038:
039: public void addService(String pvServiceInterface,
040: Object pvServiceImpl) {
041: services.put(pvServiceInterface, pvServiceImpl);
042: }
043:
044: public Object getService(String pvServiceInterface) {
045: return services.get(pvServiceInterface);
046: }
047:
048: public Object removeService(String pvServiceInterface) {
049: return services.remove(pvServiceInterface);
050: }
051:
052: public int serviceSize() {
053: return services.size();
054: }
055:
056: public Object invoke(InvocationRequest pvInvocation)
057: throws Throwable {
058: try {
059: String lvClass = (String) pvInvocation.getRequestPayload()
060: .keySet().iterator().next();
061: String lvMethod = (String) pvInvocation.getRequestPayload()
062: .get(lvClass);
063: if (log.isDebugEnabled()) {
064: log.debug("Invoke service: " + lvClass
065: + " with method: " + lvMethod);
066: }
067: Object lvObject = getService(lvClass);
068: if (lvObject == null) {
069: throw new InvocationException("For the class: "
070: + lvClass + " can't find a register service!");
071: }
072: Object lvResult = Invoker.invoke(lvObject, lvMethod,
073: (Vector) pvInvocation.getParameter(), false,
074: new Converter());
075: if (log.isDebugEnabled()) {
076: log.debug("Result invoke service " + lvClass + ": "
077: + lvResult);
078: }
079:
080: // System.out.println("--- " + lvClass + "--" + lvMethod + "--" + lvResult + "--" + pvInvocation.getParameter());
081:
082: return lvResult;
083: } catch (Exception e) {
084: return e;
085: }
086: }
087:
088: public void setMBeanServer(MBeanServer pvArg0) {
089: // make nothing
090: }
091:
092: public void setInvoker(ServerInvoker pvArg0) {
093: // make nothing
094: }
095:
096: public void addListener(InvokerCallbackHandler pvArg0) {
097: // make nothing
098: }
099:
100: public void removeListener(InvokerCallbackHandler pvArg0) {
101: // make nothing
102: }
103:
104: }
|