From 0dde9fdb9e6d8de8ebd7b1e91a101f51ce9fc462 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sat, 12 Oct 2013 15:22:53 +0200 Subject: [PATCH] Updated rome-fetcher to use generics Removed warnings --- .../fetcher/impl/AbstractFeedFetcher.java | 8 ++-- .../impl/AbstractFeedFetcherBeanInfo.java | 5 +- .../fetcher/impl/DiskFeedInfoCache.java | 48 ++++++++++++++----- .../fetcher/impl/HashMapFeedInfoCache.java | 14 +++--- .../fetcher/impl/HttpClientFeedFetcher.java | 2 +- .../impl/LinkedHashMapFeedInfoCache.java | 13 ++--- .../fetcher/samples/FeedAggregator.java | 3 +- .../rometools/fetcher/samples/FeedReader.java | 2 +- .../org/rometools/test/AbstractJettyTest.java | 4 +- .../rometools/test/FetcherTestServlet.java | 2 +- 10 files changed, 64 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcher.java b/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcher.java index c8b01b3..b1bccb1 100644 --- a/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcher.java +++ b/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcher.java @@ -34,13 +34,13 @@ import org.rometools.fetcher.FetcherListener; import com.sun.syndication.feed.synd.SyndFeed; public abstract class AbstractFeedFetcher implements FeedFetcher { - private final Set fetcherEventListeners; + private final Set fetcherEventListeners; private String userAgent; private boolean usingDeltaEncoding; private boolean preserveWireFeed; public AbstractFeedFetcher() { - fetcherEventListeners = Collections.synchronizedSet(new HashSet()); + fetcherEventListeners = Collections.synchronizedSet(new HashSet()); final Properties props = new Properties(System.getProperties()); final String resourceName = "fetcher.properties"; @@ -114,9 +114,9 @@ public abstract class AbstractFeedFetcher implements FeedFetcher { protected void fireEvent(final String eventType, final String urlStr, final SyndFeed feed) { final FetcherEvent fetcherEvent = new FetcherEvent(this, urlStr, eventType, feed); synchronized (fetcherEventListeners) { - final Iterator iter = fetcherEventListeners.iterator(); + final Iterator iter = fetcherEventListeners.iterator(); while (iter.hasNext()) { - final FetcherListener fetcherEventListener = (FetcherListener) iter.next(); + final FetcherListener fetcherEventListener = iter.next(); fetcherEventListener.fetcherEvent(fetcherEvent); } } diff --git a/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcherBeanInfo.java b/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcherBeanInfo.java index 34f43bd..9d8bf17 100644 --- a/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcherBeanInfo.java +++ b/src/main/java/org/rometools/fetcher/impl/AbstractFeedFetcherBeanInfo.java @@ -12,14 +12,12 @@ public class AbstractFeedFetcherBeanInfo extends SimpleBeanInfo { @Override public EventSetDescriptor[] getEventSetDescriptors() { try { - final Class clz = AbstractFeedFetcher.class; // get the class object which we'll describe + final Class clz = AbstractFeedFetcher.class; // get the class object which we'll describe final Method addMethod = clz.getMethod("addFetcherEventListener", new Class[] { FetcherListener.class }); final Method removeMethod = clz.getMethod("removeFetcherEventListener", new Class[] { FetcherListener.class }); final Method listenerMethod = FetcherListener.class.getMethod("fetcherEvent", new Class[] { FetcherEvent.class }); - final EventSetDescriptor est = new EventSetDescriptor("fetcherEvent", clz, new Method[] { listenerMethod }, addMethod, removeMethod); final EventSetDescriptor[] results = new EventSetDescriptor[] { est }; - return results; } catch (final Exception e) { // IntrospectionException, SecurityException and/or NoSuchMethodException can be thrown here @@ -27,4 +25,5 @@ public class AbstractFeedFetcherBeanInfo extends SimpleBeanInfo { throw new RuntimeException(e); } } + } diff --git a/src/main/java/org/rometools/fetcher/impl/DiskFeedInfoCache.java b/src/main/java/org/rometools/fetcher/impl/DiskFeedInfoCache.java index 87ab0d2..6ab254d 100644 --- a/src/main/java/org/rometools/fetcher/impl/DiskFeedInfoCache.java +++ b/src/main/java/org/rometools/fetcher/impl/DiskFeedInfoCache.java @@ -39,20 +39,33 @@ public class DiskFeedInfoCache implements FeedFetcherCache { public SyndFeedInfo getFeedInfo(final URL url) { SyndFeedInfo info = null; final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); - FileInputStream fis; + FileInputStream fis = null; + ObjectInputStream ois = null; try { fis = new FileInputStream(fileName); - final ObjectInputStream ois = new ObjectInputStream(fis); + ois = new ObjectInputStream(fis); info = (SyndFeedInfo) ois.readObject(); - fis.close(); - } catch (final FileNotFoundException fnfe) { + } catch (final FileNotFoundException e) { // That's OK, we'l return null - } catch (final ClassNotFoundException cnfe) { + } catch (final ClassNotFoundException e) { // Error writing to cache is fatal - throw new RuntimeException("Attempting to read from cache", cnfe); - } catch (final IOException fnfe) { + throw new RuntimeException("Attempting to read from cache", e); + } catch (final IOException e) { // Error writing to cache is fatal - throw new RuntimeException("Attempting to read from cache", fnfe); + throw new RuntimeException("Attempting to read from cache", e); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (final IOException e) { + } + } + if (ois != null) { + try { + ois.close(); + } catch (final IOException e) { + } + } } return info; } @@ -110,12 +123,12 @@ public class DiskFeedInfoCache implements FeedFetcherCache { public SyndFeedInfo remove(final URL url) { SyndFeedInfo info = null; final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); - FileInputStream fis; + FileInputStream fis = null; + ObjectInputStream ois = null; try { fis = new FileInputStream(fileName); - final ObjectInputStream ois = new ObjectInputStream(fis); + ois = new ObjectInputStream(fis); info = (SyndFeedInfo) ois.readObject(); - fis.close(); final File file = new File(fileName); if (file.exists()) { @@ -129,6 +142,19 @@ public class DiskFeedInfoCache implements FeedFetcherCache { } catch (final IOException fnfe) { // Error writing to cahce is fatal throw new RuntimeException("Attempting to read from cache", fnfe); + } finally { + if (fis != null) { + try { + fis.close(); + } catch (final IOException e) { + } + } + if (ois != null) { + try { + ois.close(); + } catch (final IOException e) { + } + } } return info; } diff --git a/src/main/java/org/rometools/fetcher/impl/HashMapFeedInfoCache.java b/src/main/java/org/rometools/fetcher/impl/HashMapFeedInfoCache.java index b7279f5..32877f1 100644 --- a/src/main/java/org/rometools/fetcher/impl/HashMapFeedInfoCache.java +++ b/src/main/java/org/rometools/fetcher/impl/HashMapFeedInfoCache.java @@ -40,7 +40,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { static HashMapFeedInfoCache _instance; - private Map infoCache; + private Map infoCache; /** *

@@ -68,8 +68,8 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { return _instance; } - protected Map createInfoCache() { - return Collections.synchronizedMap(new HashMap()); + protected Map createInfoCache() { + return Collections.synchronizedMap(new HashMap()); } protected Object get(final Object key) { @@ -84,7 +84,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { return (SyndFeedInfo) get(feedUrl.toString()); } - protected void put(final Object key, final Object value) { + protected void put(final String key, final SyndFeedInfo value) { getInfoCache().put(key, value); } @@ -96,7 +96,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { put(feedUrl.toString(), syndFeedInfo); } - protected synchronized final Map getInfoCache() { + protected synchronized final Map getInfoCache() { return infoCache; } @@ -105,7 +105,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { * * @param map the map to use as the info cache. */ - protected synchronized final void setInfoCache(final Map map) { + protected synchronized final void setInfoCache(final Map map) { infoCache = map; } @@ -128,7 +128,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { return null; } - return (SyndFeedInfo) infoCache.remove(url.toString()); + return infoCache.remove(url.toString()); } } diff --git a/src/main/java/org/rometools/fetcher/impl/HttpClientFeedFetcher.java b/src/main/java/org/rometools/fetcher/impl/HttpClientFeedFetcher.java index 4917f93..82898bd 100644 --- a/src/main/java/org/rometools/fetcher/impl/HttpClientFeedFetcher.java +++ b/src/main/java/org/rometools/fetcher/impl/HttpClientFeedFetcher.java @@ -42,13 +42,13 @@ import com.sun.syndication.io.XmlReader; * @author Nick Lothian */ public class HttpClientFeedFetcher extends AbstractFeedFetcher { + private CredentialSupplier credentialSupplier; private FeedFetcherCache feedInfoCache; private volatile HttpClientMethodCallbackIntf httpClientMethodCallback; private volatile HttpClientParams httpClientParams; public HttpClientFeedFetcher() { - super(); setHttpClientParams(new HttpClientParams()); } diff --git a/src/main/java/org/rometools/fetcher/impl/LinkedHashMapFeedInfoCache.java b/src/main/java/org/rometools/fetcher/impl/LinkedHashMapFeedInfoCache.java index 0ddbe22..aeb9abf 100644 --- a/src/main/java/org/rometools/fetcher/impl/LinkedHashMapFeedInfoCache.java +++ b/src/main/java/org/rometools/fetcher/impl/LinkedHashMapFeedInfoCache.java @@ -18,7 +18,9 @@ import java.util.Map; * */ public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache { - private final class CacheImpl extends LinkedHashMap { + + private final class CacheImpl extends LinkedHashMap { + private static final long serialVersionUID = -6977191330127794920L; public CacheImpl() { @@ -26,19 +28,18 @@ public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache { } @Override - protected boolean removeEldestEntry(final Map.Entry eldest) { + protected boolean removeEldestEntry(final Map.Entry eldest) { return size() > getMaxEntries(); } + } private static final int DEFAULT_MAX_ENTRIES = 20; - private static final long serialVersionUID = 1694228973357997417L; + private static final LinkedHashMapFeedInfoCache _instance = new LinkedHashMapFeedInfoCache(); private int maxEntries = DEFAULT_MAX_ENTRIES; - private final static LinkedHashMapFeedInfoCache _instance = new LinkedHashMapFeedInfoCache(); - /** * Get the global instance of the cache * @@ -64,7 +65,7 @@ public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache { } @Override - protected Map createInfoCache() { + protected Map createInfoCache() { return Collections.synchronizedMap(new CacheImpl()); } diff --git a/src/main/java/org/rometools/fetcher/samples/FeedAggregator.java b/src/main/java/org/rometools/fetcher/samples/FeedAggregator.java index 3a82797..590b157 100644 --- a/src/main/java/org/rometools/fetcher/samples/FeedAggregator.java +++ b/src/main/java/org/rometools/fetcher/samples/FeedAggregator.java @@ -26,6 +26,7 @@ import org.rometools.fetcher.impl.FeedFetcherCache; import org.rometools.fetcher.impl.HashMapFeedInfoCache; import org.rometools.fetcher.impl.HttpURLFeedFetcher; +import com.sun.syndication.feed.synd.SyndEntry; import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeedImpl; import com.sun.syndication.io.SyndFeedOutput; @@ -59,7 +60,7 @@ public class FeedAggregator { feed.setAuthor("anonymous"); feed.setLink("http://www.anonymous.com"); - final List entries = new ArrayList(); + final List entries = new ArrayList(); feed.setEntries(entries); final FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); diff --git a/src/main/java/org/rometools/fetcher/samples/FeedReader.java b/src/main/java/org/rometools/fetcher/samples/FeedReader.java index c79396a..a8fddea 100644 --- a/src/main/java/org/rometools/fetcher/samples/FeedReader.java +++ b/src/main/java/org/rometools/fetcher/samples/FeedReader.java @@ -61,7 +61,7 @@ public class FeedReader { // and the server supports conditional gets, we will get a "Feed // Unchanged" event after the Feed Polled event System.err.println("Polling " + feedUrl + " again to test conditional get support."); - final SyndFeed feed2 = fetcher.retrieveFeed(feedUrl); + fetcher.retrieveFeed(feedUrl); System.err.println("If a \"Feed Unchanged\" event fired then the server supports conditional gets."); ok = true; diff --git a/src/test/java/org/rometools/test/AbstractJettyTest.java b/src/test/java/org/rometools/test/AbstractJettyTest.java index b0fd7e1..90e8276 100644 --- a/src/test/java/org/rometools/test/AbstractJettyTest.java +++ b/src/test/java/org/rometools/test/AbstractJettyTest.java @@ -236,7 +236,7 @@ public abstract class AbstractJettyTest extends TestCase { public void testErrorHandling() { final FeedFetcher feedFetcher = getFeedFetcher(); try { - final SyndFeed feed = feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet?error=404")); + feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet?error=404")); fail("4xx error handling did not work correctly"); } catch (final FetcherException e) { // expect this exception @@ -247,7 +247,7 @@ public abstract class AbstractJettyTest extends TestCase { } try { - final SyndFeed feed = feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet?error=500")); + feedFetcher.retrieveFeed(new URL("http://localhost:" + testPort + "/rome/FetcherTestServlet?error=500")); fail("5xx error handling did not work correctly"); } catch (final FetcherException e) { // expect this exception diff --git a/src/test/java/org/rometools/test/FetcherTestServlet.java b/src/test/java/org/rometools/test/FetcherTestServlet.java index cfe918f..fd9c8e1 100644 --- a/src/test/java/org/rometools/test/FetcherTestServlet.java +++ b/src/test/java/org/rometools/test/FetcherTestServlet.java @@ -177,7 +177,7 @@ public class FetcherTestServlet extends HttpServlet { feed.setLink("http://rome.dev.java.net"); feed.setDescription("This tests using rfc3229 delta encoding."); - final List entries = new ArrayList(); + final List entries = new ArrayList(); SyndEntry entry; SyndContent description;