Author: etnu
Date: Wed Sep 24 07:52:45 2008
New Revision: 698598

URL: http://svn.apache.org/viewvc?rev=698598&view=rev
Log:
Merged ProcessedGadget and Gadget together, making Gadget-> MutableContent 
relationship has-a instead of is-a as well as providing better definition for 
the role of Gadget objects in the rendering pipeline.


Removed:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/ProcessedGadget.java
Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java?rev=698598&r1=698597&r2=698598&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/Gadget.java
 Wed Sep 24 07:52:45 2008
@@ -18,13 +18,18 @@
 package org.apache.shindig.gadgets;
 
 import org.apache.shindig.common.ContainerConfig;
+import org.apache.shindig.common.util.Check;
 import org.apache.shindig.gadgets.http.HttpResponse;
+import org.apache.shindig.gadgets.parse.GadgetHtmlNode;
 import org.apache.shindig.gadgets.parse.GadgetHtmlParser;
+import org.apache.shindig.gadgets.preload.Preloads;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.LocaleSpec;
 import org.apache.shindig.gadgets.spec.Preload;
 import org.apache.shindig.gadgets.spec.View;
 
+import edu.emory.mathcs.backport.java.util.Collections;
+
 import org.json.JSONArray;
 import org.json.JSONException;
 
@@ -37,41 +42,109 @@
  * Intermediary representation of all state associated with processing
  * of a single gadget request.
  */
