[jboss-cvs] jboss-seam/src/ui/org/jboss/seam/ui ...

Gavin King gavin.king at jboss.com
Tue Jul 11 13:48:55 EDT 2006


  User: gavin   
  Date: 06/07/11 13:48:55

  Modified:    src/ui/org/jboss/seam/ui  UICache.java
  Log:
  MUCH better impl of s:cache
  
  Revision  Changes    Path
  1.2       +17 -300   jboss-seam/src/ui/org/jboss/seam/ui/UICache.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: UICache.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/src/ui/org/jboss/seam/ui/UICache.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- UICache.java	11 Jul 2006 02:35:08 -0000	1.1
  +++ UICache.java	11 Jul 2006 17:48:55 -0000	1.2
  @@ -1,9 +1,7 @@
   package org.jboss.seam.ui;
   
   import java.io.IOException;
  -import java.io.Serializable;
  -import java.io.Writer;
  -import java.util.ArrayList;
  +import java.io.StringWriter;
   import java.util.List;
   
   import javax.faces.component.UIComponent;
  @@ -53,20 +51,27 @@
         if (enabled)
         {
            String key = evaluateKey(facesContext);
  -         Command[] commands = getFromCache(key);
  -         if (commands==null)
  +         String cachedContent = getFromCache(key);
  +         if (cachedContent==null)
            {
               log.debug("rendering from scratch: " + key);
  -            CachingResponseWriter cachingResponseWriter = new CachingResponseWriter(response);
  +            StringWriter stringWriter = new StringWriter();
  +            ResponseWriter cachingResponseWriter = response.cloneWithWriter(stringWriter);
               facesContext.setResponseWriter(cachingResponseWriter);
               renderChildren(facesContext, this);
               facesContext.setResponseWriter(response);
  -            putInCache( key, cachingResponseWriter.getCommands() );
  +            String output = stringWriter.getBuffer().toString();
  +            response.write(output);
  +            putInCache(key, output);
            }
            else
            {
               log.debug("rendering from cache: " + key);
  -            for (Command cmd: commands) cmd.apply(response);
  +            response.write("<!-- cached content for: ");
  +            response.write(key);
  +            response.write(" -->");
  +            response.write(cachedContent);
  +            response.write("<!-- end of cached content -->");
            }
         }
         else
  @@ -75,11 +80,11 @@
         }
      }
   
  -   private void putInCache(String key, Command[] commands)
  +   private void putInCache(String key, String content)
      {
         try
         {
  -         PojoCache.instance().put(region, key, commands);
  +         PojoCache.instance().put(region, key, content);
         }
         catch (CacheException ce)
         {
  @@ -87,11 +92,11 @@
         }
      }
   
  -   private Command[] getFromCache(String key)
  +   private String getFromCache(String key)
      {
         try
         {
  -         return (Command[]) PojoCache.instance().get(region, key);
  +         return (String) PojoCache.instance().get(region, key);
         }
         catch (CacheException ce)
         {
  @@ -129,294 +134,6 @@
         }
      }
   
  -   static interface Command extends Serializable
  -   {
  -      public void apply(ResponseWriter responseWriter) throws IOException;
  -   }
  -   
  -   static final class CachingResponseWriter extends ResponseWriter
  -   {
  -      private final ResponseWriter responseWriter;
  -      private List<Command> commandList = new ArrayList<Command>();
  -      
  -      public Command[] getCommands()
  -      {
  -         return commandList.toArray( new Command[commandList.size()] );
  -      }
  -
  -      private static final class WriteCommand implements Command
  -      {
  -         private final int off;
  -
  -         private final int len;
  -
  -         private final char[] cbuf;
  -
  -         private WriteCommand(int off, int len, char[] cbuf)
  -         {
  -            this.off = off;
  -            this.len = len;
  -            this.cbuf = cbuf;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.write(cbuf, off, len);
  -         }
  -      }
  -
  -      private static final class WriteAttributeCommand implements Command
  -      {
  -         private final String property;
  -
  -         private final Object value;
  -
  -         private final String name;
  -
  -         private WriteAttributeCommand(String property, Object value, String name)
  -         {
  -            this.property = property;
  -            this.value = value;
  -            this.name = name;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.writeAttribute(name, value, property);
  -         }
  -      }
  -
  -      private static final class WriteCommentCommand implements Command
  -      {
  -         private final Object comment;
  -
  -         private WriteCommentCommand(Object comment)
  -         {
  -            this.comment = comment;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.writeComment(comment);
  -         }
  -      }
  -
  -      private static final class WriteCharsCommand implements Command
  -      {
  -         private final char[] text;
  -
  -         private final int len;
  -
  -         private final int off;
  -
  -         private WriteCharsCommand(char[] text, int len, int off)
  -         {
  -            this.text = text;
  -            this.len = len;
  -            this.off = off;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.writeText(text, off, len);
  -         }
  -      }
  -
  -      private static final class WriteTextCommand implements Command
  -      {
  -         private final String property;
  -
  -         private final Object text;
  -
  -         private WriteTextCommand(String property, Object text)
  -         {
  -            this.property = property;
  -            this.text = text;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.writeText(text, property);
  -         }
  -      }
  -
  -      private static final class WriteURIAttributeCommand implements Command
  -      {
  -         private final Object value;
  -
  -         private final String property;
  -
  -         private final String name;
  -
  -         private WriteURIAttributeCommand(Object value, String property, String name)
  -         {
  -            this.value = value;
  -            this.property = property;
  -            this.name = name;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.writeURIAttribute(name, value, property);
  -         }
  -      }
  -
  -      private static final class StartElementCommand implements Command
  -      {
  -         private final UIComponent component;
  -
  -         private final String name;
  -
  -         private StartElementCommand(UIComponent component, String name)
  -         {
  -            this.component = component;
  -            this.name = name;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.startElement(name, component);
  -         }
  -      }
  -
  -      private static final class EndElementCommand implements Command
  -      {
  -         private final String name;
  -
  -         private EndElementCommand(String name)
  -         {
  -            this.name = name;
  -         }
  -
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.endElement(name);
  -         }
  -      }
  -
  -      private static final class EndDocumentCommand implements Command
  -      {
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.endDocument();
  -         }
  -      }
  -
  -      private static final class StartDocumentCommand implements Command
  -      {
  -         public void apply(ResponseWriter responseWriter) throws IOException
  -         {
  -            responseWriter.startDocument();
  -         }
  -      }
  -
  -      private CachingResponseWriter(ResponseWriter writer)
  -      {
  -         this.responseWriter = writer;
  -      }
  -
  -      @Override
  -      public ResponseWriter cloneWithWriter(Writer writer)
  -      {
  -         return new CachingResponseWriter( responseWriter.cloneWithWriter(writer) );
  -      }
  -
  -      @Override
  -      public void startDocument() throws IOException
  -      {
  -         commandList.add( new StartDocumentCommand() );
  -         responseWriter.startDocument();
  -      }
  -
  -      @Override
  -      public void endDocument() throws IOException
  -      {
  -         commandList.add( new EndDocumentCommand() );
  -         responseWriter.endDocument();
  -      }
  -
  -      @Override
  -      public void startElement(final String name, final UIComponent component) throws IOException
  -      {
  -         commandList.add( new StartElementCommand(component, name) );
  -         responseWriter.startElement(name, component);
  -      }
  -
  -      @Override
  -      public void endElement(final String name) throws IOException
  -      {
  -         commandList.add( new EndElementCommand(name) );
  -         responseWriter.endElement(name);
  -      }
  -
  -      @Override
  -      public void writeAttribute(final String name, final Object value, final String property) throws IOException
  -      {
  -         commandList.add( new WriteAttributeCommand(property, value, name) );
  -         responseWriter.writeAttribute(name, value, property);
  -      }
  -
  -      @Override
  -      public void writeComment(final Object comment) throws IOException
  -      {
  -         commandList.add( new WriteCommentCommand(comment) );
  -         responseWriter.writeComment(comment);
  -      }
  -
  -      @Override
  -      public void writeText(final char[] text, final int off, final int len) throws IOException
  -      {
  -         commandList.add( new WriteCharsCommand(text, len, off) );
  -         responseWriter.writeText(text, off, len);
  -      }
  -
  -      @Override
  -      public void writeText(final Object text, final String property) throws IOException
  -      {
  -         commandList.add( new WriteTextCommand(property, text) );
  -         responseWriter.writeText(text, property);
  -      }
  -
  -      @Override
  -      public void writeURIAttribute(final String name, final Object value, final String property) throws IOException
  -      {
  -         commandList.add( new WriteURIAttributeCommand(value, property, name) );
  -         responseWriter.writeURIAttribute(name, value, property);
  -      }
  -      
  -      @Override
  -      public void write(final char[] cbuf, final int off, final int len) throws IOException
  -      {
  -         commandList.add( new WriteCommand(off, len, cbuf) );
  -         responseWriter.write(cbuf, off, len);
  -      }
  -
  -      @Override
  -      public void flush() throws IOException
  -      {
  -         responseWriter.flush();
  -      }
  -
  -      @Override
  -      public String getCharacterEncoding()
  -      {
  -         return responseWriter.getCharacterEncoding();
  -      }
  -
  -      @Override
  -      public String getContentType()
  -      {
  -         return responseWriter.getContentType();
  -      }
  -
  -      @Override
  -      public void close() throws IOException
  -      {
  -         responseWriter.close();
  -      }
  -
  -   }
  -
      public String getKey()
      {
         return key;
  
  
  



More information about the jboss-cvs-commits mailing list