1 package net.sf.flock.impl;
2
3 import java.net.URL;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.HashMap;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11
12 import net.sf.flock.FeedFactoryI;
13 import net.sf.flock.FeedI;
14 import net.sf.flock.FilterI;
15 import net.sf.flock.FlockResourceException;
16 import net.sf.flock.SubscriptionI;
17 import net.sf.flock.SubscriptionInfoI;
18 import net.sf.flock.support.AbstractSubscriptionManager;
19
20 import org.apache.log4j.LogManager;
21 import org.apache.log4j.Logger;
22
23 /***
24 * @version $Revision: 1.4 $
25 * @author $Author: phraktle $
26 */
27 public class SimpleSubscriptionManager extends AbstractSubscriptionManager implements FeedFactoryI {
28
29 private final static Logger LOGGER = LogManager.getLogger(SimpleSubscriptionManager.class);
30
31 /*** Subscriptions hashed by location */
32 private final Map subscriptions = new HashMap();
33
34 public FeedI createFeed(SubscriptionInfoI subscriptionInfoI) {
35 return new Feed(subscriptionInfoI);
36 }
37
38 protected synchronized void addSubscription(SubscriptionI subscription) throws FlockResourceException {
39 if ( this.subscriptions.containsKey(subscription.getLocation()) ) {
40 throw new FlockResourceException("Feed "+subscription.getLocation()+" already exists in store");
41 }
42 this.subscriptions.put(subscription.getLocation(), subscription);
43 }
44
45 public SubscriptionI getSubscription(URL location) throws FlockResourceException {
46 return (SubscriptionI)this.subscriptions.get(location);
47 }
48
49 protected Collection getSubscriptions() {
50 return Collections.unmodifiableCollection(this.subscriptions.values());
51 }
52
53 public Collection getSubscriptionInfos() throws FlockResourceException {
54 return this.getSubscriptions();
55 }
56
57 public void unsubscribe(URL location) throws FlockResourceException {
58 if (this.subscriptions.remove(location)==null) {
59 throw new FlockResourceException("No subscription for location "+location);
60 }
61 }
62
63 public void refresh(URL location) throws FlockResourceException {
64 Subscription subscription = (Subscription)this.subscriptions.get(location);
65 if ( subscription==null ) {
66 throw new FlockResourceException("No subscription for location "+location);
67 }
68
69 this.refresh(subscription);
70 }
71
72 public Collection findItems(FilterI[] filters) throws FlockResourceException {
73 List items = new ArrayList();
74 for (Iterator i=this.getSubscriptions().iterator(); i.hasNext();) {
75 SubscriptionI subscription = (SubscriptionI) i.next();
76 items.addAll( subscription.getFeed().getItems() );
77 }
78
79 for (int i=0; i<filters.length; i++) {
80 items = filters[i].filter(items);
81 }
82
83 return items;
84 }
85 /***
86 * @see net.sf.flock.support.AbstractSubscriptionManager#createSubscription(net.sf.flock.SubscriptionInfoI)
87 */
88 protected SubscriptionI createSubscription(SubscriptionInfoI subscriptionInfo) throws FlockResourceException {
89 return new Subscription(subscriptionInfo.getLocation(), subscriptionInfo.getMetaData());
90 }
91
92
93 /***
94 * @see net.sf.flock.SubscriptionManagerI#subscribe(net.sf.flock.SubscriptionInfoI, boolean)
95 */
96 public SubscriptionI[] subscribe(SubscriptionInfoI subscriptionInfo, boolean load) throws FlockResourceException {
97 SubscriptionI[] subs = super.subscribe(subscriptionInfo, load);
98 for (int i=0; i<subs.length; i++){
99 this.addSubscription(subs[i]);
100 }
101 return subs;
102 }
103 /***
104 * @see net.sf.flock.support.AbstractSubscriptionManager#getFeedFactory()
105 */
106 protected FeedFactoryI getFeedFactory() {
107 return this;
108 }
109
110 }
This page was automatically generated by Maven