KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > hibernate > HibernateRollerPlanetManagerImpl


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.business.hibernate;
20
21 import com.sun.syndication.fetcher.FeedFetcher;
22 import com.sun.syndication.fetcher.impl.FeedFetcherCache;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.roller.RollerException;
35 import org.apache.roller.config.RollerRuntimeConfig;
36 import org.apache.roller.model.PluginManager;
37 import org.apache.roller.model.RollerFactory;
38 import org.apache.roller.model.UserManager;
39 import org.apache.roller.model.WeblogManager;
40 import org.apache.roller.pojos.PlanetEntryData;
41 import org.apache.roller.pojos.PlanetSubscriptionData;
42 import org.apache.roller.pojos.WeblogEntryData;
43 import org.apache.roller.pojos.WebsiteData;
44 import org.apache.commons.lang.StringUtils;
45
46
47
48 /**
49  * An extended version of the base PlanetManager implementation.
50  *
51  * This is meant for use by Roller installations that are running the planet
52  * aggregator in the same application instance and want to fetch feeds from
53  * their local Roller blogs in a more efficient manner.
54  */

55 public class HibernateRollerPlanetManagerImpl extends HibernatePlanetManagerImpl {
56     
57     private static Log log = LogFactory.getLog(HibernateRollerPlanetManagerImpl.class);
58     
59     
60     public HibernateRollerPlanetManagerImpl(HibernatePersistenceStrategy strat) {
61         
62         super(strat);
63         
64         log.info("Instantiating Hibernate Roller Planet Manager");
65     }
66     
67     
68     protected Set JavaDoc getNewEntries(PlanetSubscriptionData sub,
69                                 FeedFetcher feedFetcher,
70                                 FeedFetcherCache feedInfoCache)
71             throws RollerException {
72         
73         String JavaDoc localURL = RollerRuntimeConfig.getProperty("site.absoluteurl");
74         
75         // if this is not a local url then let parent deal with it
76
if (StringUtils.isEmpty(localURL) || !sub.getFeedURL().startsWith(localURL)) {
77             
78             log.debug("Feed is remote, letting parent handle it "+sub.getFeedURL());
79             
80             return super.getNewEntries(sub, feedFetcher, feedInfoCache);
81         }
82         
83         try {
84             // for local feeds, sub.author = website.handle
85
// feed is from our domain and we have a handle, lets deal with it
86
if(sub.getAuthor() != null) {
87                 
88                 log.debug("Getting LOCAL feed "+sub.getFeedURL());
89                 
90                 Set JavaDoc newEntries = new TreeSet JavaDoc();
91                 
92                 // get corresponding website object
93
UserManager usermgr = RollerFactory.getRoller().getUserManager();
94                 WebsiteData website = usermgr.getWebsiteByHandle(sub.getAuthor());
95                 if (website == null)
96                     return newEntries;
97                 
98                 // figure website last update time
99
WeblogManager blogmgr = RollerFactory.getRoller().getWeblogManager();
100                 
101                 Date JavaDoc siteUpdated = website.getLastModified();
102                 if (siteUpdated == null) { // Site never updated, skip it
103
log.warn("Last-publish time null, skipping local feed ["
104                             + website.getHandle() + "]");
105                     return newEntries;
106                 }
107                 
108                 // if website last update time > subsciption last update time
109
List JavaDoc entries = new ArrayList JavaDoc();
110                 if (sub.getLastUpdated()==null || siteUpdated.after(sub.getLastUpdated())) {
111                     int entryCount = RollerRuntimeConfig.getIntProperty(
112                             "site.newsfeeds.defaultEntries");
113                     entries = blogmgr.getWeblogEntries(
114                             website,
115                             null,
116                             null, // startDate
117
new Date JavaDoc(), // endDate
118
null, // catName
119
WeblogEntryData.PUBLISHED, // status
120
null, // sortby (null means pubTime)
121
null, // locale
122
0, // offset
123
entryCount);
124                     
125                     sub.setLastUpdated(siteUpdated);
126                     saveSubscription(sub);
127                     
128                 } else {
129                     if (log.isDebugEnabled()) {
130                         String JavaDoc msg = MessageFormat.format(
131                                 " Skipping ({0} / {1})", new Object JavaDoc[] {
132                             siteUpdated, sub.getLastUpdated()});
133                         log.debug(msg);
134                     }
135                 }
136                 
137                 // Populate subscription object with new entries
138
PluginManager ppmgr = RollerFactory.getRoller().getPagePluginManager();
139                 Map JavaDoc pagePlugins = ppmgr.getWeblogEntryPlugins(website);
140                 Iterator JavaDoc entryIter = entries.iterator();
141                 while (entryIter.hasNext()) {
142                     try {
143                         WeblogEntryData rollerEntry =
144                                 (WeblogEntryData)entryIter.next();
145                         PlanetEntryData entry =
146                                 new PlanetEntryData(rollerEntry, sub, pagePlugins);
147                         saveEntry(entry);
148                         newEntries.add(entry);
149                     } catch (Exception JavaDoc e) {
150                         log.error("ERROR processing subscription entry", e);
151                     }
152                 }
153                 
154                 return newEntries;
155             }
156         } catch (Exception JavaDoc e) {
157             log.warn("Problem reading local feed", e);
158         }
159         
160         log.debug("Failed to fetch locally, trying remote "+sub.getFeedURL());
161         
162         // if there was an error then try normal planet method
163
return super.getNewEntries(sub, feedFetcher, feedInfoCache);
164     }
165     
166 }
167
Popular Tags