1 16 package org.apache.roller.util.rome; 17 18 import java.io.File ; 19 import java.io.FileInputStream ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.ObjectInputStream ; 24 import java.io.ObjectOutputStream ; 25 import java.net.URL ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.sun.syndication.fetcher.impl.FeedFetcherCache; 31 import com.sun.syndication.fetcher.impl.SyndFeedInfo; 32 33 36 public class DiskFeedInfoCache implements FeedFetcherCache 37 { 38 private static Log logger = 39 LogFactory.getFactory().getInstance(DiskFeedInfoCache.class); 40 41 protected String cachePath = null; 42 public DiskFeedInfoCache(String cachePath) 43 { 44 this.cachePath = cachePath; 45 } 46 public SyndFeedInfo getFeedInfo(URL url) 47 { 48 SyndFeedInfo info = null; 49 String fileName = cachePath + File.separator + "feed_" + url.hashCode(); 50 FileInputStream fis; 51 try 52 { 53 fis = new FileInputStream (fileName); 54 ObjectInputStream ois = new ObjectInputStream (fis); 55 info = (SyndFeedInfo)ois.readObject(); 56 fis.close(); 57 } 58 catch (FileNotFoundException fnfe) 59 { 60 logger.debug("Cache miss for " + url.toString()); 61 } 62 catch (ClassNotFoundException cnfe) 63 { 64 throw new RuntimeException ("Attempting to read from cache", cnfe); 66 } 67 catch (IOException fnfe) 68 { 69 throw new RuntimeException ("Attempting to read from cache", fnfe); 71 } 72 if (info == null) logger.info("Cache MISS!"); 73 return info; 74 } 75 76 public void setFeedInfo(URL url, SyndFeedInfo feedInfo) 77 { 78 String fileName = cachePath + File.separator + "feed_" + url.hashCode(); 79 FileOutputStream fos; 80 try 81 { 82 fos = new FileOutputStream (fileName); 83 ObjectOutputStream oos = new ObjectOutputStream (fos); 84 oos.writeObject(feedInfo); 85 fos.flush(); 86 fos.close(); 87 } 88 catch (Exception e) 89 { 90 throw new RuntimeException ("Attempting to write to cache", e); 92 } 93 } 94 } 95 | Popular Tags |