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

import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.Subscribe;
import io.shardingsphere.core.util.EventBusInstance;
import io.shardingsphere.transaction.manager.ShardingTransactionManager;
import io.shardingsphere.transaction.manager.ShardingTransactionManagerRegistry;
import io.shardingsphere.transaction.TransactionTypeHolder;
import io.shardingsphere.transaction.event.TransactionEvent;

import java.sql.SQLException;

/**
 * Transaction Listener.
 *
 * @author zhaojun
 */
public final class TransactionListener {
    
    /**
     * Register transaction listener into event bus.
     */
    public void register() {
        EventBusInstance.getInstance().register(this);
    }
    
    /**
     * Listen event.
     *
     * @param transactionEvent transaction event
     * @throws SQLException SQL exception
     */
    @Subscribe
    @AllowConcurrentEvents
    public void listen(final TransactionEvent transactionEvent) throws SQLException {
        ShardingTransactionManager shardingTransactionManager = ShardingTransactionManagerRegistry.getInstance().getShardingTransactionManager(TransactionTypeHolder.get());
        switch (transactionEvent.getTclType()) {
            case BEGIN:
                shardingTransactionManager.begin(transactionEvent);
                break;
            case COMMIT:
                shardingTransactionManager.commit(transactionEvent);
                break;
            case ROLLBACK:
                shardingTransactionManager.rollback(transactionEvent);
                break;
            default:
        }
    }
}

@@ -798,29 +798,275 @@
 ore.
-util.EventBusInstance
+constant.TransactionType;%0Aimport io.shardingsphere.core.util.EventBusInstance;%0Aimport io.shardingsphere.transaction.event.LocalTransactionEvent;%0Aimport io.shardingsphere.transaction.event.TransactionEvent;%0Aimport io.shardingsphere.transaction.event.XaTransactionEvent
 ;%0Aim
@@ -1220,129 +1220,8 @@
 try;
-%0Aimport io.shardingsphere.transaction.TransactionTypeHolder;%0Aimport io.shardingsphere.transaction.event.TransactionEvent;
 %0A%0Aim
@@ -1920,16 +1920,19 @@
 Manager(
+get
 Transact
@@ -1942,19 +1942,25 @@
 Type
-Holder.get(
+(transactionEvent
 ));%0A
@@ -2382,16 +2382,16 @@
 efault:%0A
-
         
@@ -2398,10 +2398,427 @@
 %7D%0A    %7D%0A
+    %0A    private TransactionType getTransactionType(final TransactionEvent transactionEvent) %7B%0A        if (transactionEvent instanceof LocalTransactionEvent) %7B%0A            return TransactionType.LOCAL;%0A        %7D%0A        if (transactionEvent instanceof XaTransactionEvent) %7B%0A            return TransactionType.XA;%0A        %7D%0A        throw new UnsupportedOperationException(transactionEvent.getClass().getName());%0A    %7D%0A
 %7D%0A

refactor TransactionListener