SPIRoutingHook.java 1.9 KB
Newer Older
C
cherrylzhao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.infra.route.hook;
C
cherrylzhao 已提交
19

20 21
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
22
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
C
cherrylzhao 已提交
23 24 25 26

import java.util.Collection;

/**
27
 * Routing hook for SPI.
C
cherrylzhao 已提交
28
 */
29
public final class SPIRoutingHook implements RoutingHook {
C
cherrylzhao 已提交
30
    
31
    private final Collection<RoutingHook> routingHooks = ShardingSphereServiceLoader.newServiceInstances(RoutingHook.class);
C
cherrylzhao 已提交
32 33
    
    static {
34
        ShardingSphereServiceLoader.register(RoutingHook.class);
C
cherrylzhao 已提交
35 36 37 38
    }
    
    @Override
    public void start(final String sql) {
39
        for (RoutingHook each : routingHooks) {
C
cherrylzhao 已提交
40 41 42 43 44
            each.start(sql);
        }
    }
    
    @Override
45
    public void finishSuccess(final RouteContext routeContext, final ShardingSphereSchema schema) {
46
        for (RoutingHook each : routingHooks) {
47
            each.finishSuccess(routeContext, schema);
C
cherrylzhao 已提交
48 49 50 51 52
        }
    }
    
    @Override
    public void finishFailure(final Exception cause) {
53
        for (RoutingHook each : routingHooks) {
C
cherrylzhao 已提交
54 55 56 57
            each.finishFailure(cause);
        }
    }
}