1 7 package org.jboss.test.remoting.performance.synchronous; 8 9 import java.util.ArrayList ; 10 import java.util.List ; 11 import java.util.Map ; 12 import javax.management.MBeanServer ; 13 import org.jboss.logging.Logger; 14 import org.jboss.remoting.InvocationRequest; 15 import org.jboss.remoting.ServerInvocationHandler; 16 import org.jboss.remoting.ServerInvoker; 17 import org.jboss.remoting.callback.InvokerCallbackHandler; 18 import org.jboss.remoting.callback.ServerInvokerCallbackHandler; 19 import org.jboss.remoting.invocation.RemoteInvocation; 20 21 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap; 22 import EDU.oswego.cs.dl.util.concurrent.FIFOReadWriteLock; 23 import EDU.oswego.cs.dl.util.concurrent.SyncList; 24 25 28 public class PerformanceInvocationHandler implements ServerInvocationHandler 29 { 30 private ServerInvoker invoker; 31 32 private List listeners = new SyncList(new ArrayList (), new FIFOReadWriteLock()); 33 private Map callTrackers = new ConcurrentHashMap(); 34 35 private static final Logger log = Logger.getLogger(PerformanceInvocationHandler.class); 36 37 38 43 public void setMBeanServer(MBeanServer server) 44 { 45 } 46 47 52 public void setInvoker(ServerInvoker invoker) 53 { 54 this.invoker = invoker; 55 } 56 57 66 public Object invoke(InvocationRequest invocation) throws Throwable 67 { 68 Object param = invocation.getParameter(); 69 String sessionId = invocation.getSessionId(); 70 String methodName = ""; 71 Object [] params = null; 72 String [] sig = null; 73 74 if(param instanceof RemoteInvocation) 75 { 76 RemoteInvocation rminvo = (RemoteInvocation) param; 77 methodName = rminvo.getMethodName(); 78 params = rminvo.getParameters(); 79 } 80 else 81 { 82 throw new Exception ("Unknown invocation payload (" + param + "). " + 83 "Should be instance of RemoteInvocation."); 84 } 85 86 Object ret = null; 87 if(methodName.equals(PerformanceClientTest.NUM_OF_CALLS)) 88 { 89 Integer totalCountInteger = (Integer ) params[0]; 90 int totalCount = totalCountInteger.intValue(); 91 System.out.println("received totalCallCount call with total count of " + totalCount + " from " + sessionId); 92 CallTracker tracker = (CallTracker) callTrackers.get(sessionId); 93 if(tracker != null) 94 { 95 tracker.createTotalCount(totalCount); 96 ret = totalCountInteger; 97 } 98 else 99 { 100 log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId); 101 throw new Exception ("Calling " + methodName + " but no call tracker exists for session id " + sessionId); 102 } 103 } 104 else if(methodName.equals(PerformanceClientTest.TEST_INVOCATION)) 105 { 106 if(params != null) 107 { 108 Payload payload = (Payload) params[0]; 109 int clientInvokeCallCount = payload.getCallNumber(); 110 111 CallTracker tracker = (CallTracker) callTrackers.get(sessionId); 112 if(tracker != null) 113 { 114 tracker.verifyClientInvokeCount(clientInvokeCallCount); 115 } 116 else 117 { 118 log.error("Calling " + methodName + " but no call tracker exists for session id " + sessionId); 119 throw new Exception ("Calling " + methodName + " but no call tracker exists for session id " + sessionId); 120 } 121 ret = new Integer (clientInvokeCallCount); 123 } 124 else 125 { 126 log.error("no parameter passed for method call " + methodName); 127 } 128 129 } 130 else 131 { 132 throw new Exception ("Don't know what to do with call to " + methodName); 133 } 134 135 return ret; 136 137 } 138 139 private void createCallTracker(String sessionId, InvokerCallbackHandler callbackHandler) 140 { 141 CallTracker tracker = new CallTracker(sessionId, callbackHandler); 142 callTrackers.put(sessionId, tracker); 143 } 144 145 151 public void addListener(InvokerCallbackHandler callbackHandler) 152 { 153 ServerInvokerCallbackHandler handler = (ServerInvokerCallbackHandler) callbackHandler; 154 String sessionId = handler.getClientSessionId(); 155 System.out.println("Adding callback listener. Callback handler has session id: " + sessionId); 156 createCallTracker(sessionId, callbackHandler); 157 listeners.add(callbackHandler); 158 } 159 160 166 public void removeListener(InvokerCallbackHandler callbackHandler) 167 { 168 listeners.remove(callbackHandler); 172 } 173 } | Popular Tags |