1 26 28 package de.nava.informa.utils; 29 30 import java.io.IOException ; 31 import java.util.Date ; 32 import java.util.Iterator ; 33 import java.util.TimerTask ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import de.nava.informa.core.*; 39 import de.nava.informa.core.ChannelBuilderIF; 40 import de.nava.informa.impl.basic.ChannelBuilder; 41 import de.nava.informa.core.ChannelFormat; 42 import de.nava.informa.core.ChannelIF; 43 import de.nava.informa.core.ItemIF; 44 import de.nava.informa.core.ParseException; 45 import de.nava.informa.core.UnsupportedFormatException; 46 import de.nava.informa.parsers.FeedParser; 47 48 56 public class UpdateChannelTask extends TimerTask { 57 58 private static Log logger = LogFactory.getLog(UpdateChannelTask.class); 59 60 private ChannelRegistry registry; 61 private ChannelIF channel; 62 private ChannelBuilderIF builder; 63 private UpdateChannelInfo info; 64 private ChannelBuilderIF tempBuilder; 65 66 public UpdateChannelTask( 67 ChannelRegistry registry, 68 ChannelBuilderIF builder, 69 ChannelIF channel, 70 UpdateChannelInfo info) { 71 this.registry = registry; 72 this.channel = channel; 73 this.builder = builder; 74 this.info = info; 75 76 tempBuilder = new ChannelBuilder(); 79 } 80 81 public void run() { 82 logger.info("Task Run()"); 83 Thread.currentThread().setName("Informa Update Channel Task"); 84 89 synchronized (builder) { 90 if (!info.getFormatDetected()) 91 96 { 97 try { 98 builder.beginTransaction(); 99 ChannelFormat format = 100 FormatDetector.getFormat(channel.getLocation()); 101 channel.setFormat(format); 102 info.setFormatDetected(true); 103 channel.setLastUpdated(new Date ()); 104 builder.endTransaction(); 105 } catch (UnsupportedFormatException ex) { 106 logger.info("Unsupported format for Channel"); 107 incrementProblems(ex); 108 return; 109 } catch (IOException ioe) { 110 logger.info("Cannot retrieve Channel"); 111 incrementProblems(ioe); 112 return; 113 } catch (ChannelBuilderException e) { 114 e.printStackTrace(); 115 } 116 } 117 try { 118 synchronized (channel) { 119 builder.beginTransaction(); 120 ChannelIF tempChannel = 121 FeedParser.parse(tempBuilder, channel.getLocation()); 122 logger.info( 123 "Updating channel from " 124 + channel.getLocation() 125 + ": " 126 + tempChannel 127 + "(new) " 128 + channel 129 + "(old)"); 130 InformaUtils.copyChannelProperties(tempChannel, channel); 131 builder.update(channel); 132 channel.setLastUpdated(new Date ()); 133 if (tempChannel.getItems().isEmpty()) { 135 logger.warn("No items found in channel " + channel); 136 } else { 137 Iterator it = tempChannel.getItems().iterator(); 138 while (it.hasNext()) { 139 ItemIF item = (ItemIF) it.next(); 140 if (!channel.getItems().contains(item)) { 141 logger.debug("Found new item: " + item); 142 channel.addItem(builder.createItem(null, item)); 143 } 145 } } 147 builder.endTransaction(); 148 } 149 } catch (ParseException pe) { 150 incrementProblems(pe); 151 } catch (IOException ioe) { 152 incrementProblems(ioe); 153 } catch (ChannelBuilderException e) { 154 e.printStackTrace(); 155 } 156 } 157 } 158 159 163 private void incrementProblems(Exception e) { 164 info.increaseProblemsOccurred(e); 165 if (info.shouldDeactivate()) { 166 logger.warn( 167 "Deactivating channel after " 168 + info.getNrProblemsOccurred() 169 + " problems occurred."); 170 registry.deactivateChannel(channel); 171 } 172 logger.warn(e); 173 } 174 175 } 176 | Popular Tags |