/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.transaction.common.config;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.common.listener.TransactionListener;
import io.shardingsphere.transaction.common.spi.TransactionManager;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * Abstract transaction manager configuration.
 *
 * @author zhaojun
 */
public abstract class TransactionConfigurationAdapter implements TransactionConfiguration {
    
    @Override
    public TransactionManager configTransactionContext(final TransactionType transactionType) {
        TransactionManager result = null;
        switch (transactionType) {
            case XA:
                result = doXaTransactionConfiguration();
                break;
            case BASE:
                break;
            default:
        }
        return result;
    }
    
    @Override
    public void registerListener() {
        EventBusInstance.getInstance().register(TransactionListener.getInstance());
    }
    
    protected abstract TransactionManager doXaTransactionConfiguration();
    
    protected Optional<TransactionManager> doSPIConfiguration() {
        Iterator<TransactionManager> iterator = ServiceLoader.load(TransactionManager.class).iterator();
        return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.<TransactionManager>absent();
    }
}

/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.transaction.common.config;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import io.shardingsphere.core.constant.TransactionType;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.common.listener.TransactionListener;
import io.shardingsphere.transaction.common.spi.TransactionManager;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.ServiceLoader;

/**
 * Abstract transaction manager configuration.
 *
 * @author zhaojun
 */
@Slf4j
public abstract class TransactionConfigurationAdapter implements TransactionConfiguration {
    
    @Override
    public TransactionManager configTransactionContext(final TransactionType transactionType) {
        TransactionManager result = null;
        switch (transactionType) {
            case XA:
                result = doXaTransactionConfiguration();
                break;
            case BASE:
                break;
            default:
        }
        return result;
    }
    
    @Override
    public void registerListener() {
        EventBusInstance.getInstance().register(TransactionListener.getInstance());
    }
    
    protected abstract TransactionManager doXaTransactionConfiguration();
    
    protected Optional<TransactionManager> doSPIConfiguration() {
        List<TransactionManager> transactionManagerList = Lists.newArrayList(ServiceLoader.load(TransactionManager.class).iterator());
        if (transactionManagerList.size() > 1) {
            log.info("there is more than one transaction manger existing, chosen first one default.");
        }
        return transactionManagerList.isEmpty() ? Optional.<TransactionManager>absent() : Optional.of(transactionManagerList.get(0));
    }
}

add log for transactionConfiguration when existing more than one SPI implement.