KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bindings > ResponseCorrelator


1 package org.objectweb.celtix.bindings;
2
3
4 import java.util.HashMap JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.logging.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
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 /**
14  * Class to manage correlation of decoupled responses.
15  */

16 public class ResponseCorrelator implements ResponseCallback {
17
18     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ResponseCorrelator.class);
19     
20     private HandlerInvoker fixedHandlerInvoker;
21     private Map JavaDoc<String JavaDoc, Response> responseMap;
22     private Map JavaDoc<String JavaDoc, HandlerInvoker> relatedRequestMap;
23     private AbstractBindingBase binding;
24
25     protected ResponseCorrelator(AbstractBindingBase b) {
26         // a fixed snap-shot of the stream and system handler chains
27
// are used, as the incoming (possibly asynchronous) response
28
// cannot yet be corellated with a particular request, hence
29
// may not include any dynamic (i.e. programmatic) changes
30
// made to the handler chains
31
fixedHandlerInvoker = b.createHandlerInvoker();
32         responseMap = new HashMap JavaDoc<String JavaDoc, Response>();
33         relatedRequestMap = new HashMap JavaDoc<String JavaDoc, HandlerInvoker>();
34         binding = b;
35     }
36
37     /**
38      * Used by the ClientTransport to dispatch decoupled responses.
39      *
40      * @param responseContext context with InputStream containing the
41      * incoming the response
42      */

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 JavaDoc 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                 // this is expected for partial responses
69
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     /**
80      * Wait for a correlated response.
81      *
82      * @param outContext outgoing context containing correlation ID property
83      * @return binding-specific context for the correlated response
84      */

85     public Response getResponse(Request request) {
86         
87         String JavaDoc 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 JavaDoc ie) {
102                         // ignore
103
}
104       
105                     count++;
106                 }
107             }
108         } else {
109             LOG.warning("NO_OUTGOING_CORRELATION_ID_MSG");
110         }
111         return response;
112     }
113 }
114
Popular Tags