001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.rss;
020
021
022import java.io.BufferedWriter;
023import java.io.File;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.OutputStreamWriter;
027import java.io.Reader;
028import java.io.StringReader;
029import java.io.Writer;
030
031import org.apache.log4j.Logger;
032import org.apache.wiki.WatchDog;
033import org.apache.wiki.WikiBackgroundThread;
034import org.apache.wiki.WikiEngine;
035import org.apache.wiki.util.FileUtil;
036
037/**
038 *  Runs the RSS generation thread.
039 *  FIXME: MUST be somewhere else, this is not a good place.
040 */
041public class RSSThread extends WikiBackgroundThread
042{
043    static Logger              log = Logger.getLogger( RSSThread.class );
044        
045    private final File m_rssFile;
046    private final RSSGenerator m_generator;
047        
048    private WatchDog m_watchdog;
049    
050    /**
051     *  Create a new RSS thread.
052     *  
053     *  @param engine A WikiEngine to own this thread.
054     *  @param rssFile A File to write the RSS data to.
055     *  @param rssInterval How often the RSS should be generated.
056     */
057    public RSSThread( WikiEngine engine, File rssFile, int rssInterval )
058    {
059        super( engine, rssInterval );
060        m_generator = engine.getRSSGenerator();
061        m_rssFile = rssFile;
062        setName("JSPWiki RSS Generator");
063        log.debug( "RSS file will be at "+m_rssFile.getAbsolutePath() );
064        log.debug( "RSS refresh interval (seconds): "+rssInterval );
065    }
066    
067    /**
068     *  {@inheritDoc}
069     */
070    @Override
071    public void startupTask() throws Exception
072    {
073        m_watchdog = getEngine().getCurrentWatchDog();
074    }
075    
076    /**
077     * Runs the RSS generator thread.
078     * If a previous RSS generation operation encountered a 
079     * file I/O or other error, this method will turn off generation.
080     * <code>false</code>.
081     * @see java.lang.Thread#run()
082     * @throws Exception All exceptions are thrown upwards.
083     */
084    @Override
085    public void backgroundTask() throws Exception
086    {
087        if ( m_generator.isEnabled() )
088        {
089            Writer out = null;
090            Reader in  = null;
091
092            m_watchdog.enterState( "Generating RSS feed", 60 );
093            
094            try
095            {
096                //
097                //  Generate RSS file, output it to
098                //  default "rss.rdf".
099                //
100                log.debug("Regenerating RSS feed to "+m_rssFile);
101
102                String feed = m_generator.generate();
103
104                in  = new StringReader(feed);
105                out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( m_rssFile ), "UTF-8") );
106
107                FileUtil.copyContents( in, out );
108            }
109            catch( IOException e )
110            {
111                log.error("Cannot generate RSS feed to "+m_rssFile.getAbsolutePath(), e );
112                m_generator.setEnabled( false );
113            }
114            finally
115            {
116                try
117                {
118                    if( in != null )  in.close();
119                    if( out != null ) out.close();
120                }
121                catch( IOException e )
122                {
123                    log.fatal("Could not close I/O for RSS", e );
124                    m_generator.setEnabled( false );
125                }
126                m_watchdog.exitState();
127            }
128
129        }
130    }
131        
132}