From ace52cfa9fcb780abaf4244178e61e6b6f5124d5 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Mon, 16 Feb 2015 22:25:26 +0100 Subject: [PATCH] Refactoring code --- .../fetcher/impl/DiskFeedInfoCache.java | 152 ++++++++---------- .../fetcher/impl/FeedFetcherCache.java | 2 + .../fetcher/impl/HashMapFeedInfoCache.java | 9 +- .../fetcher/impl/HttpClientFeedFetcher.java | 5 +- .../fetcher/impl/HttpURLFeedFetcher.java | 15 +- 5 files changed, 85 insertions(+), 98 deletions(-) diff --git a/src/main/java/com/rometools/fetcher/impl/DiskFeedInfoCache.java b/src/main/java/com/rometools/fetcher/impl/DiskFeedInfoCache.java index c8fd021..92bc9b1 100644 --- a/src/main/java/com/rometools/fetcher/impl/DiskFeedInfoCache.java +++ b/src/main/java/com/rometools/fetcher/impl/DiskFeedInfoCache.java @@ -24,8 +24,10 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.URL; +import com.rometools.utils.IO; + /** - * Disk based cache. + * Disk based feed cache. */ public class DiskFeedInfoCache implements FeedFetcherCache { @@ -37,71 +39,42 @@ public class DiskFeedInfoCache implements FeedFetcherCache { @Override public SyndFeedInfo getFeedInfo(final URL url) { - SyndFeedInfo info = null; - final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); - FileInputStream fis = null; - ObjectInputStream ois = null; - try { - fis = new FileInputStream(fileName); - ois = new ObjectInputStream(fis); - info = (SyndFeedInfo) ois.readObject(); - } catch (final FileNotFoundException e) { - // That's OK, we'l return null - } catch (final ClassNotFoundException e) { - // Error writing to cache is fatal - 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", e); - } finally { - if (fis != null) { - try { - fis.close(); - } catch (final IOException e) { - } - } - if (ois != null) { - try { - ois.close(); - } catch (final IOException e) { - } - } - } - return info; + final String fileName = generateFilename(url); + return getFeedInfo(fileName); } @Override public void setFeedInfo(final URL url, final SyndFeedInfo feedInfo) { - final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); - FileOutputStream fos; + + final String fileName = generateFilename(url); + + FileOutputStream fos = null; + ObjectOutputStream oos = null; + try { + fos = new FileOutputStream(fileName); - final ObjectOutputStream oos = new ObjectOutputStream(fos); + oos = new ObjectOutputStream(fos); oos.writeObject(feedInfo); fos.flush(); - fos.close(); - } catch (final Exception e) { - // Error writing to cache is fatal - throw new RuntimeException("Attempting to write to cache", e); + + } catch (final FileNotFoundException e) { + + throw new RuntimeException("Error while writing to cache", e); + + } catch (final IOException e) { + + throw new RuntimeException("Error while writing to cache", e); + + } finally { + + IO.closeQuietly(fos); + IO.closeQuietly(oos); + } + } - public static String replaceNonAlphanumeric(final String str, final char subst) { - final StringBuffer ret = new StringBuffer(str.length()); - final char[] testChars = str.toCharArray(); - for (final char testChar : testChars) { - if (Character.isLetterOrDigit(testChar)) { - ret.append(testChar); - } else { - ret.append(subst); - } - } - return ret.toString(); - } - - /** - * Clear the cache. - */ @Override public synchronized void clear() { final File file = new File(cachePath); @@ -114,57 +87,72 @@ public class DiskFeedInfoCache implements FeedFetcherCache { final File deleteMe = new File(cachePath + File.separator + files[i]); deleteMe.delete(); } - // don't delete the cache directory } } @Override public SyndFeedInfo remove(final URL url) { + final String fileName = generateFilename(url); + final SyndFeedInfo info = getFeedInfo(fileName); + if (info != null) { + final File file = new File(fileName); + if (file.exists()) { + file.delete(); + } + } + return info; + } + + private SyndFeedInfo getFeedInfo(final String fileName) { SyndFeedInfo info = null; - final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); FileInputStream fis = null; ObjectInputStream ois = null; - boolean consumed = false; try { fis = new FileInputStream(fileName); ois = new ObjectInputStream(fis); info = (SyndFeedInfo) ois.readObject(); - consumed = true; } catch (final FileNotFoundException e) { - // That's OK, we'l return null + + // feed is not cached yet + } catch (final ClassNotFoundException e) { - // Error writing to cache is fatal - throw new RuntimeException("Attempting to read from cache", e); + + throw new RuntimeException("Unable to read from cache", e); + } catch (final IOException e) { - // Error writing to cache is fatal - throw new RuntimeException("Attempting to read from cache", e); + + throw new RuntimeException("Unable 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) { - } - } - if (consumed) { - final File file = new File(fileName); - if (file.exists()) { - file.delete(); - } - } + + IO.closeQuietly(fis); + IO.closeQuietly(ois); + } return info; } + + private static String replaceNonAlphanumeric(final String string, final char character) { + final StringBuffer buffer = new StringBuffer(string.length()); + for (final char singleChar : string.toCharArray()) { + if (Character.isLetterOrDigit(singleChar)) { + buffer.append(singleChar); + } else { + buffer.append(character); + } + } + return buffer.toString(); + } + + private String generateFilename(final URL url) { + return cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); + } + } diff --git a/src/main/java/com/rometools/fetcher/impl/FeedFetcherCache.java b/src/main/java/com/rometools/fetcher/impl/FeedFetcherCache.java index c71b519..4c0ff0a 100644 --- a/src/main/java/com/rometools/fetcher/impl/FeedFetcherCache.java +++ b/src/main/java/com/rometools/fetcher/impl/FeedFetcherCache.java @@ -28,6 +28,7 @@ import java.net.URL; * */ public interface FeedFetcherCache { + /** * Get a SyndFeedInfo object from the cache. * @@ -55,4 +56,5 @@ public interface FeedFetcherCache { * @return The removed SyndFeedInfo */ public SyndFeedInfo remove(URL feedUrl); + } diff --git a/src/main/java/com/rometools/fetcher/impl/HashMapFeedInfoCache.java b/src/main/java/com/rometools/fetcher/impl/HashMapFeedInfoCache.java index e4d7442..7508ce7 100644 --- a/src/main/java/com/rometools/fetcher/impl/HashMapFeedInfoCache.java +++ b/src/main/java/com/rometools/fetcher/impl/HashMapFeedInfoCache.java @@ -38,9 +38,10 @@ import java.util.Map; * */ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { + private static final long serialVersionUID = 1L; - static HashMapFeedInfoCache _instance; + static HashMapFeedInfoCache instance; private Map infoCache; @@ -65,10 +66,10 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { * @return an implementation of FeedFetcherCache */ public static synchronized FeedFetcherCache getInstance() { - if (_instance == null) { - _instance = new HashMapFeedInfoCache(); + if (instance == null) { + instance = new HashMapFeedInfoCache(); } - return _instance; + return instance; } protected Map createInfoCache() { diff --git a/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java b/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java index a666d4b..c15833f 100644 --- a/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java +++ b/src/main/java/com/rometools/fetcher/impl/HttpClientFeedFetcher.java @@ -42,6 +42,7 @@ import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.XmlReader; +import com.rometools.utils.IO; /** * @author Nick Lothian @@ -279,9 +280,7 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher { } finally { - if (stream != null) { - stream.close(); - } + IO.close(stream); } diff --git a/src/main/java/com/rometools/fetcher/impl/HttpURLFeedFetcher.java b/src/main/java/com/rometools/fetcher/impl/HttpURLFeedFetcher.java index bdca6bf..70a8863 100644 --- a/src/main/java/com/rometools/fetcher/impl/HttpURLFeedFetcher.java +++ b/src/main/java/com/rometools/fetcher/impl/HttpURLFeedFetcher.java @@ -30,6 +30,7 @@ import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.XmlReader; +import com.rometools.utils.IO; /** *

@@ -70,7 +71,7 @@ import com.rometools.rome.io.XmlReader; */ public class HttpURLFeedFetcher extends AbstractFeedFetcher { - private volatile int connectTimeout = -1; + private volatile int connectTimeout = -1; static final int POLL_EVENT = 1; static final int RETRIEVE_EVENT = 2; @@ -170,9 +171,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher { } catch (final java.io.IOException e) { handleErrorCodes(((HttpURLConnection) connection).getResponseCode()); } finally { - if (inputStream != null) { - inputStream.close(); - } + IO.close(inputStream); httpConnection.disconnect(); } // we will never actually get to this line @@ -234,9 +233,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher { syndFeedInfo.setSyndFeed(syndFeed); } finally { - if (inputStream != null) { - inputStream.close(); - } + IO.close(inputStream); } } @@ -249,7 +246,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher { * @param syndFeedInfo The SyndFeedInfo for the feed to be retrieved. May be null * @param userAgent the name of the user-agent to be placed in HTTP-header. */ - protected void setRequestHeaders(final URLConnection connection, final SyndFeedInfo syndFeedInfo, String userAgent) { + protected void setRequestHeaders(final URLConnection connection, final SyndFeedInfo syndFeedInfo, final String userAgent) { if (syndFeedInfo != null) { // set the headers to get feed only if modified // we support the use of both last modified and eTag headers @@ -288,7 +285,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher { // SyndFeedInput input = new SyndFeedInput(); - XmlReader reader; + final XmlReader reader; if (connection.getHeaderField("Content-Type") != null) { reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true); } else {