001    /**
002     * Copyright (c) 2000-2010 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    
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("BeanLocator is null");
039    
040                            throw new BeanLocatorException("BeanLocator has not been set");
041                    }
042                    else {
043                            return beanLocator.locate(name);
044                    }
045            }
046    
047            public static void setBeanLocator(
048                    String servletContextName, BeanLocator beanLocator) {
049    
050                    if (_log.isDebugEnabled()) {
051                            if (beanLocator != null) {
052                                    _log.debug("Setting BeanLocator " + beanLocator.hashCode());
053                            }
054                            else {
055                                    _log.debug("Setting BeanLocator null");
056                            }
057                    }
058    
059                    _beanLocators.put(servletContextName, beanLocator);
060            }
061    
062            private static Log _log = LogFactoryUtil.getLog(
063                    PortletBeanLocatorUtil.class);
064    
065            private static Map<String, BeanLocator> _beanLocators =
066                    new HashMap<String, BeanLocator>();
067    
068    }