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