001    /**
002     * Copyright (c) 2000-present 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.tools.deploy;
016    
017    import com.liferay.portal.kernel.model.Plugin;
018    import com.liferay.portal.kernel.plugin.PluginPackage;
019    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.Portal;
023    import com.liferay.portal.kernel.util.PortalUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.ServerDetector;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.kernel.xml.Document;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
033    import com.liferay.portal.tools.ToolDependencies;
034    import com.liferay.portal.util.PrefsPropsUtil;
035    import com.liferay.portal.util.PropsValues;
036    
037    import java.io.File;
038    
039    import java.util.ArrayList;
040    import java.util.List;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Brian Myunghun Kim
045     */
046    public class PortletDeployer extends BaseDeployer {
047    
048            public static final String JSF_STANDARD =
049                    "javax.portlet.faces.GenericFacesPortlet";
050    
051            public static void main(String[] args) {
052                    ToolDependencies.wireDeployers();
053    
054                    List<String> wars = new ArrayList<>();
055                    List<String> jars = new ArrayList<>();
056    
057                    for (String arg : args) {
058                            if (arg.endsWith(".war")) {
059                                    wars.add(arg);
060                            }
061                            else if (arg.endsWith(".jar")) {
062                                    jars.add(arg);
063                            }
064                    }
065    
066                    new PortletDeployer(wars, jars);
067            }
068    
069            public PortletDeployer() {
070            }
071    
072            public PortletDeployer(List<String> wars, List<String> jars) {
073                    super(wars, jars);
074            }
075    
076            @Override
077            public void checkArguments() {
078                    super.checkArguments();
079    
080                    if (Validator.isNull(portletTaglibDTD)) {
081                            throw new IllegalArgumentException(
082                                    "The system property deployer.portlet.taglib.dtd is not set");
083                    }
084            }
085    
086            @Override
087            public void copyXmls(
088                            File srcFile, String displayName, PluginPackage pluginPackage)
089                    throws Exception {
090    
091                    super.copyXmls(srcFile, displayName, pluginPackage);
092    
093                    copyTomcatContextXml(srcFile);
094    
095                    copyDependencyXml(
096                            "_servlet_context_include.jsp", srcFile + "/WEB-INF/jsp");
097            }
098    
099            @Override
100            public String getExtraContent(
101                            double webXmlVersion, File srcFile, String displayName)
102                    throws Exception {
103    
104                    StringBundler sb = new StringBundler();
105    
106                    if (ServerDetector.isWebSphere()) {
107                            sb.append("<context-param>");
108                            sb.append("<param-name>");
109                            sb.append("com.ibm.websphere.portletcontainer.");
110                            sb.append("PortletDeploymentEnabled");
111                            sb.append("</param-name>");
112                            sb.append("<param-value>false</param-value>");
113                            sb.append("</context-param>");
114                    }
115    
116                    File portletXML = new File(
117                            srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);
118                    File webXML = new File(srcFile + "/WEB-INF/web.xml");
119    
120                    updatePortletXML(portletXML);
121    
122                    sb.append(getServletContent(portletXML, webXML));
123    
124                    String extraContent = super.getExtraContent(
125                            webXmlVersion, srcFile, displayName);
126    
127                    sb.append(extraContent);
128    
129                    return sb.toString();
130            }
131    
132            @Override
133            public String getExtraFiltersContent(double webXmlVersion, File srcFile)
134                    throws Exception {
135    
136                    StringBundler sb = new StringBundler(4);
137    
138                    String extraFiltersContent = super.getExtraFiltersContent(
139                            webXmlVersion, srcFile);
140    
141                    sb.append(extraFiltersContent);
142    
143                    // Ignore filters
144    
145                    sb.append(getIgnoreFiltersContent(srcFile));
146    
147                    // Speed filters
148    
149                    sb.append(getSpeedFiltersContent(srcFile));
150    
151                    // Servlet context include filters
152    
153                    sb.append(
154                            getServletContextIncludeFiltersContent(webXmlVersion, srcFile));
155    
156                    return sb.toString();
157            }
158    
159            @Override
160            public String getPluginType() {
161                    return Plugin.TYPE_PORTLET;
162            }
163    
164            public String getServletContent(File portletXML, File webXML)
165                    throws Exception {
166    
167                    if (!portletXML.exists()) {
168                            return StringPool.BLANK;
169                    }
170    
171                    StringBundler sb = new StringBundler();
172    
173                    Document document = UnsecureSAXReaderUtil.read(portletXML);
174    
175                    Element rootElement = document.getRootElement();
176    
177                    List<Element> portletElements = rootElement.elements("portlet");
178    
179                    for (Element portletElement : portletElements) {
180                            String portletName = PortalUtil.getJsSafePortletId(
181                                    portletElement.elementText("portlet-name"));
182                            String portletClassName = portletElement.elementText(
183                                    "portlet-class");
184    
185                            String servletName = portletName + " Servlet";
186    
187                            sb.append("<servlet>");
188                            sb.append("<servlet-name>");
189                            sb.append(servletName);
190                            sb.append("</servlet-name>");
191                            sb.append("<servlet-class>");
192                            sb.append("com.liferay.portal.kernel.servlet.PortletServlet");
193                            sb.append("</servlet-class>");
194                            sb.append("<init-param>");
195                            sb.append("<param-name>portlet-class</param-name>");
196                            sb.append("<param-value>");
197                            sb.append(portletClassName);
198                            sb.append("</param-value>");
199                            sb.append("</init-param>");
200                            sb.append("<load-on-startup>1</load-on-startup>");
201                            sb.append("</servlet>");
202    
203                            sb.append("<servlet-mapping>");
204                            sb.append("<servlet-name>");
205                            sb.append(servletName);
206                            sb.append("</servlet-name>");
207                            sb.append("<url-pattern>/");
208                            sb.append(portletName);
209                            sb.append("/*</url-pattern>");
210                            sb.append("</servlet-mapping>");
211                    }
212    
213                    return sb.toString();
214            }
215    
216            @Override
217            public void updateDeployDirectory(File srcFile) throws Exception {
218                    boolean customPortletXML = false;
219    
220                    try {
221                            customPortletXML = PrefsPropsUtil.getBoolean(
222                                    PropsKeys.AUTO_DEPLOY_CUSTOM_PORTLET_XML,
223                                    PropsValues.AUTO_DEPLOY_CUSTOM_PORTLET_XML);
224                    }
225                    catch (Exception e) {
226    
227                            // This will only happen when running the deploy tool in Ant in the
228                            // classical way where the WAR file is actually massaged and
229                            // packaged.
230    
231                            customPortletXML = PropsValues.AUTO_DEPLOY_CUSTOM_PORTLET_XML;
232                    }
233    
234                    customPortletXML = GetterUtil.getBoolean(
235                            System.getProperty("deployer.custom.portlet.xml"),
236                            customPortletXML);
237    
238                    if (!customPortletXML) {
239                            return;
240                    }
241    
242                    File portletXML = new File(
243                            srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);
244    
245                    if (portletXML.exists()) {
246                            File portletCustomXML = new File(
247                                    srcFile + "/WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_CUSTOM);
248    
249                            if (portletCustomXML.exists()) {
250                                    portletCustomXML.delete();
251                            }
252    
253                            portletXML.renameTo(portletCustomXML);
254                    }
255            }
256    
257            public void updatePortletXML(File portletXML) throws Exception {
258                    if (!portletXML.exists()) {
259                            return;
260                    }
261    
262                    String content = FileUtil.read(portletXML);
263    
264                    content = StringUtil.replace(
265                            content, "com.liferay.util.bridges.jsp.JSPPortlet",
266                            MVCPortlet.class.getName());
267    
268                    FileUtil.write(portletXML, content);
269            }
270    
271    }