001    /**
002     * Copyright (c) 2000-2010 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.kernel.bean;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class PortletBeanLocatorUtil {
027    
028            public static BeanLocator getBeanLocator(String servletContextName) {
029                    return _beanLocators.get(servletContextName);
030            }
031    
032            public static Object locate(String servletContextName, String name)
033                    throws BeanLocatorException {
034    
035                    BeanLocator beanLocator = getBeanLocator(servletContextName);
036    
037                    if (beanLocator == null) {
038                            _log.error(
039                                    "BeanLocator is null for servlet context " +
040                                            servletContextName);
041    
042                            throw new BeanLocatorException(
043                                    "BeanLocator has not been set for servlet context " +
044                                            servletContextName);
045                    }
046                    else {
047                            return beanLocator.locate(name);
048                    }
049            }
050    
051            public static void setBeanLocator(
052                    String servletContextName, BeanLocator beanLocator) {
053    
054                    if (_log.isDebugEnabled()) {
055                            if (beanLocator != null) {
056                                    _log.debug(
057                                            "Setting BeanLocator " + beanLocator.hashCode() +
058                                                    " for servlet context " + servletContextName);
059                            }
060                            else {
061                                    _log.debug(
062                                            "Removing BeanLocator for servlet context " +
063                                                    servletContextName);
064                            }
065                    }
066    
067                    _beanLocators.put(servletContextName, beanLocator);
068            }
069    
070            private static Log _log = LogFactoryUtil.getLog(
071                    PortletBeanLocatorUtil.class);
072    
073            private static Map<String, BeanLocator> _beanLocators =
074                    new HashMap<String, BeanLocator>();
075    
076    }