//
// JmsaMessageConsumer.java - JMS Adapter MessageConsumer
//
// When this implementation receives a message, it converts it to
// JmsaMessage form and transforms it (using whatever flow is applicable).
//

package nyangau.jmsadapter;

import javax.jms.*;

public class JmsaMessageConsumer implements MessageConsumer
  {
  protected MessageConsumer mc;
  protected FlowProcessor fp;
//...sJmsaMessageConsumer:2:
public JmsaMessageConsumer(
  MessageConsumer consumer,
  FlowProcessor flowproc
  )
  {
  mc = consumer;
  fp = flowproc;
  }
//...e
//...sgetMessageSelector:2:
public String getMessageSelector()
  throws JMSException
  { return mc.getMessageSelector(); }
//...e
//...sMessageListener:2:
// A little bit of trickery here:
// When the user supplies a listener, we wrap it in our own.
// This converts the inbound message to our format and delivers it.

public MessageListener getMessageListener()
  throws JMSException
  {
  JmsaMessageListener ml = (JmsaMessageListener) mc.getMessageListener();
  return ml != null ? ml.getMessageListener() : null;
  }
public void setMessageListener(MessageListener listener)
  throws JMSException
  { mc.setMessageListener( listener != null ? new JmsaMessageListener(listener, fp) : null ); }
//...e
//...sclose:2:
public void close()
  throws JMSException
  { mc.close(); }
//...e
//...sreceive\44\ receiveNoWait:2:
protected JmsaMessage postReceive(Message message)
  throws JMSException
  {
  JmsaMessage m = JmsaMessage.copyOfMessageWithWrappedDestinations(message);

  // Invoke the Flow Processor
  m = fp.processIn(m);

  m.protect();

  return m;
  }

public Message receive()
  throws JMSException
  {
  Message message = mc.receive();
  return postReceive(message);
  }

public Message receive(long timeout)
  throws JMSException
  {
  Message message = mc.receive(timeout);
  return message != null ? postReceive(message) : null;
  }

public Message receiveNoWait()
  throws JMSException
  {
  Message message = mc.receiveNoWait();
  return message != null ? postReceive(message) : null;
  }
//...e
  }
