001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2006 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.mts.jms;
027:
028: import javax.jms.JMSException;
029: import javax.jms.Message;
030: import javax.jms.ObjectMessage;
031:
032: import org.cougaar.core.mts.MessageAttributes;
033: import org.cougaar.mts.base.MessageDeliverer;
034: import org.cougaar.mts.base.MisdeliveredMessageException;
035: import org.cougaar.mts.std.AttributedMessage;
036: import org.cougaar.util.log.Logger;
037: import org.cougaar.util.log.Logging;
038:
039: /**
040: * This utility class handles incoming JMS messages
041: */
042: public class MessageReceiver {
043: protected final Logger log;
044: private final MessageDeliverer deliverer;
045: private final ReplySync sync;
046:
047: public MessageReceiver(ReplySync sync, MessageDeliverer deliverer) {
048: this .sync = sync;
049: this .deliverer = deliverer;
050: this .log = Logging.getLogger(getClass().getName());
051: }
052:
053: public void handleIncomingMessage(Message msg) {
054: if (log.isDebugEnabled())
055: log.debug("Received JMS message=" + msg);
056: if (deliverer == null) {
057: log
058: .error("Message arrived before MessageDelivererService was available");
059: return;
060: }
061: if (msg instanceof ObjectMessage) {
062: ObjectMessage omsg = (ObjectMessage) msg;
063: if (sync.isReply(omsg)) {
064: // it's an ack -- Work is done in isReply
065: return;
066: }
067: try {
068: Object domainObject = omsg.getObject();
069: if (domainObject instanceof AttributedMessage) {
070: AttributedMessage message = (AttributedMessage) domainObject;
071: try {
072: MessageAttributes reply = deliverer
073: .deliverMessage(message, message
074: .getTarget());
075: sync.replyToMessage(omsg, reply);
076: } catch (MisdeliveredMessageException e) {
077: sync.replyToMessage(omsg, e);
078: }
079: } else {
080: if (log.isWarnEnabled())
081: log.warn(domainObject
082: + " is not an AttributedMessage");
083: }
084: } catch (JMSException e1) {
085: if (log.isWarnEnabled())
086: log.warn("JMS Error handling new message: Cause="
087: + e1.getMessage());
088: } catch (Exception e2) {
089: if (log.isWarnEnabled())
090: log.warn("Error handling new message: Cause="
091: + e2.getMessage());
092: }
093: } else {
094: if (log.isWarnEnabled())
095: log
096: .warn("Received a JMS message that wasn't an ObjectMessage");
097: }
098: }
099:
100: }
|