1   /**
2    * Copyright (c) 2000-2007 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.deploy.hot.HotDeployListener;
28  import com.liferay.portal.kernel.plugin.PluginPackage;
29  import com.liferay.portal.kernel.util.GetterUtil;
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.Http;
35  import com.liferay.util.Version;
36  
37  import java.io.IOException;
38  import java.io.InputStream;
39  
40  import java.util.jar.Attributes;
41  import java.util.jar.Manifest;
42  
43  import javax.servlet.ServletContext;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import org.dom4j.DocumentException;
49  
50  /**
51   * <a href="PluginPackageHotDeployListener.java.html"><b><i>View Source</i></b>
52   * </a>
53   *
54   * @author Jorge Ferrer
55   *
56   */
57  public class PluginPackageHotDeployListener implements HotDeployListener {
58  
59      public static PluginPackage readPluginPackage(ServletContext ctx)
60          throws DocumentException, IOException {
61  
62          PluginPackage pluginPackage = null;
63  
64          String servletContextName = ctx.getServletContextName();
65  
66          String xml = Http.URLtoString(
67              ctx.getResource("/WEB-INF/liferay-plugin-package.xml"));
68  
69          if (_log.isInfoEnabled()) {
70              if (servletContextName == null) {
71                  _log.info("Reading plugin package for the root context");
72              }
73              else {
74                  _log.info("Reading plugin package for " + servletContextName);
75              }
76          }
77  
78          if (xml == null) {
79              if (_log.isDebugEnabled()) {
80                  _log.debug("Reading plugin package from MANIFEST.MF");
81              }
82  
83              Attributes attributes = null;
84  
85              InputStream is = ctx.getResourceAsStream("/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 == 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         return pluginPackage;
159     }
160 
161     public void invokeDeploy(HotDeployEvent event) throws HotDeployException {
162         String servletContextName = null;
163 
164         try {
165             ServletContext ctx = event.getServletContext();
166 
167             servletContextName = ctx.getServletContextName();
168 
169             if (_log.isDebugEnabled()) {
170                 _log.debug("Invoking deploy for " + servletContextName);
171             }
172 
173             if (ctx.getResource("/WEB-INF/liferay-theme-loader.xml") != null) {
174                 return;
175             }
176 
177             PluginPackage pluginPackage = readPluginPackage(ctx);
178 
179             if (pluginPackage != null) {
180                 pluginPackage.setContext(servletContextName);
181 
182                 event.setPluginPackage(pluginPackage);
183 
184                 PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
185 
186                 if (_log.isInfoEnabled()) {
187                     _log.info(
188                         "Plugin package " + pluginPackage.getModuleId() +
189                             " registered successfully");
190                 }
191             }
192         }
193         catch (Exception e) {
194             throw new HotDeployException(
195                 "Error registering plugins for " + servletContextName,
196                 e);
197         }
198     }
199 
200     public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
201         String servletContextName = null;
202 
203         try {
204             ServletContext ctx = event.getServletContext();
205 
206             servletContextName = ctx.getServletContextName();
207 
208             if (_log.isDebugEnabled()) {
209                 _log.debug("Invoking deploy for " + servletContextName);
210             }
211 
212             PluginPackage pluginPackage = readPluginPackage(ctx);
213 
214             if (pluginPackage != null) {
215                 event.setPluginPackage(pluginPackage);
216 
217                 PluginPackageUtil.unregisterInstalledPluginPackage(
218                     pluginPackage);
219 
220                 if (_log.isInfoEnabled()) {
221                     _log.info(
222                         "Plugin package " + pluginPackage.getModuleId() +
223                             " unregistered successfully");
224                 }
225             }
226         }
227         catch (Exception e) {
228             throw new HotDeployException(
229                 "Error unregistering plugins for " + servletContextName,
230                 e);
231         }
232     }
233 
234     private static Log _log =
235         LogFactory.getLog(PluginPackageHotDeployListener.class);
236 
237 }