/*
 * Copyright 2012 ZerothAngel <zerothangel@tyrannyofheaven.org>
 *
 * 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.
 */
package org.tyrannyofheaven.bukkit.util.transaction;

import java.util.concurrent.Executor;

/**
 * Simple Executor implementation that simply queues up Runnables within a
 * TransactionRunnable.
 * 
 * @author zerothangel
 */
class TransactionExecutor implements Executor {

    private final TransactionStrategy transactionStrategy;

    private TransactionRunnable currentTransactionRunnable;

    public TransactionExecutor(TransactionStrategy transactionStrategy) {
        this.transactionStrategy = transactionStrategy;
    }

    @Override
    public void execute(Runnable command) {
        if (currentTransactionRunnable == null)
            throw new IllegalStateException("No current TransactionRunnable");
        currentTransactionRunnable.addRunnable(command);
    }

    public void begin(boolean readOnly) {
        if (currentTransactionRunnable != null)
            throw new IllegalStateException("Existing TransactionRunnable found");
        currentTransactionRunnable = new TransactionRunnable(transactionStrategy, readOnly);
    }

    public TransactionRunnable end() {
        if (currentTransactionRunnable == null)
            throw new IllegalStateException("No current TransactionRunnable");
        TransactionRunnable current = currentTransactionRunnable;
        currentTransactionRunnable = null;
        return current;
    }

}

@@ -970,16 +970,34 @@
 private 
+final ThreadLocal%3C
 Transact
@@ -999,32 +999,33 @@
 nsactionRunnable
+%3E
  currentTransact
@@ -1035,16 +1035,63 @@
 Runnable
+Holder = new ThreadLocal%3CTransactionRunnable%3E()
 ;%0A%0A    p
@@ -1276,24 +1276,121 @@
  command) %7B%0A
+        TransactionRunnable currentTransactionRunnable = currentTransactionRunnableHolder.get();%0A
         if (
@@ -1606,24 +1606,121 @@
 readOnly) %7B%0A
+        TransactionRunnable currentTransactionRunnable = currentTransactionRunnableHolder.get();%0A
         if (
@@ -1872,19 +1872,27 @@
 Runnable
- = 
+Holder.set(
 new Tran
@@ -1937,16 +1937,17 @@
 eadOnly)
+)
 ;%0A    %7D%0A
@@ -1982,24 +1982,121 @@
 ble end() %7B%0A
+        TransactionRunnable currentTransactionRunnable = currentTransactionRunnableHolder.get();%0A
         if (
@@ -2221,38 +2221,8 @@
     
- TransactionRunnable current =
  cur
@@ -2244,16 +2244,31 @@
 Runnable
+Holder.remove()
 ;%0A      
@@ -2261,32 +2261,39 @@
 move();%0A        
+return 
 currentTransacti
@@ -2306,39 +2306,8 @@
 able
- = null;%0A        return current
 ;%0A  

Make TransactionExecutor thread-safe for badly written chat plugins.