KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > newsfeeds > NewsfeedCache


1 package org.roller.presentation.newsfeeds;
2
3 import java.io.InputStreamReader JavaDoc;
4 import java.net.URL JavaDoc;
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 /**
15  * Returns parsed RSS feed by pulling one from a cache or by retrieving and
16  * parging the specified feed using the Flock RSS parser.
17  * <br />
18  * TODO: use PlanetRoller to implement NewsfeedCache instead.
19  * <br />
20  * @author Lance Lavandowska
21  * @author Dave Johnson
22  */

23 public class NewsfeedCache
24 {
25     private static Log mLogger = LogFactory.getFactory().getInstance(
26             NewsfeedCache.class);
27
28     /** Static singleton * */
29     private static NewsfeedCache mInstance = null;
30
31     /** Instance vars * */
32     private boolean aggregator_enabled = true;
33     private boolean aggregator_cache_enabled = true;
34     private int aggregator_cache_timeout = 14400;
35
36     /** LRU cache */
37     LRUCache2 mCache = null;
38
39     /** Constructor */
40     private NewsfeedCache()
41     {
42         // lookup the props we need
43
String JavaDoc enabled = RollerConfig.getProperty("aggregator.enabled");
44         String JavaDoc usecache = RollerConfig.getProperty("aggregator.cache.enabled");
45         String JavaDoc 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 JavaDoc e) { mLogger.warn(e); }
56         
57         // finally ... create the cache
58
this.mCache = new LRUCache2(100, 1000 * this.aggregator_cache_timeout);
59     }
60
61     /** static singleton retriever */
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     /**
79      * Returns a Channel object for the supplied RSS newsfeed URL.
80      *
81      * @param feedUrl
82      * RSS newsfeed URL.
83      * @return FlockFeedI for specified RSS newsfeed URL.
84      */

85     public SyndFeed getChannel(String JavaDoc feedUrl)
86     {
87         SyndFeed feed = null;
88         try
89         {
90             // If aggregator has been disable return null
91
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                 // Get pre-parsed feed from the cache
104
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                         // Parse the feed
115
SyndFeedInput feedInput = new SyndFeedInput();
116                         feed = feedInput.build(new InputStreamReader JavaDoc(
117                                 new URL JavaDoc(feedUrl).openStream()));
118                     }
119                     catch (Exception JavaDoc e1)
120                     {
121                         mLogger.info("Error parsing RSS: " + feedUrl);
122                     }
123                 }
124                 // Store parsed feed in the cache
125
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                     // Parse the feed
137
SyndFeedInput feedInput = new SyndFeedInput();
138                     feed = feedInput.build(new InputStreamReader JavaDoc(
139                             new URL JavaDoc(feedUrl).openStream()));
140                 }
141                 catch (Exception JavaDoc e1)
142                 {
143                     mLogger.info("Error parsing RSS: " + feedUrl);
144                 }
145             }
146         }
147         catch (Exception JavaDoc ioe)
148         {
149             if (mLogger.isDebugEnabled())
150             {
151                 mLogger.debug("Newsfeed: Unexpected exception", ioe);
152             }
153         }
154         return feed;
155     }
156 }
Popular Tags