⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rssserviceimpl.java

📁 GridSphere 门户 提供一个基于 portlet 的高级开放源代码门户。GridSphere 是在欧盟提供基金的 GridLab 项目下开发的
💻 JAVA
字号:
package org.gridsphere.services.core.rss.impl;

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

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

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

    private long CACHE_TIME = 20 * 60 * 1000; // in minutes
    private Map<String, SyndFeed> cachedStore = new HashMap<String, SyndFeed>();
    private Map<String, Long> cachedTime = new HashMap<String, Long>();

    public SyndFeed getFeed(String url) throws FeedException {
        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) {
            try {
                URL rssUrl = new URL(url);
                URLConnection c = rssUrl.openConnection();
                c.setConnectTimeout(2000);
                c.setReadTimeout(2000);
                c.connect();
                InputSource src = new InputSource(c.getInputStream());
                feed = input.build(src);
            } catch (IOException e) {
                throw new FeedException("Invalid 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() * 60 * 1000;
    }

    public void destroy() {
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -