KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > velocity > deprecated > NewsfeedCache


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

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 JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
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 /**
34  * Returns parsed RSS feed by pulling one from a cache or by retrieving and
35  * parging the specified feed using the Flock RSS parser.
36  *
37  * TODO: use PlanetRoller to implement NewsfeedCache instead.
38  */

39 public class NewsfeedCache {
40     
41     private static Log mLogger = LogFactory.getLog(NewsfeedCache.class);
42     
43     /** Static singleton * */
44     private static NewsfeedCache mInstance = null;
45     
46     /** Instance vars * */
47     private boolean aggregator_enabled = true;
48     private boolean aggregator_cache_enabled = true;
49     private int aggregator_cache_timeout = 14400;
50     
51     /** LRU cache */
52     LRUCache2 mCache = null;
53     
54     
55     /** Constructor */
56     private NewsfeedCache() {
57         // lookup the props we need
58
String JavaDoc enabled = RollerConfig.getProperty("aggregator.enabled");
59         String JavaDoc usecache = RollerConfig.getProperty("aggregator.cache.enabled");
60         String JavaDoc 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 JavaDoc e) { mLogger.warn(e); }
71         
72         // finally ... create the cache
73
this.mCache = new LRUCache2(100, 1000 * this.aggregator_cache_timeout);
74     }
75     
76     
77     /** static singleton retriever */
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     /**
92      * Returns a Channel object for the supplied RSS newsfeed URL.
93      *
94      * @param feedUrl RSS newsfeed URL.
95      * @return FlockFeedI for specified RSS newsfeed URL.
96      */

97     public SyndFeed getChannel(String JavaDoc feedUrl) {
98         
99         SyndFeed feed = null;
100         try {
101             // If aggregator has been disable return null
102
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                 // Get pre-parsed feed from the cache
112
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                         // Parse the feed
120
SyndFeedInput feedInput = new SyndFeedInput();
121                         feed = feedInput.build(new InputStreamReader JavaDoc(
122                                 new URL JavaDoc(feedUrl).openStream()));
123                     } catch (Exception JavaDoc e1) {
124                         mLogger.info("Error parsing RSS: " + feedUrl);
125                     }
126                 }
127                 // Store parsed feed in the cache
128
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                     // charset fix from Jason Rumney (see ROL-766)
137
URLConnection JavaDoc connection = new URL JavaDoc(feedUrl).openConnection();
138                     connection.connect();
139                     String JavaDoc contentType = connection.getContentType();
140                     // Default charset to UTF-8, since we are expecting XML
141
String JavaDoc 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                             // Check that charset is recognized by Java
150
try {
151                                 byte[] test = "test".getBytes(charset);
152                             } catch (UnsupportedEncodingException JavaDoc codingEx) {
153                                 // default to UTF-8
154
charset = "UTF-8";
155                             }
156                         }
157                     }
158                     // Parse the feed
159
SyndFeedInput feedInput = new SyndFeedInput();
160                     feed = feedInput.build(new InputStreamReader JavaDoc(
161                             connection.getInputStream(), charset));
162                 } catch (Exception JavaDoc e1) {
163                     mLogger.info("Error parsing RSS: " + feedUrl);
164                 }
165             }
166             
167         } catch (Exception JavaDoc ioe) {
168             if (mLogger.isDebugEnabled()) {
169                 mLogger.debug("Newsfeed: Unexpected exception", ioe);
170             }
171         }
172         
173         return feed;
174     }
175     
176 }
177
Popular Tags