Author: julien_viet
Date: 2010-02-12 19:59:26 -0500 (Fri, 12 Feb 2010)
New Revision: 1665
Modified:
portal/trunk/component/web/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/javascript/JavascriptServlet.java
Log:
- DO NOT CALL ByteArrayOutputStream#toByteArray() on every call to
JavascriptConfigService.getMergedJavascript() as this is non sense, basically it does:
public synchronized byte toByteArray()[] {
return Arrays.copyOf(buf, count);
}
which means:
1/ unecessary contention
2/ unecessary serialization
3/ unecessary memory allocation (and thus unecessary garbage collector activity)
- added a reminder for later
Modified:
portal/trunk/component/web/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java
===================================================================
---
portal/trunk/component/web/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java 2010-02-12
22:48:43 UTC (rev 1664)
+++
portal/trunk/component/web/src/main/java/org/exoplatform/web/application/javascript/JavascriptConfigService.java 2010-02-13
00:59:26 UTC (rev 1665)
@@ -21,12 +21,12 @@
import org.exoplatform.commons.utils.Safe;
import org.exoplatform.container.ExoContainerContext;
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
import org.gatein.wci.impl.DefaultServletContainerFactory;
import org.picocontainer.Startable;
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.*;
import java.util.*;
import javax.servlet.ServletContext;
@@ -34,6 +34,9 @@
public class JavascriptConfigService implements Startable
{
+ /** Our logger. */
+ private final Logger log = LoggerFactory.getLogger(JavascriptConfigService.class);
+
private Collection<String> availableScripts_;
private Collection<String> availableScriptsPaths_;
@@ -44,7 +47,7 @@
private HashMap<String, String> extendedJavascripts;
- private ByteArrayOutputStream jsStream_ = null;
+ private byte[] jsBytes = null;
/** . */
private JavascriptDeployer deployer;
@@ -210,29 +213,53 @@
mergedJavascript = buffer.toString();
}
- public byte[] getMergedJavascript()
+ /**
+ * Write the merged javascript in a provided output stream.
+ *
+ * @param out the output stream
+ * @throws IOException any io exception
+ */
+ public void writeMergedJavascript(OutputStream out) throws IOException
{
- if (jsStream_ == null)
+ if (jsBytes == null)
{
- jsStream_ = new ByteArrayOutputStream();
+ // Generate javascript in a buffer
StringBuffer allJavascript = new StringBuffer();
allJavascript.append(mergedJavascript);
for (String script : extendedJavascripts.values())
{
allJavascript.append(script);
}
- ByteArrayInputStream input = new
ByteArrayInputStream(allJavascript.toString().getBytes());
- JSMin jsMin = new JSMin(input, jsStream_);
+
+ // Get bytes
+ byte[] bytes;
try
{
+ bytes = allJavascript.toString().getBytes("UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new AssertionError("No access to UTF-8, e");
+ }
+
+ // Minify
+ try
+ {
+ ByteArrayInputStream input = new ByteArrayInputStream(bytes);
+ ByteArrayOutputStream jsStream = new ByteArrayOutputStream();
+ JSMin jsMin = new JSMin(input, jsStream);
jsMin.jsmin();
+ jsBytes = jsStream.toByteArray();
}
catch (Exception e)
{
- e.printStackTrace();
+ log.error("Error when generating minified javascript, will use normal
javascript instead", e);
+ jsBytes = bytes;
}
}
- return jsStream_.toByteArray();
+
+ //
+ out.write(jsBytes);
}
public boolean isModuleLoaded(CharSequence module)
@@ -247,7 +274,7 @@
String path = "/" + servletContextName + scriptPath;
availableScriptsPaths_.remove(path);
extendedJavascripts.remove(path);
- jsStream_ = null;
+ jsBytes = null;
}
public void start()
Modified:
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/javascript/JavascriptServlet.java
===================================================================
---
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/javascript/JavascriptServlet.java 2010-02-12
22:48:43 UTC (rev 1664)
+++
portal/trunk/webui/portal/src/main/java/org/exoplatform/portal/webui/javascript/JavascriptServlet.java 2010-02-13
00:59:26 UTC (rev 1665)
@@ -58,9 +58,11 @@
JavascriptConfigService service =
(JavascriptConfigService)ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(
JavascriptConfigService.class);
+
+ // Julien: should we also set charset along with the content type ?
response.setContentType("application/x-javascript");
ServletOutputStream stream = response.getOutputStream();
- stream.write(service.getMergedJavascript());
+ service.writeMergedJavascript(stream);
}
}