1 package org.objectweb.celtix.bindings; 2 3 4 import java.util.HashMap ; 5 import java.util.Map ; 6 import java.util.logging.Level ; 7 import java.util.logging.Logger ; 8 9 import org.objectweb.celtix.common.logging.LogUtils; 10 import org.objectweb.celtix.context.InputStreamMessageContext; 11 import org.objectweb.celtix.handlers.HandlerInvoker; 12 13 16 public class ResponseCorrelator implements ResponseCallback { 17 18 private static final Logger LOG = LogUtils.getL7dLogger(ResponseCorrelator.class); 19 20 private HandlerInvoker fixedHandlerInvoker; 21 private Map <String , Response> responseMap; 22 private Map <String , HandlerInvoker> relatedRequestMap; 23 private AbstractBindingBase binding; 24 25 protected ResponseCorrelator(AbstractBindingBase b) { 26 fixedHandlerInvoker = b.createHandlerInvoker(); 32 responseMap = new HashMap <String , Response>(); 33 relatedRequestMap = new HashMap <String , HandlerInvoker>(); 34 binding = b; 35 } 36 37 43 44 public void dispatch(InputStreamMessageContext responseContext) { 45 assert responseContext != null; 46 47 Response response = new Response(binding, fixedHandlerInvoker); 48 response.processProtocol(responseContext); 49 50 synchronized (this) { 51 String inCorrelation = response.getCorrelationId(); 52 if (inCorrelation != null) { 53 HandlerInvoker alternate = 54 relatedRequestMap.remove(inCorrelation); 55 if (alternate == null) { 56 LOG.log(Level.INFO, "response correlation ID: {0}", inCorrelation); 57 responseMap.put(inCorrelation, response); 58 notifyAll(); 59 } else { 60 DataBindingCallback callback = 61 BindingContextUtils.retrieveDataBindingCallback(responseContext); 62 if (callback != null) { 63 response.getHandlerInvoker().adoptLogicalHandlers(alternate); 64 response.processLogical(callback); 65 } 66 } 67 } else { 68 LOG.info("no correlation ID in incoming message"); 70 DataBindingCallback callback = 71 BindingContextUtils.retrieveDataBindingCallback(responseContext); 72 if (callback != null) { 73 response.processLogical(callback); 74 } 75 } 76 } 77 } 78 79 85 public Response getResponse(Request request) { 86 87 String outCorrelation = request.getCorrelationId(); 88 Response response = null; 89 if (outCorrelation != null) { 90 LOG.log(Level.INFO, "request correlation ID: {0}", outCorrelation); 91 synchronized (this) { 92 response = responseMap.remove(outCorrelation); 93 int count = 0; 94 while (response == null && count < 10) { 95 try { 96 wait(); 97 response = responseMap.remove(outCorrelation); 98 if (request.isRelatedRequestExpected()) { 99 relatedRequestMap.put(outCorrelation, request.getHandlerInvoker()); 100 } 101 } catch (InterruptedException ie) { 102 } 104 105 count++; 106 } 107 } 108 } else { 109 LOG.warning("NO_OUTGOING_CORRELATION_ID_MSG"); 110 } 111 return response; 112 } 113 } 114 | Popular Tags |