KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > ime > internal > FirstComeFirstServeDispatchPolicy


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.ime.internal;
17
18 import org.apache.axis.MessageContext;
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.ime.MessageExchangeCorrelator;
21 import org.apache.axis.ime.MessageExchangeEventListener;
22 import org.apache.axis.ime.event.MessageFaultEvent;
23 import org.apache.axis.ime.event.MessageReceiveEvent;
24 import org.apache.axis.ime.internal.util.KeyedBuffer;
25 import org.apache.commons.logging.Log;
26
27 /**
28  * @author James M Snell (jasnell@us.ibm.com)
29  * @author Ray Chun (rchun@sonicsoftware.com)
30  */

31 public class FirstComeFirstServeDispatchPolicy
32         implements ReceivedMessageDispatchPolicy {
33   
34     protected static Log log =
35         LogFactory.getLog(FirstComeFirstServeDispatchPolicy.class.getName());
36   
37     protected KeyedBuffer RECEIVE_REQUESTS;
38     protected KeyedBuffer RECEIVE;
39     
40     public FirstComeFirstServeDispatchPolicy(
41             KeyedBuffer RECEIVE,
42             KeyedBuffer RECEIVE_REQUESTS) {
43         this.RECEIVE = RECEIVE;
44         this.RECEIVE_REQUESTS = RECEIVE_REQUESTS;
45     }
46   
47     public void dispatch(
48             MessageExchangeSendContext context) {
49
50         if (log.isDebugEnabled()) {
51             log.debug("Enter: FirstComeFirstServeDispatchPolicy::dispatch");
52         }
53     
54       // 1. Get the correlator
55
// 2. See if there are any receive requests based on the correlator
56
// 3. If there are receive requests for the correlator, deliver to the first one
57
// 4. If there are no receive requests for the correlator, deliver to the first "anonymous" receive request
58
// 5. If there are no receive requests, put the message back on the Queue
59

60         MessageExchangeReceiveContext receiveContext = null;
61         MessageExchangeCorrelator correlator =
62             context.getMessageExchangeCorrelator();
63         receiveContext = (MessageExchangeReceiveContext)RECEIVE_REQUESTS.get(correlator);
64         if (receiveContext == null) {
65             receiveContext = (MessageExchangeReceiveContext)RECEIVE_REQUESTS.get(SimpleMessageExchangeCorrelator.NULL_CORRELATOR);
66         }
67         if (receiveContext == null)
68             RECEIVE.put(correlator,context);
69         else {
70             MessageExchangeEventListener eventListener =
71               receiveContext.getMessageExchangeEventListener();
72             MessageContext msgContext =
73               context.getMessageContext();
74             try {
75                 MessageReceiveEvent receiveEvent =
76                     new org.apache.axis.ime.event.MessageReceiveEvent(
77                             correlator,
78                             receiveContext,
79                             context.getMessageContext());
80                 eventListener.onEvent(receiveEvent);
81             } catch (Exception JavaDoc exception) {
82               if (eventListener != null) {
83                   MessageFaultEvent faultEvent = new MessageFaultEvent(
84                         correlator,
85                         exception);
86                   eventListener.onEvent(faultEvent);
87               }
88             }
89         }
90         if (log.isDebugEnabled()) {
91             log.debug("Exit: FirstComeFirstServeDispatchPolicy::dispatch");
92         }
93         
94     }
95   
96 }
97
Popular Tags