/*
 * 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();
    }
}

@@ -707,16 +707,56 @@
 tional;%0A
+import com.google.common.collect.Lists;%0A
 import i
@@ -997,16 +997,50 @@
 Manager;
+%0Aimport lombok.extern.slf4j.Slf4j;
 %0A%0Aimport
@@ -1050,24 +1050,20 @@
 va.util.
-Iterator
+List
 ;%0Aimport
@@ -1166,16 +1166,23 @@
 jun%0A */%0A
+@Slf4j%0A
 public a
@@ -1972,16 +1972,12 @@
     
-Iterator
+List
 %3CTra
@@ -1997,19 +1997,52 @@
 er%3E 
-iterator = 
+transactionManagerList = Lists.newArrayList(
 Serv
@@ -2092,16 +2092,17 @@
 erator()
+)
 ;%0A      
@@ -2107,64 +2107,249 @@
     
-return iterator.hasNext() ? Optional.of(iterator.nex
+if (transactionManagerList.size() %3E 1) %7B%0A            log.info(%22there is more than one transaction manger existing, chosen first one default.%22);%0A        %7D%0A        return transactionManagerList.isEmpty() ? Optional.%3CTransactionManager%3Eabsen
 t()
-)
  : O
@@ -2348,34 +2348,36 @@
 nt() : Optional.
-%3CT
+of(t
 ransactionManage
@@ -2377,24 +2377,27 @@
 nManager
-%3Eabsent(
+List.get(0)
 );%0A    %7D

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