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.util.ArrayList;
022: import java.util.Date;
023: import java.util.Iterator;
024: import java.util.List;
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.RollerRuntimeConfig;
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.PlanetGroupData;
035: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
036: import org.apache.roller.pojos.WebsiteData;
037: import org.apache.roller.util.URLUtilities;
038:
039: /**
040: * Ensure that every weblog has a subscription in Planet Roller database.
041: */
042: public class SyncWebsitesTask extends RollerTask {
043:
044: private static Log log = LogFactory.getLog(SyncWebsitesTask.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 1 day
050: private int interval = 1440;
051:
052: // lease time given to ping task lock, default is 30 minutes
053: private int leaseTime = 30;
054:
055: public String getName() {
056: return "SyncWebsitesTask";
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: * Ensure there's a subscription in the "all" group for every Roller weblog.
105: */
106: public void runTask() {
107:
108: // make sure we have an absolute url value
109: String absUrl = RollerRuntimeConfig
110: .getProperty("site.absoluteurl");
111: if (absUrl == null || absUrl.trim().length() == 0) {
112: log
113: .error("ERROR: cannot sync websites with Planet Roller - "
114: + "absolute URL not specified in Roller Config");
115: return;
116: }
117:
118: try {
119: PlanetManager planet = RollerFactory.getRoller()
120: .getPlanetManager();
121: UserManager userManager = RollerFactory.getRoller()
122: .getUserManager();
123:
124: // first, make sure there is an "all" planet group
125: PlanetGroupData group = planet.getGroup("all");
126: if (group == null) {
127: group = new PlanetGroupData();
128: group.setHandle("all");
129: group.setTitle("all");
130: planet.saveGroup(group);
131: }
132:
133: // walk through all enable weblogs and add/update subs as needed
134: List liveUserFeeds = new ArrayList();
135: Iterator websites = userManager.getWebsites(null,
136: Boolean.TRUE, Boolean.TRUE, null, null, 0, -1)
137: .iterator();
138: while (websites.hasNext()) {
139: WebsiteData weblog = (WebsiteData) websites.next();
140:
141: String siteUrl = URLUtilities.getWeblogURL(weblog,
142: null, true);
143: String feedUrl = URLUtilities
144: .getWeblogFeedURL(weblog, null, "entries",
145: "rss", null, null, false, true);
146:
147: // add feed url to the "live" list
148: liveUserFeeds.add(feedUrl);
149:
150: // if sub already exists then update it, otherwise add it
151: PlanetSubscriptionData sub = planet
152: .getSubscription(feedUrl);
153: if (sub == null) {
154: log.info("ADDING feed: " + feedUrl);
155:
156: sub = new PlanetSubscriptionData();
157: sub.setTitle(weblog.getName());
158: sub.setFeedURL(feedUrl);
159: sub.setSiteURL(siteUrl);
160: sub.setAuthor(weblog.getHandle());
161:
162: planet.saveSubscription(sub);
163: group.getSubscriptions().add(sub);
164: } else {
165: sub.setTitle(weblog.getName());
166: sub.setAuthor(weblog.getHandle());
167:
168: planet.saveSubscription(sub);
169: }
170: }
171:
172: // new subs added, existing subs updated, now delete old subs
173: Iterator subs = group.getSubscriptions().iterator();
174: while (subs.hasNext()) {
175: PlanetSubscriptionData sub = (PlanetSubscriptionData) subs
176: .next();
177: if (!liveUserFeeds.contains(sub.getFeedURL())) {
178: log.info("DELETING feed: " + sub.getFeedURL());
179: planet.deleteSubscription(sub);
180: group.getSubscriptions().remove(sub);
181: }
182: }
183:
184: // all done, lets save
185: planet.saveGroup(group);
186: RollerFactory.getRoller().flush();
187:
188: } catch (RollerException e) {
189: log.error("ERROR refreshing entries", e);
190: } finally {
191: // don't forget to release
192: RollerFactory.getRoller().release();
193: }
194: }
195:
196: /**
197: * Task may be run from the command line
198: */
199: public static void main(String[] args) {
200: try {
201: SyncWebsitesTask task = new SyncWebsitesTask();
202: task.init();
203: task.run();
204: System.exit(0);
205: } catch (Throwable t) {
206: t.printStackTrace();
207: System.exit(-1);
208: }
209: }
210:
211: }
|