KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > planet > SyncWebsitesTask


1 package org.roller.presentation.planet;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.TimerTask JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.roller.RollerException;
13 import org.roller.model.PlanetManager;
14 import org.roller.model.Roller;
15 import org.roller.model.RollerFactory;
16 import org.roller.model.ScheduledTask;
17 import org.roller.model.UserManager;
18 import org.roller.pojos.PlanetConfigData;
19 import org.roller.pojos.PlanetGroupData;
20 import org.roller.pojos.PlanetSubscriptionData;
21 import org.roller.pojos.UserData;
22 import org.roller.pojos.WebsiteData;
23 import org.roller.util.Technorati;
24
25 /**
26  * Ensure that every user is represented by a subscription in Planet Roller
27  * database. Also "ranks" each subscription by populating Technorati inbound
28  * blogs and links counts.
29  * @author Dave Johnson
30  */

31 public class SyncWebsitesTask extends TimerTask JavaDoc implements ScheduledTask
32 {
33     private static Log logger =
34         LogFactory.getFactory().getInstance(SyncWebsitesTask.class);
35     private Roller roller = null;
36  
37     /** Task may be run from the command line */
38     public static void main(String JavaDoc[] args) throws Exception JavaDoc
39     {
40         RollerFactory.setRoller(
41             "org.roller.business.hibernate.HibernateRollerImpl");
42         SyncWebsitesTask task = new SyncWebsitesTask();
43         task.init(RollerFactory.getRoller(), "dummy");
44         task.run();
45     }
46     public void init(Roller roller, String JavaDoc realPath) throws RollerException
47     {
48         this.roller = roller;
49     }
50     public void run()
51     {
52         syncWebsites();
53         rankSubscriptions();
54     }
55     /**
56      * Ensure there's a subscription in the "all" group for every Roller user.
57      */

58     private void syncWebsites()
59     {
60         try
61         {
62             List JavaDoc liveUserFeeds = new ArrayList JavaDoc();
63             String JavaDoc baseURL =
64                 roller.getConfigManager().getRollerConfig().getAbsoluteURL();
65             if (baseURL == null || baseURL.trim().length()==0)
66             {
67                 logger.error("ERROR: cannot sync websites with Planet Roller - "
68                             +"absolute URL not specified in Roller Config");
69             }
70             else
71             {
72                 roller.begin();
73                 PlanetManager planet = roller.getPlanetManager();
74                 UserManager userManager = roller.getUserManager();
75                 PlanetGroupData group = planet.getGroup("all");
76                 if (group == null)
77                 {
78                     group = new PlanetGroupData();
79                     group.setHandle("all");
80                     group.setTitle("all");
81                     planet.saveGroup(group);
82                     roller.commit();
83                 }
84                 try
85                 {
86                     String JavaDoc baseFeedURL = baseURL + "/rss/";
87                     String JavaDoc baseSiteURL = baseURL + "/page/";
88                     Iterator JavaDoc users = roller.getUserManager().getUsers().iterator();
89                     while (users.hasNext())
90                     {
91                         UserData user = (UserData) users.next();
92                         
93                         StringBuffer JavaDoc sitesb = new StringBuffer JavaDoc();
94                         sitesb.append(baseSiteURL);
95                         sitesb.append(user.getUserName());
96                         String JavaDoc siteUrl = sitesb.toString();
97                         
98                         StringBuffer JavaDoc feedsb = new StringBuffer JavaDoc();
99                         feedsb.append(baseFeedURL);
100                         feedsb.append(user.getUserName());
101                         String JavaDoc feedUrl = feedsb.toString();
102                         
103                         liveUserFeeds.add(feedUrl);
104                         
105                         PlanetSubscriptionData sub =
106                                 planet.getSubscription(feedUrl);
107                         WebsiteData website =
108                                 userManager.getWebsite(user.getUserName());
109                         if (sub == null)
110                         {
111                             logger.info("ADDING feed: "+feedUrl);
112                             sub = new PlanetSubscriptionData();
113                             sub.setTitle(website.getName());
114                             sub.setFeedUrl(feedUrl);
115                             sub.setSiteUrl(siteUrl);
116                             sub.setAuthor(user.getUserName());
117                             planet.saveSubscription(sub);
118                             group.addSubscription(sub);
119                         }
120                         else
121                         {
122                             sub.setTitle(website.getName());
123                             sub.setAuthor(user.getUserName());
124                             planet.saveSubscription(sub);
125                         }
126                     }
127                     planet.saveGroup(group);
128                     roller.commit();
129                     roller.release();
130                     
131                     roller.begin();
132                     group = group = planet.getGroup("all");
133                     Iterator JavaDoc subs = group.getSubscriptions().iterator();
134                     while (subs.hasNext())
135                     {
136                         PlanetSubscriptionData sub =
137                                 (PlanetSubscriptionData)subs.next();
138                         if (!liveUserFeeds.contains(sub.getFeedUrl()))
139                         {
140                             logger.info("DELETING feed: "+sub.getFeedUrl());
141                             planet.deleteSubscription(sub);
142                         }
143                     }
144                     roller.commit();
145                 }
146                 finally
147                 {
148                     roller.release();
149                 }
150             }
151         }
152         catch (RollerException e)
153         {
154             logger.error("ERROR refreshing entries", e);
155         }
156     }
157     
158     /**
159      * Loop through all subscriptions get get Technorati rankings for each
160      */

161     private void rankSubscriptions()
162     {
163         int count = 0;
164         int errorCount = 0;
165         try
166         {
167             roller.begin();
168             PlanetManager planet = roller.getPlanetManager();
169             PlanetConfigData config = planet.getConfiguration();
170             Technorati technorati = null;
171             if (config.getProxyHost()!=null && config.getProxyPort() != -1)
172             {
173                 technorati = new Technorati(
174                         config.getProxyHost(), config.getProxyPort());
175             }
176             else
177             {
178                 technorati = new Technorati();
179             }
180             UserManager userManager = roller.getUserManager();
181             try
182             {
183                 // Technorati API allows only 500 queries per-day
184
int limit = 500;
185                 int userCount = planet.getSubscriptionCount();
186                 int mod = (userCount / limit) + 1;
187                 
188                 Calendar JavaDoc cal = Calendar.getInstance();
189                 cal.setTime(new Date JavaDoc());
190                 int day = cal.get(Calendar.DAY_OF_YEAR);
191                 
192                 int start = (day % mod) * limit;
193                 int end = start + limit;
194                 end = end > userCount ? userCount : end;
195                 logger.info("Updating subscriptions ["+start+":"+end+"]");
196                 
197                 Iterator JavaDoc subs = planet.getAllSubscriptions();
198                 while (subs.hasNext())
199                 {
200                     PlanetSubscriptionData sub =
201                             (PlanetSubscriptionData)subs.next();
202                     if (count >= start && count < end)
203                     {
204                         try
205                         {
206                             Technorati.Result result =
207                                     technorati.getBloginfo(sub.getSiteUrl());
208                             if (result != null && result.getWeblog() != null)
209                             {
210                               sub.setInboundblogs(
211                                       result.getWeblog().getInboundblogs());
212                               sub.setInboundlinks(
213                                       result.getWeblog().getInboundlinks());
214                               logger.debug("Adding rank for "
215                                       +sub.getFeedUrl()+" ["+count+"|"
216                                       +sub.getInboundblogs()+"|"
217                                       +sub.getInboundlinks()+"]");
218                             }
219                             else
220                             {
221                               logger.debug(
222                                 "No ranking available for "
223                                       +sub.getFeedUrl()+" ["+count+"]");
224                               sub.setInboundlinks(0);
225                               sub.setInboundblogs(0);
226                             }
227                             planet.saveSubscription(sub);
228                         }
229                         catch (Exception JavaDoc e)
230                         {
231                             logger.warn("WARN ranking subscription ["
232                                         + count + "]: " + e.getMessage());
233                             if (errorCount++ > 5)
234                             {
235                                 logger.warn(
236                                     " Stopping ranking, too many errors");
237                                 break;
238                             }
239                         }
240                     }
241                     count++;
242                 }
243                 roller.commit();
244             }
245             finally
246             {
247                 roller.release();
248             }
249         }
250         catch (Exception JavaDoc e)
251         {
252             logger.error("ERROR ranking subscriptions", e);
253         }
254     }
255 }
256
257
Popular Tags