1 7 package org.jboss.remoting.transport.mock; 8 9 import org.jboss.logging.Logger; 10 import org.jboss.remoting.Client; 11 import org.jboss.remoting.ComplexReturn; 12 import org.jboss.remoting.InvocationRequest; 13 import org.jboss.remoting.InvokerCallbackHandler; 14 import org.jboss.remoting.InvokerLocator; 15 import org.jboss.remoting.ServerInvocationHandler; 16 import org.jboss.remoting.ServerInvoker; 17 import org.jboss.remoting.byvalue.ByValuePayload; 18 import org.jboss.remoting.invocation.InternalInvocation; 19 import org.jboss.remoting.invocation.NameBasedInvocation; 20 21 import javax.management.MBeanServer ; 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 35 public class MockServerInvocationHandler implements ServerInvocationHandler 36 { 37 private ServerInvoker invoker; 38 private List listeners = new ArrayList (); 39 private Map clientListeners = new HashMap (); 40 41 private static final Logger log = Logger.getLogger(MockServerInvocationHandler.class); 42 43 44 49 public void setInvoker(ServerInvoker invoker) 50 { 51 this.invoker = invoker; 52 } 53 54 59 public void setMBeanServer(MBeanServer server) 60 { 61 } 62 63 public Object invoke(InvocationRequest invocation) 64 throws Throwable 65 { 66 Object param = invocation.getParameter(); 67 String methodName = ""; 68 Object [] params = null; 69 String [] sig = null; 70 71 if(param instanceof NameBasedInvocation) 72 { 73 NameBasedInvocation nbi = (NameBasedInvocation) param; 74 methodName = nbi.getMethodName(); 75 params = nbi.getParameters(); 76 sig = nbi.getSignature(); 77 } 78 else if(param instanceof InternalInvocation) 79 { 80 InternalInvocation ii = (InternalInvocation) param; 81 methodName = ii.getMethodName(); 82 params = ii.getParameters(); 83 } 84 else 85 { 86 log.info("Don't recognize the parameter type, so just returning it."); 87 return param; 88 } 89 90 String sessionId = invocation.getSessionId(); 91 String subsystem = invocation.getSubsystem(); 92 93 log.debug("invoke() called with method: " + methodName + 94 "\tsessionId: " + sessionId + "\tsubsystem:" + subsystem); 95 if(methodName.equals("testComplexReturn")) 97 { 98 ComplexReturn ret = new ComplexReturn(); 100 return ret; 101 } 102 else if(methodName.equals("test")) 103 { 104 log.debug("test called on server invocation handler, so should do callback."); 106 CallbackDispatcher callbackDispatcher = new CallbackDispatcher(invocation.getSessionId(), 107 invocation.getSubsystem(), 108 new NameBasedInvocation("handleCallback", 109 params, 110 sig)); 111 Thread callbackThread = new Thread (callbackDispatcher); 112 callbackThread.start(); 113 } 114 else if(methodName.equals("addClientListener")) 115 { 116 Object obj = params[0]; 117 InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj; 118 119 clientListeners.put(invocation.getSessionId(), clientHandler); 120 } 121 else if(methodName.equals("removeClientListener")) 122 { 123 Object obj = params[0]; 124 InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) obj; 125 126 clientListeners.remove(invocation.getSessionId()); 127 } 128 else if(methodName.equals("handleCallback")) 129 { 130 InvokerCallbackHandler clientHandler = (InvokerCallbackHandler) clientListeners.get(sessionId); 132 clientHandler.handleCallback(invocation); 133 } 134 else if(methodName.equals("testByValue")) 135 { 136 Object arg = params[0]; 138 if(arg instanceof ByValuePayload) 139 { 140 ByValuePayload byValuePayload = (ByValuePayload) arg; 141 return new Boolean (byValuePayload.wasMarshalled()); 142 } 143 else 144 { 145 return Boolean.FALSE; 147 } 148 } 149 Object ret = null; 150 if(params != null) 151 { 152 ret = params[0]; 153 log.info("Found a parameter to return " + ret); 154 } else 156 { 157 log.info("returning null"); 158 } 160 return ret; 161 } 162 163 167 private void handleRemoveNotificationListener(String sessionId) 168 { 169 listeners.remove(sessionId); 170 } 171 172 179 private void handleAddNotificationListener(InvokerLocator clientLocator, 180 String subsystem, 181 String sessionId) throws Exception 182 { 183 Client callBackClient = new Client(clientLocator, subsystem); 184 callBackClient.connect(); 185 listeners.add(callBackClient); 186 } 187 188 public void addListener(InvokerCallbackHandler callbackHandler) 189 { 190 listeners.add(callbackHandler); 191 log.debug("added listener " + callbackHandler); 192 } 193 194 public void removeListener(InvokerCallbackHandler callbackHandler) 195 { 196 listeners.remove(callbackHandler); 197 log.debug("removed listener " + callbackHandler); 198 } 199 200 private class CallbackDispatcher implements Runnable 201 { 202 private String sessionId; 203 private String subsystem; 204 private Object param; 205 206 public CallbackDispatcher(String sessionId, String subsystem, Object param) 207 { 208 this.sessionId = sessionId; 209 this.subsystem = subsystem; 210 this.param = param; 211 } 212 213 public void run() 214 { 215 Iterator itr = listeners.iterator(); 216 while(itr.hasNext()) 217 { 218 try 219 { 220 InvokerCallbackHandler handler = (InvokerCallbackHandler) itr.next(); 221 InvocationRequest invocation = new InvocationRequest(sessionId, 222 subsystem, 223 param, 224 229 null, null, null); handler.handleCallback(invocation); 233 } 234 catch(Throwable e) 235 { 236 e.printStackTrace(); 237 } 238 239 } 240 } 241 } 242 } 243 | Popular Tags |