Coverage Report - yarfraw.utils.reader.FeedReaderUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FeedReaderUtils
87% 
100% 
0
 
 1  
 package yarfraw.utils.reader;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.util.ArrayList;
 6  
 import java.util.List;
 7  
 import java.util.concurrent.Callable;
 8  
 import java.util.concurrent.ExecutionException;
 9  
 import java.util.concurrent.ExecutorService;
 10  
 import java.util.concurrent.Future;
 11  
 
 12  
 import org.apache.commons.httpclient.HttpURL;
 13  
 import org.apache.commons.lang.ArrayUtils;
 14  
 import org.apache.commons.logging.Log;
 15  
 import org.apache.commons.logging.LogFactory;
 16  
 
 17  
 import yarfraw.core.datamodel.ChannelFeed;
 18  
 import yarfraw.core.datamodel.FeedFormat;
 19  
 import yarfraw.core.datamodel.YarfrawException;
 20  
 import yarfraw.io.FeedReader;
 21  
 
 22  
 public class FeedReaderUtils{
 23  0
   private FeedReaderUtils(){}
 24  6
   private static final Log LOG = LogFactory.getLog(FeedReaderUtils.class);
 25  144
   private static class FeedReaderCaller implements Callable<ChannelFeed>{
 26  
     private HttpURL _url;
 27  144
     public FeedReaderCaller(HttpURL url){
 28  144
       _url = url;
 29  144
     }
 30  
     public ChannelFeed call() throws YarfrawException, IOException {
 31  144
       return new FeedReader(_url).readChannel();
 32  
     }
 33  
   }
 34  
   
 35  
   /**
 36  
    *  Static method for reading feed(s) remotely. This method will submit a Callable object to the input
 37  
    *  ExecutorService for every input url and will only return when it finishes reading all requested feeds.
 38  
    *  The ChannelFeeds in the returned list will in the exact same order as the input url. 
 39  
    *  If for whatever reasons the method fails to read a feed, the corresponding ChannelFeed in the list will be null. 
 40  
    *  <br/>
 41  
    *  This method will detect the formats automatically, but since it does not remember any states, 
 42  
    *  it has to performance format detection every time it is called.
 43  
    *  It is recommended that you keep an instance of the reader in memory so the reader can re-use the detected
 44  
    *  formats.
 45  
    * @param files - {@link File}s pointing to Rss feed files. 
 46  
    * @param executorService - @see {@link ExecutorService}
 47  
    * @param urls - @see {@link HttpURL}
 48  
    * @return - a list of {@link ChannelFeed}
 49  
    * @throws YarfrawException - If there is a failure reading any of the feeds.
 50  
    */
 51  
   public static List<ChannelFeed> readAll(ExecutorService executorService, HttpURL... urls) 
 52  
   throws YarfrawException{
 53  3
     List<ChannelFeed> ret = new ArrayList<ChannelFeed>();
 54  3
     List<Future<ChannelFeed>> futures = new ArrayList<Future<ChannelFeed>>(); 
 55  3
     if(!ArrayUtils.isEmpty(urls)){
 56  147
       for(final HttpURL url : urls){
 57  144
         futures.add(executorService.submit(new FeedReaderCaller(url)));
 58  
       }
 59  
     }
 60  3
     for(Future<ChannelFeed> f : futures){
 61  
       try {
 62  144
         ret.add(f.get());
 63  
       }
 64  0
       catch (InterruptedException e) {
 65  0
         LOG.error("Interrupted exception received", e);
 66  0
         ret.add(null);
 67  
       }
 68  3
       catch (ExecutionException e) {
 69  3
         LOG.error("Execution exception received", e);
 70  3
         ret.add(null);
 71  285
       }
 72  
     }
 73  3
     return ret;
 74  
   }
 75  
   
 76  
   /**
 77  
    * Read all Rss feed and return them in a list that is in the same order.
 78  
    * 
 79  
    * @param files - {@link File}s pointing to Rss feed files.
 80  
    * @param format - {@link FeedFormat}
 81  
    * @return - a list of {@link ChannelFeed} 
 82  
    * @throws YarfrawException - If there is a failure reading any of the feeds.
 83  
    */
 84  
   public static List<ChannelFeed> readAll(FeedFormat format, File... files) throws YarfrawException{
 85  9
     List<ChannelFeed> ret = new ArrayList<ChannelFeed>();
 86  9
     if(!ArrayUtils.isEmpty(files)){
 87  24
       for(File f : files){
 88  15
         FeedReader reader = new FeedReader(f, format);
 89  15
         ret.add(reader.readChannel());
 90  
       }
 91  
     }
 92  9
     return ret;
 93  
   }
 94  
   
 95  
   /**
 96  
    * Read a Rss feed in to a {@link ChannelFeed} data object.
 97  
    * 
 98  
    * @param file - {@link File} pointing to a Rss feed file.
 99  
    * @param format - {@link FeedFormat}
 100  
    * @return - A {@link ChannelFeed} data object representation of the feed.
 101  
    * @throws YarfrawException - If there is a failure reading the feeds.
 102  
    */
 103  
   public static ChannelFeed read(FeedFormat format, File file) throws YarfrawException{
 104  6
     List<ChannelFeed> ret = readAll(format, file);
 105  6
     return ret.size() == 0 ? null : ret.get(0);
 106  
   }
 107  
 }