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.deploy.hot;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.plugin.PluginPackage;
020    import com.liferay.portal.kernel.util.PropertiesUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import java.util.HashSet;
027    import java.util.Properties;
028    import java.util.Set;
029    
030    import javax.servlet.ServletContext;
031    
032    /**
033     * @author Ivica Cardic
034     * @author Brian Wing Shun Chan
035     */
036    public class HotDeployEvent {
037    
038            public HotDeployEvent(
039                    ServletContext servletContext, ClassLoader contextClassLoader) {
040    
041                    this(servletContext, contextClassLoader, true);
042            }
043    
044            public HotDeployEvent(
045                    ServletContext servletContext, ClassLoader contextClassLoader,
046                    boolean dependencyManagementEnabled) {
047    
048                    _servletContext = servletContext;
049                    _contextClassLoader = contextClassLoader;
050                    _dependencyManagementEnabled = dependencyManagementEnabled;
051    
052                    try {
053                            initDependentServletContextNames();
054                    }
055                    catch (IOException ioe) {
056                            _log.error(ioe, ioe);
057                    }
058            }
059    
060            public ClassLoader getContextClassLoader() {
061                    return _contextClassLoader;
062            }
063    
064            public Set<String> getDependentServletContextNames() {
065                    return _dependentServletContextNames;
066            }
067    
068            public PluginPackage getPluginPackage() {
069                    return _pluginPackage;
070            }
071    
072            public ServletContext getServletContext() {
073                    return _servletContext;
074            }
075    
076            public String getServletContextName() {
077                    return _servletContext.getServletContextName();
078            }
079    
080            public boolean isDependencyManagementEnabled() {
081                    return _dependencyManagementEnabled;
082            }
083    
084            public void setPluginPackage(PluginPackage pluginPackage) {
085                    _pluginPackage = pluginPackage;
086            }
087    
088            protected void initDependentServletContextNames() throws IOException {
089                    if (!_dependencyManagementEnabled) {
090                            return;
091                    }
092    
093                    InputStream is = _servletContext.getResourceAsStream(
094                            "/WEB-INF/liferay-plugin-package.properties");
095    
096                    if (is != null) {
097                            String propertiesString = StringUtil.read(is);
098    
099                            is.close();
100    
101                            Properties properties = PropertiesUtil.load(propertiesString);
102    
103                            String[] requiredDeploymentContexts = StringUtil.split(
104                                    properties.getProperty("required-deployment-contexts"));
105    
106                            if ((requiredDeploymentContexts.length > 0) &&
107                                    _log.isInfoEnabled()) {
108    
109                                    _log.info(
110                                            "Plugin " + _servletContext.getServletContextName() +
111                                                    " requires " +
112                                                    StringUtil.merge(requiredDeploymentContexts, ", "));
113                            }
114    
115                            for (String requiredDeploymentContext :
116                                            requiredDeploymentContexts) {
117    
118                                    _dependentServletContextNames.add(
119                                            requiredDeploymentContext.trim());
120                            }
121                    }
122            }
123    
124            private static Log _log = LogFactoryUtil.getLog(HotDeployEvent.class);
125    
126            private ClassLoader _contextClassLoader;
127            private boolean _dependencyManagementEnabled = true;
128            private Set<String> _dependentServletContextNames = new HashSet<String>();
129            private PluginPackage _pluginPackage;
130            private ServletContext _servletContext;
131    
132    }