001    /**
002     * Copyright (c) 2000-2012 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.spring.context;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.PortalClassLoaderServletContextListener;
020    import com.liferay.portal.kernel.util.ContextPathUtil;
021    import com.liferay.portal.kernel.util.PortalClassInvoker;
022    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
023    
024    import javax.servlet.ServletContext;
025    import javax.servlet.ServletContextListener;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortletContextLoaderListener
031            extends PortalClassLoaderServletContextListener {
032    
033            public static String getLockKey(ServletContext servletContext) {
034                    String contextPath = ContextPathUtil.getContextPath(servletContext);
035    
036                    return getLockKey(contextPath);
037            }
038    
039            public static String getLockKey(String contextPath) {
040                    try {
041                            Object returnValue = PortalClassInvoker.invoke(
042                                    false, _CLASS_NAME, "getLockKey",
043                                    new String[] {String.class.getName()}, contextPath);
044    
045                            return String.valueOf(returnValue);
046                    }
047                    catch (Exception e) {
048                            _log.error(e, e);
049    
050                            throw new IllegalStateException("Unable to get lock key", e);
051                    }
052            }
053    
054            @Override
055            protected ServletContextListener getInstance() throws Exception {
056                    Class<?> clazz = Class.forName(
057                            _CLASS_NAME, true, PortalClassLoaderUtil.getClassLoader());
058    
059                    return (ServletContextListener)clazz.newInstance();
060            }
061    
062            private static final String _CLASS_NAME =
063                    "com.liferay.portal.spring.context.PortletContextLoaderListener";
064    
065            private static Log _log = LogFactoryUtil.getLog(
066                    PortletContextLoaderListener.class.getName());
067    
068    }