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
021import org.apache.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.WatchDog;
024import org.apache.wiki.WikiBackgroundThread;
025import org.apache.wiki.api.core.Engine;
026import org.apache.wiki.util.FileUtil;
027
028import java.io.BufferedWriter;
029import java.io.File;
030import java.io.IOException;
031import java.io.OutputStreamWriter;
032import java.io.Reader;
033import java.io.StringReader;
034import java.io.Writer;
035import java.nio.charset.StandardCharsets;
036import java.nio.file.Files;
037
038
039/**
040 *  Runs the RSS generation thread.
041 *  FIXME: MUST be somewhere else, this is not a good place.
042 */
043public class RSSThread extends WikiBackgroundThread {
044
045    private static final Logger log = LogManager.getLogger( RSSThread.class );
046    private final File m_rssFile;
047    private final RSSGenerator m_generator;
048    private WatchDog m_watchdog;
049    
050    /**
051     *  Create a new RSS thread.
052     *  
053     *  @param engine A Engine 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( final Engine engine, final File rssFile, final int rssInterval ) {
058        super( engine, rssInterval );
059        m_generator = engine.getManager( RSSGenerator.class );
060        m_rssFile = rssFile;
061        setName("JSPWiki RSS Generator");
062        log.debug( "RSS file will be at "+m_rssFile.getAbsolutePath() );
063        log.debug( "RSS refresh interval (seconds): "+rssInterval );
064    }
065    
066    /**
067     *  {@inheritDoc}
068     */
069    @Override
070    public void startupTask() {
071        m_watchdog = WatchDog.getCurrentWatchDog( getEngine() );
072    }
073    
074    /**
075     * Runs the RSS generator thread.
076     * If a previous RSS generation operation encountered a 
077     * file I/O or other error, this method will turn off generation.
078     * <code>false</code>.
079     * @see java.lang.Thread#run()
080     * @throws Exception All exceptions are thrown upwards.
081     */
082    @Override
083    public void backgroundTask() throws Exception {
084        if( m_generator.isEnabled() ) {
085            m_watchdog.enterState( "Generating RSS feed", 60 );
086            final String feed = m_generator.generate();
087            log.debug( "Regenerating RSS feed to " + m_rssFile );
088
089            // Generate RSS file, output it to default "rss.rdf".
090            try( final Reader in  = new StringReader( feed );
091                final Writer out = new BufferedWriter( new OutputStreamWriter( Files.newOutputStream( m_rssFile.toPath() ), StandardCharsets.UTF_8 ) ) ) {
092                FileUtil.copyContents( in, out );
093            } catch( final IOException e ) {
094                log.error( "Cannot generate RSS feed to " + m_rssFile.getAbsolutePath(), e );
095                m_generator.setEnabled( false );
096            } finally {
097                m_watchdog.exitState();
098            }
099        }
100    }
101        
102}