001    /**
002     * Copyright (c) 2000-present 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.deploy.hot;
016    
017    import com.liferay.portal.kernel.messaging.MessageListener;
018    import com.liferay.portal.kernel.util.ProxyFactory;
019    
020    import java.lang.reflect.Field;
021    import java.lang.reflect.Method;
022    
023    import javax.servlet.ServletContext;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public abstract class BaseHotDeployListener implements HotDeployListener {
029    
030            public void throwHotDeployException(
031                            HotDeployEvent event, String msg, Throwable t)
032                    throws HotDeployException {
033    
034                    ServletContext servletContext = event.getServletContext();
035    
036                    String servletContextName = servletContext.getServletContextName();
037    
038                    throw new HotDeployException(msg + servletContextName, t);
039            }
040    
041            protected String getClpServletContextName(
042                            Class<?> clpMessageListenerClass,
043                            MessageListener clpMessageListener)
044                    throws Exception {
045    
046                    Exception e = null;
047    
048                    try {
049                            Method servletContextNameMethod = clpMessageListenerClass.getMethod(
050                                    "getServletContextName");
051    
052                            String clpServletContextName =
053                                    (String)servletContextNameMethod.invoke(null);
054    
055                            return clpServletContextName;
056                    }
057                    catch (Exception e1) {
058                            e = e1;
059                    }
060    
061                    try {
062                            Field servletContextNameField = clpMessageListenerClass.getField(
063                                    "SERVLET_CONTEXT_NAME");
064    
065                            Object clpServletContextName = servletContextNameField.get(
066                                    clpMessageListener);
067    
068                            return clpServletContextName.toString();
069                    }
070                    catch (Exception e2) {
071                    }
072    
073                    throw e;
074            }
075    
076            protected Object newInstance(
077                            ClassLoader portletClassLoader, Class<?> interfaceClass,
078                            String implClassName)
079                    throws Exception {
080    
081                    return ProxyFactory.newInstance(
082                            portletClassLoader, interfaceClass, implClassName);
083            }
084    
085            protected Object newInstance(
086                            ClassLoader portletClassLoader, Class<?>[] interfaceClasses,
087                            String implClassName)
088                    throws Exception {
089    
090                    return ProxyFactory.newInstance(
091                            portletClassLoader, interfaceClasses, implClassName);
092            }
093    
094    }