//
// JmsaSession.java - JMS Adapter Session
//
// We must keep a record of every temporary destination we create.
// We must also be advised when they are deleted with their .delete() methods.
// Any that remain when the session is .close()d must be forgotten.
//
// Connections also track the sessions they create, and we must advise when
// we are closed, as any that survive until Connection.close() must be told
// to forget all their remaining temporary destinations.
//

package nyangau.jmsadapter;

import java.util.Set;
import java.util.HashSet;
import java.io.Serializable;
import javax.jms.*;

public class JmsaSession implements Session
  {
  protected Session s;
  protected JmsaConnection c;
  protected FlowProcessor fp;
  protected Set<JmsaTemporaryQueue> tqs;
  protected Set<JmsaTemporaryTopic> tts;
//...sJmsaSession:2:
public JmsaSession(Session sess, JmsaConnection conn, FlowProcessor flowproc)
  {
  s   = sess;
  c   = conn;
  fp  = flowproc;
  tqs = new HashSet<JmsaTemporaryQueue>();
  tts = new HashSet<JmsaTemporaryTopic>();
  }
//...e

//...screateXxxMessage:2:
public Message createMessage()
  throws JMSException
  { return new JmsaMessage(); }

public BytesMessage createBytesMessage()
  throws JMSException
  { return new JmsaBytesMessage(); }

public TextMessage createTextMessage()
  throws JMSException
  { return new JmsaTextMessage(); }

public TextMessage createTextMessage(String text)
  throws JMSException
  { return new JmsaTextMessage(text); }

public MapMessage createMapMessage()
  throws JMSException
  { return new JmsaMapMessage(); }

public StreamMessage createStreamMessage()
  throws JMSException
  { return new JmsaStreamMessage(); }

public ObjectMessage createObjectMessage()
  throws JMSException
  { return new JmsaObjectMessage(); }

public ObjectMessage createObjectMessage(Serializable object)
  throws JMSException
  { return new JmsaObjectMessage(object); }
//...e

//...sgetTransacted:2:
public boolean getTransacted()
  throws JMSException
  { return s.getTransacted(); }
//...e
//...sgetAcknowledgeMode:2:
public int getAcknowledgeMode()
  throws JMSException
  { return s.getAcknowledgeMode(); }
//...e

//...scommit:2:
public void commit()
  throws JMSException
  { s.commit(); }
//...e
//...srollback:2:
public void rollback()
  throws JMSException
  { s.rollback(); }
//...e
//...srecover:2:
public void recover()
  throws JMSException
  { s.recover(); }
//...e
//...sclose:2:
// Note that some users are lazy and do not call here.
// They rely on sessions being closed when connections are closed.
// So connections are required to track the sessions they create.
// And we are obliged to let them know when they don't need to any more.

public void close()
  throws JMSException
  {
  c.forgetSession(this);
  forgetUnclosedTemporarys();
  s.close();
  }
//...e
//...srun:2:
public void run()
  { s.run(); }
//...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) s.getMessageListener();
  return ml != null ? ml.getMessageListener() : null;
  }
public void setMessageListener(MessageListener listener)
  throws JMSException
  { s.setMessageListener( listener != null ? new JmsaMessageListener(listener, fp) : null ); }
//...e

//...screateProducer:2:
public MessageProducer createProducer(Destination destination)
  throws JMSException
  {
  return new JmsaMessageProducer(
    destination != null
      ? s.createProducer(((JmsaD)destination).getDest())
      : null,
    destination,
    fp
    );
  }
//...e
//...screateConsumer:2:
public MessageConsumer createConsumer(Destination destination)
  throws JMSException
  {
  return new JmsaMessageConsumer(
    s.createConsumer(((JmsaD)destination).getDest()),
    fp
    );
  }

public MessageConsumer createConsumer(Destination destination, String messageSelector)
  throws JMSException
  {
  return new JmsaMessageConsumer(
    s.createConsumer(((JmsaD)destination).getDest(), messageSelector),
    fp
    );
  }

public MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal)
  throws JMSException
  {
  return new JmsaMessageConsumer(
    s.createConsumer(((JmsaD)destination).getDest(), messageSelector, noLocal),
    fp
    );
  }
//...e
//...screateDurableSubscriber:2:
public TopicSubscriber createDurableSubscriber(Topic topic, String name)
  throws JMSException
  {
  return new JmsaTopicSubscriber(
    s.createDurableSubscriber((Topic)(((JmsaTopic)topic).getDest()), name),
    fp
    );
  }

public TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal)
  throws JMSException
  {
  return new JmsaTopicSubscriber(
    s.createDurableSubscriber((Topic)(((JmsaTopic)topic).getDest()), name, messageSelector, noLocal),
    fp
    );
  }
//...e
//...sunsubscribe:2:
public void unsubscribe(String subscriptionName)
  throws JMSException
  { s.unsubscribe(subscriptionName); }
//...e
//...screateBrowser:2:
public QueueBrowser createBrowser(Queue queue)
  throws JMSException
  {
  return new JmsaQueueBrowser(
    s.createBrowser((Queue)(((JmsaQueue)queue).getDest())),
    fp
    );
  }

public QueueBrowser createBrowser(Queue queue, String messageSelector)
  throws JMSException
  {
  return new JmsaQueueBrowser(
    s.createBrowser((Queue)(((JmsaQueue)queue).getDest()), messageSelector),
    fp
    );
  }
//...e

//...screateQueue:2:
public Queue createQueue(String queueName)
  throws JMSException
  { return new JmsaQueue( s.createQueue(queueName), null ); }
//...e
//...screateTopic:2:
public Topic createTopic(String topicName)
  throws JMSException
  { return new JmsaTopic( s.createTopic(topicName), null ); }
//...e
//...screateTemporaryQueue:2:
public TemporaryQueue createTemporaryQueue()
  throws JMSException
  {
  JmsaTemporaryQueue tq = new JmsaTemporaryQueue( s.createTemporaryQueue(), this, fp );
  tqs.add(tq);
  return tq;
  }
//...e
//...screateTemporaryTopic:2:
public TemporaryTopic createTemporaryTopic()
  throws JMSException
  {
  JmsaTemporaryTopic tt = new JmsaTemporaryTopic( s.createTemporaryTopic(), this, fp );
  tts.add(tt);
  return tt;
  }
//...e
//...sforgetTemporaryQueue:2:
public void forgetTemporaryQueue(JmsaTemporaryQueue tq)
  throws JMSException
  {
  tqs.remove(tq);
  String qn = tq.getQueueName();
  fp.forgetTemporaryQueue(qn);
  }
//...e
//...sforgetTemporaryTopic:2:
public void forgetTemporaryTopic(JmsaTemporaryTopic tt)
  throws JMSException
  {
  tts.remove(tt);
  String tn = tt.getTopicName();
  fp.forgetTemporaryTopic(tn);
  }
//...e
//...sforgetUnclosedTemporarys:2:
public void forgetUnclosedTemporarys()
  throws JMSException
  {
  for ( JmsaTemporaryQueue tq : tqs )
    {
    String qn = tq.getQueueName();
    fp.forgetTemporaryQueue(qn);
    }
  for ( JmsaTemporaryTopic tt : tts )
    {
    String tn = tt.getTopicName();
    fp.forgetTemporaryTopic(tn);
    }
  }
//...e
  }
