/*
 * 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.spi;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.ServiceLoader;

/**
 * SPI service loader for new instance for every call.
 *
 * @author zhangliang
 * @author zhaojun
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class NewInstanceServiceLoader {
    
    private static final Map<Class, Collection> SERVICE_MAP = new HashMap<>();
    
    /**
     * Load SPI service, hold the class in service map for new instance.
     * 
     * @param service service type
     * @param <T> type of service
     * @return new service class loader
     */
    public static <T> Collection<T> load(final Class<T> service) {
        Collection<T> result = new LinkedList<>();
        for (T each : ServiceLoader.load(service)) {
            registerServiceClass(service, each);
            result.add(each);
        }
        return result;
    }
    
    @SuppressWarnings("unchecked")
    private static <T> void registerServiceClass(final Class<T> service, final T instance) {
        Collection serviceClasses = SERVICE_MAP.get(service);
        if (null == serviceClasses) {
            serviceClasses = new LinkedList<>();
        }
        serviceClasses.add(instance.getClass());
        SERVICE_MAP.put(service, serviceClasses);
    }
}

@@ -645,16 +645,75 @@
 e.spi;%0A%0A
+import io.shardingsphere.core.exception.ShardingException;%0A
 import l
@@ -731,16 +731,16 @@
 sLevel;%0A
-
 import l
@@ -1157,16 +1157,26 @@
 llection
+%3CClass%3C?%3E%3E
 %3E SERVIC
@@ -1851,16 +1851,26 @@
 llection
+%3CClass%3C?%3E%3E
  service
@@ -2051,16 +2051,16 @@
 ass());%0A
-
         
@@ -2107,10 +2107,627 @@
 ;%0A    %7D%0A
+    %0A    /**%0A     * New service instances.%0A     *%0A     * @param service service class%0A     * @param %3CT%3E type of service%0A     * @return service instances%0A     */%0A    @SuppressWarnings(%22unchecked%22)%0A    public static %3CT%3E Collection newServiceInstances(final Class%3CT%3E service) %7B%0A        Collection%3CT%3E result = new LinkedList%3C%3E();%0A        for (Class%3C?%3E each : SERVICE_MAP.get(service)) %7B%0A            try %7B%0A                result.add((T) each.newInstance());%0A            %7D catch (final ReflectiveOperationException ex) %7B%0A                throw new ShardingException(ex);%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A
 %7D%0A

Add NewInstanceServiceLoader.newServiceInstances().