1 26 27 29 package de.nava.informa.utils; 30 31 import java.net.URL ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.Map ; 36 import java.util.Timer ; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import de.nava.informa.core.ChannelBuilderIF; 42 import de.nava.informa.core.ChannelGroupIF; 43 import de.nava.informa.core.ChannelIF; 44 45 52 public class ChannelRegistry { 53 54 private static Log logger = LogFactory.getLog(ChannelRegistry.class); 55 public static final int DEFAULT_ACCEPT_NR_ERRORS = 10; 56 57 private int acceptNrOfErrors; 58 private ChannelBuilderIF builder; 59 private ChannelGroupIF channels; 60 private Timer updateDaemon; 61 private Map updateTasks; 62 private Map channelInfos; 63 64 70 public ChannelRegistry(ChannelBuilderIF builder) { 71 this.builder = builder; 72 this.channels = builder.createChannelGroup("Default"); 73 updateDaemon = new Timer (true); 75 updateTasks = new HashMap (); 76 channelInfos = new HashMap (); 77 acceptNrOfErrors = DEFAULT_ACCEPT_NR_ERRORS; 78 } 79 80 89 public ChannelIF addChannel(URL url, int interval, boolean active) { 90 return addChannel(url, null, interval, active); 91 } 92 93 103 public ChannelIF addChannel(URL url, Collection categories, 104 int interval, boolean active) { 105 106 ChannelIF channel = builder.createChannel("[uninitialized channel]"); 107 channel.setCategories(categories); 108 channel.setLocation(url); 109 110 channel = addChannel(channel, active, interval); 111 return channel; 112 } 113 114 122 public ChannelIF addChannel(ChannelIF channel, boolean active, int interval) { 123 channels.add(channel); 124 logger.debug("added channel " + channel.getId() + " to registry"); 125 if (active) { 126 activateChannel(channel, interval); 127 } 128 return channel; 129 } 130 131 137 public void activateChannel(ChannelIF channel, int interval) { 138 if (updateTasks.get(channel.getLocation()) == null) { 142 UpdateChannelInfo info = (UpdateChannelInfo) 144 channelInfos.get(channel.getLocation()); 145 if (info == null) { 146 info = new UpdateChannelInfo(acceptNrOfErrors); 147 info.setFormatDetected(false); 148 channelInfos.put(channel.getLocation(), info); 149 } else { 150 info.reset(); 151 } 152 UpdateChannelTask task = new UpdateChannelTask(this, builder, 154 channel, info); 155 updateDaemon.schedule(task, 100, interval * 1000); 158 logger.info("activating channel updates for " + channel.getTitle()); 159 updateTasks.put(channel.getLocation(), task); 160 } 164 } 165 166 public ChannelIF getChannel(long id) { 167 return channels.getById(id); 168 } 169 170 175 public Collection getChannels() { 176 return channels.getAll(); 177 } 178 179 public ChannelGroupIF getChannelGroup() { 180 return channels; 181 } 182 183 public void setChannelGroup(ChannelGroupIF channels) { 184 this.channels = channels; 185 Iterator it = channels.getAll().iterator(); 187 while (it.hasNext()) { 188 ChannelIF channel = (ChannelIF) it.next(); 189 } 195 } 196 197 202 public void removeChannel(ChannelIF channel) { 203 deactivateChannel(channel); 204 channels.remove(channel); 205 logger.debug("removing channel from registry: " + channel.getTitle()); 206 } 207 208 211 public void deactivateChannel(ChannelIF channel) { 212 UpdateChannelTask task = (UpdateChannelTask) 213 updateTasks.get(channel.getLocation()); 214 if (task != null) { 215 logger.debug("update task canceled for " + channel.getTitle()); 216 task.cancel(); 217 updateTasks.remove(channel.getLocation()); 218 } 221 } 222 223 226 public boolean isActiveChannel(ChannelIF channel) { 227 UpdateChannelTask task = (UpdateChannelTask) 228 updateTasks.get(channel.getLocation()); 229 return (task != null); 230 } 231 232 public UpdateChannelInfo getUpdateInfo(ChannelIF channel) { 233 return (UpdateChannelInfo) channelInfos.get(channel.getLocation()); 234 } 235 236 242 public long getScheduledUpdateTime(ChannelIF channel) { 243 UpdateChannelTask task = (UpdateChannelTask) 244 updateTasks.get(channel.getLocation()); 245 if (task != null) { 246 return task.scheduledExecutionTime(); 247 } 248 return 0; 249 } 250 251 public int getAcceptNrOfErrors() { 252 return acceptNrOfErrors; 253 } 254 255 259 public void setAcceptNrOfErrors(int acceptNrOfErrors) { 260 this.acceptNrOfErrors = acceptNrOfErrors; 261 } 262 263 } 264 | Popular Tags |