Package org.jboss.narayana.blacktie.jatmibroker.core.transport

Source Code of org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory

/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* 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,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA  02110-1301, USA.
*/
package org.jboss.narayana.blacktie.jatmibroker.core.transport;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jboss.narayana.blacktie.jatmibroker.core.conf.ConfigurationException;
import org.jboss.narayana.blacktie.jatmibroker.core.transport.hybrid.TransportImpl;
import org.jboss.narayana.blacktie.jatmibroker.core.transport.hybrid.stomp.StompManagement;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException;

public class TransportFactory {

  private static final Logger log = LogManager
      .getLogger(TransportFactory.class);
  private Properties properties;
  private OrbManagement orbManagement;
  private StompManagement momManagement;
  private List<Transport> transports = new ArrayList<Transport>();

  private boolean closed;

  public TransportFactory(Properties properties)
      throws ConfigurationException {
    log.debug("Creating Transportfactory: " + this);
    this.properties = properties;

    try {
      momManagement = new StompManagement(properties);
    } catch (Throwable t) {
      throw new ConfigurationException(
          "Could not create the required connection", t);
    }

    try {
      orbManagement = OrbManagement.getInstance(properties);
    } catch (Throwable t) {
      throw new ConfigurationException(
          "Could not create the orb management function", t);
    }

    log.debug("Created OrbManagement");
  }

  public synchronized Transport createTransport() {
    log.debug("Creating transport from factory: " + this);
    TransportImpl instance = new TransportImpl(orbManagement,
        momManagement, properties, this);
    transports.add(instance);
    log.debug("Created transport from factory: " + this + " transport: "
        + instance);
    return instance;
  }

  public void removeTransport(TransportImpl transportImpl) {
    boolean remove = transports.remove(transportImpl);
    log.debug("Transport was removed: " + transportImpl + " from: " + this
        + " result: " + remove);
  }

  /**
   * Make sure that the
   */
  public synchronized final void close() {
    log.debug("Close called: " + this);
    if (!closed) {
      log.debug("Going into shutdown");
      log.debug("Closing factory: " + this);
      Transport[] transport = new Transport[transports.size()];
      transport = transports.toArray(transport);
      for (int i = 0; i < transport.length; i++) {
        try {
          log.debug("Closing transport: " + transport[i]
              + " from factory: " + this);
          transport[i].close();
        } catch (ConnectionException e) {
          log.warn(
              "Transport could not be closed: " + e.getMessage(),
              e);
        }
      }
      transports.clear();
      momManagement.close();
      closed = true;
    }
    log.debug("Closed factory: " + getClass().getName());
  }
}
TOP

Related Classes of org.jboss.narayana.blacktie.jatmibroker.core.transport.TransportFactory

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.