KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tasks > TechnoratiRankingsTask


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.core.tasks;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.TimerTask JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.roller.RollerException;
29 import org.apache.roller.config.RollerConfig;
30 import org.apache.roller.model.PlanetManager;
31 import org.apache.roller.model.Roller;
32 import org.apache.roller.model.RollerFactory;
33 import org.apache.roller.model.ScheduledTask;
34 import org.apache.roller.model.UserManager;
35 import org.apache.roller.pojos.PlanetConfigData;
36 import org.apache.roller.pojos.PlanetSubscriptionData;
37 import org.apache.roller.util.Technorati;
38
39
40 /**
41  * Rank each subscription by populating Technorati inbound blog and link counts.
42  */

43 public class TechnoratiRankingsTask extends TimerTask JavaDoc implements ScheduledTask {
44     
45     private static Log log = LogFactory.getLog(TechnoratiRankingsTask.class);
46     
47     
48     /**
49      * Task may be run from the command line
50      */

51     public static void main(String JavaDoc[] args) {
52         try {
53             RollerFactory.setRoller(
54                     "org.apache.roller.business.hibernate.HibernateRollerImpl");
55             TechnoratiRankingsTask task = new TechnoratiRankingsTask();
56             task.init(RollerFactory.getRoller(), "dummy");
57             task.run();
58             System.exit(0);
59         } catch (Throwable JavaDoc t) {
60             t.printStackTrace();
61             System.exit(-1);
62         }
63     }
64     
65     
66     public void init(Roller roller, String JavaDoc realPath) throws RollerException {
67         // no-op
68
}
69     
70     
71     public void run() {
72         rankSubscriptions();
73     }
74     
75     
76     /**
77      * Loop through all subscriptions get get Technorati rankings for each
78      */

79     private void rankSubscriptions() {
80         
81         int count = 0;
82         int errorCount = 0;
83         try {
84             PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
85             PlanetConfigData config = planet.getConfiguration();
86             Technorati technorati = null;
87             try {
88                 if (config.getProxyHost()!=null && config.getProxyPort() != -1) {
89                     technorati = new Technorati(
90                             config.getProxyHost(), config.getProxyPort());
91                 } else {
92                     technorati = new Technorati();
93                 }
94             } catch (IOException JavaDoc e) {
95                 log.error("Aborting collection of Technorati rankings.\n"
96                         +"technorati.license not found at root of classpath.\n"
97                         +"Get license at http://technorati.com/developers/apikey.html\n"
98                         +"Put the license string in a file called technorati.license.\n"
99                         +"And place that file at the root of Roller's classpath.\n"
100                         +"For example, in the /WEB-INF/classes directory.");
101                 return;
102             }
103             
104             UserManager userManager = RollerFactory.getRoller().getUserManager();
105             try {
106                 int limit = RollerConfig.getIntProperty(
107                         "planet.aggregator.technorati.limit", 500);
108                 int userCount = planet.getSubscriptionCount();
109                 int mod = (userCount / limit) + 1;
110                 
111                 Calendar JavaDoc cal = Calendar.getInstance();
112                 cal.setTime(new Date JavaDoc());
113                 int day = cal.get(Calendar.DAY_OF_YEAR);
114                 
115                 int start = (day % mod) * limit;
116                 int end = start + limit;
117                 end = end > userCount ? userCount : end;
118                 log.info("Updating subscriptions ["+start+":"+end+"]");
119                 
120                 Iterator JavaDoc subs = planet.getAllSubscriptions();
121                 while (subs.hasNext()) {
122                     PlanetSubscriptionData sub =
123                             (PlanetSubscriptionData)subs.next();
124                     if (count >= start && count < end) {
125                         try {
126                             Technorati.Result result =
127                                     technorati.getBloginfo(sub.getSiteURL());
128                             if (result != null && result.getWeblog() != null) {
129                                 sub.setInboundblogs(
130                                         result.getWeblog().getInboundblogs());
131                                 sub.setInboundlinks(
132                                         result.getWeblog().getInboundlinks());
133                                 log.debug("Adding rank for "
134                                         +sub.getFeedURL()+" ["+count+"|"
135                                         +sub.getInboundblogs()+"|"
136                                         +sub.getInboundlinks()+"]");
137                             } else {
138                                 log.debug(
139                                         "No ranking available for "
140                                         +sub.getFeedURL()+" ["+count+"]");
141                                 sub.setInboundlinks(0);
142                                 sub.setInboundblogs(0);
143                             }
144                             planet.saveSubscription(sub);
145                         } catch (Exception JavaDoc e) {
146                             log.warn("WARN ranking subscription ["
147                                     + count + "]: " + e.getMessage());
148                             if (errorCount++ > 5) {
149                                 log.warn(
150                                         " Stopping ranking, too many errors");
151                                 break;
152                             }
153                         }
154                     }
155                     count++;
156                 }
157                 
158                 // all done, flush results to db
159
RollerFactory.getRoller().flush();
160                 
161             } finally {
162                 RollerFactory.getRoller().release();
163             }
164             
165         } catch (Exception JavaDoc e) {
166             log.error("ERROR ranking subscriptions", e);
167         }
168     }
169     
170 }
171
Popular Tags