001    /**
002     * Copyright (c) 2000-2013 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.service;
016    
017    import com.liferay.portal.kernel.util.AutoResetThreadLocal;
018    
019    import java.util.Stack;
020    
021    /**
022     * @author Michael C. Han
023     */
024    public class ServiceContextThreadLocal {
025    
026            public static ServiceContext getServiceContext() {
027                    Stack<ServiceContext> serviceContextStack =
028                            _serviceContextThreadLocal.get();
029    
030                    if (serviceContextStack.isEmpty()) {
031                            return null;
032                    }
033    
034                    return serviceContextStack.peek();
035            }
036    
037            public static ServiceContext popServiceContext() {
038                    Stack<ServiceContext> serviceContextStack =
039                            _serviceContextThreadLocal.get();
040    
041                    if (serviceContextStack.isEmpty()) {
042                            return null;
043                    }
044    
045                    return serviceContextStack.pop();
046            }
047    
048            public static void pushServiceContext(ServiceContext serviceContext) {
049                    Stack<ServiceContext> serviceContextStack =
050                            _serviceContextThreadLocal.get();
051    
052                    serviceContextStack.push(serviceContext);
053            }
054    
055            private static ThreadLocal<Stack<ServiceContext>>
056                    _serviceContextThreadLocal =
057                            new AutoResetThreadLocal<Stack<ServiceContext>>(
058                                    ServiceContextThreadLocal.class + "._serviceContextThreadLocal",
059                                    new Stack<ServiceContext>());
060    
061    }