You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/01/18 11:11:06 UTC

svn commit: r613120 - in /incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets: JsLibrary.java ProcessingOptions.java http/GadgetRenderingServlet.java

Author: etnu
Date: Fri Jan 18 02:11:06 2008
New Revision: 613120

URL: http://svn.apache.org/viewvc?rev=613120&view=rev
Log:
Introduced the first part of a js lib performance optimization tweak. This change allows the parent site to force which js libraries the gadget will load by passing a libs= parameter to the servlet. The purpose of this change is to allow the parent site to pre calculate the aggregate feature set that all gadgets being rendered on the current page will require and then pass that set along to every other gadget, which will ultimately result in significantly improved cache performance. The next phase of this improvement is inlining URL type JsLibrary instances so that they can have the same behavior as file/inline/resource based loaders.


Modified:
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsLibrary.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessingOptions.java
    incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsLibrary.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsLibrary.java?rev=613120&r1=613119&r2=613120&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsLibrary.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/JsLibrary.java Fri Jan 18 02:11:06 2008
@@ -24,6 +24,8 @@
 /**
  * Represents a javascript library, either as an external resource (url)
  * or as an inline script.
+ * TODO: pull in url type libraries and treat them the same as file, resource,
+ * or inline scripts.
  */
 public final class JsLibrary {
   private Type type;

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessingOptions.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessingOptions.java?rev=613120&r1=613119&r2=613120&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessingOptions.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessingOptions.java Fri Jan 18 02:11:06 2008
@@ -19,5 +19,18 @@
  * become a catch-all for unnecessary pieces of functionality.
  */
 public class ProcessingOptions {
+
+  /**
+   * Whether or not to bypass the gadget and message bundle caches for the
+   * current request.
+   */
   public boolean ignoreCache = false;
+
+  /**
+   * Overrides javascript library processing by forcing the use of a fixed set
+   * of libraries. This is mainly intended for use by parent sites that are
+   * using the core library to fetch meta data for many gadgets and aggregating
+   * the required libraries.
+   */
+  public String forceJsLibs = null;
 }

Modified: incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java?rev=613120&r1=613119&r2=613120&view=diff
==============================================================================
--- incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java (original)
+++ incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/GadgetRenderingServlet.java Fri Jan 18 02:11:06 2008
@@ -147,6 +147,7 @@
     GadgetView.ID gadgetId = new Gadget.GadgetId(uri, moduleId);
     ProcessingOptions options = new ProcessingOptions();
     options.ignoreCache = getIgnoreCache(req);
+    options.forceJsLibs = getForceJsLibs(req);
 
     // Prepare a list of GadgetContentFilters applied to the output
     List<GadgetContentFilter> contentFilters =
@@ -162,22 +163,33 @@
                                           context.getLocale(),
                                           RenderingContext.GADGET,
                                           options);
-      outputGadget(gadget, contentFilters, resp);
+      outputGadget(gadget, options, contentFilters, resp);
     } catch (GadgetServer.GadgetProcessException e) {
       outputErrors(e, resp);
     }
   }
 
+  /**
+   * Renders a successfully processed gadget.
+   *
+   * @param gadget
+   * @param options
+   * @param contentFilters
+   * @param resp
+   * @throws IOException
+   * @throws GadgetServer.GadgetProcessException
+   */
   private void outputGadget(Gadget gadget,
+                            ProcessingOptions options,
                             List<GadgetContentFilter> contentFilters,
                             HttpServletResponse resp)
       throws IOException, GadgetServer.GadgetProcessException {
     switch(gadget.getContentType()) {
     case HTML:
-      outputHtmlGadget(gadget, contentFilters, resp);
+      outputHtmlGadget(gadget, options, contentFilters, resp);
       break;
     case URL:
-      outputUrlGadget(gadget, resp);
+      outputUrlGadget(gadget, options, resp);
       break;
     default:
       resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
@@ -186,7 +198,18 @@
     }
   }
 
+  /**
+   * Handles type=html gadget output.
+   *
+   * @param gadget
+   * @param options
+   * @param contentFilters
+   * @param resp
+   * @throws IOException
+   * @throws GadgetServer.GadgetProcessException
+   */
   private void outputHtmlGadget(Gadget gadget,
+                                ProcessingOptions options,
                                 List<GadgetContentFilter> contentFilters,
                                 HttpServletResponse resp)
       throws IOException, GadgetServer.GadgetProcessException {
@@ -207,12 +230,19 @@
 
     for (JsLibrary library : gadget.getJsLibraries()) {
       if (library.getType() == JsLibrary.Type.URL) {
+        // TODO: This case needs to be handled more gracefully by the js
+        // servlet. We should probably inline external JS as well.
         externJs.append(String.format(externFmt, library.getContent()));
-      } else {
+      } else if (options.forceJsLibs == null) {
         inlineJs.append(library.getContent()).append("\n");
       }
     }
 
+    if (options.forceJsLibs != null) {
+      externJs.append(String.format(externFmt,
+          DEFAULT_JS_SERVICE_PATH + options.forceJsLibs + JS_FILE_SUFFIX));
+    }
+
     if (inlineJs.length() > 0) {
       markup.append("<script><!--\n").append(inlineJs)
             .append("\n-->\n</script>");
@@ -242,15 +272,20 @@
     resp.getOutputStream().print(markup.toString());
   }
 
-  private void outputUrlGadget(Gadget gadget, HttpServletResponse resp)
-      throws IOException {
+  private void outputUrlGadget(Gadget gadget,
+      ProcessingOptions options, HttpServletResponse resp) throws IOException {
     // UserPrefs portion of query string to tack on
     // TODO: generalize this as injectedArgs on Gadget object
     // TODO: userprefs on the fragment rather than query string
     String prefsQuery = getPrefsQueryString(gadget.getUserPrefValues());
     String libsQuery = null;
 
-    libsQuery = getLibsQueryString(gadget.getRequires().keySet());
+    if (options.forceJsLibs == null) {
+      libsQuery = getLibsQueryString(gadget.getRequires().keySet());
+    } else {
+      libsQuery
+          = DEFAULT_JS_SERVICE_PATH + options.forceJsLibs + JS_FILE_SUFFIX;
+    }
 
     URI redirURI = gadget.getContentHref();
     try {
@@ -341,6 +376,10 @@
     return buf.toString();
   }
 
+  /**
+   * @param req
+   * @return Whether or not to ignore the cache.
+   */
   protected boolean getIgnoreCache(HttpServletRequest req) {
     String noCacheParam = req.getParameter("nocache");
     if (noCacheParam == null) {
@@ -349,6 +388,18 @@
     return noCacheParam != null && noCacheParam.equals("1");
   }
 
+  /**
+   * @param req
+   * @return Forced JS libs, or null if no forcing is to be done.
+   */
+  protected String getForceJsLibs(HttpServletRequest req) {
+    return req.getParameter("libs");
+  }
+
+  /**
+   * @param req
+   * @return Whether or not to use caja.
+   */
   protected boolean getUseCaja(HttpServletRequest req) {
     String cajaParam = req.getParameter(CAJA_PARAM);
     return cajaParam != null && cajaParam.equals("1");