001    /**
002     * Copyright (c) 2000-2013 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.deploy.auto;
016    
017    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018    import com.liferay.portal.kernel.plugin.PluginPackage;
019    import com.liferay.portal.kernel.portlet.DefaultFriendlyURLMapper;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.util.bridges.wai.WAIPortlet;
023    
024    import java.io.File;
025    
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.Properties;
029    
030    /**
031     * @author Jorge Ferrer
032     * @author Connor McKay
033     */
034    public class WAIAutoDeployer extends PortletAutoDeployer {
035    
036            public WAIAutoDeployer() throws AutoDeployException {
037                    try {
038                            addRequiredJar(jars, "portals-bridges.jar");
039                    }
040                    catch (Exception e) {
041                            throw new AutoDeployException(e);
042                    }
043            }
044    
045            @Override
046            public void copyXmls(
047                            File srcFile, String displayName, PluginPackage pluginPackage)
048                    throws Exception {
049    
050                    super.copyXmls(srcFile, displayName, pluginPackage);
051    
052                    // The default context.xml file for Tomcat causes portlets to be run
053                    // from the temp directory, which prevents some applications from saving
054                    // their settings. There is no easy way to prevent this file from being
055                    // copied, so it must be deleted afterwards.
056    
057                    FileUtil.delete(srcFile + "/META-INF/context.xml");
058    
059                    String portletName = displayName;
060    
061                    if (pluginPackage != null) {
062                            portletName = pluginPackage.getName();
063                    }
064    
065                    Map<String, String> filterMap = new HashMap<String, String>();
066    
067                    filterMap.put("portlet_name", displayName);
068                    filterMap.put("portlet_title", portletName);
069    
070                    if (pluginPackage != null) {
071                            Properties deploymentSettings =
072                                    pluginPackage.getDeploymentSettings();
073    
074                            filterMap.put(
075                                    "portlet_class",
076                                    deploymentSettings.getProperty(
077                                            "wai.portlet", WAIPortlet.class.getName()));
078    
079                            filterMap.put(
080                                    "friendly_url_mapper_class",
081                                    deploymentSettings.getProperty(
082                                            "wai.friendly.url.mapper",
083                                            DefaultFriendlyURLMapper.class.getName()));
084    
085                            filterMap.put(
086                                    "friendly_url_mapping",
087                                    deploymentSettings.getProperty(
088                                            "wai.friendly.url.mapping", "waiapp"));
089    
090                            filterMap.put(
091                                    "friendly_url_routes",
092                                    deploymentSettings.getProperty(
093                                            "wai.friendly.url.routes",
094                                            "com/liferay/util/bridges/wai/" +
095                                                    "wai-friendly-url-routes.xml"));
096                    }
097                    else {
098                            filterMap.put("portlet_class", WAIPortlet.class.getName());
099    
100                            filterMap.put(
101                                    "friendly_url_mapper_class",
102                                    DefaultFriendlyURLMapper.class.getName());
103    
104                            filterMap.put("friendly_url_mapping", "waiapp");
105    
106                            filterMap.put(
107                                    "friendly_url_routes",
108                                    "com/liferay/util/bridges/wai/wai-friendly-url-routes.xml");
109                    }
110    
111                    _setInitParams(filterMap, pluginPackage);
112    
113                    copyDependencyXml(
114                            "liferay-display.xml", srcFile + "/WEB-INF", filterMap);
115                    copyDependencyXml(
116                            "liferay-portlet.xml", srcFile + "/WEB-INF", filterMap);
117                    copyDependencyXml("portlet.xml", srcFile + "/WEB-INF", filterMap);
118                    copyDependencyXml("iframe.jsp", srcFile + "/WEB-INF/jsp/liferay/wai");
119            }
120    
121            private void _setInitParams(
122                    Map<String, String> filterMap, PluginPackage pluginPackage) {
123    
124                    for (int i = 0; i < _INIT_PARAM_NAMES.length; i++) {
125                            String name = _INIT_PARAM_NAMES[i];
126    
127                            String value = null;
128    
129                            if (pluginPackage != null) {
130                                    Properties deploymentSettings =
131                                            pluginPackage.getDeploymentSettings();
132    
133                                    value = deploymentSettings.getProperty(name);
134                            }
135    
136                            if (Validator.isNull(value)) {
137                                    value = _INIT_PARAM_DEFAULT_VALUES[i];
138                            }
139    
140                            filterMap.put("init_param_name_" + i, name);
141                            filterMap.put("init_param_value_" + i, value);
142                    }
143            }
144    
145            private static final String[] _INIT_PARAM_DEFAULT_VALUES = new String[] {
146                    "500"
147            };
148    
149            private static final String[] _INIT_PARAM_NAMES = new String[] {
150                    "wai.connector.iframe.height.default"
151            };
152    
153    }