1 28 package de.nava.informa.utils.poller; 29 30 import de.nava.informa.impl.basic.ChannelBuilder; 31 import de.nava.informa.parsers.FeedParser; 32 import de.nava.informa.utils.FormatDetector; 33 import de.nava.informa.utils.InformaUtils; 34 import de.nava.informa.utils.toolkit.ChannelRecord; 35 import de.nava.informa.utils.toolkit.WorkerThread; 36 import de.nava.informa.core.ParseException; 37 import de.nava.informa.core.ChannelBuilderIF; 38 import de.nava.informa.core.ChannelIF; 39 import de.nava.informa.core.UnsupportedFormatException; 40 import de.nava.informa.core.ChannelFormat; 41 import de.nava.informa.core.ItemIF; 42 43 import java.io.IOException ; 44 import java.util.Collection ; 45 import java.net.URL ; 46 47 55 public class PollerWorkerThread extends WorkerThread { 56 private static final ChannelBuilderIF BUILDER = new ChannelBuilder(); 57 58 private PollerObserverIF observer; 59 private PollerApproverIF approver; 60 61 67 public PollerWorkerThread(PollerObserverIF observer, PollerApproverIF approver) { 68 super("Poller - Worker thread"); 69 70 this.observer = observer; 71 this.approver = approver; 72 } 73 74 79 protected final void processRecord(ChannelRecord record) { 80 final ChannelIF channel = record.getChannel(); 81 82 observer.pollStarted(channel); 84 85 try { 86 if (!record.isFormatResolved()) { 88 resolveFormat(record); 89 } 90 91 checkContents(record); 93 94 observer.pollFinished(channel); 96 97 } catch (Exception e) { 98 observer.channelErrored(channel, e); 99 } 100 } 101 102 109 private void resolveFormat(ChannelRecord record) 110 throws IOException , UnsupportedFormatException { 111 final ChannelIF channel = record.getChannel(); 113 final ChannelFormat format = FormatDetector.getFormat(channel.getLocation()); 114 115 channel.setFormat(format); 116 record.setFormatResolved(true); 117 } 118 119 126 private void checkContents(ChannelRecord record) 127 throws IOException , ParseException { 128 final ChannelIF channel = record.getChannel(); 130 final ChannelIF tempChannel = FeedParser.parse(BUILDER, channel.getLocation()); 131 132 if (channelHasChanged(channel, tempChannel)) { 134 InformaUtils.copyChannelProperties(tempChannel, channel); 135 observer.channelChanged(channel); 136 } 137 138 checkItems(tempChannel, channel); 140 } 141 142 148 final void checkItems(ChannelIF newChannel, ChannelIF existingChannel) { 149 final ItemIF[] newItems = (ItemIF[]) newChannel.getItems().toArray(new ItemIF[0]); 150 final Collection currentItems = existingChannel.getItems(); 151 152 for (int i = 0; i < newItems.length; i++) { 153 final ItemIF newItem = newItems[i]; 154 155 if (!currentItems.contains(newItem) && approver.canAddItem(newItem, existingChannel)) { 157 try { 159 observer.itemFound(newItem, existingChannel); 160 } catch (RuntimeException e) { 161 } 163 } 164 } 165 } 166 167 174 static boolean channelHasChanged(ChannelIF o, ChannelIF n) { 175 180 return o == null 181 || differ(o.getTitle(), n.getTitle()) 182 || differ(o.getDescription(), n.getDescription()) 183 || differ(o.getSite(), n.getSite()) 184 || differ(o.getCreator(), n.getCreator()) 185 || differ(o.getCopyright(), n.getCopyright()) 186 || differ(o.getPublisher(), n.getPublisher()) 187 || differ(o.getLanguage(), n.getLanguage()) 188 || differ(o.getRating(), n.getRating()) 189 || differ(o.getGenerator(), n.getGenerator()) 190 || differ(o.getDocs(), n.getDocs()) 191 || o.getTtl() != n.getTtl() 192 || differ(o.getUpdateBase(), n.getUpdateBase()) 193 || o.getUpdateFrequency() != n.getUpdateFrequency() 194 || differ(o.getUpdatePeriod(), n.getUpdatePeriod()) 195 || differ(o.getPubDate(), n.getPubDate()); 196 } 197 198 205 static boolean differ(Object a, Object b) { 206 return !((a == null && b == null) 207 || (a instanceof URL && b instanceof URL && (a.toString().equals(b.toString()))) 208 || (a != null && a.equals(b))); 209 } 210 } 211 | Popular Tags |