All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xeiam.xchange.ExchangeFactory Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/**
 * Copyright (C) 2012 - 2013 Xeiam LLC http://xeiam.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.xeiam.xchange;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xeiam.xchange.utils.Assert;

/**
 * 

* Factory to provide the following to {@link Exchange}: *

*
    *
  • Manages the creation of specific Exchange implementations using runtime dependencies
  • *
*/ public enum ExchangeFactory { INSTANCE; private final Logger log = LoggerFactory.getLogger(ExchangeFactory.class); /** * Constructor */ private ExchangeFactory() { } /** * Create an Exchange object * * @param exchangeClassName * @return */ public Exchange createExchange(String exchangeClassName) { Assert.notNull(exchangeClassName, "exchangeClassName cannot be null"); log.debug("Creating default exchange from class name"); // Attempt to create an instance of the exchange provider try { // Attempt to locate the exchange provider on the classpath Class exchangeProviderClass = Class.forName(exchangeClassName); // Test that the class implements Exchange if (Exchange.class.isAssignableFrom(exchangeProviderClass)) { // Instantiate through the default constructor and use the default exchange specification Exchange exchange = (Exchange) exchangeProviderClass.newInstance(); exchange.applySpecification(exchange.getDefaultExchangeSpecification()); return exchange; } else { throw new ExchangeException("Class '" + exchangeClassName + "' does not implement Exchange"); } } catch (ClassNotFoundException e) { throw new ExchangeException("Problem creating Exchange (class not found)", e); } catch (InstantiationException e) { throw new ExchangeException("Problem creating Exchange (instantiation)", e); } catch (IllegalAccessException e) { throw new ExchangeException("Problem creating Exchange (illegal access)", e); } // Cannot be here due to exceptions } public Exchange createExchange(ExchangeSpecification exchangeSpecification) { Assert.notNull(exchangeSpecification, "exchangeSpecfication cannot be null"); log.debug("Creating exchange from specification"); String exchangeClassName = exchangeSpecification.getExchangeClassName(); // Attempt to create an instance of the exchange provider try { // Attempt to locate the exchange provider on the classpath Class exchangeProviderClass = Class.forName(exchangeClassName); // Test that the class implements Exchange if (Exchange.class.isAssignableFrom(exchangeProviderClass)) { // Instantiate through the default constructor Exchange exchange = (Exchange) exchangeProviderClass.newInstance(); exchange.applySpecification(exchangeSpecification); return exchange; } else { throw new ExchangeException("Class '" + exchangeClassName + "' does not implement Exchange"); } } catch (ClassNotFoundException e) { throw new ExchangeException("Problem starting exchange provider (class not found)", e); } catch (InstantiationException e) { throw new ExchangeException("Problem starting exchange provider (instantiation)", e); } catch (IllegalAccessException e) { throw new ExchangeException("Problem starting exchange provider (illegal access)", e); } // Cannot be here due to exceptions } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy