]> source.dussan.org Git - vaadin-framework.git/commitdiff
= CustomLayout preloading =
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>
Sun, 22 Apr 2007 16:43:58 +0000 (16:43 +0000)
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>
Sun, 22 Apr 2007 16:43:58 +0000 (16:43 +0000)
== Tested on ==
 * IE6 on XP
 * IE7 on XP
 * Nokia E90
 * FF2 on XP
 * FF2 on Mac
 * Safari 2 on Mac
 * WebKit nightly on Mac
 * Opera 9 on Mac
With FeatureBrpwser and CustomLayout -example applications.

== Design ==
Server-side adds <precache resource="layout/foo.html">layout-file in CDATA-block</precache> tags to <changes>. Server precaches all the layouts that are redrawn on given UIDL-changeset. In future client should maybe notify the server of the layouts it has already.

== Causes ==
 * E90 works!
 * Loading custom layouts does not need additional synchronous requests (this should make first appearance of layouts faster)
 * Layouts are sometimes loaded even when they are not needed. This makes UIDL-changesets heavier.

(Also changeset fixes a CDATA UIDL bug in PaintTarget)

svn changeset:1280/svn branch:trunk

src/com/itmill/toolkit/terminal/web/AjaxApplicationManager.java
src/com/itmill/toolkit/terminal/web/AjaxPaintTarget.java
src/com/itmill/toolkit/terminal/web/ApplicationServlet.java

index 9c43abd5b828b817bae5344e0c166773472f9fa1..2727b63042b0a532fadbec99067d788e3d4b0dc3 100644 (file)
@@ -30,6 +30,7 @@ package com.itmill.toolkit.terminal.web;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
@@ -140,7 +141,8 @@ public class AjaxApplicationManager implements
         *             if the writing failed due to input/output error.
         */
        public void handleUidlRequest(HttpServletRequest request,
-                       HttpServletResponse response) throws IOException {
+                       HttpServletResponse response, ThemeSource themeSource) 
+               throws IOException {
 
                // repaint requested or sesssion has timed out and new one is created
                boolean repaintAll = (request.getParameter(GET_PARAM_REPAINT_ALL) != null)
@@ -320,6 +322,39 @@ public class AjaxApplicationManager implements
                         paintTarget.endTag("meta");
                     }
 
+                    // Precache custom layouts
+                    // TODO Does not support theme-get param or different themes in different windows -> Allways preload layouts with the theme specified by the applications
+                    String themeName = application.getTheme() != null ? application.getTheme() : ApplicationServlet.DEFAULT_THEME;
+                    // TODO We should only precache the layouts that are not cached already
+                    for (Iterator i=paintTarget.preCachedResources.iterator(); i.hasNext();) {
+                       String resource = (String) i.next();
+                       InputStream is = null;
+                       try {
+                                       is = themeSource.getResource(themeName + "/" +  resource);
+                               } catch (ThemeSource.ThemeException e) {
+                                       Log.info(e.getMessage());
+                               }
+                       if (is != null) {
+                               paintTarget.startTag("precache");
+                               paintTarget.addAttribute("resource", resource);
+                               StringBuffer layout = new StringBuffer();
+
+                               try {
+                                       InputStreamReader r = new InputStreamReader(is);
+                                               char[] buffer = new char[20000];
+                                               int charsRead = 0;
+                                               while ((charsRead = r.read(buffer)) > 0)
+                                                       layout.append(buffer, 0, charsRead);
+                                               r.close();
+                               } catch (java.io.IOException e) {
+                                       Log.info("Resource transfer failed:  " + request.getRequestURI()
+                                                       + ". (" + e.getMessage() + ")");
+                               }
+                               paintTarget.addCharacterData(layout.toString());
+                               paintTarget.endTag("precache");
+                       }
+                    }
+                    
                                        paintTarget.close();
                                        out.flush();
                                } else {
index 6f6840833c1b83a3d27f0c414a0c253fbd96a733..70ece88985b50b270476df8a8c9e21f9c4e10b41 100644 (file)
@@ -44,6 +44,8 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.Stack;
 
 /**
@@ -80,6 +82,9 @@ public class AjaxPaintTarget implements PaintTarget {
        private boolean trackPaints = false;
 
        private int numberOfPaints = 0;
+       
+       Set preCachedResources = new HashSet();
+       private boolean customLayoutArgumentsOpen = false;
 
        /**
         * Creates a new XMLPrintWriter, without automatic line flushing.
@@ -128,6 +133,7 @@ public class AjaxPaintTarget implements PaintTarget {
                if (mTagArgumentListOpen) {
                        append(">");
                        mTagArgumentListOpen = false;
+                       customLayoutArgumentsOpen = false;
                }
        }
 
@@ -182,6 +188,9 @@ public class AjaxPaintTarget implements PaintTarget {
                append("<" + tagName);
 
                mTagArgumentListOpen = true;
+               
+               if ("customlayout".equals(tagName))
+                       customLayoutArgumentsOpen = true;
        }
 
        /**
@@ -217,6 +226,7 @@ public class AjaxPaintTarget implements PaintTarget {
                if (mTagArgumentListOpen) {
                        append(">");
                        mTagArgumentListOpen = false;
+                       customLayoutArgumentsOpen = false;
                }
 
                // Writes the end (closing) tag
@@ -444,6 +454,9 @@ public class AjaxPaintTarget implements PaintTarget {
                        throw new PaintException("XML argument list not open.");
 
                append(" " + name + "=\"" + escapeXML(value) + "\"");
+               
+               if (customLayoutArgumentsOpen && "style".equals(name))
+                       preCachedResources.add("layout/" + value + ".html");
        }
 
        /**
@@ -701,9 +714,9 @@ public class AjaxPaintTarget implements PaintTarget {
         * @see com.itmill.toolkit.terminal.PaintTarget#addCharacterData(java.lang.String)
         */
        public void addCharacterData(String text) throws PaintException {
-               // TODO: This should check the validity of characters
                ensureClosedTag();
-               append(escapeXML(text));
+               if (text != null)
+                       append("<![CDATA[" + text + "]]>");
        }
 
        /**
index 10973f8213417240980f117136774441d3c5901e..261a803f095a7ec465faba63193d3787ed0810a4 100644 (file)
@@ -565,7 +565,7 @@ public class ApplicationServlet extends HttpServlet implements
                                if (resourceId != null && resourceId.startsWith(AJAX_UIDL_URI)) {
 
                                        getApplicationManager(application).handleUidlRequest(
-                                                       request, response);
+                                                       request, response, themeSource);
 
                                        return;
                                }