001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.spring.transaction;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    import java.util.concurrent.Callable;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class TransactionCommitCallbackUtil {
028    
029            public static void registerCallback(Callable<?> callable) {
030                    List<List<Callable<?>>> callbackListList =
031                            _callbackListListThreadLocal.get();
032    
033                    int index = callbackListList.size() - 1;
034    
035                    List<Callable<?>> callableList = callbackListList.get(index);
036    
037                    if (callableList == Collections.EMPTY_LIST) {
038                            callableList = new ArrayList<Callable<?>>();
039    
040                            callbackListList.set(index, callableList);
041                    }
042    
043                    callableList.add(callable);
044            }
045    
046            protected static List<Callable<?>> popCallbackList() {
047                    List<List<Callable<?>>> callbackListList =
048                            _callbackListListThreadLocal.get();
049    
050                    return callbackListList.remove(callbackListList.size() - 1);
051            }
052    
053            protected static void pushCallbackList() {
054                    List<List<Callable<?>>> callbackListList =
055                            _callbackListListThreadLocal.get();
056    
057                    callbackListList.add(Collections.EMPTY_LIST);
058            }
059    
060            private static ThreadLocal<List<List<Callable<?>>>>
061                    _callbackListListThreadLocal =
062                            new AutoResetThreadLocal<List<List<Callable<?>>>>(
063                                    TransactionCommitCallbackUtil.class +
064                                            "._callbackListListThreadLocal") {
065    
066                                    @Override
067                                    protected List<List<Callable<?>>> initialValue() {
068                                            return new ArrayList<List<Callable<?>>>();
069                                    }
070    
071                            };
072    
073    }