Package org.jboss.narayana.blacktie.jatmibroker.xatmi

Source Code of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionFactory

package org.jboss.narayana.blacktie.jatmibroker.xatmi;

import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.AtmiBrokerEnvXML;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException;

/**
* This is a factory that will create connections to remote Blacktie services.
*
* @see Connection
* @see ConnectionException
*/
public class ConnectionFactory {
  private static final Logger log = LogManager
      .getLogger(ConnectionFactory.class);

  /**
   * The properties inside the connection factory.
   */
  private Properties properties = new Properties();

  /**
   * The connection factory will allocate a connection per thread.
   */
  private static ThreadLocal<Connection> connections = new ThreadLocal<Connection>();

  /**
   * Get the default connection factory
   *
   * @return The connection factory
   * @throws ConfigurationException
   *             If the configuration cannot be parsed.
   */
  public static synchronized ConnectionFactory getConnectionFactory()
      throws ConfigurationException {
    return new ConnectionFactory();
  }

  /**
   * Create the connection factory
   *
   * @throws ConfigurationException
   *             In case the configuration could not be loaded
   */
  private ConnectionFactory() throws ConfigurationException {
    log.debug("Creating connection factory: " + this);
    AtmiBrokerEnvXML xml = new AtmiBrokerEnvXML();
    properties.putAll(xml.getProperties());
    log.debug("Created connection factory: " + this);
  }

  /**
   * Get the connection for this thread.
   *
   * @return The connection for this thread.
   * @throws ConfigurationException
   */
  public Connection getConnection() throws ConfigurationException {
    Connection connection = connections.get();
    if (connection == null) {
      connection = new Connection(this, properties);
      connections.set(connection);
      log.debug("Returning connection: " + connection);
    }
    return connection;
  }

  /**
   * Remove the connection from the factory after closure.
   *
   * @param connection
   *            The connection to remove.
   */
  void removeConnection(Connection connection) {
    connections.set(null);
  }
}
TOP

Related Classes of org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.