Author: hoang_to
Date: 2011-04-28 04:57:38 -0400 (Thu, 28 Apr 2011)
New Revision: 6388
Added:
portal/trunk/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java
Modified:
portal/trunk/component/web/resources/pom.xml
portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java
portal/trunk/pom.xml
portal/trunk/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml
Log:
GTNPORTAL-198: Use Closure compiler in Javascript serving engine
Modified: portal/trunk/component/web/resources/pom.xml
===================================================================
--- portal/trunk/component/web/resources/pom.xml 2011-04-28 08:38:44 UTC (rev 6387)
+++ portal/trunk/component/web/resources/pom.xml 2011-04-28 08:57:38 UTC (rev 6388)
@@ -49,6 +49,10 @@
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
+ <groupId>com.google.javascript</groupId>
+ <artifactId>closure-compiler</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.exoplatform.portal</groupId>
<artifactId>exo.portal.component.test.core</artifactId>
<scope>test</scope>
Added:
portal/trunk/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java
===================================================================
---
portal/trunk/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java
(rev 0)
+++
portal/trunk/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java 2011-04-28
08:57:38 UTC (rev 6388)
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.portal.resource.compressor.impl;
+
+import com.google.javascript.jscomp.CheckLevel;
+import com.google.javascript.jscomp.CompilationLevel;
+import com.google.javascript.jscomp.Compiler;
+import com.google.javascript.jscomp.CompilerOptions;
+import com.google.javascript.jscomp.JSSourceFile;
+import com.google.javascript.jscomp.WarningLevel;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.portal.resource.compressor.BaseResourceCompressorPlugin;
+import org.exoplatform.portal.resource.compressor.ResourceCompressorException;
+import org.exoplatform.portal.resource.compressor.ResourceType;
+import java.io.Reader;
+import java.io.Writer;
+
+public class ClosureCompressorPlugin extends BaseResourceCompressorPlugin
+{
+
+ public ClosureCompressorPlugin(InitParams params) throws Exception
+ {
+ super(params);
+ }
+
+ public ResourceType getResourceType()
+ {
+ return ResourceType.JAVASCRIPT;
+ }
+
+ public void compress(Reader input, Writer output) throws ResourceCompressorException
+ {
+ Compiler compiler = new Compiler();
+ CompilerOptions options = new CompilerOptions();
+ CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
+ WarningLevel.QUIET.setOptionsForWarningLevel(options);
+ JSSourceFile extern = JSSourceFile.fromCode("extern", "");
+
+ JSSourceFile jsInput;
+ try
+ {
+ String code = JSSourceFile.fromReader("code", input).getCode();
+ jsInput = JSSourceFile.fromCode("jsInput", code);
+ compiler.compile(extern, jsInput, options);
+ output.write(compiler.toSource());
+ }
+ catch (Exception ex)
+ {
+ throw new ResourceCompressorException(ex);
+ }
+ }
+}
Modified:
portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java
===================================================================
---
portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java 2011-04-28
08:38:44 UTC (rev 6387)
+++
portal/trunk/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java 2011-04-28
08:57:38 UTC (rev 6388)
@@ -18,19 +18,32 @@
*/
package org.exoplatform.portal.resource.compressor;
+import com.google.javascript.jscomp.*;
+import com.google.javascript.jscomp.Compiler;
+import org.apache.commons.io.IOUtils;
+import org.exoplatform.commons.utils.IOUtil;
import org.exoplatform.component.test.AbstractKernelTest;
import org.exoplatform.component.test.ConfigurationUnit;
import org.exoplatform.component.test.ConfiguredBy;
import org.exoplatform.component.test.ContainerScope;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.Parameter;
+import org.exoplatform.container.xml.ValueParam;
+import org.exoplatform.portal.resource.compressor.impl.ClosureCompressorPlugin;
import org.exoplatform.portal.resource.compressor.impl.JSMinCompressorPlugin;
import org.exoplatform.portal.resource.compressor.impl.ResourceCompressorService;
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
+import java.util.Scanner;
/**
* @author <a href="trong.tran(a)exoplatform.com">Trong Tran</a>
@@ -85,6 +98,66 @@
+ jsCompressedFile.getAbsolutePath() + " (" +
getFileSize(jsCompressedFile) + ")");
}
+ public void testClosureCompressing() throws Exception
+ {
+ File jsFile = new File("src/test/resources/javascript.js");
+ File jsCompressedFile = new File("target/closure-compressed-file.js");
+ Reader reader = new FileReader(jsFile);
+ Writer writer = new FileWriter(jsCompressedFile);
+
+ ResourceCompressorService compressor =
+
(ResourceCompressorService)getContainer().getComponentInstanceOfType(ResourceCompressor.class);
+
+ InitParams priorityParam = new InitParams();
+ ValueParam param = new ValueParam();
+ param.setName("plugin.priority");
+ param.setValue("10");
+ priorityParam.addParameter(param);
+ compressor.registerCompressorPlugin(new ClosureCompressorPlugin(priorityParam));
+ try
+ {
+ compressor.compress(reader, writer, ResourceType.JAVASCRIPT);
+ }
+ catch (Exception e)
+ {
+ fail(e.getLocalizedMessage());
+ }
+ finally
+ {
+ reader.close();
+ writer.close();
+ }
+
+ assertTrue(jsCompressedFile.length() > 0);
+ assertTrue(jsFile.length() > jsCompressedFile.length());
+ log.info("The original javascript (" + getFileSize(jsFile) + ") is
compressed by CLOSURE COMPILER into "
+ + jsCompressedFile.getAbsolutePath() + " (" +
getFileSize(jsCompressedFile) + ")");
+
+ String expectedJS = closureCompress(jsFile);
+ assertEquals(expectedJS.length(), jsCompressedFile.length());
+ }
+
+ private String closureCompress(File input) throws Exception
+ {
+ Compiler compiler = new Compiler();
+ CompilerOptions options = new CompilerOptions();
+ CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel(options);
+ JSSourceFile extern = JSSourceFile.fromCode("", "");
+
+ JSSourceFile jsInput;
+ try
+ {
+ jsInput = JSSourceFile.fromFile(input);
+ }
+ catch (Exception ex)
+ {
+ throw new ResourceCompressorException(ex);
+ }
+
+ compiler.compile(extern, jsInput, options);
+ return compiler.toSource();
+ }
+
public void testYUICSSCompressing() throws IOException
{
File cssFile = new File("src/test/resources/Stylesheet.css");
Modified: portal/trunk/pom.xml
===================================================================
--- portal/trunk/pom.xml 2011-04-28 08:38:44 UTC (rev 6387)
+++ portal/trunk/pom.xml 2011-04-28 08:57:38 UTC (rev 6388)
@@ -686,6 +686,12 @@
</dependency>
<dependency>
+ <groupId>com.google.javascript</groupId>
+ <artifactId>closure-compiler</artifactId>
+ <version>r706</version>
+ </dependency>
+
+ <dependency>
<groupId>org.gatein.shindig</groupId>
<artifactId>shindig-gadgets</artifactId>
<version>${org.shindig.version}</version>
@@ -746,7 +752,7 @@
<scope>test</scope>
</dependency>
</dependencies>
-
+
<build>
<resources>
<resource>
Modified:
portal/trunk/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml
===================================================================
---
portal/trunk/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml 2011-04-28
08:38:44 UTC (rev 6387)
+++
portal/trunk/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml 2011-04-28
08:57:38 UTC (rev 6388)
@@ -27,6 +27,7 @@
<key>org.exoplatform.portal.resource.compressor.ResourceCompressor</key>
<type>org.exoplatform.portal.resource.compressor.impl.ResourceCompressorService</type>
<component-plugins>
+ <!--
<component-plugin>
<name>JSMinCompressorPlugin</name>
<set-method>registerCompressorPlugin</set-method>
@@ -38,7 +39,19 @@
</value-param>
</init-params>
</component-plugin>
+ -->
<component-plugin>
+ <name>ClosureCompressorPlugin</name>
+ <set-method>registerCompressorPlugin</set-method>
+
<type>org.exoplatform.portal.resource.compressor.impl.ClosureCompressorPlugin</type>
+ <init-params>
+ <value-param>
+ <name>plugin.priority</name>
+ <value>2</value>
+ </value-param>
+ </init-params>
+ </component-plugin>
+ <component-plugin>
<name>YUICSSCompressorPlugin</name>
<set-method>registerCompressorPlugin</set-method>
<type>org.exoplatform.portal.resource.compressor.css.YUICSSCompressorPlugin</type>