KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > ime > internal > util > handler > HandlerMessageExchange


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
17 package org.apache.axis.ime.internal.util.handler;
18
19 import org.apache.axis.Handler;
20 import org.apache.axis.MessageContext;
21 import org.apache.axis.TargetedChain;
22 import org.apache.axis.components.logger.LogFactory;
23 import org.apache.axis.ime.MessageExchangeCorrelator;
24 import org.apache.axis.ime.MessageExchangeEvent;
25 import org.apache.axis.ime.MessageExchangeEventListener;
26 import org.apache.axis.ime.event.MessageFaultEvent;
27 import org.apache.axis.ime.event.MessageSendEvent;
28 import org.apache.axis.ime.internal.FirstComeFirstServeDispatchPolicy;
29 import org.apache.axis.ime.internal.MessageExchangeProvider;
30 import org.apache.axis.ime.internal.MessageExchangeSendContext;
31 import org.apache.axis.ime.internal.ReceivedMessageDispatchPolicy;
32 import org.apache.commons.logging.Log;
33
34 import java.util.Hashtable JavaDoc;
35
36 /**
37  * Used to wrap synchronous handlers (e.g. Axis 1.0 transports)
38  *
39  * @author James M Snell (jasnell@us.ibm.com)
40  */

41 public class HandlerMessageExchange
42         extends MessageExchangeProvider {
43
44     protected static Log log =
45         LogFactory.getLog(HandlerMessageExchange.class.getName());
46
47     private Handler handler;
48
49     public HandlerMessageExchange(Handler handler) {
50         this.handler = handler;
51     }
52
53     /**
54      * @see org.apache.axis.ime.internal.MessageExchangeProvider1#createSendMessageContextListener()
55      */

56     protected MessageExchangeEventListener getMessageExchangeEventListener() {
57         return new Listener(handler);
58     }
59
60     protected ReceivedMessageDispatchPolicy getReceivedMessageDispatchPolicy() {
61         return new FirstComeFirstServeDispatchPolicy(
62             RECEIVE,
63             RECEIVE_REQUESTS);
64     }
65
66     public Handler getSendHandler() {
67       Handler h = super.getSendHandler();
68       if (h == null && handler instanceof TargetedChain) {
69         h = ((TargetedChain)handler).getRequestHandler();
70       }
71       return h;
72     }
73     
74     public Handler getReceiveHandler() {
75       Handler h = super.getReceiveHandler();
76       if (h == null && handler instanceof TargetedChain) {
77         h = ((TargetedChain)handler).getResponseHandler();
78       }
79       return h;
80     }
81
82     public class Listener
83             implements MessageExchangeEventListener {
84
85         private Handler handler;
86
87         public Listener(Handler handler) {
88             this.handler = handler;
89         }
90
91         /**
92          * @see org.apache.axis.ime.MessageExchangeContextListener#onMessageExchangeContext(MessageExchangeContext)
93          */

94         public void onEvent(
95                 MessageExchangeEvent event) {
96             if (!(event instanceof MessageSendEvent))
97                 return;
98             
99             MessageSendEvent sendEvent = (MessageSendEvent)event;
100             MessageExchangeSendContext context = sendEvent.getMessageExchangeSendContext();
101             
102             if (log.isDebugEnabled()) {
103                 log.debug("Enter: HandlerMessageExchange.Listener::onSend");
104             }
105             MessageExchangeEventListener listener =
106                 context.getMessageExchangeEventListener();
107             try {
108                 MessageContext msgContext =
109                         context.getMessageContext();
110                 MessageExchangeCorrelator correlator =
111                         context.getMessageExchangeCorrelator();
112             
113                 if (handler instanceof TargetedChain) {
114                   ((TargetedChain)handler).getPivotHandler().invoke(msgContext);
115                 } else {
116                   handler.invoke(msgContext);
117                 }
118
119
120                 RECEIVE.put(correlator, context);
121             } catch (Exception JavaDoc exception) {
122                 if (listener != null) {
123                     MessageFaultEvent faultEvent = new MessageFaultEvent(
124                             context.getMessageExchangeCorrelator(),
125                             exception);
126                     listener.onEvent(faultEvent);
127                 }
128             } finally {
129                 if (log.isDebugEnabled()) {
130                     log.debug("Exit: HandlerMessageExchange.Listener::onSend");
131                 }
132             }
133         }
134     }
135     
136   public void clearOptions() {
137     handler.setOptions(null);
138   }
139
140   public Hashtable JavaDoc getOptions() {
141     return handler.getOptions();
142   }
143
144   public Object JavaDoc getOption(String JavaDoc propertyId, Object JavaDoc defaultValue) {
145     Object JavaDoc value = getOption(propertyId);
146     return (value == null) ? defaultValue : value;
147   }
148
149   public Object JavaDoc getOption(String JavaDoc propertyId) {
150     return handler.getOption(propertyId);
151   }
152
153   public void setOptions(Hashtable JavaDoc properties) {
154     handler.setOptions(properties);
155   }
156
157   public void setOption(String JavaDoc propertyId, Object JavaDoc propertyValue) {
158     handler.setOption(propertyId, propertyValue);
159   }
160
161 }
162
Popular Tags