Refactoring code

This commit is contained in:
Patrick Gotthard 2015-02-16 22:25:26 +01:00
parent 3d42211970
commit ace52cfa9f
5 changed files with 85 additions and 98 deletions

View file

@ -24,8 +24,10 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.net.URL; import java.net.URL;
import com.rometools.utils.IO;
/** /**
* Disk based cache. * Disk based feed cache.
*/ */
public class DiskFeedInfoCache implements FeedFetcherCache { public class DiskFeedInfoCache implements FeedFetcherCache {
@ -37,71 +39,42 @@ public class DiskFeedInfoCache implements FeedFetcherCache {
@Override @Override
public SyndFeedInfo getFeedInfo(final URL url) { public SyndFeedInfo getFeedInfo(final URL url) {
SyndFeedInfo info = null; final String fileName = generateFilename(url);
final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); return getFeedInfo(fileName);
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;
} }
@Override @Override
public void setFeedInfo(final URL url, final SyndFeedInfo feedInfo) { 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 { try {
fos = new FileOutputStream(fileName); fos = new FileOutputStream(fileName);
final ObjectOutputStream oos = new ObjectOutputStream(fos); oos = new ObjectOutputStream(fos);
oos.writeObject(feedInfo); oos.writeObject(feedInfo);
fos.flush(); fos.flush();
fos.close();
} catch (final Exception e) { } catch (final FileNotFoundException e) {
// Error writing to cache is fatal
throw new RuntimeException("Attempting to write to cache", 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 @Override
public synchronized void clear() { public synchronized void clear() {
final File file = new File(cachePath); 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]); final File deleteMe = new File(cachePath + File.separator + files[i]);
deleteMe.delete(); deleteMe.delete();
} }
// don't delete the cache directory // don't delete the cache directory
} }
} }
@Override @Override
public SyndFeedInfo remove(final URL url) { 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; SyndFeedInfo info = null;
final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim();
FileInputStream fis = null; FileInputStream fis = null;
ObjectInputStream ois = null; ObjectInputStream ois = null;
boolean consumed = false;
try { try {
fis = new FileInputStream(fileName); fis = new FileInputStream(fileName);
ois = new ObjectInputStream(fis); ois = new ObjectInputStream(fis);
info = (SyndFeedInfo) ois.readObject(); info = (SyndFeedInfo) ois.readObject();
consumed = true;
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
// That's OK, we'l return null
// feed is not cached yet
} catch (final ClassNotFoundException e) { } 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) { } 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 { } finally {
if (fis != null) {
try { IO.closeQuietly(fis);
fis.close(); IO.closeQuietly(ois);
} 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();
}
}
} }
return info; 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();
}
} }

View file

@ -28,6 +28,7 @@ import java.net.URL;
* *
*/ */
public interface FeedFetcherCache { public interface FeedFetcherCache {
/** /**
* Get a SyndFeedInfo object from the cache. * Get a SyndFeedInfo object from the cache.
* *
@ -55,4 +56,5 @@ public interface FeedFetcherCache {
* @return The removed SyndFeedInfo * @return The removed SyndFeedInfo
*/ */
public SyndFeedInfo remove(URL feedUrl); public SyndFeedInfo remove(URL feedUrl);
} }

View file

@ -38,9 +38,10 @@ import java.util.Map;
* *
*/ */
public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable { public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
static HashMapFeedInfoCache _instance; static HashMapFeedInfoCache instance;
private Map<String, SyndFeedInfo> infoCache; private Map<String, SyndFeedInfo> infoCache;
@ -65,10 +66,10 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
* @return an implementation of FeedFetcherCache * @return an implementation of FeedFetcherCache
*/ */
public static synchronized FeedFetcherCache getInstance() { public static synchronized FeedFetcherCache getInstance() {
if (_instance == null) { if (instance == null) {
_instance = new HashMapFeedInfoCache(); instance = new HashMapFeedInfoCache();
} }
return _instance; return instance;
} }
protected Map<String, SyndFeedInfo> createInfoCache() { protected Map<String, SyndFeedInfo> createInfoCache() {

View file

@ -42,6 +42,7 @@ import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.FeedException; import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader; import com.rometools.rome.io.XmlReader;
import com.rometools.utils.IO;
/** /**
* @author Nick Lothian * @author Nick Lothian
@ -279,9 +280,7 @@ public class HttpClientFeedFetcher extends AbstractFeedFetcher {
} finally { } finally {
if (stream != null) { IO.close(stream);
stream.close();
}
} }

View file

@ -30,6 +30,7 @@ import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.FeedException; import com.rometools.rome.io.FeedException;
import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader; import com.rometools.rome.io.XmlReader;
import com.rometools.utils.IO;
/** /**
* <p> * <p>
@ -170,9 +171,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher {
} catch (final java.io.IOException e) { } catch (final java.io.IOException e) {
handleErrorCodes(((HttpURLConnection) connection).getResponseCode()); handleErrorCodes(((HttpURLConnection) connection).getResponseCode());
} finally { } finally {
if (inputStream != null) { IO.close(inputStream);
inputStream.close();
}
httpConnection.disconnect(); httpConnection.disconnect();
} }
// we will never actually get to this line // we will never actually get to this line
@ -234,9 +233,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher {
syndFeedInfo.setSyndFeed(syndFeed); syndFeedInfo.setSyndFeed(syndFeed);
} finally { } finally {
if (inputStream != null) { IO.close(inputStream);
inputStream.close();
}
} }
} }
@ -249,7 +246,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher {
* @param syndFeedInfo The SyndFeedInfo for the feed to be retrieved. May be null * @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. * @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) { if (syndFeedInfo != null) {
// set the headers to get feed only if modified // set the headers to get feed only if modified
// we support the use of both last modified and eTag headers // we support the use of both last modified and eTag headers
@ -288,7 +285,7 @@ public class HttpURLFeedFetcher extends AbstractFeedFetcher {
// SyndFeedInput input = new SyndFeedInput(); // SyndFeedInput input = new SyndFeedInput();
XmlReader reader; final XmlReader reader;
if (connection.getHeaderField("Content-Type") != null) { if (connection.getHeaderField("Content-Type") != null) {
reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true); reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true);
} else { } else {