-public class Gadget extends MutableContent {
-  private final GadgetContext context;
-  
+public class Gadget {
+  private GadgetContext context;
+  private GadgetSpec spec;
+  private Preloads preloads;
+  private MutableContent mutableContent;
+
+  @Deprecated
+  private Collection<JsLibrary> jsLibraries = Collections.emptyList();
+
+  @Deprecated
+  private final Map<Preload, Future<HttpResponse>> preloadMap
+      = new HashMap<Preload, Future<HttpResponse>>();
+
+  @Deprecated
+  private View currentView;
+
+  public Gadget() {}
+
+  /**
+   * @deprecated Use default ctor and setter methods instead.
+   */
+  @Deprecated
+  public Gadget(GadgetContext context, GadgetSpec spec,
+      Collection<JsLibrary> jsLibraries, ContainerConfig containerConfig,
+      GadgetHtmlParser contentParser) {
+
+    this.context = context;
+    this.spec = spec;
+    this.jsLibraries = jsLibraries;
+    this.currentView = getView(containerConfig);
+
+    mutableContent = new MutableContent(contentParser);
+    if (this.currentView != null) {
+      // View might be invalid or associated with no content (type=URL)
+      mutableContent.setContent(this.currentView.getContent());
+    } else {
+      mutableContent.setContent(null);
+    }
+  }
+
   /**
-   * @return The context in which this gadget was created.
+   * @param context The request that the gadget is being processed for.
    */
+  public Gadget setContext(GadgetContext context) {
+    this.context = context;
+    return this;
+  }
+
   public GadgetContext getContext() {
     return context;
   }
 
-  private final GadgetSpec spec;
-  
   /**
-   * @return The spec from which this gadget was originally built.
+   * @param spec The spec for the gadget that is being processed.
    */
+  public Gadget setSpec(GadgetSpec spec) {
+    this.spec = spec;
+    return this;
+  }
+
   public GadgetSpec getSpec() {
     return spec;
   }
 
-  private final Collection<JsLibrary> jsLibraries;
-  
   /**
-   * @return A mutable collection of JsLibrary objects attached to this Gadget.
+   * @param mutableContent Content associated with rendering this gadget.
    */
-  public Collection<JsLibrary> getJsLibraries() {
-    return jsLibraries;
+  public Gadget setMutableContent(MutableContent mutableContent) {
+    this.mutableContent = mutableContent;
+    return this;
+  }
+
+  public MutableContent getMutableContent() {
+    return mutableContent;
   }
 
-  private final Map<Preload, Future<HttpResponse>> preloads
-      = new HashMap<Preload, Future<HttpResponse>>();
-  
   /**
-   * @return A mutable map of preloads.
+   * Sets the current content of the rendered output of this gadget.
    */
-  public Map<Preload, Future<HttpResponse>> getPreloadMap() {
+  public Gadget setContent(String newContent) {
+    Check.notNull(mutableContent, "Can not set content without setting mutable 
content.");
+    mutableContent.setContent(newContent);
+    return this;
+  }
+
+  public String getContent() {
+    Check.notNull(mutableContent, "Can not get content without setting mutable 
content.");
+    return mutableContent.getContent();
+  }
+
+  public GadgetHtmlNode getParseTree() {
+    return mutableContent.getParseTree();
+  }
+
+  /**
+   * @param preloads The preloads for the gadget that is being processed.
+   */
+  public Gadget setPreloads(Preloads preloads) {
+    this.preloads = preloads;
+    return this;
+  }
+
+  public Preloads getPreloads() {
     return preloads;
   }
 
@@ -85,12 +158,29 @@
   public LocaleSpec getLocale() {
     return spec.getModulePrefs().getLocale(context.getLocale());
   }
-  
-  private final View currentView;
-  
+
+
+
+  /**
+   * @return A mutable collection of JsLibrary objects attached to this Gadget.
+   */
+  @Deprecated
+  public Collection<JsLibrary> getJsLibraries() {
+    return jsLibraries;
+  }
+
+  /**
+   * @return A mutable map of preloads.
+   */
+  @Deprecated
+  public Map<Preload, Future<HttpResponse>> getPreloadMap() {
+    return preloadMap;
+  }
+
   /**
-   * @return The (immutable) View applicable for the current request (part of 
GadgetSpec).
+   * @return The View applicable for the current request.
    */
+  @Deprecated
   public View getCurrentView() {
     return currentView;
   }
@@ -101,6 +191,7 @@
    * @param config The container configuration; used to look for any view name
    *        aliases for the container specified in the context.
    */
+  @Deprecated
   View getView(ContainerConfig config) {
     String viewName = context.getView();
     View view = spec.getView(viewName);
@@ -127,20 +218,4 @@
     }
     return view;
   }
-  public Gadget(GadgetContext context, GadgetSpec spec,
-      Collection<JsLibrary> jsLibraries, ContainerConfig containerConfig,
-      GadgetHtmlParser contentParser) {
-    super(contentParser);
-    
-    this.context = context;
-    this.spec = spec;
-    this.jsLibraries = jsLibraries;
-    this.currentView = getView(containerConfig);
-    if (this.currentView != null) {
-      // View might be invalid or associated with no content (type=URL)
-      setContent(this.currentView.getContent());
-    } else {
-      setContent(null);
-    }
-  }
 }
\ No newline at end of file

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java?rev=698598&r1=698597&r2=698598&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/render/Renderer.java
 Wed Sep 24 07:52:45 2008
@@ -19,10 +19,10 @@
 package org.apache.shindig.gadgets.render;
 
 import org.apache.shindig.common.uri.Uri;
+import org.apache.shindig.gadgets.Gadget;
 import org.apache.shindig.gadgets.GadgetContext;
 import org.apache.shindig.gadgets.GadgetException;
 import org.apache.shindig.gadgets.GadgetSpecFactory;
-import org.apache.shindig.gadgets.ProcessedGadget;
 import org.apache.shindig.gadgets.http.ContentFetcherFactory;
 import org.apache.shindig.gadgets.http.HttpRequest;
 import org.apache.shindig.gadgets.http.HttpResponse;
@@ -67,7 +67,7 @@
     try {
       GadgetSpec spec = gadgetSpecFactory.getGadgetSpec(context);
 
-      ProcessedGadget gadget = new ProcessedGadget()
+      Gadget gadget = new Gadget()
           .setContext(context)
           .setSpec(spec);
 

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java?rev=698598&r1=698597&r2=698598&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/AbstractHttpCacheTest.java
 Wed Sep 24 07:52:45 2008
@@ -17,18 +17,10 @@
  */
 package org.apache.shindig.gadgets.http;
 
-import org.apache.shindig.gadgets.Gadget;
-import org.apache.shindig.gadgets.MutableContent;
-import org.apache.shindig.gadgets.rewrite.ContentRewriter;
-import org.apache.shindig.gadgets.rewrite.RewriterResults;
-
 import static org.easymock.EasyMock.expect;
 import static org.easymock.classextension.EasyMock.replay;
-import org.easymock.classextension.EasyMock;
 
-import com.google.inject.AbstractModule;
-import com.google.inject.Guice;
-import com.google.inject.Injector;
+import org.easymock.classextension.EasyMock;
 
 import junit.framework.TestCase;
 
@@ -41,13 +33,7 @@
  * in properly rewriting cacheable content.
  */
 public class AbstractHttpCacheTest extends TestCase {
-  private Injector injector;
-  
-  @Override
-  protected void setUp() throws Exception {
-    injector = Guice.createInjector(new TestCacheModule());
-  }
-  
+
   public void testCache() {
     // Setup: could move this elsewhere, but no real need right now.
     HttpCacheKey key = EasyMock.createNiceMock(HttpCacheKey.class);
@@ -58,12 +44,12 @@
     replay(key, request);
     HttpResponse response = new HttpResponseBuilder().setHttpStatusCode(200)
         .setResponse("foo".getBytes()).setCacheTtl(Integer.MAX_VALUE).create();
-    
+
     // Actual test.
-    AbstractHttpCache ahc = injector.getInstance(TestHttpCache.class);
+    HttpCache ahc = new TestHttpCache();
     HttpResponse added = ahc.addResponse(key, request, response);
     assertNotSame(added, response);
-    
+
     // Not rewritten (anymore).
     assertEquals("foo", added.getResponseAsString());
     assertSame(added, ahc.getResponse(key, request));
@@ -72,45 +58,25 @@
 
   private static class TestHttpCache extends AbstractHttpCache {
     private final Map<String, HttpResponse> map;
-    
+
     public TestHttpCache() {
       super();
       map = new HashMap<String, HttpResponse>();
     }
-    
+
     @Override
     public void addResponseImpl(String key, HttpResponse response) {
       map.put(key, response);
     }
-    
+
     @Override
     public HttpResponse getResponseImpl(String key) {
       return map.get(key);
     }
-    
+
     @Override
     public HttpResponse removeResponseImpl(String key) {
       return map.remove(key);
     }
   }
-  
-  private static String PFX_STR = "--prefixtest--";
-  private static class TestContentRewriter implements ContentRewriter {
-    public RewriterResults rewrite(Gadget gadget) {
-      gadget.setContent(PFX_STR + gadget.getContent());
-      return null;
-    }
-    
-    public RewriterResults rewrite(HttpRequest req, HttpResponse resp, 
MutableContent c) {
-      c.setContent(PFX_STR + c.getContent());
-      return null;
-    }
-  }
-  
-  private static class TestCacheModule extends AbstractModule {
-    @Override
-    protected void configure() {
-      bind(ContentRewriter.class).to(TestContentRewriter.class);
-    }
-  }
 }


Reply via email to