| 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 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 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 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 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 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 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 |
|
} |