1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.plugin.PluginPackage;
26 import com.liferay.portal.kernel.util.FileUtil;
27 import com.liferay.portal.kernel.util.ServerDetector;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Plugin;
30 import com.liferay.portal.util.DocumentUtil;
31 import com.liferay.portal.util.Portal;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.PrefsPropsUtil;
34 import com.liferay.portal.util.PropsKeys;
35 import com.liferay.portal.util.PropsValues;
36 import com.liferay.util.TextFormatter;
37 import com.liferay.util.xml.XMLFormatter;
38 import com.liferay.util.xml.XMLMerger;
39 import com.liferay.util.xml.descriptor.FacesXMLDescriptor;
40
41 import java.io.File;
42
43 import java.util.ArrayList;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Properties;
49
50 import org.dom4j.Document;
51 import org.dom4j.Element;
52
53
60 public class PortletDeployer extends BaseDeployer {
61
62 public static final String JSF_MYFACES =
63 "org.apache.myfaces.portlet.MyFacesGenericPortlet";
64
65 public static final String JSF_SUN =
66 "com.sun.faces.portlet.FacesPortlet";
67
68 public static final String LIFERAY_RENDER_KIT_FACTORY =
69 "com.liferay.util.jsf.sun.faces.renderkit.LiferayRenderKitFactoryImpl";
70
71 public static final String MYFACES_CONTEXT_FACTORY =
72 "com.liferay.util.bridges.jsf.myfaces.MyFacesContextFactoryImpl";
73
74 public static void main(String[] args) {
75 List<String> wars = new ArrayList<String>();
76 List<String> jars = new ArrayList<String>();
77
78 for (String arg : args) {
79 if (arg.endsWith(".war")) {
80 wars.add(arg);
81 }
82 else if (arg.endsWith(".jar")) {
83 jars.add(arg);
84 }
85 }
86
87 new PortletDeployer(wars, jars);
88 }
89
90 protected PortletDeployer() {
91 }
92
93 protected PortletDeployer(List<String> wars, List<String> jars) {
94 super(wars, jars);
95 }
96
97 protected void checkArguments() {
98 super.checkArguments();
99
100 if (Validator.isNull(portletTaglibDTD)) {
101 throw new IllegalArgumentException(
102 "The system property deployer.portlet.taglib.dtd is not set");
103 }
104 }
105
106 protected void copyXmls(
107 File srcFile, String displayName, PluginPackage pluginPackage)
108 throws Exception {
109
110 super.copyXmls(srcFile, displayName, pluginPackage);
111
112 if (appServerType.equals(ServerDetector.TOMCAT_ID)) {
113 copyDependencyXml("context.xml", srcFile + "/META-INF");
114 }
115 }
116
117 protected String getExtraContent(
118 double webXmlVersion, File srcFile, String displayName)
119 throws Exception {
120
121 String extraContent = super.getExtraContent(
122 webXmlVersion, srcFile, displayName);
123
124 File facesXML = new File(srcFile + "/WEB-INF/faces-config.xml");
125 File portletXML = new File(
126 srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);
127 File webXML = new File(srcFile + "/WEB-INF/web.xml");
128
129 extraContent += getServletContent(portletXML, webXML);
130
131 setupJSF(facesXML, portletXML);
132
133 if (_sunFacesPortlet) {
134 extraContent +=
135 "<listener>" +
136 "<listener-class>" +
137 "com.liferay.util.bridges.jsf.sun.LiferayConfigureListener" +
138 "</listener-class>" +
139 "</listener>";
140 }
141
142 return extraContent;
143 }
144
145 protected String getServletContent(File portletXML, File webXML)
146 throws Exception {
147
148 StringBuilder sb = new StringBuilder();
149
150
152 Document doc = DocumentUtil.readDocumentFromFile(portletXML);
153
154 Element root = doc.getRootElement();
155
156 Iterator<Element> itr1 = root.elements("portlet").iterator();
157
158 while (itr1.hasNext()) {
159 Element portlet = itr1.next();
160
161 String portletName = PortalUtil.getJsSafePortletId(
162 portlet.elementText("portlet-name"));
163 String portletClass = portlet.elementText("portlet-class");
164
165 sb.append("<servlet>");
166 sb.append("<servlet-name>");
167 sb.append(portletName);
168 sb.append("</servlet-name>");
169 sb.append("<servlet-class>");
170 sb.append("com.liferay.portal.kernel.servlet.PortletServlet");
171 sb.append("</servlet-class>");
172 sb.append("<init-param>");
173 sb.append("<param-name>portlet-class</param-name>");
174 sb.append("<param-value>");
175 sb.append(portletClass);
176 sb.append("</param-value>");
177 sb.append("</init-param>");
178 sb.append("<load-on-startup>0</load-on-startup>");
179 sb.append("</servlet>");
180
181 sb.append("<servlet-mapping>");
182 sb.append("<servlet-name>");
183 sb.append(portletName);
184 sb.append("</servlet-name>");
185 sb.append("<url-pattern>/");
186 sb.append(portletName);
187 sb.append("/*</url-pattern>");
188 sb.append("</servlet-mapping>");
189 }
190
191
193 doc = DocumentUtil.readDocumentFromFile(webXML);
194
195 root = doc.getRootElement();
196
197
199 itr1 = root.elements("servlet").iterator();
200
201 while (itr1.hasNext()) {
202 Element servlet = itr1.next();
203
204 String icon = servlet.elementText("icon");
205 String servletName = servlet.elementText("servlet-name");
206 String displayName = servlet.elementText("display-name");
207 String description = servlet.elementText("description");
208 String servletClass = servlet.elementText("servlet-class");
209 List<Element> initParams = servlet.elements("init-param");
210 String loadOnStartup = servlet.elementText("load-on-startup");
211 String runAs = servlet.elementText("run-as");
212 List<Element> securityRoleRefs = servlet.elements(
213 "security-role-ref");
214
215 if ((servletClass != null) &&
216 (servletClass.equals(
217 "com.liferay.portal.servlet.SharedServletWrapper"))) {
218
219 sb.append("<servlet>");
220
221 if (icon != null) {
222 sb.append("<icon>");
223 sb.append(icon);
224 sb.append("</icon>");
225 }
226
227 if (servletName != null) {
228 sb.append("<servlet-name>");
229 sb.append(servletName);
230 sb.append("</servlet-name>");
231 }
232
233 if (displayName != null) {
234 sb.append("<display-name>");
235 sb.append(displayName);
236 sb.append("</display-name>");
237 }
238
239 if (description != null) {
240 sb.append("<description>");
241 sb.append(description);
242 sb.append("</description>");
243 }
244
245 Iterator<Element> itr2 = initParams.iterator();
246
247 while (itr2.hasNext()) {
248 Element initParam = itr2.next();
249
250 String paramName = initParam.elementText("param-name");
251 String paramValue = initParam.elementText("param-value");
252
253 if ((paramName != null) &&
254 (paramName.equals("servlet-class"))) {
255
256 sb.append("<servlet-class>");
257 sb.append(paramValue);
258 sb.append("</servlet-class>");
259 }
260 }
261
262 itr2 = initParams.iterator();
263
264 while (itr2.hasNext()) {
265 Element initParam = itr2.next();
266
267 String paramName = initParam.elementText("param-name");
268 String paramValue = initParam.elementText("param-value");
269 String paramDesc = initParam.elementText("description");
270
271 if ((paramName != null) &&
272 (!paramName.equals("servlet-class"))) {
273
274 sb.append("<init-param>");
275 sb.append("<param-name>");
276 sb.append(paramName);
277 sb.append("</param-name>");
278
279 if (paramValue != null) {
280 sb.append("<param-value>");
281 sb.append(paramValue);
282 sb.append("</param-value>");
283 }
284
285 if (paramDesc != null) {
286 sb.append("<description>");
287 sb.append(paramDesc);
288 sb.append("</description>");
289 }
290
291 sb.append("</init-param>");
292 }
293 }
294
295 if (loadOnStartup != null) {
296 sb.append("<load-on-startup>");
297 sb.append(loadOnStartup);
298 sb.append("</load-on-startup>");
299 }
300
301 if (runAs != null) {
302 sb.append("<run-as>");
303 sb.append(runAs);
304 sb.append("</run-as>");
305 }
306
307 itr2 = securityRoleRefs.iterator();
308
309 while (itr2.hasNext()) {
310 Element roleRef = itr2.next();
311
312 String roleDesc = roleRef.elementText("description");
313 String roleName = roleRef.elementText("role-name");
314 String roleLink = roleRef.elementText("role-link");
315
316 sb.append("<security-role-ref>");
317
318 if (roleDesc != null) {
319 sb.append("<description>");
320 sb.append(roleDesc);
321 sb.append("</description>");
322 }
323
324 if (roleName != null) {
325 sb.append("<role-name>");
326 sb.append(roleName);
327 sb.append("</role-name>");
328 }
329
330 if (roleLink != null) {
331 sb.append("<role-link>");
332 sb.append(roleLink);
333 sb.append("</role-link>");
334 }
335
336 sb.append("</security-role-ref>");
337 }
338
339 sb.append("</servlet>");
340 }
341 }
342
343 return sb.toString();
344 }
345
346 protected void processPluginPackageProperties(
347 File srcFile, String displayName, PluginPackage pluginPackage)
348 throws Exception {
349
350 if (pluginPackage == null) {
351 return;
352 }
353
354 Properties props = getPluginPackageProperties(srcFile);
355
356 if ((props == null) || (props.size() == 0)) {
357 return;
358 }
359
360 String moduleGroupId = pluginPackage.getGroupId();
361 String moduleArtifactId = pluginPackage.getArtifactId();
362 String moduleVersion = pluginPackage.getVersion();
363
364 String pluginName = pluginPackage.getName();
365 String pluginType = pluginPackage.getTypes().get(0);
366 String pluginTypeName = TextFormatter.format(
367 pluginType, TextFormatter.J);
368
369 if (!pluginType.equals(Plugin.TYPE_PORTLET)) {
370 return;
371 }
372
373 String tags = getPluginPackageTagsXml(pluginPackage.getTags());
374 String shortDescription = pluginPackage.getShortDescription();
375 String longDescription = pluginPackage.getLongDescription();
376 String changeLog = pluginPackage.getChangeLog();
377 String pageURL = pluginPackage.getPageURL();
378 String author = pluginPackage.getAuthor();
379 String licenses = getPluginPackageLicensesXml(
380 pluginPackage.getLicenses());
381 String liferayVersions = getPluginPackageLiferayVersionsXml(
382 pluginPackage.getLiferayVersions());
383
384 Map<String, String> filterMap = new HashMap<String, String>();
385
386 filterMap.put("module_group_id", moduleGroupId);
387 filterMap.put("module_artifact_id", moduleArtifactId);
388 filterMap.put("module_version", moduleVersion);
389
390 filterMap.put("plugin_name", pluginName);
391 filterMap.put("plugin_type", pluginType);
392 filterMap.put("plugin_type_name", pluginTypeName);
393
394 filterMap.put("tags", tags);
395 filterMap.put("short_description", shortDescription);
396 filterMap.put("long_description", longDescription);
397 filterMap.put("change_log", changeLog);
398 filterMap.put("page_url", pageURL);
399 filterMap.put("author", author);
400 filterMap.put("licenses", licenses);
401 filterMap.put("liferay_versions", liferayVersions);
402
403 copyDependencyXml(
404 "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
405 true);
406 }
407
408 protected void setupJSF(File facesXML, File portletXML) throws Exception {
409 _myFacesPortlet = false;
410 _sunFacesPortlet = false;
411
412 if (!facesXML.exists()) {
413 return;
414 }
415
416
418 Document doc = DocumentUtil.readDocumentFromFile(portletXML, true);
419
420 Element root = doc.getRootElement();
421
422 List<Element> elements = root.elements("portlet");
423
424 Iterator<Element> itr = elements.iterator();
425
426 while (itr.hasNext()) {
427 Element portlet = itr.next();
428
429 String portletClass = portlet.elementText("portlet-class");
430
431 if (portletClass.equals(JSF_MYFACES)) {
432 _myFacesPortlet = true;
433
434 break;
435 }
436 else if (portletClass.equals(JSF_SUN)) {
437 _sunFacesPortlet = true;
438
439 break;
440 }
441 }
442
443
445 doc = DocumentUtil.readDocumentFromFile(facesXML, true);
446
447 root = doc.getRootElement();
448
449 Element factoryEl = root.element("factory");
450
451 Element renderKitFactoryEl = null;
452 Element facesContextFactoryEl = null;
453
454 if (factoryEl == null) {
455 factoryEl = root.addElement("factory");
456 }
457
458 renderKitFactoryEl = factoryEl.element("render-kit-factory");
459 facesContextFactoryEl = factoryEl.element("faces-context-factory");
460
461 if ((appServerType.equals("orion") && (_sunFacesPortlet) &&
462 (renderKitFactoryEl == null))) {
463
464 renderKitFactoryEl = factoryEl.addElement("render-kit-factory");
465
466 renderKitFactoryEl.addText(LIFERAY_RENDER_KIT_FACTORY);
467 }
468 else if (_myFacesPortlet && (facesContextFactoryEl == null)) {
469 facesContextFactoryEl =
470 factoryEl.addElement("faces-context-factory");
471
472 facesContextFactoryEl.addText(MYFACES_CONTEXT_FACTORY);
473 }
474
475 if (!appServerType.equals("orion") && (_sunFacesPortlet)) {
476 factoryEl.detach();
477 }
478
479 XMLMerger merger = new XMLMerger(new FacesXMLDescriptor());
480
481 merger.organizeXML(doc);
482
483 FileUtil.write(facesXML, XMLFormatter.toString(doc), true);
484 }
485
486 protected void updateDeployDirectory(File srcFile) throws Exception {
487 try {
488 if (!PrefsPropsUtil.getBoolean(
489 PropsKeys.AUTO_DEPLOY_CUSTOM_PORTLET_XML,
490 PropsValues.AUTO_DEPLOY_CUSTOM_PORTLET_XML)) {
491
492 return;
493 }
494 }
495 catch (Exception e) {
496
497
501 if (!PropsValues.AUTO_DEPLOY_CUSTOM_PORTLET_XML) {
502 return;
503 }
504 }
505
506 File portletXML = new File(
507 srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);
508
509 if (portletXML.exists()) {
510 File portletCustomXML = new File(
511 srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM);
512
513 if (portletCustomXML.exists()) {
514 portletCustomXML.delete();
515 }
516
517 portletXML.renameTo(portletCustomXML);
518 }
519 }
520
521 private boolean _myFacesPortlet;
522 private boolean _sunFacesPortlet;
523
524 }