1 18 19 package org.apache.roller.ui.rendering.velocity.deprecated; 20 21 import com.sun.syndication.feed.synd.SyndFeed; 22 import com.sun.syndication.io.SyndFeedInput; 23 import java.io.InputStreamReader ; 24 import java.io.UnsupportedEncodingException ; 25 import java.net.URL ; 26 import java.net.URLConnection ; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.config.RollerConfig; 30 import org.apache.roller.util.LRUCache2; 31 32 33 39 public class NewsfeedCache { 40 41 private static Log mLogger = LogFactory.getLog(NewsfeedCache.class); 42 43 44 private static NewsfeedCache mInstance = null; 45 46 47 private boolean aggregator_enabled = true; 48 private boolean aggregator_cache_enabled = true; 49 private int aggregator_cache_timeout = 14400; 50 51 52 LRUCache2 mCache = null; 53 54 55 56 private NewsfeedCache() { 57 String enabled = RollerConfig.getProperty("aggregator.enabled"); 59 String usecache = RollerConfig.getProperty("aggregator.cache.enabled"); 60 String cachetime = RollerConfig.getProperty("aggregator.cache.timeout"); 61 62 if("true".equalsIgnoreCase(enabled)) 63 this.aggregator_enabled = true; 64 65 if("true".equalsIgnoreCase(usecache)) 66 this.aggregator_cache_enabled = true; 67 68 try { 69 this.aggregator_cache_timeout = Integer.parseInt(cachetime); 70 } catch(Exception e) { mLogger.warn(e); } 71 72 this.mCache = new LRUCache2(100, 1000 * this.aggregator_cache_timeout); 74 } 75 76 77 78 public static NewsfeedCache getInstance() { 79 synchronized (NewsfeedCache.class) { 80 if (mInstance == null) { 81 if (mLogger.isDebugEnabled()) { 82 mLogger.debug("Instantiating new NewsfeedCache"); 83 } 84 mInstance = new NewsfeedCache(); 85 } 86 } 87 return mInstance; 88 } 89 90 91 97 public SyndFeed getChannel(String feedUrl) { 98 99 SyndFeed feed = null; 100 try { 101 if (!aggregator_enabled) { 103 return null; 104 } 105 106 if (aggregator_cache_enabled) { 107 if (mLogger.isDebugEnabled()) { 108 mLogger.debug("Newsfeed: use Cache for " + feedUrl); 109 } 110 111 feed = (SyndFeed) mCache.get(feedUrl); 113 if (mLogger.isDebugEnabled()) { 114 mLogger.debug("Newsfeed: got from Cache"); 115 } 116 117 if (feed == null) { 118 try { 119 SyndFeedInput feedInput = new SyndFeedInput(); 121 feed = feedInput.build(new InputStreamReader ( 122 new URL (feedUrl).openStream())); 123 } catch (Exception e1) { 124 mLogger.info("Error parsing RSS: " + feedUrl); 125 } 126 } 127 mCache.put(feedUrl, feed); 129 mLogger.debug("Newsfeed: not in Cache"); 130 131 } else { 132 if (mLogger.isDebugEnabled()) { 133 mLogger.debug("Newsfeed: not using Cache for " + feedUrl); 134 } 135 try { 136 URLConnection connection = new URL (feedUrl).openConnection(); 138 connection.connect(); 139 String contentType = connection.getContentType(); 140 String charset = "UTF-8"; 142 if (contentType != null) { 143 int charsetStart = contentType.indexOf("charset="); 144 if (charsetStart >= 0) { 145 int charsetEnd = contentType.indexOf(";", charsetStart); 146 if (charsetEnd == -1) charsetEnd = contentType.length(); 147 charsetStart += "charset=".length(); 148 charset = contentType.substring(charsetStart, charsetEnd); 149 try { 151 byte[] test = "test".getBytes(charset); 152 } catch (UnsupportedEncodingException codingEx) { 153 charset = "UTF-8"; 155 } 156 } 157 } 158 SyndFeedInput feedInput = new SyndFeedInput(); 160 feed = feedInput.build(new InputStreamReader ( 161 connection.getInputStream(), charset)); 162 } catch (Exception e1) { 163 mLogger.info("Error parsing RSS: " + feedUrl); 164 } 165 } 166 167 } catch (Exception ioe) { 168 if (mLogger.isDebugEnabled()) { 169 mLogger.debug("Newsfeed: Unexpected exception", ioe); 170 } 171 } 172 173 return feed; 174 } 175 176 } 177 | Popular Tags |