package org.gridsphere.services.core.rss.impl;

import org.gridsphere.services.core.rss.RssService;
import org.gridsphere.portlet.service.spi.PortletServiceProvider;
import org.gridsphere.portlet.service.spi.PortletServiceConfig;
import org.gridsphere.portlet.service.PortletServiceUnavailableException;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import com.sun.syndication.io.FeedException;

import java.net.URL;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;

/**
 * Implementation of the RssService.
 */
public class RssServiceImpl implements RssService, PortletServiceProvider {

    private long CACHE_TIME = 20*60*1000; // in minutes
    private Map cachedStore = new HashMap();
    private Map cachedTime = new HashMap();

    public SyndFeed getFeed(String url) throws FeedException, IOException {
        SyndFeedInput input = new SyndFeedInput();
        SyndFeed feed = null;
        long diff = CACHE_TIME+1;
        // check if the url is in the cachedStore
        if (cachedStore.containsKey(url)) {
            Long cachedTime = (Long)this.cachedTime.get(url);
            diff = System.currentTimeMillis() - cachedTime.longValue();
        }
        if (diff > CACHE_TIME) {
            feed = input.build(new XmlReader(new URL(url)));
            cachedStore.put(url, feed);
            cachedTime.put(url, new Long(System.currentTimeMillis()));
        }
        return (SyndFeed)cachedStore.get(url);
    }

    public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
        Long cacheTime = new Long(config.getInitParameter("cache_time"));
        CACHE_TIME = cacheTime.longValue();
    }

    public void destroy() {
    }
}

@@ -54,51 +54,180 @@
 ort 
-org.gridsphere.services.core.rss.RssService
+com.sun.syndication.feed.synd.SyndFeed;%0D%0Aimport com.sun.syndication.io.FeedException;%0D%0Aimport com.sun.syndication.io.SyndFeedInput;%0D%0Aimport com.sun.syndication.io.XmlReader
 ;%0D%0Ai
@@ -255,36 +255,32 @@
 portlet.service.
-spi.
 PortletServicePr
@@ -277,24 +277,36 @@
 tService
-Provider
+UnavailableException
 ;%0D%0Aimpor
@@ -395,32 +395,36 @@
 portlet.service.
+spi.
 PortletServiceUn
@@ -425,201 +425,91 @@
 vice
-UnavailableException;%0D%0Aimport com.sun.syndication.feed.synd.SyndFeed;%0D%0Aimport com.sun.syndication.io.SyndFeedInput;%0D%0Aimport com.sun.syndication.io.XmlReader;%0D%0Aimport com.sun.syndication.io.Feed
+Provider;%0D%0Aimport org.gridsphere.services.core.rss.RssService;%0D%0A%0D%0Aimport java.io.IO
 Exce
@@ -516,18 +516,16 @@
 ption;%0D%0A
-%0D%0A
 import j
@@ -542,37 +542,8 @@
 L;%0D%0A
-import java.io.IOException;%0D%0A
 impo
@@ -555,16 +555,20 @@
 va.util.
+Hash
 Map;%0D%0Aim
@@ -578,28 +578,24 @@
 t java.util.
-Hash
 Map;%0D%0A%0D%0A/**%0D
@@ -753,12 +753,16 @@
 = 20
-*60*
+ * 60 * 
 1000
@@ -1065,9 +1065,11 @@
 TIME
-+
+ + 
 1;%0D%0A
@@ -1200,16 +1200,17 @@
 = (Long)
+ 
 this.cac
@@ -1557,16 +1557,17 @@
 yndFeed)
+ 
 cachedSt
@@ -1798,24 +1798,36 @@
 .longValue()
+ * 60 * 1000
 ;%0D%0A    %7D%0D%0A%0D%0A

fix for the caching time