KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > kernel > IncomingClientMessageRouter


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Jan 19, 2004
48  * Manta LTD
49  */

50 package org.mr.kernel;
51
52 import java.util.HashMap JavaDoc;
53 import java.util.HashSet JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.Set JavaDoc;
56
57 import javax.jms.JMSException JavaDoc;
58
59 import org.apache.commons.logging.Log;
60 import org.apache.commons.logging.LogFactory;
61 import org.mr.IMessageListener;
62 import org.mr.MantaAgent;
63 import org.mr.MantaAgentConstants;
64 import org.mr.MessageManipulator;
65 import org.mr.api.jms.MantaMessage;
66 import org.mr.core.protocol.MantaBusMessage;
67 import org.mr.core.protocol.MantaBusMessageConsts;
68 import org.mr.core.util.Stage;
69 import org.mr.core.util.StageHandler;
70 import org.mr.core.util.StageParams;
71
72
73 /**
74  * IncomingClientMessageRouter sends the message to the logical listeners
75  * the routing is based on a destenation key
76  * @see org.mr.kernel.IncomingMessageListener
77  * @since Jan 19, 2004
78  * @version 1.0
79  * @author Amir Shevat
80  *
81  */

82 public class IncomingClientMessageRouter implements StageHandler ,IncomingMessageListener {
83     private Log log;
84     Stage stage = null;
85     HashMap JavaDoc listenersMap = new HashMap JavaDoc();
86     
87     
88     public IncomingClientMessageRouter(){
89         log=LogFactory.getLog("IncomingClientMessageRouter");
90         IncomingMessageListenerRegister.setClientRouter(this);
91         StageParams params = new StageParams();
92         params.setBlocking(false);
93         params.setPersistent(false);
94         params.setHandler(this);
95         params.setNumberOfStartThreads(1);
96         params.setStageName("IncomingClientMessages");
97         stage = new Stage(params);
98     }
99     
100     
101     
102     public void addIncommingClientMessageListener(String JavaDoc destenation ,IMessageListener listener){
103         synchronized (listenersMap) {
104             Set JavaDoc listeners =(Set JavaDoc) listenersMap.get(destenation);
105             if(listeners == null){
106                 listeners = new HashSet JavaDoc();
107                 listenersMap.put(destenation , listeners);
108             }
109             listeners.add(listener);
110         }
111     }//addIncommingMessageListener
112

113     public void removeIncomingClientMessageListener(String JavaDoc destenation ,IMessageListener listener){
114         synchronized (listenersMap) {
115             Set JavaDoc listeners =(Set JavaDoc) listenersMap.get(destenation);
116             if(listeners != null){
117                 listeners.remove(listener);
118                 if(listeners.size() == 0)
119                     listenersMap.remove(destenation);
120             }
121         }
122     }//removeIncommingMessageListener
123

124
125     /**
126      * should be called only from the SLA stage
127      */

128     public boolean handle(Object JavaDoc event) {
129         
130         MantaBusMessage msg = (MantaBusMessage)event;
131         
132         //add MessageManipulator hook
133
MessageManipulator mm =MantaAgent.getInstance().getSingletonRepository().getMessageManipulator();
134          if(mm!= null){
135             msg = mm.manipulate(msg, null, MessageManipulator.INCOMING);
136          }
137
138          // check if this is a reject ack
139
boolean isAck = false;
140          String JavaDoc ackRejectTo = msg.getHeader(MantaBusMessageConsts.HEADER_NAME_ACK_REJECT_RESPONSE_REFERENCE);
141          if (ackRejectTo != null) {
142              MantaAgent.getInstance().gotAckReject(ackRejectTo,
143                                                    msg.getSource());
144              isAck = true;
145          }
146
147         // check if there is a ack on this message
148
String JavaDoc ackTo = msg.getHeader(MantaBusMessageConsts.HEADER_NAME_ACK_RESPONSE_REFERENCE);
149         if(ackTo!= null){
150             if(log.isDebugEnabled()){
151                 log.debug("Got message. Message ID="+msg.getMessageId()+". Ack for message "+ackTo);
152             }
153             MantaAgent.getInstance().gotAck(ackTo ,msg.getSource());
154             isAck = true;
155         }
156         else {
157             if(log.isDebugEnabled()){
158                 log.debug("Got message. Message ID="+msg.getMessageId());
159             }
160         }
161         String JavaDoc dest = msg.getLogicalDestination();
162         boolean sentToListeners = false;
163         
164         synchronized (listenersMap) {
165             Set JavaDoc listeners = (Set JavaDoc) listenersMap.get(dest);
166             if (listeners != null && listeners.size() > 0) {
167                 Iterator JavaDoc curListeners = listeners.iterator();
168                 while (curListeners.hasNext()) {
169                     IMessageListener listener =(IMessageListener)curListeners.next();
170                     listener.onMessage(msg);
171                 }
172                 sentToListeners = true;
173             }
174         }
175         if (sentToListeners) {
176             // auto ack if needed
177
byte ackType = msg.getRecipient().getAcknowledgeMode();
178             if(ackType == MantaAgentConstants.AUTO_ACK || ackType == MantaAgentConstants.DUPLICATE_ACK){
179                 if(msg.getPayload()==null || !(msg.getPayload() instanceof MantaMessage)){
180                     // if this is a JMS message the JMS will ack it
181
MantaAgent.getInstance().ack(msg);
182                 }
183             }
184         }
185         else {
186             if (!isAck) {
187                 if (log.isInfoEnabled()) {
188                     log.info("No listeners found for message, message will be discarded. Message ID="+msg.getMessageId()+", message was"+msg);
189                 }
190                 MantaAgent.getInstance().ackReject(msg);
191             }
192         }
193         return true;
194     }
195
196
197     /* (non-Javadoc)
198      * @see org.mr.core.protocol.MantaBusMessageListener#messageArrived(org.mr.core.protocol.MantaBusMessage)
199      */

200     public void messageArrived(MantaBusMessage msg) {
201         stage.enqueue(msg);
202     }
203
204 }
205
Popular Tags