001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }