1/*2 * Copyright 2000-2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.daemon.impl;
181920//jetspeed stuff21import org.apache.jetspeed.daemon.Daemon;
22import org.apache.jetspeed.daemon.DaemonConfig;
23import org.apache.jetspeed.daemon.DaemonEntry;
24import org.apache.jetspeed.daemon.Feed;
25import org.apache.jetspeed.util.SimpleTransform;
26import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
27import org.apache.jetspeed.services.logging.JetspeedLogger;
28import org.apache.jetspeed.services.resources.JetspeedResources;
29import org.apache.jetspeed.services.registry.FileRegistry;
30import org.apache.jetspeed.services.registry.RegistryService;
3132//turbine stuff33import org.apache.turbine.services.TurbineServices;
3435//java stuff36import java.io.Reader;
37import java.util.Vector;
3839/***40<p>41A daemon that parses out Jetspeed content sources. It also handles multiple 42updating Feeds within PortletFactory. When it encounters any RSS feeds that are43remote it will pull them locally into the JetspeedDiskCache class via the 44bulkdownloader class.45</p>4647<p>48The major goals of this Daemon are:4950<ul>51 <li>Parse out OCS feeds</li>52 <li>Put the new Entry into the PortletRegistry</li>53 <li>Get the URL from the Internet if it hasn't been placed in the cache.</li> 54 <li>Instantiate the Portlet if it already isn't in the cache.</li>55</ul>5657</p>5859@author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>60@author <A HREF="mailto:sgala@apache.org">Santiago Gala</A>61@version $Id: FeedDaemon.java,v 1.42 2004/02/23 02:48:05 jford Exp $62*/63publicclassFeedDaemon implements Daemon64 {
65publicstaticfinal String TEMP_FILE_KEY = "FeedDaemon-debug";
6667publicstatic String TEMP_DIRECTORY =
68 JetspeedResources.getString( JetspeedResources.TEMP_DIRECTORY_KEY );
6970privatestaticboolean processed = false;
7172privatestaticFeedDaemon instance = null;
73747576privateint status = Daemon.STATUS_NOT_PROCESSED;
77privateint result = Daemon.RESULT_UNKNOWN;
78privateDaemonConfig config = null;
79privateDaemonEntry entry = null;
80privateboolean initialized = false;
8182/***83 The total number of entries found by the daemon84 */85privatestaticint count = 0;
8687/***88 * Static initialization of the logger for this class89 */90privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(FeedDaemon.class.getName());
9192/***93 Get the feed count.94 */95publicstaticint getCount()
96 {
97return FeedDaemon.count;
98 }
99100101/***102 Get the feeds that are available to Jetspeed103 */104publicstaticFeed[] getFeeds()
105 {
106 Vector v = JetspeedResources.getVector( "contentfeeds.feed.name" );
107 Vector found = new Vector();
108109for( int i = 0; i < v.size(); ++i) {
110 String name = (String)v.elementAt(i);
111112 String description = JetspeedResources.getString( "contentfeeds.feed." + name + ".description" );
113114 String url = JetspeedResources.getString( "contentfeeds.feed." + name + ".url" );
115116 found.addElement( new Feed( name,
117 description,
118 url ) );
119 }
120121//now that you have the properties file for the feeds transform them122//into PML123124Feed[] feeds = newFeed[found.size()];
125 found.copyInto(feeds);
126return feeds;
127128 }
129130131/***132 */133publicvoid run()
134 {
135try136 {
137this.setResult( Daemon.RESULT_PROCESSING );
138139 logger.info( "Jetspeed: FeedDaemon -- BEGIN -- " );
140141 FeedDaemon.count = 0;
142Feed[] feeds = getFeeds();
143144for (int i = 0; i < feeds.length; ++i ) {
145146 String url = feeds[i].getURL();
147148 String name = "feed_"+feeds[i].getName();
149150 Reader transformed;
151152try {
153154 logger.info( "BEGIN FEED -> " + url );
155156//get the list of PortletMarkup entries from OCS157 transformed = getEntries( url );
158159//the string transformed should now contain PML... tranform it into PortletMarkup160 logger.info("Determining portlets...");
161162FileRegistry registry = (FileRegistry)TurbineServices.getInstance()
163 .getService( RegistryService.SERVICE_NAME );
164165 registry.createFragment( name, transformed , true );
166167 logger.info( "END FEED -> " + url + " -> SUCCESS");
168169this.setResult( Daemon.RESULT_SUCCESS );
170171 } catch ( Exception e ) {
172 error( e, "FeedDaemon: Couldn't process URL: " + url );
173174 } catch ( Throwable t ) {
175 error( t, "FeedDaemon: Couldn't process URL: " + url );
176 }
177178 }
179180 } finally {
181 logger.info( "Jetspeed: FeedDaemon -- END -- " );
182 }
183184 }
185186/***187 Logs a message to the logging service but also sets the result for this daemon.188 */189privatevoid error( Throwable t, String message )
190 {
191this.setResult( Daemon.RESULT_FAILED );
192 logger.error( message, t );
193 }
194195/***196 */197privatevoid error( String message )
198 {
199this.error( null, message );
200 }
201202/***203 Get the PML for the given URL204 */205publicstaticfinal Reader getEntries( String url ) throws Exception
206 {
207//this should be the URL to the original document. Transform208//it into PML209210 String stylesheet = JetspeedResources.getString( JetspeedResources.CONTENTFEEDS_STYLESHEET_URL_KEY );
211212 logger.info( "FeedDaemon: transforming url: " +
213 url +
214" with stylesheet: " +
215 stylesheet );
216217return SimpleTransform.SAXTransform( url, stylesheet, null );
218 }
219220221222/***223 String the DOCTYPE from the transformed document. Castor can't handle this.224 */225privatestatic String strip(String target)
226 {
227if ( target.indexOf("<!DOCTYPE") != -1 ) {
228229int begin = target.indexOf( "\">" ) + 2;
230231 target = target.substring( begin, target.length() );
232233 }
234235return target;
236 }
237238/* *** Daemon interface *** */239240/***241 Init this Daemon from the DaemonFactory242 */243publicvoid init( DaemonConfig config, DaemonEntry entry )
244 {
245this.config = config;
246this.entry = entry;
247 }
248249/***250 */251publicDaemonConfig getDaemonConfig()
252 {
253returnthis.config;
254 }
255256/***257 */258publicDaemonEntry getDaemonEntry()
259 {
260returnthis.entry;
261 }
262263/***264 Return the status for this Daemon265266 @see Daemon#STATUS_NOT_PROCESSED267 @see Daemon#STATUS_PROCESSED268 @see Daemon#STATUS_PROCESSING269 */270publicint getStatus()
271 {
272returnthis.status;
273 }
274275/***276 Set the status for this Daemon277278 @see #STATUS_NOT_PROCESSED279 @see #STATUS_PROCESSED280 @see #STATUS_PROCESSING281 */282publicvoid setStatus(int status)
283 {
284this.status = status;
285 }
286287/***288 @see Daemon#getResult()289 */290publicint getResult()
291 {
292returnthis.result;
293 }
294295/***296 @see Daemon#setResult(int result)297 */298publicvoid setResult( int result )
299 {
300this.result = result;
301 }
302303/***304 @see Daemon#getMessage()305 */306public String getMessage()
307 {
308return"Total number of content feeds found: " + getCount();
309 }
310311 }