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.business.hibernate;
020:
021: import com.sun.syndication.fetcher.FeedFetcher;
022: import com.sun.syndication.fetcher.impl.FeedFetcherCache;
023: import java.text.MessageFormat;
024: import java.util.ArrayList;
025: import java.util.Date;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030: import java.util.Set;
031: import java.util.TreeSet;
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.roller.RollerException;
035: import org.apache.roller.config.RollerRuntimeConfig;
036: import org.apache.roller.business.PluginManager;
037: import org.apache.roller.business.RollerFactory;
038: import org.apache.roller.business.UserManager;
039: import org.apache.roller.business.WeblogManager;
040: import org.apache.roller.planet.business.hibernate.HibernatePlanetManagerImpl;
041: import org.apache.roller.planet.pojos.PlanetEntryData;
042: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
043: import org.apache.roller.pojos.WeblogEntryData;
044: import org.apache.roller.pojos.WebsiteData;
045: import org.apache.commons.lang.StringUtils;
046:
047: /**
048: * An extended version of the base PlanetManager implementation.
049: *
050: * This is meant for use by Roller installations that are running the planet
051: * aggregator in the same application instance and want to fetch feeds from
052: * their local Roller blogs in a more efficient manner.
053: */
054: public class HibernateRollerPlanetManagerImpl extends
055: HibernatePlanetManagerImpl {
056:
057: private static Log log = LogFactory
058: .getLog(HibernateRollerPlanetManagerImpl.class);
059:
060: public HibernateRollerPlanetManagerImpl(
061: HibernatePersistenceStrategy strat) {
062:
063: super (strat);
064:
065: log.info("Instantiating Hibernate Roller Planet Manager");
066: }
067:
068: protected Set getNewEntries(PlanetSubscriptionData sub,
069: FeedFetcher feedFetcher, FeedFetcherCache feedInfoCache)
070: throws RollerException {
071:
072: String localURL = RollerRuntimeConfig
073: .getProperty("site.absoluteurl");
074:
075: // if this is not a local url then let parent deal with it
076: if (StringUtils.isEmpty(localURL)
077: || !sub.getFeedURL().startsWith(localURL)) {
078:
079: log.debug("Feed is remote, letting parent handle it "
080: + sub.getFeedURL());
081:
082: return super .getNewEntries(sub, feedFetcher, feedInfoCache);
083: }
084:
085: try {
086: // for local feeds, sub.author = website.handle
087: // feed is from our domain and we have a handle, lets deal with it
088: if (sub.getAuthor() != null) {
089:
090: log.debug("Getting LOCAL feed " + sub.getFeedURL());
091:
092: Set newEntries = new TreeSet();
093:
094: // get corresponding website object
095: UserManager usermgr = RollerFactory.getRoller()
096: .getUserManager();
097: WebsiteData website = usermgr.getWebsiteByHandle(sub
098: .getAuthor());
099: if (website == null)
100: return newEntries;
101:
102: // figure website last update time
103: WeblogManager blogmgr = RollerFactory.getRoller()
104: .getWeblogManager();
105:
106: Date siteUpdated = website.getLastModified();
107: if (siteUpdated == null) { // Site never updated, skip it
108: log
109: .warn("Last-publish time null, skipping local feed ["
110: + website.getHandle() + "]");
111: return newEntries;
112: }
113:
114: // if website last update time > subsciption last update time
115: List entries = new ArrayList();
116: if (sub.getLastUpdated() == null
117: || siteUpdated.after(sub.getLastUpdated())) {
118: int entryCount = RollerRuntimeConfig
119: .getIntProperty("site.newsfeeds.defaultEntries");
120: entries = blogmgr.getWeblogEntries(website, null,
121: null, // startDate
122: new Date(), // endDate
123: null, // catName
124: null, // tags
125: WeblogEntryData.PUBLISHED, // status
126: null, // sortby (null means pubTime)
127: null, // locale
128: 0, // offset
129: entryCount);
130:
131: sub.setLastUpdated(siteUpdated);
132: saveSubscription(sub);
133:
134: } else {
135: if (log.isDebugEnabled()) {
136: String msg = MessageFormat.format(
137: " Skipping ({0} / {1})",
138: new Object[] { siteUpdated,
139: sub.getLastUpdated() });
140: log.debug(msg);
141: }
142: }
143:
144: // Populate subscription object with new entries
145: PluginManager ppmgr = RollerFactory.getRoller()
146: .getPagePluginManager();
147: Map pagePlugins = ppmgr.getWeblogEntryPlugins(website);
148: Iterator entryIter = entries.iterator();
149: while (entryIter.hasNext()) {
150: try {
151: WeblogEntryData rollerEntry = (WeblogEntryData) entryIter
152: .next();
153: PlanetEntryData entry = new PlanetEntryData(
154: rollerEntry, sub, pagePlugins);
155: saveEntry(entry);
156: newEntries.add(entry);
157: } catch (Exception e) {
158: log.error(
159: "ERROR processing subscription entry",
160: e);
161: }
162: }
163:
164: return newEntries;
165: }
166: } catch (Exception e) {
167: log.warn("Problem reading local feed", e);
168: }
169:
170: log.debug("Failed to fetch locally, trying remote "
171: + sub.getFeedURL());
172:
173: // if there was an error then try normal planet method
174: return super.getNewEntries(sub, feedFetcher, feedInfoCache);
175: }
176:
177: }
|