1
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.HttpUtil;
31 import com.liferay.portal.kernel.util.StringPool;
32 import com.liferay.portal.kernel.util.Validator;
33 import com.liferay.portal.plugin.PluginPackageImpl;
34 import com.liferay.portal.plugin.PluginPackageUtil;
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
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 = HttpUtil.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.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 String servletContextName = null;
165
166 try {
167 ServletContext ctx = event.getServletContext();
168
169 servletContextName = ctx.getServletContextName();
170
171 if (_log.isDebugEnabled()) {
172 _log.debug("Invoking deploy for " + servletContextName);
173 }
174
175 if (ctx.getResource("/WEB-INF/liferay-theme-loader.xml") != null) {
176 return;
177 }
178
179 PluginPackage pluginPackage = readPluginPackage(ctx);
180
181 if (pluginPackage != null) {
182 pluginPackage.setContext(servletContextName);
183
184 event.setPluginPackage(pluginPackage);
185
186 PluginPackageUtil.registerInstalledPluginPackage(pluginPackage);
187
188 if (_log.isInfoEnabled()) {
189 _log.info(
190 "Plugin package " + pluginPackage.getModuleId() +
191 " registered successfully");
192 }
193 }
194 }
195 catch (Exception e) {
196 throw new HotDeployException(
197 "Error registering plugins for " + servletContextName,
198 e);
199 }
200 }
201
202 public void invokeUndeploy(HotDeployEvent event) throws HotDeployException {
203 String servletContextName = null;
204
205 try {
206 ServletContext ctx = event.getServletContext();
207
208 servletContextName = ctx.getServletContextName();
209
210 if (_log.isDebugEnabled()) {
211 _log.debug("Invoking deploy for " + servletContextName);
212 }
213
214 PluginPackage pluginPackage = readPluginPackage(ctx);
215
216 if (pluginPackage != null) {
217 event.setPluginPackage(pluginPackage);
218
219 PluginPackageUtil.unregisterInstalledPluginPackage(
220 pluginPackage);
221
222 if (_log.isInfoEnabled()) {
223 _log.info(
224 "Plugin package " + pluginPackage.getModuleId() +
225 " unregistered successfully");
226 }
227 }
228 }
229 catch (Exception e) {
230 throw new HotDeployException(
231 "Error unregistering plugins for " + servletContextName,
232 e);
233 }
234 }
235
236 private static Log _log =
237 LogFactory.getLog(PluginPackageHotDeployListener.class);
238
239 }