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