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