001    /**
002     * Copyright (c) 2000-present 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.kernel.process.local;
016    
017    import com.liferay.portal.kernel.concurrent.AsyncBroker;
018    import com.liferay.portal.kernel.util.CentralizedThreadLocal;
019    
020    import java.io.Serializable;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class AsyncBrokerThreadLocal {
026    
027            public static AsyncBroker<Long, Serializable> getAsyncBroker() {
028                    AsyncBroker<Long, Serializable> asyncBroker =
029                            _asyncBrokerThreadLocal.get();
030    
031                    if (asyncBroker == null) {
032                            throw new IllegalStateException("Async broker is not set");
033                    }
034    
035                    return asyncBroker;
036            }
037    
038            public static void removeAsyncBroker() {
039                    _asyncBrokerThreadLocal.remove();
040            }
041    
042            public static void setAsyncBroker(
043                    AsyncBroker<Long, Serializable> asyncBroker) {
044    
045                    _asyncBrokerThreadLocal.set(asyncBroker);
046            }
047    
048            private static final ThreadLocal<AsyncBroker<Long, Serializable>>
049                    _asyncBrokerThreadLocal = new CentralizedThreadLocal<>(false);
050    
051    }