001: /*
002: * (C) Copyright 2000 - 2006 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.ws.service.feedcacher;
020:
021: import java.io.IOException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Iterator;
026:
027: import com.nabhinc.ws.core.WebServiceException;
028: import com.nabhinc.ws.server.ServerObjectConfig;
029: import com.nabhinc.ws.service.core.BaseJavaWebService;
030: import com.sun.syndication.feed.synd.SyndFeed;
031: import com.sun.syndication.io.FeedException;
032: import com.sun.syndication.io.SyndFeedInput;
033: import com.sun.syndication.io.XmlReader;
034:
035: /**
036: *
037: *
038: * @author Padmanabh Dabke
039: * (c) 2006 Nabh Information Systems, Inc. All Rights Reserved.
040: */
041: public class FeedCacherService extends BaseJavaWebService {
042: private int fcsFeedRefreshMinutes = 15;
043: private Hashtable<String, SyndFeed> fcsFeedCache = new Hashtable<String, SyndFeed>();
044:
045: private class CacheRefresher extends Thread {
046: public boolean keepWorking = true;
047: public int refreshPeriod = 15000 * 60;
048:
049: public void run() {
050: try {
051: Thread.sleep(this .refreshPeriod);
052: } catch (InterruptedException e) {
053: return;
054: }
055: if (!this .keepWorking)
056: return;
057: // Copy the hashmap to eliminate chances of map modifications during
058: // refresh iteration.
059: HashMap<String, SyndFeed> newMap = new HashMap<String, SyndFeed>();
060: synchronized (fcsFeedCache) {
061: newMap.putAll(fcsFeedCache);
062: }
063:
064: Iterator<String> urls = newMap.keySet().iterator();
065: String url = null;
066: while (urls.hasNext()) {
067: url = urls.next();
068: try {
069: updateFeed(url);
070: } catch (Exception e) {
071: boiLogger.error(
072: "Problem getting feed content from URL: "
073: + url, e);
074: }
075: }
076:
077: }
078: }
079:
080: private CacheRefresher fcsRefresherThread = null;
081:
082: public void init(ServerObjectConfig config)
083: throws WebServiceException {
084: super .init(config);
085: if (this .fcsRefresherThread == null) {
086: setFeedRefreshMinutes(fcsFeedRefreshMinutes);
087: }
088: }
089:
090: public int getFeedRefreshMinutes() {
091: return fcsFeedRefreshMinutes;
092: }
093:
094: public void setFeedRefreshMinutes(int m) {
095: if (m > 0)
096: this .fcsFeedRefreshMinutes = m;
097: if (fcsRefresherThread != null)
098: fcsRefresherThread.keepWorking = false;
099: fcsRefresherThread = new CacheRefresher();
100: fcsRefresherThread.refreshPeriod = m * 60 * 1000;
101: fcsRefresherThread.start();
102: }
103:
104: public SyndFeed getFeed(String url) throws FeedException,
105: IllegalArgumentException, IOException {
106: SyndFeed feed = fcsFeedCache.get(url);
107: if (feed != null)
108: return feed;
109: return updateFeed(url);
110: }
111:
112: private SyndFeed updateFeed(String url)
113: throws IllegalArgumentException, FeedException, IOException {
114: SyndFeedInput input = new SyndFeedInput();
115: URL feedUrl = new URL(url);
116: SyndFeed feed = input.build(new XmlReader(feedUrl));
117: fcsFeedCache.put(url, feed);
118: return feed;
119: }
120: }
|