//
// JmsaConnection.java - JMS Adapter Connection
//
// We must track the sessions we create, and they must let us know when they
// are closed, so that when we are closed, we can tell any surviving sessions
// to forget about their unclosed temporary destinations.
//

package nyangau.jmsadapter;

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

public class JmsaConnection implements Connection
  {
  protected Connection c;
  protected FlowProcessor fp;
  protected Set<JmsaSession> ss;
//...sJmsaConnection:2:
public JmsaConnection(Connection conn, FlowProcessor flowproc)
  {
  c = conn;
  fp = flowproc;
  ss = new HashSet<JmsaSession>();
  }
//...e
//...sgetDest:2:
protected Destination getDest(Destination d)
  throws JMSException
  {
  if ( ! (d instanceof JmsaD) )
    throw new JMSException("JMS Adapter Destination required");
  return ((JmsaD)d).getDest();
  }
//...e
//...sClientID:2:
public String getClientID()
  throws JMSException
  { return c.getClientID(); }
public void setClientID(String clientID)
  throws JMSException
  { c.setClientID(clientID); }
//...e
//...sgetMetaData:2:
public ConnectionMetaData getMetaData()
  throws JMSException
  { return new JmsaConnectionMetaData(c.getMetaData()); }
//...e
//...sExceptionListener:2:
public ExceptionListener getExceptionListener()
  throws JMSException
  { return c.getExceptionListener(); }
public void setExceptionListener(ExceptionListener listener)
  throws JMSException
  { c.setExceptionListener(listener); }
//...e
//...sstart:2:
public void start()
  throws JMSException
  { c.start(); }
//...e
//...sstop:2:
public void stop()
  throws JMSException
  { c.stop(); }
//...e
//...sclose:2:
public void close()
  throws JMSException
  {
  for ( JmsaSession s : ss )
    s.forgetUnclosedTemporarys();
  c.close();
  }
//...e
//...screateConnectionConsumer:2:
public ConnectionConsumer createConnectionConsumer(
  Destination destination,
  String messageSelector,
  ServerSessionPool sessionPool,
  int maxMessages
  )
  throws JMSException
  {
  return c.createConnectionConsumer(
    getDest(destination),
    messageSelector,
    sessionPool,
    maxMessages
    );
  }    
//...e
//...screateDurableConnectionConsumer:2:
public ConnectionConsumer createDurableConnectionConsumer(
  Topic topic,
  String subscriptionName,
  String messageSelector,
  ServerSessionPool sessionPool,
  int maxMessages
  )
  throws JMSException
  {
  return c.createDurableConnectionConsumer(
    (Topic) getDest(topic),
    subscriptionName,
    messageSelector,
    sessionPool,
    maxMessages
    );
  }    
//...e
//...screateSession:2:
public Session createSession(boolean transacted, int acknowledgeMode)
  throws JMSException
  {
  JmsaSession s = new JmsaSession( c.createSession(transacted, acknowledgeMode), this, fp );
  ss.add(s);
  return s;
  }
//...e
//...sforgetSession:2:
public void forgetSession(JmsaSession s)
  { ss.remove(s); }
//...e
  }
