[gatein-commits] gatein SVN: r5974 - in portal/branches/branch-GTNPORTAL-1822: component/web/resources and 9 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Mar 8 01:43:53 EST 2011


Author: hoang_to
Date: 2011-03-08 01:43:52 -0500 (Tue, 08 Mar 2011)
New Revision: 5974

Added:
   portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java
Modified:
   portal/branches/branch-GTNPORTAL-1822/component/web/resources/pom.xml
   portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java
   portal/branches/branch-GTNPORTAL-1822/gadgets/server/pom.xml
   portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as5/pkg/pom.xml
   portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as6/pkg/pom.xml
   portal/branches/branch-GTNPORTAL-1822/packaging/jetty/pkg/pom.xml
   portal/branches/branch-GTNPORTAL-1822/packaging/module/src/main/javascript/portal.packaging.module.js
   portal/branches/branch-GTNPORTAL-1822/packaging/tomcat/pkg/pom.xml
   portal/branches/branch-GTNPORTAL-1822/pom.xml
   portal/branches/branch-GTNPORTAL-1822/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml
Log:
GTNPORTAL-198: Use the Closure compiler in the Javascript serving engine

Modified: portal/branches/branch-GTNPORTAL-1822/component/web/resources/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/component/web/resources/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/component/web/resources/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -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/branches/branch-GTNPORTAL-1822/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java	                        (rev 0)
+++ portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/main/java/org/exoplatform/portal/resource/compressor/impl/ClosureCompressorPlugin.java	2011-03-08 06:43:52 UTC (rev 5974)
@@ -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/branches/branch-GTNPORTAL-1822/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/component/web/resources/src/test/java/org/exoplatform/portal/resource/compressor/TestResourceCompressorService.java	2011-03-08 06:43:52 UTC (rev 5974)
@@ -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 at 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/branches/branch-GTNPORTAL-1822/gadgets/server/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/gadgets/server/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/gadgets/server/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -86,8 +86,8 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
-					<groupId>com.google.collections</groupId>
-			 		<artifactId>google-collections</artifactId>
+					<groupId>com.google.guava</groupId>
+			 		<artifactId>guava</artifactId>
 					<scope>provided</scope>
         </dependency>
         <dependency>

Modified: portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as5/pkg/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as5/pkg/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as5/pkg/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -599,10 +599,14 @@
          <version>2.0.235</version>
       </dependency>
       <dependency>
-         <groupId>com.google.collections</groupId>
-         <artifactId>google-collections</artifactId>
+         <groupId>com.google.guava</groupId>
+         <artifactId>guava</artifactId>
       </dependency>
       <dependency>
+         <groupId>com.google.javascript</groupId>
+         <artifactId>closure-compiler</artifactId>
+      </dependency>
+      <dependency>
          <groupId>org.codehaus.groovy</groupId>
          <artifactId>groovy-all</artifactId>
       </dependency>

Modified: portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as6/pkg/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as6/pkg/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/packaging/jboss-as6/pkg/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -590,10 +590,14 @@
          <version>2.0.235</version>
       </dependency>
       <dependency>
-         <groupId>com.google.collections</groupId>
-         <artifactId>google-collections</artifactId>
+         <groupId>com.google.guava</groupId>
+         <artifactId>guava</artifactId>
       </dependency>
       <dependency>
+         <groupId>com.google.javascript</groupId>
+         <artifactId>closure-compiler</artifactId>
+      </dependency>
+      <dependency>
          <groupId>org.codehaus.groovy</groupId>
          <artifactId>groovy-all</artifactId>
       </dependency>

Modified: portal/branches/branch-GTNPORTAL-1822/packaging/jetty/pkg/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/packaging/jetty/pkg/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/packaging/jetty/pkg/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -598,10 +598,14 @@
       <artifactId>filters</artifactId>
     </dependency>
     <dependency>
