Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.resource.adapter.jms;
 
 
 
Implements the JMS Connection API and produces JmsSession objects.

Author(s):
Peter Antman.
Jason Dillon
Adrian Brock
Version:
$Revision: 71790 $
 
 public class JmsSessionFactoryImpl
    implements JmsSessionFactoryReferenceable
 {
    private static final Logger log = Logger.getLogger(JmsSessionFactoryImpl.class);

   
Are we closed?
 
    private boolean closed = false;

   
Whether trace is enabled
 
    private boolean trace = .isTraceEnabled();
    
    private Reference reference;
 
    // Used from JmsConnectionFactory
    private String userName;
    private String password;
    private String clientID;
    private int type;
 
    /* Whether we are started */
    private boolean started = false;
   
   
JmsRa own factory
 
    private JmsManagedConnectionFactory mcf;

   
Hook to the appserver
 
    private ConnectionManager cm;

   
The sessions
 
    private HashSet sessions = new HashSet();

   
The temporary queues
 
    private HashSet tempQueues = new HashSet();

   
The temporary topics
 
    private HashSet tempTopics = new HashSet();
    
                                 final ConnectionManager cm,
                                 final int type)
    {
       this. = (JmsManagedConnectionFactorymcf;
      this. = cm;
      
      if (cm == null)
         // This is standalone usage, no appserver
         this. = new JmsConnectionManager();
      else 
         this. = cm;
      this. = type;
      if ()
         .trace("mcf=" + mcf + ", cm=" + cm + ", type=" + type);
   }
   public void setReference(final Reference reference)
   {
      this. = reference;
   }
    
   public Reference getReference()
   {
      return ;
   }
    
   // --- API for JmsConnectionFactoryImpl
   
   public void setUserName(final String name
   {
       = name;
   }
    
   public void setPassword(final String password
   {
      this. = password;
   }
   //---- QueueConnection ---
   
   public QueueSession createQueueSession(final boolean transacted
                                          final int acknowledgeMode
      throws JMSException
   {
      checkClosed();
      if ( == .)
         throw new IllegalStateException("Can not get a queue session from a topic connection");
      return allocateConnection(transactedacknowledgeMode);
   }
    
      (Queue queue,
       String messageSelector,
       ServerSessionPool sessionPool,
       int maxMessages
      throws JMSException 
   {
      throw new IllegalStateException();
   }
    
   //--- TopicConnection ---
    
   public TopicSession createTopicSession(final boolean transacted
                                          final int acknowledgeMode
      throws JMSException
   { 
      checkClosed();
      if ( == .)
         throw new IllegalStateException("Can not get a topic session from a queue connection");
      return allocateConnection(transactedacknowledgeMode);
   }
      (Topic topic,
       String messageSelector,
       ServerSessionPool sessionPool,
       int maxMessages
      throws JMSException 
   {
      throw new IllegalStateException();
   }		       
      Topic topic
      String subscriptionName,
      String messageSelector,
      ServerSessionPool sessionPool
      int maxMessages
      throws JMSException
   {
      throw new IllegalStateException();
   }
   
   //--- All the Connection methods
   
   public String getClientID() throws JMSException
   {
      checkClosed();
      return ;
   }
    
   public void setClientID(String cIDthrows JMSException
   {
      if (.isStrict())
         throw new IllegalStateException();
      
      checkClosed();
      if ( != null)
         throw new IllegalStateException("Cannot change client id");
       = cID;
   }
    
   {
      checkClosed();
      return .getMetaData();
   }
    
   {
      throw new IllegalStateException();
   }
    
   public void setExceptionListener(ExceptionListener listener)
      throws JMSException
   {
      throw new IllegalStateException();
   }
    
   public void start() throws JMSException
   {
      checkClosed();
      if ()
         .trace("start() " + this);
      synchronized ()
      {
         if ()
            return;
          = true;
         for (Iterator i = .iterator(); i.hasNext();)
         {
            JmsSession session = (JmsSessioni.next();
            session.start();
         }
      }
   }
    
   public void stop() throws JMSException
   {
      if (.isStrict())
         throw new IllegalStateException();
      checkClosed();
      if ()
         .trace("stop() " + this);
      synchronized ()
      {
         if ( == false)
            return;
          = true;
         for (Iterator i = .iterator(); i.hasNext();)
         {
            JmsSession session = (JmsSessioni.next();
            session.stop();
         }
      }
   }
   public void close() throws JMSException
   {
      if ()
         return;
       = true;
      if ()
         .trace("close() " + this);
      
      synchronized ()
      {
         for (Iterator i = .iterator(); i.hasNext();)
         {
            JmsSession session = (JmsSessioni.next();
            try
            {
               session.closeSession();
            }
            catch (Throwable t)
            {
               .trace("Error closing session"t);
            }
            i.remove();
         }
      }
      
      synchronized ()
      {
         for (Iterator i = .iterator(); i.hasNext();)
         {
            TemporaryQueue temp = (TemporaryQueuei.next();
            try
            {
               if ()
                  .trace("Closing temporary queue " + temp + " for " + this);
               temp.delete();
            }
            catch (Throwable t)
            {
               .trace("Error deleting temporary queue"t);
            }
            i.remove();
         }
      }
      
      synchronized ()
      {
         for (Iterator i = .iterator(); i.hasNext();)
         {
            TemporaryTopic temp = (TemporaryTopici.next();
            try
            {
               if ()
                  .trace("Closing temporary topic " + temp + " for " + this);
               temp.delete();
            }
            catch (Throwable t)
            {
               .trace("Error deleting temporary queue"t);
            }
            i.remove();
         }
      }
   }
   public void closeSession(JmsSession sessionthrows JMSException
   {
      synchronized ()
      {
         .remove(session);
      }
   }
   
   public void addTemporaryQueue(TemporaryQueue temp)
   {
      synchronized()
      {
         .add(temp);
      }
   }
   
   public void addTemporaryTopic(TemporaryTopic temp)
   {
      synchronized()
      {
         .add(temp);
      }
   }
   
   // -- JMS 1.1
   public ConnectionConsumer createConnectionConsumer(Destination destinationServerSessionPool poolint maxMessagesthrows JMSException
   {
      throw new IllegalStateException();
   }
   public ConnectionConsumer createConnectionConsumer(Destination destinationString nameServerSessionPool poolint maxMessagesthrows JMSException
   {
      throw new IllegalStateException();
   }
   public Session createSession(boolean transactedint acknowledgeMode)
      throws JMSException
   {
      checkClosed();
      return allocateConnection(transactedacknowledgeMode);
   }
   protected JmsSession allocateConnection(boolean transactedint acknowledgeModeint sessionTypethrows JMSException
   {
      try
      {
         synchronized ()
         {
            if (.isStrict() && .isEmpty() == false)
               throw new IllegalStateException("Only allowed one session per connection. See the J2EE spec, e.g. J2EE1.4 Section 6.6");
            if (transacted)
               acknowledgeMode = .;
            JmsConnectionRequestInfo info = new JmsConnectionRequestInfo(transactedacknowledgeModesessionType);
            info.setUserName();
            info.setPassword();
            info.setClientID();
            if ()
               .trace("Allocating session for " + this + " with request info=" + info);
            JmsSession session = (JmsSession.allocateConnection(info);
            try
            {
               if ()
                  .trace("Allocated  " + this + " session=" + session);
               session.setJmsSessionFactory(this);
               if ()
                  session.start();
               .add(session);
               return session;
            }
            catch (Throwable t)
            {
               try
               {
                  session.close();
               }
               catch (Throwable ignored)
               {
               }
               if (t instanceof Exception)
                  throw (Exceptiont;
               else
                  throw new RuntimeException("Unexpected error: "t);
            }
         }
      }
      catch (Exception e)
      {
         .error("could not create session"e);
         
         JMSException je = new JMSException
            ("Could not create a session: " + e);
         je.setLinkedException(e);
         throw je;
      }               
   }  
   protected void checkClosed() throws IllegalStateException
   {
      if ()
         throw new IllegalStateException("The connection is closed");
   }
   
   
Check whether a tranasction is active

Throws:
javax.jms.IllegalStateException if the transaction is not active, preparing, prepared or committing or for any error in the transaction manager
   protected void checkTransactionActive() throws IllegalStateException
   {
      if ( == null)
         throw new IllegalStateException("No connection manager");
      try
      {
         if ( instanceof JTATransactionChecker)
            ((JTATransactionChecker).checkTransactionActive();
      }
      catch (Exception e)
      {
         IllegalStateException ex = new IllegalStateException("Transaction not active");
         ex.initCause(e);
         ex.setLinkedException(e);
         throw ex;
      }
   }
New to GrepCode? Check out our FAQ X