/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.transaction;

import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.transaction.core.ResourceDataSource;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.apache.shardingsphere.transaction.rule.TransactionRule;
import org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

/**
 * ShardingSphere transaction manager engine.
 */
@Slf4j
public final class ShardingSphereTransactionManagerEngine {
    
    private final Map<TransactionType, ShardingSphereTransactionManager> transactionManagers = new EnumMap<>(TransactionType.class);
    
    public ShardingSphereTransactionManagerEngine() {
        loadTransactionManager();
    }
    
    private void loadTransactionManager() {
        for (ShardingSphereTransactionManager each : ServiceLoader.load(ShardingSphereTransactionManager.class)) {
            if (transactionManagers.containsKey(each.getTransactionType())) {
                log.warn("Find more than one {} transaction manager implementation class, use `{}` now",
                    each.getTransactionType(), transactionManagers.get(each.getTransactionType()).getClass().getName());
                continue;
            }
            transactionManagers.put(each.getTransactionType(), each);
        }
    }
    
    /**
     * Initialize transaction managers.
     *
     * @param databaseType database type
     * @param dataSourceMap data source map
     * @param transactionRule transaction rule
     */
    public void init(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final TransactionRule transactionRule) {
        transactionManagers.forEach((key, value) -> value.init(databaseType, getResourceDataSources(dataSourceMap), transactionRule));
    }
    
    private Collection<ResourceDataSource> getResourceDataSources(final Map<String, DataSource> dataSourceMap) {
        return dataSourceMap.entrySet().stream().map(entry -> new ResourceDataSource(entry.getKey(), entry.getValue())).collect(Collectors.toList());
    }
    
    /**
     * Get transaction manager.
     *
     * @param transactionType transaction type
     * @return transaction manager
     */
    public ShardingSphereTransactionManager getTransactionManager(final TransactionType transactionType) {
        ShardingSphereTransactionManager result = transactionManagers.get(transactionType);
        if (TransactionType.LOCAL != transactionType) {
            Preconditions.checkNotNull(result, "Cannot find transaction manager of [%s]", transactionType);
        }
        return result;
    }
    
    /**
     * Close transaction managers.
     * 
     * @throws Exception exception
     */
    public void close() throws Exception {
        for (Entry<TransactionType, ShardingSphereTransactionManager> entry : transactionManagers.entrySet()) {
            entry.getValue().close();
        }
    }
}

@@ -986,24 +986,90 @@
 tabaseType;%0A
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;%0A
 import org.a
@@ -1122,24 +1122,24 @@
 DataSource;%0A
-
 import org.a
@@ -1482,40 +1482,8 @@
 ry;%0A
-import java.util.ServiceLoader;%0A
 impo
@@ -1775,24 +1775,134 @@
 lass);%0A    %0A
+    static %7B%0A        ShardingSphereServiceLoader.register(ShardingSphereTransactionManager.class);%0A    %7D%0A    %0A
     public S
@@ -2090,25 +2090,54 @@
  : S
-erviceLoader.load
+hardingSphereServiceLoader.newServiceInstances
 (Sha
@@ -3809,16 +3809,17 @@
 lt, %22Can
+ 
 not find
@@ -3846,12 +3846,12 @@
  of 
-%5B%25s%5D
+%60%25s%60
 %22, t

Use ShardingSphereServiceLoader process ShardingSphereTransactionManager (#12095)