-      <groupId>com.google.collections</groupId>
-      <artifactId>google-collections</artifactId>
+         <groupId>com.google.guava</groupId>
+         <artifactId>guava</artifactId>
     </dependency>
     <dependency>
+         <groupId>com.google.javascript</groupId>
+         <artifactId>closure-compiler</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.codehaus.groovy</groupId>
       <artifactId>groovy-all</artifactId>
     </dependency>

Modified: portal/branches/branch-GTNPORTAL-1822/packaging/module/src/main/javascript/portal.packaging.module.js
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/packaging/module/src/main/javascript/portal.packaging.module.js	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/packaging/module/src/main/javascript/portal.packaging.module.js	2011-03-08 06:43:52 UTC (rev 5974)
@@ -227,7 +227,8 @@
       addDependency(new Project("commons-io", "commons-io", "jar", "1.4")).
       addDependency(new Project("commons-codec", "commons-codec", "jar", "1.3")).
       addDependency(new Project("net.oauth", "core", "jar", "20080621")).
-      addDependency(new Project("com.google.collections", "google-collections", "jar", "1.0-rc2")).
+      addDependency(new Project("com.google.guava", "guava", "jar", "r07")).
+     addDependency(new Project("com.google.javascript", "closure-compiler", "jar", "r706")).
       addDependency(new Project("com.google.code.guice", "guice", "jar", "2.0")).
       addDependency(new Project("com.google.code.guice", "guice-jmx", "jar", "2.0")).
       addDependency(new Project("commons-lang", "commons-lang", "jar", "2.4")).
@@ -298,3 +299,4 @@
 
    return module;
 }
+
\ No newline at end of file

Modified: portal/branches/branch-GTNPORTAL-1822/packaging/tomcat/pkg/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/packaging/tomcat/pkg/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/packaging/tomcat/pkg/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -590,10 +590,14 @@
       <artifactId>filters</artifactId>
     </dependency>
     <dependency>
-      <groupId>com.google.collections</groupId>
-      <artifactId>google-collections</artifactId>
+         <groupId>com.google.guava</groupId>
+         <artifactId>guava</artifactId>
     </dependency>
     <dependency>
+         <groupId>com.google.javascript</groupId>
+         <artifactId>closure-compiler</artifactId>
+    </dependency>
+    <dependency>
       <groupId>org.codehaus.groovy</groupId>
       <artifactId>groovy-all</artifactId>
     </dependency>

Modified: portal/branches/branch-GTNPORTAL-1822/pom.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/pom.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/pom.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -796,9 +796,9 @@
             <version>20080621</version>
          </dependency>
          <dependency>
-            <groupId>com.google.collections</groupId>
-            <artifactId>google-collections</artifactId>
-            <version>1.0-rc2</version>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>r07</version>
          </dependency>
          <dependency>
             <groupId>com.google.code.guice</groupId>
@@ -811,6 +811,11 @@
            <version>2.0</version>
          </dependency>
          <dependency>
+            <groupId>com.google.javascript</groupId>
+            <artifactId>closure-compiler</artifactId>
+            <version>r706</version>
+         </dependency>
+         <dependency>
             <groupId>rome</groupId>
             <artifactId>rome</artifactId>
             <version>0.9</version>
@@ -900,7 +905,7 @@
          <scope>test</scope>
       </dependency>
    </dependencies>
-
+   
    <build>
       <resources>
          <resource>

Modified: portal/branches/branch-GTNPORTAL-1822/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml
===================================================================
--- portal/branches/branch-GTNPORTAL-1822/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml	2011-03-08 04:07:03 UTC (rev 5973)
+++ portal/branches/branch-GTNPORTAL-1822/web/portal/src/main/webapp/WEB-INF/conf/common/resource-compressor-configuration.xml	2011-03-08 06:43:52 UTC (rev 5974)
@@ -39,6 +39,17 @@
             </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>1</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>



More information about the gatein-commits mailing list