View Javadoc
1 package net.sf.flock.support; 2 3 import java.net.URL; 4 import java.util.ArrayList; 5 import java.util.Arrays; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import net.sf.flock.FeedFactoryI; 10 import net.sf.flock.FeedI; 11 import net.sf.flock.FeedInfoI; 12 import net.sf.flock.FlockResourceException; 13 import net.sf.flock.MetaData; 14 import net.sf.flock.SubscriptionI; 15 import net.sf.flock.SubscriptionInfoI; 16 import net.sf.flock.SubscriptionManagerI; 17 import net.sf.flock.parser.FeedLoader; 18 import net.sf.flock.parser.UnsupportedFeedFormatException; 19 import net.sf.flock.util.DocumentLoader; 20 21 import org.apache.log4j.LogManager; 22 import org.apache.log4j.Logger; 23 import org.jdom.Document; 24 25 public abstract class AbstractSubscriptionManager implements SubscriptionManagerI { 26 27 private final static Logger LOGGER = LogManager.getLogger(AbstractSubscriptionManager.class); 28 29 public SubscriptionI[] subscribe(final URL location, final MetaData metaData) throws FlockResourceException { 30 return this.subscribe(new SubscriptionInfoI() { 31 32 public URL getLocation() { 33 return location; 34 } 35 public MetaData getMetaData() { 36 return metaData; 37 } 38 39 public FeedInfoI getFeedInfo() { 40 return new FeedInfoI() { 41 public String getTitle() { 42 return "New feed"; 43 } 44 public URL getSite() { 45 return null; 46 } 47 public SubscriptionInfoI getSubscriptionInfo() { 48 return null; 49 } 50 51 }; 52 } 53 54 }, true); 55 } 56 57 protected abstract SubscriptionI createSubscription(SubscriptionInfoI subscriptionInfo) throws FlockResourceException; 58 59 /*** 60 * @return List of new ItemI objects 61 */ 62 protected List refresh(SubscriptionI subscription) throws FlockResourceException { 63 Document doc = new DocumentLoader().loadDocument(subscription.getLocation()); 64 return refresh(subscription, doc); 65 } 66 67 /*** 68 * @return List of new ItemI objects 69 */ 70 protected List refresh(SubscriptionI subscription, Document doc) throws FlockResourceException { 71 FeedLoader factory = new FeedLoader(this.getFeedFactory()); 72 FeedI feed = factory.parseFeed(subscription, doc); 73 return subscription.getFeed().merge( feed ); 74 } 75 76 protected abstract FeedFactoryI getFeedFactory(); 77 78 public SubscriptionI[] subscribe(SubscriptionInfoI si, boolean loadFeed) throws FlockResourceException { 79 80 Document doc = null; 81 try { 82 83 SubscriptionI subscription = this.createSubscription(si); 84 85 86 87 if (loadFeed) { 88 doc = new DocumentLoader().loadDocument(si.getLocation()); 89 this.refresh(subscription,doc); 90 } 91 92 return new SubscriptionI[] { subscription }; 93 94 } catch (UnsupportedFeedFormatException e) { 95 96 LOGGER.info("Unknown feed format, trying OPML..."); 97 OpmlSubscriptionImport importer = new OpmlSubscriptionImport( si.getMetaData() ); 98 99 // doc must be not null, unless there is no UnsupportedFeedFormatException, 100 // nice, isn't it ? :-) 101 102 if (!importer.isSuitable(doc)) { 103 throw new FlockResourceException("Unsupported feed format"); 104 } 105 106 LOGGER.info("Parsing as OPML"); 107 SubscriptionInfoI[] subInfos = importer.load(doc); 108 109 List subs = new ArrayList(subInfos.length); 110 for (int i=0; i<subInfos.length; i++) { 111 try { 112 subs.addAll( Arrays.asList( 113 this.subscribe(subInfos[i], false) 114 ) ); 115 } catch (FlockResourceException ex) { 116 LOGGER.warn("Unable to subscribe to "+subInfos[i].getLocation(), ex); 117 } 118 } 119 120 return (SubscriptionI[])subs.toArray( new SubscriptionI[subs.size()] ); 121 } 122 123 } 124 125 126 boolean alreadyRunningRefreshThread = false; 127 128 /*** 129 * TODO: implement proper balking 130 */ 131 public boolean refreshAll() { 132 synchronized(this) { 133 if (alreadyRunningRefreshThread) 134 return false; 135 alreadyRunningRefreshThread = true; 136 } 137 Thread t = new Thread("FlockAsyncRefresh") { 138 public void run() { 139 try { 140 doRefreshAll(); 141 } catch (FlockResourceException e) { 142 LOGGER.warn("Problem in refreshAll", e); 143 } finally { 144 alreadyRunningRefreshThread = false; 145 } 146 } 147 }; 148 t.start(); 149 return true; 150 } 151 152 protected void doRefreshAll() throws FlockResourceException { 153 for (Iterator i=this.getSubscriptionInfos().iterator(); i.hasNext(); ) { 154 SubscriptionInfoI si = (SubscriptionInfoI) i.next(); 155 SubscriptionI subscription = this.getSubscription( si.getLocation() ); 156 try { 157 refresh( subscription ); 158 } catch (FlockResourceException e) { 159 LOGGER.warn("Failed to refresh feed["+subscription.getLocation()+"], skipping", e); 160 } 161 } 162 } 163 164 165 }

This page was automatically generated by Maven