Author: johnh
Date: Thu Sep 18 13:50:17 2008
New Revision: 696796

URL: http://svn.apache.org/viewvc?rev=696796&view=rev
Log:
Getting rid of CachingWebRetrievalFactory.


Removed:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/CachingWebRetrievalFactory.java
Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/AbstractMessageBundleFactory.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicMessageBundleFactory.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/AbstractMessageBundleFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/AbstractMessageBundleFactory.java?rev=696796&r1=696795&r2=696796&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/AbstractMessageBundleFactory.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/AbstractMessageBundleFactory.java
 Thu Sep 18 13:50:17 2008
@@ -17,7 +17,6 @@
  */
 package org.apache.shindig.gadgets;
 
-import org.apache.shindig.common.cache.CacheProvider;
 import org.apache.shindig.gadgets.spec.GadgetSpec;
 import org.apache.shindig.gadgets.spec.LocaleSpec;
 import org.apache.shindig.gadgets.spec.MessageBundle;
@@ -29,15 +28,7 @@
  * Core implementation of MessageBundleFactory that ensures proper 
MessageBundle creation and
  * delegates caching and network retrieval to concreate implementations.
  */
-public abstract class AbstractMessageBundleFactory
-    extends CachingWebRetrievalFactory<MessageBundle, LocaleSpec, URI>
-    implements MessageBundleFactory {
-  
-  protected AbstractMessageBundleFactory(CacheProvider cacheProvider,
-      int capacity, long minTtl, long maxTtl) {
-    super(cacheProvider, capacity, minTtl, maxTtl);
-  }
-  
+public abstract class AbstractMessageBundleFactory implements 
MessageBundleFactory {
   private static final Locale ALL_ALL = new Locale("all", "ALL");
 
   public MessageBundle getBundle(GadgetSpec spec, Locale locale, boolean 
ignoreCache)

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java?rev=696796&r1=696795&r2=696796&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicGadgetSpecFactory.java
 Thu Sep 18 13:50:17 2008
@@ -19,6 +19,7 @@
 package org.apache.shindig.gadgets;
 
 import org.apache.shindig.common.cache.CacheProvider;
+import org.apache.shindig.common.cache.TtlCache;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.http.HttpFetcher;
 import org.apache.shindig.gadgets.http.HttpRequest;
@@ -41,40 +42,51 @@
  * Basic implementation of a gadget spec factory.
  */
 @Singleton
-public class BasicGadgetSpecFactory extends 
CachingWebRetrievalFactory<GadgetSpec, URI, URI> 
-    implements GadgetSpecFactory {
+public class BasicGadgetSpecFactory implements GadgetSpecFactory {
   static final Logger logger = 
Logger.getLogger(BasicGadgetSpecFactory.class.getName());
 
   private final HttpFetcher fetcher;
   private final ExecutorService executor;
-
-  @Override
-  protected URI getCacheKeyFromQueryObj(URI queryObj) {
-    return queryObj;
-  }
-  
-  @Override
-  protected Logger getLogger() {
-    return logger;
-  }
+  private final TtlCache<URI, GadgetSpec> ttlCache;
 
   public GadgetSpec getGadgetSpec(GadgetContext context) throws 
GadgetException {
     return getGadgetSpec(context.getUrl(), context.getIgnoreCache());
   }
-
+  
   /**
    * Retrieves a gadget specification from the cache or from the Internet.
    */
-  public GadgetSpec getGadgetSpec(URI url, boolean ignoreCache) throws 
GadgetException {
-    return doCachedFetch(url, ignoreCache);
+  public GadgetSpec getGadgetSpec(URI gadgetUri, boolean ignoreCache) throws 
GadgetException {     
+    if (ignoreCache) {
+      return fetchObjectAndCache(gadgetUri, ignoreCache);
+    }
+    
+    TtlCache.CachedObject<GadgetSpec> cached = null;
+    synchronized(ttlCache) {
+      cached = ttlCache.getElementWithExpiration(gadgetUri);
+    }
+    
+    if (cached.obj == null || cached.isExpired) {
+      try {
+        return fetchObjectAndCache(gadgetUri, ignoreCache);
+      } catch (GadgetException e) {
+        // Failed to re-fetch raw object. Use cached object if it exists.
+        if (cached.obj == null) {
+          throw e;
+        } else {
+          logger.info("GadgetSpec fetch failed for " + gadgetUri + " -  using 
cached.");
+        }
+      }
+    }
+    
+    return cached.obj;
   }
 
   /**
    * Retrieves a gadget specification from the Internet, processes its views 
and
    * adds it to the cache.
    */
-  protected FetchedObject<GadgetSpec> retrieveRawObject(URI url, boolean 
ignoreCache)
-      throws GadgetException {
+  private GadgetSpec fetchObjectAndCache(URI url, boolean ignoreCache) throws 
GadgetException {
     HttpRequest request = new 
HttpRequest(Uri.fromJavaUri(url)).setIgnoreCache(ignoreCache);
     HttpResponse response = fetcher.fetch(request);
     if (response.getHttpStatusCode() != HttpResponse.SC_OK) {
@@ -114,23 +126,21 @@
       }
     }
 
-    // Annotate this spec instance with the expiration time (as a Long) 
associated
-    // with its retrieval. This enables CachingContentRewriterRegistry to 
properly
-    // cache rewritten content generated from Gadgets based on the spec.
-    spec.setAttribute(GadgetSpec.EXPIRATION_ATTRIB, new 
Long(response.getCacheExpiration()));
+    ttlCache.addElement(url, spec, response.getCacheExpiration());
     
-    return new FetchedObject<GadgetSpec>(spec, response.getCacheExpiration());
+    return spec;
   }
 
   @Inject
   public BasicGadgetSpecFactory(HttpFetcher fetcher,
-                                CacheProvider cacheProvider,
-                                ExecutorService executor,
-                                
@Named("shindig.gadget-spec.cache.capacity")int gadgetSpecCacheCapacity,
-                                @Named("shindig.gadget-spec.cache.minTTL")long 
minTtl,
-                                @Named("shindig.gadget-spec.cache.maxTTL")long 
maxTtl) {
-    super(cacheProvider, gadgetSpecCacheCapacity, minTtl, maxTtl);
+      CacheProvider cacheProvider,
+      ExecutorService executor,
+      @Named("shindig.gadget-spec.cache.capacity")int gadgetSpecCacheCapacity,
+      @Named("shindig.gadget-spec.cache.minTTL")long minTtl,
+      @Named("shindig.gadget-spec.cache.maxTTL")long maxTtl) {
     this.fetcher = fetcher;
     this.executor = executor;
+    this.ttlCache =
+        new TtlCache<URI, GadgetSpec>(cacheProvider, gadgetSpecCacheCapacity, 
minTtl, maxTtl);
   }
 }

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicMessageBundleFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicMessageBundleFactory.java?rev=696796&r1=696795&r2=696796&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicMessageBundleFactory.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/BasicMessageBundleFactory.java
 Thu Sep 18 13:50:17 2008
@@ -19,6 +19,7 @@
 package org.apache.shindig.gadgets;
 
 import org.apache.shindig.common.cache.CacheProvider;
+import org.apache.shindig.common.cache.TtlCache;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.gadgets.http.HttpFetcher;
 import org.apache.shindig.gadgets.http.HttpRequest;
@@ -40,9 +41,37 @@
 public class BasicMessageBundleFactory extends AbstractMessageBundleFactory {
   static final Logger logger = 
Logger.getLogger(BasicMessageBundleFactory.class.getName());
   private final HttpFetcher fetcher;
-
+  private final TtlCache<URI, MessageBundle> ttlCache;
+  
   @Override
-  protected FetchedObject<MessageBundle> retrieveRawObject(LocaleSpec locale,
+  protected MessageBundle fetchBundle(LocaleSpec locale, boolean ignoreCache)
+      throws GadgetException {
+    if (ignoreCache) {
+      return fetchAndCacheBundle(locale, ignoreCache);
+    }
+    
+    TtlCache.CachedObject<MessageBundle> cached = null;
+    
+    synchronized(ttlCache) {
+      cached = ttlCache.getElementWithExpiration(locale.getMessages());
+    }
+    
+    if (cached.obj == null || cached.isExpired) {
+      try {
+        return fetchAndCacheBundle(locale, ignoreCache);
+      } catch (GadgetException e) {
+        if (cached.obj == null) {
+          throw e;
+        } else {
+          logger.info("Message bundle fetch failed for " + locale + " -  using 
cached.");
+        }
+      }
+    }
+    
+    return cached.obj;
+  }
+
+  private MessageBundle fetchAndCacheBundle(LocaleSpec locale,
       boolean ignoreCache) throws GadgetException {
     URI url = locale.getMessages();
     HttpRequest request = new 
HttpRequest(Uri.fromJavaUri(url)).setIgnoreCache(ignoreCache);
@@ -54,22 +83,8 @@
     }
 
     MessageBundle bundle  = new MessageBundle(locale, 
response.getResponseAsString());
-    return new FetchedObject<MessageBundle>(bundle, 
response.getCacheExpiration());
-  }
-
-  @Override
-  protected URI getCacheKeyFromQueryObj(LocaleSpec queryObj) {
-    return queryObj.getMessages();
-  }
-  
-  @Override
-  protected Logger getLogger() {
-    return logger;
-  }
-
-  protected MessageBundle fetchBundle(LocaleSpec locale, boolean ignoreCache)
-      throws GadgetException {
-    return doCachedFetch(locale, ignoreCache);
+    ttlCache.addElement(url, bundle, response.getCacheExpiration());
+    return bundle;
   }
 
   @Inject
@@ -78,7 +93,7 @@
                                    
@Named("shindig.message-bundle.cache.capacity")int capacity,
                                    
@Named("shindig.message-bundle.cache.minTTL")long minTtl,
                                    
@Named("shindig.message-bundle.cache.maxTTL")long maxTtl) {
-    super(cacheProvider, capacity, minTtl, maxTtl);
     this.fetcher = fetcher;
+    this.ttlCache = new TtlCache<URI, MessageBundle>(cacheProvider, capacity, 
minTtl, maxTtl);
   }
 }


Reply via email to