1 package org.roller.presentation.newsfeeds; 2 3 import java.io.InputStreamReader ; 4 import java.net.URL ; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.roller.util.LRUCache2; 9 10 import com.sun.syndication.feed.synd.SyndFeed; 11 import com.sun.syndication.io.SyndFeedInput; 12 import org.roller.config.RollerConfig; 13 14 23 public class NewsfeedCache 24 { 25 private static Log mLogger = LogFactory.getFactory().getInstance( 26 NewsfeedCache.class); 27 28 29 private static NewsfeedCache mInstance = null; 30 31 32 private boolean aggregator_enabled = true; 33 private boolean aggregator_cache_enabled = true; 34 private int aggregator_cache_timeout = 14400; 35 36 37 LRUCache2 mCache = null; 38 39 40 private NewsfeedCache() 41 { 42 String enabled = RollerConfig.getProperty("aggregator.enabled"); 44 String usecache = RollerConfig.getProperty("aggregator.cache.enabled"); 45 String cachetime = RollerConfig.getProperty("aggregator.cache.timeout"); 46 47 if("true".equalsIgnoreCase(enabled)) 48 this.aggregator_enabled = true; 49 50 if("true".equalsIgnoreCase(usecache)) 51 this.aggregator_cache_enabled = true; 52 53 try { 54 this.aggregator_cache_timeout = Integer.parseInt(cachetime); 55 } catch(Exception e) { mLogger.warn(e); } 56 57 this.mCache = new LRUCache2(100, 1000 * this.aggregator_cache_timeout); 59 } 60 61 62 public static NewsfeedCache getInstance() 63 { 64 synchronized (NewsfeedCache.class) 65 { 66 if (mInstance == null) 67 { 68 if (mLogger.isDebugEnabled()) 69 { 70 mLogger.debug("Instantiating new NewsfeedCache"); 71 } 72 mInstance = new NewsfeedCache(); 73 } 74 } 75 return mInstance; 76 } 77 78 85 public SyndFeed getChannel(String feedUrl) 86 { 87 SyndFeed feed = null; 88 try 89 { 90 if (!aggregator_enabled) 92 { 93 return null; 94 } 95 96 if (aggregator_cache_enabled) 97 { 98 if (mLogger.isDebugEnabled()) 99 { 100 mLogger.debug("Newsfeed: use Cache for " + feedUrl); 101 } 102 103 feed = (SyndFeed) mCache.get(feedUrl); 105 if (mLogger.isDebugEnabled()) 106 { 107 mLogger.debug("Newsfeed: got from Cache"); 108 } 109 110 if (feed == null) 111 { 112 try 113 { 114 SyndFeedInput feedInput = new SyndFeedInput(); 116 feed = feedInput.build(new InputStreamReader ( 117 new URL (feedUrl).openStream())); 118 } 119 catch (Exception e1) 120 { 121 mLogger.info("Error parsing RSS: " + feedUrl); 122 } 123 } 124 mCache.put(feedUrl, feed); 126 mLogger.debug("Newsfeed: not in Cache"); 127 } 128 else 129 { 130 if (mLogger.isDebugEnabled()) 131 { 132 mLogger.debug("Newsfeed: not using Cache for " + feedUrl); 133 } 134 try 135 { 136 SyndFeedInput feedInput = new SyndFeedInput(); 138 feed = feedInput.build(new InputStreamReader ( 139 new URL (feedUrl).openStream())); 140 } 141 catch (Exception e1) 142 { 143 mLogger.info("Error parsing RSS: " + feedUrl); 144 } 145 } 146 } 147 catch (Exception ioe) 148 { 149 if (mLogger.isDebugEnabled()) 150 { 151 mLogger.debug("Newsfeed: Unexpected exception", ioe); 152 } 153 } 154 return feed; 155 } 156 } | Popular Tags |