001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.planet.tasks;
020:
021: import java.io.IOException;
022: import java.util.Calendar;
023: import java.util.Date;
024: import java.util.Iterator;
025: import java.util.Properties;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.RollerException;
029: import org.apache.roller.business.runnable.RollerTask;
030: import org.apache.roller.config.RollerConfig;
031: import org.apache.roller.planet.business.PlanetManager;
032: import org.apache.roller.business.RollerFactory;
033: import org.apache.roller.business.UserManager;
034: import org.apache.roller.planet.pojos.PlanetConfigData;
035: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
036: import org.apache.roller.util.Technorati;
037:
038: /**
039: * Rank each subscription by populating Technorati inbound blog and link counts.
040: */
041: public class TechnoratiRankingsTask extends RollerTask {
042:
043: private static Log log = LogFactory
044: .getLog(TechnoratiRankingsTask.class);
045:
046: // a String description of when to start this task
047: private String startTimeDesc = "startOfDay";
048:
049: // interval at which the task is run, default is 5 minutes
050: private int interval = 1440;
051:
052: // lease time given to task lock, default is 30 minutes
053: private int leaseTime = 30;
054:
055: public String getName() {
056: return "TechnoratiRankingsTask";
057: }
058:
059: public Date getStartTime(Date currentTime) {
060: return getAdjustedTime(currentTime, startTimeDesc);
061: }
062:
063: public int getInterval() {
064: return this .interval;
065: }
066:
067: public int getLeaseTime() {
068: return this .leaseTime;
069: }
070:
071: public void init() throws RollerException {
072:
073: // get relevant props
074: Properties props = this .getTaskProperties();
075:
076: // extract start time
077: String startTimeStr = props.getProperty("startTime");
078: if (startTimeStr != null) {
079: this .startTimeDesc = startTimeStr;
080: }
081:
082: // extract interval
083: String intervalStr = props.getProperty("interval");
084: if (intervalStr != null) {
085: try {
086: this .interval = Integer.parseInt(intervalStr);
087: } catch (NumberFormatException ex) {
088: log.warn("Invalid interval: " + intervalStr);
089: }
090: }
091:
092: // extract lease time
093: String leaseTimeStr = props.getProperty("leaseTime");
094: if (leaseTimeStr != null) {
095: try {
096: this .leaseTime = Integer.parseInt(leaseTimeStr);
097: } catch (NumberFormatException ex) {
098: log.warn("Invalid leaseTime: " + leaseTimeStr);
099: }
100: }
101: }
102:
103: /**
104: * Loop through all subscriptions get get Technorati rankings for each
105: */
106: public void runTask() {
107:
108: int count = 0;
109: int errorCount = 0;
110: try {
111: PlanetManager planet = RollerFactory.getRoller()
112: .getPlanetManager();
113: PlanetConfigData config = planet.getConfiguration();
114: Technorati technorati = null;
115: try {
116: if (config.getProxyHost() != null
117: && config.getProxyPort() != -1) {
118: technorati = new Technorati(config.getProxyHost(),
119: config.getProxyPort());
120: } else {
121: technorati = new Technorati();
122: }
123: } catch (IOException e) {
124: log
125: .error("Aborting collection of Technorati rankings.\n"
126: + "technorati.license not found at root of classpath.\n"
127: + "Get license at http://technorati.com/developers/apikey.html\n"
128: + "Put the license string in a file called technorati.license.\n"
129: + "And place that file at the root of Roller's classpath.\n"
130: + "For example, in the /WEB-INF/classes directory.");
131: return;
132: }
133:
134: UserManager userManager = RollerFactory.getRoller()
135: .getUserManager();
136: try {
137: int limit = RollerConfig.getIntProperty(
138: "planet.aggregator.technorati.limit", 500);
139: int userCount = planet.getSubscriptionCount();
140: int mod = (userCount / limit) + 1;
141:
142: Calendar cal = Calendar.getInstance();
143: cal.setTime(new Date());
144: int day = cal.get(Calendar.DAY_OF_YEAR);
145:
146: int start = (day % mod) * limit;
147: int end = start + limit;
148: end = end > userCount ? userCount : end;
149: log.info("Updating subscriptions [" + start + ":" + end
150: + "]");
151:
152: Iterator subs = planet.getAllSubscriptions();
153: while (subs.hasNext()) {
154: PlanetSubscriptionData sub = (PlanetSubscriptionData) subs
155: .next();
156: if (count >= start && count < end) {
157: try {
158: Technorati.Result result = technorati
159: .getBloginfo(sub.getSiteURL());
160: if (result != null
161: && result.getWeblog() != null) {
162: sub.setInboundblogs(result.getWeblog()
163: .getInboundblogs());
164: sub.setInboundlinks(result.getWeblog()
165: .getInboundlinks());
166: log.debug("Adding rank for "
167: + sub.getFeedURL() + " ["
168: + count + "|"
169: + sub.getInboundblogs() + "|"
170: + sub.getInboundlinks() + "]");
171: } else {
172: log.debug("No ranking available for "
173: + sub.getFeedURL() + " ["
174: + count + "]");
175: sub.setInboundlinks(0);
176: sub.setInboundblogs(0);
177: }
178: planet.saveSubscription(sub);
179: } catch (Exception e) {
180: log.warn("WARN ranking subscription ["
181: + count + "]: " + e.getMessage());
182: if (errorCount++ > 5) {
183: log
184: .warn(" Stopping ranking, too many errors");
185: break;
186: }
187: }
188: }
189: count++;
190: }
191:
192: // all done, flush results to db
193: RollerFactory.getRoller().flush();
194:
195: } finally {
196: RollerFactory.getRoller().release();
197: }
198:
199: } catch (Exception e) {
200: log.error("ERROR ranking subscriptions", e);
201: }
202: }
203:
204: /**
205: * Task may be run from the command line
206: */
207: public static void main(String[] args) {
208: try {
209: TechnoratiRankingsTask task = new TechnoratiRankingsTask();
210: task.init();
211: task.run();
212: System.exit(0);
213: } catch (Throwable t) {
214: t.printStackTrace();
215: System.exit(-1);
216: }
217: }
218:
219: }
|