Updated rome-fetcher to use generics

Removed warnings
This commit is contained in:
Patrick Gotthard 2013-10-12 15:22:53 +02:00
parent 12fa5bbab6
commit 0dde9fdb9e
10 changed files with 64 additions and 37 deletions

View file

@ -34,13 +34,13 @@ import org.rometools.fetcher.FetcherListener;
import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.feed.synd.SyndFeed;
public abstract class AbstractFeedFetcher implements FeedFetcher { public abstract class AbstractFeedFetcher implements FeedFetcher {
private final Set fetcherEventListeners; private final Set<FetcherListener> fetcherEventListeners;
private String userAgent; private String userAgent;
private boolean usingDeltaEncoding; private boolean usingDeltaEncoding;
private boolean preserveWireFeed; private boolean preserveWireFeed;
public AbstractFeedFetcher() { public AbstractFeedFetcher() {
fetcherEventListeners = Collections.synchronizedSet(new HashSet()); fetcherEventListeners = Collections.synchronizedSet(new HashSet<FetcherListener>());
final Properties props = new Properties(System.getProperties()); final Properties props = new Properties(System.getProperties());
final String resourceName = "fetcher.properties"; 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) { protected void fireEvent(final String eventType, final String urlStr, final SyndFeed feed) {
final FetcherEvent fetcherEvent = new FetcherEvent(this, urlStr, eventType, feed); final FetcherEvent fetcherEvent = new FetcherEvent(this, urlStr, eventType, feed);
synchronized (fetcherEventListeners) { synchronized (fetcherEventListeners) {
final Iterator iter = fetcherEventListeners.iterator(); final Iterator<FetcherListener> iter = fetcherEventListeners.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
final FetcherListener fetcherEventListener = (FetcherListener) iter.next(); final FetcherListener fetcherEventListener = iter.next();
fetcherEventListener.fetcherEvent(fetcherEvent); fetcherEventListener.fetcherEvent(fetcherEvent);
} }
} }

View file

@ -12,14 +12,12 @@ public class AbstractFeedFetcherBeanInfo extends SimpleBeanInfo {
@Override @Override
public EventSetDescriptor[] getEventSetDescriptors() { public EventSetDescriptor[] getEventSetDescriptors() {
try { try {
final Class clz = AbstractFeedFetcher.class; // get the class object which we'll describe final Class<AbstractFeedFetcher> clz = AbstractFeedFetcher.class; // get the class object which we'll describe
final Method addMethod = clz.getMethod("addFetcherEventListener", new Class[] { FetcherListener.class }); final Method addMethod = clz.getMethod("addFetcherEventListener", new Class[] { FetcherListener.class });
final Method removeMethod = clz.getMethod("removeFetcherEventListener", 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 Method listenerMethod = FetcherListener.class.getMethod("fetcherEvent", new Class[] { FetcherEvent.class });
final EventSetDescriptor est = new EventSetDescriptor("fetcherEvent", clz, new Method[] { listenerMethod }, addMethod, removeMethod); final EventSetDescriptor est = new EventSetDescriptor("fetcherEvent", clz, new Method[] { listenerMethod }, addMethod, removeMethod);
final EventSetDescriptor[] results = new EventSetDescriptor[] { est }; final EventSetDescriptor[] results = new EventSetDescriptor[] { est };
return results; return results;
} catch (final Exception e) { } catch (final Exception e) {
// IntrospectionException, SecurityException and/or NoSuchMethodException can be thrown here // IntrospectionException, SecurityException and/or NoSuchMethodException can be thrown here
@ -27,4 +25,5 @@ public class AbstractFeedFetcherBeanInfo extends SimpleBeanInfo {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
} }

View file

@ -39,20 +39,33 @@ public class DiskFeedInfoCache implements FeedFetcherCache {
public SyndFeedInfo getFeedInfo(final URL url) { public SyndFeedInfo getFeedInfo(final URL url) {
SyndFeedInfo info = null; SyndFeedInfo info = null;
final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim();
FileInputStream fis; FileInputStream fis = null;
ObjectInputStream ois = null;
try { try {
fis = new FileInputStream(fileName); fis = new FileInputStream(fileName);
final ObjectInputStream ois = new ObjectInputStream(fis); ois = new ObjectInputStream(fis);
info = (SyndFeedInfo) ois.readObject(); info = (SyndFeedInfo) ois.readObject();
fis.close(); } catch (final FileNotFoundException e) {
} catch (final FileNotFoundException fnfe) {
// That's OK, we'l return null // That's OK, we'l return null
} catch (final ClassNotFoundException cnfe) { } catch (final ClassNotFoundException e) {
// Error writing to cache is fatal // Error writing to cache is fatal
throw new RuntimeException("Attempting to read from cache", cnfe); throw new RuntimeException("Attempting to read from cache", e);
} catch (final IOException fnfe) { } catch (final IOException e) {
// Error writing to cache is fatal // 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; return info;
} }
@ -110,12 +123,12 @@ public class DiskFeedInfoCache implements FeedFetcherCache {
public SyndFeedInfo remove(final URL url) { public SyndFeedInfo remove(final URL url) {
SyndFeedInfo info = null; SyndFeedInfo info = null;
final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim(); final String fileName = cachePath + File.separator + "feed_" + replaceNonAlphanumeric(url.toString(), '_').trim();
FileInputStream fis; FileInputStream fis = null;
ObjectInputStream ois = null;
try { try {
fis = new FileInputStream(fileName); fis = new FileInputStream(fileName);
final ObjectInputStream ois = new ObjectInputStream(fis); ois = new ObjectInputStream(fis);
info = (SyndFeedInfo) ois.readObject(); info = (SyndFeedInfo) ois.readObject();
fis.close();
final File file = new File(fileName); final File file = new File(fileName);
if (file.exists()) { if (file.exists()) {
@ -129,6 +142,19 @@ public class DiskFeedInfoCache implements FeedFetcherCache {
} catch (final IOException fnfe) { } catch (final IOException fnfe) {
// Error writing to cahce is fatal // Error writing to cahce is fatal
throw new RuntimeException("Attempting to read from cache", fnfe); 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; return info;
} }

View file

@ -40,7 +40,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
static HashMapFeedInfoCache _instance; static HashMapFeedInfoCache _instance;
private Map infoCache; private Map<String, SyndFeedInfo> infoCache;
/** /**
* <p> * <p>
@ -68,8 +68,8 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
return _instance; return _instance;
} }
protected Map createInfoCache() { protected Map<String, SyndFeedInfo> createInfoCache() {
return Collections.synchronizedMap(new HashMap()); return Collections.synchronizedMap(new HashMap<String, SyndFeedInfo>());
} }
protected Object get(final Object key) { protected Object get(final Object key) {
@ -84,7 +84,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
return (SyndFeedInfo) get(feedUrl.toString()); 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); getInfoCache().put(key, value);
} }
@ -96,7 +96,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
put(feedUrl.toString(), syndFeedInfo); put(feedUrl.toString(), syndFeedInfo);
} }
protected synchronized final Map getInfoCache() { protected synchronized final Map<String, SyndFeedInfo> getInfoCache() {
return infoCache; return infoCache;
} }
@ -105,7 +105,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
* *
* @param map the map to use as the info cache. * @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<String, SyndFeedInfo> map) {
infoCache = map; infoCache = map;
} }
@ -128,7 +128,7 @@ public class HashMapFeedInfoCache implements FeedFetcherCache, Serializable {
return null; return null;
} }
return (SyndFeedInfo) infoCache.remove(url.toString()); return infoCache.remove(url.toString());
} }
} }

View file

@ -42,13 +42,13 @@ import com.sun.syndication.io.XmlReader;
* @author Nick Lothian * @author Nick Lothian
*/ */
public class HttpClientFeedFetcher extends AbstractFeedFetcher { public class HttpClientFeedFetcher extends AbstractFeedFetcher {
private CredentialSupplier credentialSupplier; private CredentialSupplier credentialSupplier;
private FeedFetcherCache feedInfoCache; private FeedFetcherCache feedInfoCache;
private volatile HttpClientMethodCallbackIntf httpClientMethodCallback; private volatile HttpClientMethodCallbackIntf httpClientMethodCallback;
private volatile HttpClientParams httpClientParams; private volatile HttpClientParams httpClientParams;
public HttpClientFeedFetcher() { public HttpClientFeedFetcher() {
super();
setHttpClientParams(new HttpClientParams()); setHttpClientParams(new HttpClientParams());
} }

View file

@ -18,7 +18,9 @@ import java.util.Map;
* *
*/ */
public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache { public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache {
private final class CacheImpl extends LinkedHashMap {
private final class CacheImpl extends LinkedHashMap<String, SyndFeedInfo> {
private static final long serialVersionUID = -6977191330127794920L; private static final long serialVersionUID = -6977191330127794920L;
public CacheImpl() { public CacheImpl() {
@ -26,19 +28,18 @@ public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache {
} }
@Override @Override
protected boolean removeEldestEntry(final Map.Entry eldest) { protected boolean removeEldestEntry(final Map.Entry<String, SyndFeedInfo> eldest) {
return size() > getMaxEntries(); return size() > getMaxEntries();
} }
} }
private static final int DEFAULT_MAX_ENTRIES = 20; private static final int DEFAULT_MAX_ENTRIES = 20;
private static final long serialVersionUID = 1694228973357997417L; private static final long serialVersionUID = 1694228973357997417L;
private static final LinkedHashMapFeedInfoCache _instance = new LinkedHashMapFeedInfoCache();
private int maxEntries = DEFAULT_MAX_ENTRIES; private int maxEntries = DEFAULT_MAX_ENTRIES;
private final static LinkedHashMapFeedInfoCache _instance = new LinkedHashMapFeedInfoCache();
/** /**
* Get the global instance of the cache * Get the global instance of the cache
* *
@ -64,7 +65,7 @@ public class LinkedHashMapFeedInfoCache extends HashMapFeedInfoCache {
} }
@Override @Override
protected Map createInfoCache() { protected Map<String, SyndFeedInfo> createInfoCache() {
return Collections.synchronizedMap(new CacheImpl()); return Collections.synchronizedMap(new CacheImpl());
} }

View file

@ -26,6 +26,7 @@ import org.rometools.fetcher.impl.FeedFetcherCache;
import org.rometools.fetcher.impl.HashMapFeedInfoCache; import org.rometools.fetcher.impl.HashMapFeedInfoCache;
import org.rometools.fetcher.impl.HttpURLFeedFetcher; 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.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl; import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.io.SyndFeedOutput; import com.sun.syndication.io.SyndFeedOutput;
@ -59,7 +60,7 @@ public class FeedAggregator {
feed.setAuthor("anonymous"); feed.setAuthor("anonymous");
feed.setLink("http://www.anonymous.com"); feed.setLink("http://www.anonymous.com");
final List entries = new ArrayList(); final List<SyndEntry> entries = new ArrayList<SyndEntry>();
feed.setEntries(entries); feed.setEntries(entries);
final FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance(); final FeedFetcherCache feedInfoCache = HashMapFeedInfoCache.getInstance();

View file

@ -61,7 +61,7 @@ public class FeedReader {
// and the server supports conditional gets, we will get a "Feed // and the server supports conditional gets, we will get a "Feed
// Unchanged" event after the Feed Polled event // Unchanged" event after the Feed Polled event
System.err.println("Polling " + feedUrl + " again to test conditional get support."); 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."); System.err.println("If a \"Feed Unchanged\" event fired then the server supports conditional gets.");
ok = true; ok = true;

View file

@ -236,7 +236,7 @@ public abstract class AbstractJettyTest extends TestCase {
public void testErrorHandling() { public void testErrorHandling() {
final FeedFetcher feedFetcher = getFeedFetcher(); final FeedFetcher feedFetcher = getFeedFetcher();
try { 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"); fail("4xx error handling did not work correctly");
} catch (final FetcherException e) { } catch (final FetcherException e) {
// expect this exception // expect this exception
@ -247,7 +247,7 @@ public abstract class AbstractJettyTest extends TestCase {
} }
try { 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"); fail("5xx error handling did not work correctly");
} catch (final FetcherException e) { } catch (final FetcherException e) {
// expect this exception // expect this exception

View file

@ -177,7 +177,7 @@ public class FetcherTestServlet extends HttpServlet {
feed.setLink("http://rome.dev.java.net"); feed.setLink("http://rome.dev.java.net");
feed.setDescription("This tests using rfc3229 delta encoding."); feed.setDescription("This tests using rfc3229 delta encoding.");
final List entries = new ArrayList(); final List<SyndEntry> entries = new ArrayList<SyndEntry>();
SyndEntry entry; SyndEntry entry;
SyndContent description; SyndContent description;