1 25 27 package de.nava.informa.utils; 28 29 import de.nava.informa.core.ChannelBuilderIF; 30 import de.nava.informa.core.ChannelIF; 31 import de.nava.informa.core.FeedIF; 32 import de.nava.informa.core.ParseException; 33 import de.nava.informa.parsers.OPMLParser; 34 35 import java.io.IOException ; 36 37 import java.util.ArrayList ; 38 import java.util.Collection ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 56 public class FeedManager { 57 58 59 private static final ChannelBuilderIF DEFAULT_BUILDER = new de.nava.informa.impl.basic.ChannelBuilder(); 60 61 private ChannelBuilderIF channelBuilder; 62 63 private String defaultUpdatePeriod; 64 65 private int defaultUpdateFrequency; 66 67 68 private Map feeds; 69 70 71 private CacheSettingsIF cacheSettings = new CacheSettings(); 72 73 74 private FeedRefreshDaemon refreshDaemon = new FeedRefreshDaemon(); 75 76 79 public FeedManager() { 80 feeds = new HashMap (); 81 this.defaultUpdatePeriod = ChannelIF.UPDATE_DAILY; 82 this.defaultUpdateFrequency = 1; 83 setCacheSettings(defaultUpdatePeriod, defaultUpdateFrequency); 84 } 85 86 92 public FeedManager(String defaultUpdatePeriod, int defaultUpdateFrequency) { 93 feeds = new HashMap (); 94 this.defaultUpdatePeriod = defaultUpdatePeriod; 95 this.defaultUpdateFrequency = defaultUpdateFrequency; 96 setCacheSettings(defaultUpdatePeriod, defaultUpdateFrequency); 97 } 98 99 102 private void setCacheSettings(String updatePeriod, int updateFrequency) { 103 long MILLISECONDS_IN_HOUR = 3600000L; 105 long MILLISECONDS_IN_DAY = 86400000L; 106 long MILLISECONDS_IN_MONTH = 2419200000L; 107 long MILLISECONDS_IN_YEAR = 31536000000L; 108 109 long msInPeriod = 0L; 110 111 if (updatePeriod.equals(ChannelIF.UPDATE_HOURLY)) { 112 msInPeriod = MILLISECONDS_IN_HOUR; 113 } else if (updatePeriod.equals(ChannelIF.UPDATE_DAILY)) { 114 msInPeriod = MILLISECONDS_IN_DAY; 115 } else if (updatePeriod.equals(ChannelIF.UPDATE_MONTHLY)) { 116 msInPeriod = MILLISECONDS_IN_MONTH; 117 } else if (updatePeriod.equals(ChannelIF.UPDATE_YEARLY)) { 118 msInPeriod = MILLISECONDS_IN_YEAR; 119 } else { 120 throw new IllegalArgumentException ("updatePeriod " + updatePeriod 121 + " is not valid"); 122 } 123 this.cacheSettings.setDefaultTtl(msInPeriod / updateFrequency); 124 } 125 126 135 public FeedIF addFeed(FeedIF feed) throws FeedManagerException { 136 return addFeed(feed.getLocation().toString()); 137 } 138 139 150 public FeedIF addFeed(FeedIF feed, int ttl) throws FeedManagerException { 151 return addFeed(feed.getLocation().toString(), ttl); 152 } 153 154 164 public Collection addFeeds(String opmlFeedUri) throws FeedManagerException { 165 Collection retFeeds = null; 166 167 try { 168 Collection feedsColl = OPMLParser.parse(opmlFeedUri); 169 retFeeds = new ArrayList (); 170 171 for (Iterator iter = feedsColl.iterator(); iter.hasNext();) { 172 FeedIF element = (FeedIF) iter.next(); 173 retFeeds.add(addFeed(element)); 174 } 175 } catch (IOException e) { 176 throw new FeedManagerException(e); 177 } catch (ParseException e) { 178 throw new FeedManagerException(e); 179 } 180 181 return retFeeds; 182 } 183 184 196 public Collection addFeeds(String opmlFeedUri, int ttl) 197 throws FeedManagerException { 198 Collection retFeeds = null; 199 200 try { 201 Collection feedsColl = OPMLParser.parse(opmlFeedUri); 202 retFeeds = new ArrayList (); 203 204 for (Iterator iter = feedsColl.iterator(); iter.hasNext();) { 205 FeedIF element = (FeedIF) iter.next(); 206 retFeeds.add(addFeed(element, ttl)); 207 } 208 } catch (IOException e) { 209 throw new FeedManagerException(e); 210 } catch (ParseException e) { 211 throw new FeedManagerException(e); 212 } 213 214 return retFeeds; 215 } 216 217 222 public void setChannelBuilder(ChannelBuilderIF chBuilder) { 223 channelBuilder = chBuilder; 224 } 225 226 231 public ChannelBuilderIF getChannelBuilder() { 232 if (channelBuilder == null) { 233 return DEFAULT_BUILDER; 234 } else { 235 return channelBuilder; 236 } 237 } 238 239 246 public boolean hasFeed(String feedUri) { 247 return feeds.containsKey(feedUri); 248 } 249 250 260 public FeedIF addFeed(String feedUri) throws FeedManagerException { 261 if (!hasFeed(feedUri)) { 262 FeedManagerEntry fme = new FeedManagerEntry(feedUri, getChannelBuilder(), 263 defaultUpdatePeriod, defaultUpdateFrequency); 264 feeds.put(feedUri, fme); 265 refreshDaemon.addFeed(fme); 266 } 267 268 return getFeed(feedUri); 269 } 270 271 287 public FeedIF addFeed(String feedUri, int wantedTtlmins) 288 throws FeedManagerException { 289 if (!hasFeed(feedUri)) { 290 FeedManagerEntry FMEntry = new FeedManagerEntry(feedUri, 291 getChannelBuilder(), defaultUpdatePeriod, defaultUpdateFrequency); 292 293 if (wantedTtlmins > 0) { 294 FMEntry.setWantedTtl(wantedTtlmins * (60 * 1000)); 295 } 296 feeds.put(feedUri, FMEntry); 297 refreshDaemon.addFeed(FMEntry); 298 } else { 299 } 301 302 return getFeed(feedUri); 303 } 304 305 311 public void removeFeed(String feedUri) { 312 feeds.remove(feedUri); 313 } 314 315 326 public FeedIF getFeed(String feedUri) throws FeedManagerException { 327 FeedIF feed = null; 328 329 if (hasFeed(feedUri)) { 330 FeedManagerEntry entry = (FeedManagerEntry) feeds.get(feedUri); 331 feed = entry.getFeed(); 332 } 333 334 return feed; 335 } 336 337 } | Popular Tags |