1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.deploy.hot;
24  
25  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
26  import com.liferay.portal.kernel.deploy.hot.HotDeployException;
27  import com.liferay.portal.kernel.plugin.PluginPackage;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.plugin.PluginPackageImpl;
33  import com.liferay.portal.plugin.PluginPackageUtil;
34  import com.liferay.util.Version;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  
39  import java.util.jar.Attributes;
40  import java.util.jar.Manifest;
41  
42  import javax.servlet.ServletContext;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  import org.dom4j.DocumentException;
48  
49  /**
50   * <a href="PluginPackageHotDeployListener.java.html"><b><i>View Source</i></b>
51   * </a>
52   *
53   * @author Jorge Ferrer
54   *
55   */
56  public class PluginPackageHotDeployListener extends BaseHotDeployListener {
57  
58      public static PluginPackage readPluginPackage(ServletContext servletContext)
59          throws DocumentException, IOException {
60  
61          PluginPackage pluginPackage = null;
62  
63          String servletContextName = servletContext.getServletContextName();
64  
65          String xml = HttpUtil.URLtoString(
66              servletContext.getResource("/WEB-INF/liferay-plugin-package.xml"));
67  
68          if (_log.isInfoEnabled()) {
69              if (servletContextName == null) {
70                  _log.info("Reading plugin package for the root context");
71              }
72              else {
73                  _log.info("Reading plugin package for " + servletContextName);
74              }
75          }
76  
77          if (xml == null) {
78              if (_log.isDebugEnabled()) {
79                  _log.debug("Reading plugin package from MANIFEST.MF");
80              }
81  
82              Attributes attributes = null;
83  
84              InputStream is = servletContext.getResourceAsStream(
85                  "/META-INF/MANIFEST.MF");
86  
87              if (is != null) {
88                  Manifest manifest = new Manifest(is);
89  
90                  attributes = manifest.getMainAttributes();
91              }
92              else {
93                  attributes = new Attributes();
94              }
95  
96              String artifactGroupId = attributes.getValue(
97                  "Implementation-Vendor-Id");
98  
99              if (Validator.isNull(artifactGroupId)) {
100                 artifactGroupId = attributes.getValue("Implementation-Vendor");
101             }
102 
103             if (Validator.isNull(artifactGroupId)) {
104                 artifactGroupId = GetterUtil.getString(
105                     attributes.getValue("Bundle-Vendor"), servletContextName);
106             }
107 
108             String artifactId = attributes.getValue("Implementation-Title");
109 
110             if (Validator.isNull(artifactId)) {
111                 artifactId = GetterUtil.getString(
112                     attributes.getValue("Bundle-Name"), servletContextName);
113             }
114 
115             String version = attributes.getValue("Implementation-Version");
116 
117             if (Validator.isNull(version)) {
118                 version = GetterUtil.getString(
119                     attributes.getValue("Bundle-Version"), Version.UNKNOWN);
120             }
121 
122             if (version.equals(Version.UNKNOWN) && _log.isWarnEnabled()) {
123                 _log.warn(
124                     "Plugin package on context " + servletContextName +
125                         " cannot be tracked because this WAR does not " +
126                             "contain a liferay-plugin-package.xml file");
127             }
128 
129             pluginPackage =
130                 new PluginPackageImpl(
131                     artifactGroupId + StringPool.SLASH + artifactId +
132                         StringPool.SLASH + version + StringPool.SLASH +
133                             "war");
134 
135             pluginPackage.setName(artifactId);
136 
137             String shortDescription = attributes.getValue("Bundle-Description");
138 
139             if (Validator.isNotNull(shortDescription)) {
140                 pluginPackage.setShortDescription(shortDescription);
141             }
142 
143             String pageURL = attributes.getValue("Bundle-DocURL");
144 
145             if (Validator.isNotNull(pageURL)) {
146                 pluginPackage.setPageURL(pageURL);
147             }
148         }
149         else {
150             if (_log.isDebugEnabled()) {
151                 _log.debug(
152                     "Reading plugin package from liferay-plugin-package.xml");
153             }
154 
155             pluginPackage = PluginPackageUtil.readPluginPackageXml(xml);
156         }
157 
158         pluginPackage.setContext(servletContextName);
159 
160         return pluginPackage;
161     }
162 
163     public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
164         try {
165             doInvokeDeploy(event);
166         }
167         catch (Exception e) {
168             throwHotDeployException(event, "Error registering plugins for ", e);
169         }
170     }
171 
172     public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
173         try {
174             doInvokeUndeploy(event);
175         }
176         catch (Exception e) {
177             throwHotDeployException(
178                 event, "Error unregistering plugins for ", e);
179         }
180     }
181 
182     protected void doInvokeDeploy(HotDeployEvent event) throws Exception {
183         ServletContext servletContext = event.getServletContext();
184 
185         String servletContextName = servletContext.getServletContextName();
186 
187         if (_log.isDebugEnabled()) {
188             _log.debug("Invoking deploy for " + servletContextName);
189         }
190 
191         if (servletContext.getResource(
192                 "/WEB-INF/liferay-theme-loader.xml") != null) {
193 
194             return;
195         }
196 
197         PluginPackage pluginPackage = readPluginPackage(servletContext);
198 
199         if (pluginPackage == null) {
200             return;
201         }
202 
203         pluginPackage.setContext(servletContextName);
204 
205         event.setPluginPackage(pluginPackage);
206 
207         PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
208 
209         if (_log.isInfoEnabled()) {
210             _log.info(
211                 "Plugin package " + pluginPackage.getModuleId() +
212                     " registered successfully");
213         }
214     }
215 
216     protected void doInvokeUndeploy(HotDeployEvent event) throws Exception {
217         ServletContext servletContext = event.getServletContext();
218 
219         String servletContextName = servletContext.getServletContextName();
220 
221         if (_log.isDebugEnabled()) {
222             _log.debug("Invoking deploy for " + servletContextName);
223         }
224 
225         PluginPackage pluginPackage = readPluginPackage(servletContext);
226 
227         if (pluginPackage == null) {
228             return;
229         }
230 
231         event.setPluginPackage(pluginPackage);
232 
233         PluginPackageUtil.unregisterInstalledPluginPackage(pluginPackage);
234 
235         if (_log.isInfoEnabled()) {
236             _log.info(
237                 "Plugin package " + pluginPackage.getModuleId() +
238                     " unregistered successfully");
239         }
240     }
241 
242     private static Log _log =
243         LogFactory.getLog(PluginPackageHotDeployListener.class);
244 
245 }