1   /**
2    * Copyright (c) 2000-2008 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.tools;
24  
25  import com.liferay.portal.kernel.plugin.PluginPackage;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Plugin;
30  import com.liferay.util.TextFormatter;
31  
32  import java.io.File;
33  
34  import java.util.ArrayList;
35  import java.util.HashMap;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Properties;
39  
40  /**
41   * <a href="ThemeDeployer.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ThemeDeployer extends BaseDeployer {
47  
48      public static void main(String[] args) {
49          List<String> wars = new ArrayList<String>();
50          List<String> jars = new ArrayList<String>();
51  
52          for (String arg : args) {
53              if (arg.endsWith(".war")) {
54                  wars.add(arg);
55              }
56              else if (arg.endsWith(".jar")) {
57                  jars.add(arg);
58              }
59          }
60  
61          new ThemeDeployer(wars, jars);
62      }
63  
64      protected ThemeDeployer() {
65      }
66  
67      protected ThemeDeployer(List<String> wars, List<String> jars) {
68          super(wars, jars);
69      }
70  
71      protected void checkArguments() {
72          super.checkArguments();
73  
74          if (Validator.isNull(themeTaglibDTD)) {
75              throw new IllegalArgumentException(
76                  "The system property deployer.theme.taglib.dtd is not set");
77          }
78  
79          if (Validator.isNull(utilTaglibDTD)) {
80              throw new IllegalArgumentException(
81                  "The system property deployer.util.taglib.dtd is not set");
82          }
83      }
84  
85      protected String getExtraContent(
86              double webXmlVersion, File srcFile, String displayName)
87          throws Exception {
88  
89          StringMaker sm = new StringMaker();
90  
91          String extraContent = super.getExtraContent(
92              webXmlVersion, srcFile, displayName);
93  
94          sm.append(extraContent);
95  
96          // HeaderFilter
97  
98          sm.append("<filter>");
99          sm.append("<filter-name>Header Filter</filter-name>");
100         sm.append("<filter-class>");
101         sm.append("com.liferay.portal.kernel.servlet.PortalClassLoaderFilter");
102         sm.append("</filter-class>");
103         sm.append("<init-param>");
104         sm.append("<param-name>filter-class</param-name>");
105         sm.append("<param-value>");
106         sm.append("com.liferay.portal.servlet.filters.header.HeaderFilter");
107         sm.append("</param-value>");
108         sm.append("</init-param>");
109         sm.append("<init-param>");
110         sm.append("<param-name>Cache-Control</param-name>");
111         sm.append("<param-value>max-age=172801, public</param-value>");
112         sm.append("</init-param>");
113         sm.append("<init-param>");
114         sm.append("<param-name>Expires</param-name>");
115         sm.append("<param-value>172801</param-value>");
116         sm.append("</init-param>");
117         sm.append("</filter>");
118 
119         sm.append("<filter-mapping>");
120         sm.append("<filter-name>Header Filter</filter-name>");
121         sm.append("<url-pattern>*.css</url-pattern>");
122         sm.append("</filter-mapping>");
123         sm.append("<filter-mapping>");
124         sm.append("<filter-name>Header Filter</filter-name>");
125         sm.append("<url-pattern>*.gif</url-pattern>");
126         sm.append("</filter-mapping>");
127         sm.append("<filter-mapping>");
128         sm.append("<filter-name>Header Filter</filter-name>");
129         sm.append("<url-pattern>*.html</url-pattern>");
130         sm.append("</filter-mapping>");
131         sm.append("<filter-mapping>");
132         sm.append("<filter-name>Header Filter</filter-name>");
133         sm.append("<url-pattern>*.jpg</url-pattern>");
134         sm.append("</filter-mapping>");
135         sm.append("<filter-mapping>");
136         sm.append("<filter-name>Header Filter</filter-name>");
137         sm.append("<url-pattern>*.js</url-pattern>");
138         sm.append("</filter-mapping>");
139         sm.append("<filter-mapping>");
140         sm.append("<filter-name>Header Filter</filter-name>");
141         sm.append("<url-pattern>*.png</url-pattern>");
142         sm.append("</filter-mapping>");
143 
144         // CompressionFilter
145 
146         sm.append("<filter>");
147         sm.append("<filter-name>Compression Filter</filter-name>");
148         sm.append("<filter-class>");
149         sm.append("com.liferay.portal.kernel.servlet.PortalClassLoaderFilter");
150         sm.append("</filter-class>");
151         sm.append("<init-param>");
152         sm.append("<param-name>filter-class</param-name>");
153         sm.append("<param-value>");
154         sm.append(
155             "com.liferay.portal.servlet.filters.compression.CompressionFilter");
156         sm.append("</param-value>");
157         sm.append("</init-param>");
158         sm.append("</filter>");
159 
160         sm.append("<filter-mapping>");
161         sm.append("<filter-name>Compression Filter</filter-name>");
162         sm.append("<url-pattern>*.css</url-pattern>");
163         sm.append("</filter-mapping>");
164         sm.append("<filter-mapping>");
165         sm.append("<filter-name>Compression Filter</filter-name>");
166         sm.append("<url-pattern>*.js</url-pattern>");
167         sm.append("</filter-mapping>");
168 
169         // VelocityFilter
170 
171         sm.append("<filter>");
172         sm.append("<filter-name>Velocity Filter</filter-name>");
173         sm.append("<filter-class>");
174         sm.append("com.liferay.portal.kernel.servlet.PortalClassLoaderFilter");
175         sm.append("</filter-class>");
176         sm.append("<init-param>");
177         sm.append("<param-name>filter-class</param-name>");
178         sm.append("<param-value>");
179         sm.append("com.liferay.portal.servlet.filters.velocity.VelocityFilter");
180         sm.append("</param-value>");
181         sm.append("</init-param>");
182         sm.append("<init-param>");
183         sm.append("<param-name>pattern</param-name>");
184         sm.append("<param-value>(.+)/css/main.css(.+)</param-value>");
185         sm.append("</init-param>");
186         sm.append("</filter>");
187 
188         /*sm.append("<filter-mapping>");
189         sm.append("<filter-name>Velocity Filter</filter-name>");
190         sm.append("<url-pattern>*.css</url-pattern>");
191 
192         if (webXmlVersion > 2.3) {
193             sm.append("<dispatcher>FORWARD</dispatcher>");
194             sm.append("<dispatcher>INCLUDE</dispatcher>");
195             sm.append("<dispatcher>REQUEST</dispatcher>");
196         }
197 
198         sm.append("</filter-mapping>");*/
199 
200         // ThemeContextListener
201 
202         sm.append("<listener>");
203         sm.append("<listener-class>");
204         sm.append("com.liferay.portal.kernel.servlet.ThemeContextListener");
205         sm.append("</listener-class>");
206         sm.append("</listener>");
207 
208         return sm.toString();
209     }
210 
211     protected void processPluginPackageProperties(
212             File srcFile, String displayName, PluginPackage pluginPackage)
213         throws Exception {
214 
215         if (pluginPackage == null) {
216             return;
217         }
218 
219         Properties props = getPluginPackageProperties(srcFile);
220 
221         if ((props == null) || (props.size() == 0)) {
222             return;
223         }
224 
225         String moduleGroupId = pluginPackage.getGroupId();
226         String moduleArtifactId = pluginPackage.getArtifactId();
227         String moduleVersion = pluginPackage.getVersion();
228 
229         String pluginName = pluginPackage.getName();
230         String pluginType = pluginPackage.getTypes().get(0);
231         String pluginTypeName = TextFormatter.format(
232             pluginType, TextFormatter.J);
233 
234         if (!pluginType.equals(Plugin.TYPE_THEME)) {
235             return;
236         }
237 
238         String tags = getPluginPackageTagsXml(pluginPackage.getTags());
239         String shortDescription = pluginPackage.getShortDescription();
240         String longDescription = pluginPackage.getLongDescription();
241         String changeLog = pluginPackage.getChangeLog();
242         String pageURL = pluginPackage.getPageURL();
243         String author = pluginPackage.getAuthor();
244         String licenses = getPluginPackageLicensesXml(
245             pluginPackage.getLicenses());
246         String liferayVersions = getPluginPackageLiferayVersionsXml(
247             pluginPackage.getLiferayVersions());
248 
249         int pos = moduleArtifactId.indexOf("-theme");
250 
251         String themeId = moduleArtifactId.substring(0, pos);
252         String themeName = pluginName;
253 
254         Map<String, String> filterMap = new HashMap<String, String>();
255 
256         filterMap.put("module_group_id", moduleGroupId);
257         filterMap.put("module_artifact_id", moduleArtifactId);
258         filterMap.put("module_version", moduleVersion);
259 
260         filterMap.put("plugin_name", pluginName);
261         filterMap.put("plugin_type", pluginType);
262         filterMap.put("plugin_type_name", pluginTypeName);
263 
264         filterMap.put("tags", tags);
265         filterMap.put("short_description", shortDescription);
266         filterMap.put("long_description", longDescription);
267         filterMap.put("change_log", changeLog);
268         filterMap.put("page_url", pageURL);
269         filterMap.put("author", author);
270         filterMap.put("licenses", licenses);
271         filterMap.put("liferay_versions", liferayVersions);
272 
273         filterMap.put("theme_id", themeId);
274         filterMap.put("theme_name", themeName);
275         filterMap.put(
276             "theme_versions",
277             StringUtil.replace(liferayVersions, "liferay-version", "version"));
278 
279         copyDependencyXml(
280             "liferay-look-and-feel.xml", srcFile + "/WEB-INF", filterMap, true);
281         copyDependencyXml(
282             "liferay-plugin-package.xml", srcFile + "/WEB-INF", filterMap,
283             true);
284     }
285 
286 }