    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 */
    042public class RSSThread extends WikiBackgroundThread {
    043
    044    private static final Logger LOG = LogManager.getLogger( RSSThread.class );
    045    private final File m_rssFile;
    046    private final RSSGenerator m_generator;
    047    private WatchDog m_watchdog;
    048
    049    /**
    050     *  Create a new RSS thread.
    051     *
    052     *  @param engine A Engine to own this thread.
    053     *  @param rssFile A File to write the RSS data to.
    054     *  @param rssInterval How often the RSS should be generated.
    055     */
    056    public RSSThread( final Engine engine, final File rssFile, final int rssInterval ) {
    057        super( engine, rssInterval );
    058        m_generator = engine.getManager( RSSGenerator.class );
    059        m_rssFile = rssFile;
    060        setName("JSPWiki RSS Generator");
    061        LOG.debug( "RSS file will be at "+m_rssFile.getAbsolutePath() );
    062        LOG.debug( "RSS refresh interval (seconds): "+rssInterval );
    063    }
    064
    065    /**
    066     *  {@inheritDoc}
    067     */
    068    @Override
    069    public void startupTask() {
    070        m_watchdog = WatchDog.getCurrentWatchDog( getEngine() );
    071    }
    072
    073    /**
    074     * Runs the RSS generator thread.
    075     * If a previous RSS generation operation encountered a
    076     * file I/O or other error, this method will turn off generation.
    077     * <code>false</code>.
    078     * @see java.lang.Thread#run()
    079     * @throws Exception All exceptions are thrown upwards.
    080     */
    081    @Override
    082    public void backgroundTask() throws Exception {
    083        if( m_generator.isEnabled() ) {
    084            m_watchdog.enterState( "Generating RSS feed", 60 );
    085            final String feed = m_generator.generate();
    086            LOG.debug( "Regenerating RSS feed to " + m_rssFile );
    087
    088            // Generate RSS file, output it to default "rss.rdf".
    089            try( final Reader in  = new StringReader( feed );
    090                final Writer out = new BufferedWriter( new OutputStreamWriter( Files.newOutputStream( m_rssFile.toPath() ), StandardCharsets.UTF_8 ) ) ) {
    091                FileUtil.copyContents( in, out );
    092            } catch( final IOException e ) {
    093                LOG.error( "Cannot generate RSS feed to " + m_rssFile.getAbsolutePath(), e );
    094                m_generator.setEnabled( false );
    095            } finally {
    096                m_watchdog.exitState();
    097            }
    098        }
    099    }
    100
    101}