001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2005-2007 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.rss;
021:
022: import java.io.BufferedWriter;
023: import java.io.File;
024:
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.OutputStreamWriter;
028: import java.io.Reader;
029: import java.io.StringReader;
030: import java.io.Writer;
031:
032: import org.apache.log4j.Logger;
033:
034: import com.ecyrd.jspwiki.FileUtil;
035: import com.ecyrd.jspwiki.WikiEngine;
036: import com.ecyrd.jspwiki.util.WatchDog;
037: import com.ecyrd.jspwiki.util.WikiBackgroundThread;
038:
039: /**
040: * Runs the RSS generation thread.
041: * FIXME: MUST be somewhere else, this is not a good place.
042: */
043: public class RSSThread extends WikiBackgroundThread {
044: static Logger log = Logger.getLogger(RSSThread.class);
045:
046: private final File m_rssFile;
047: private final int m_rssInterval;
048: private final RSSGenerator m_generator;
049:
050: private WatchDog m_watchdog;
051:
052: public RSSThread(WikiEngine engine, File rssFile, int rssInterval) {
053: super (engine, rssInterval);
054: m_generator = engine.getRSSGenerator();
055: m_rssFile = rssFile;
056: m_rssInterval = rssInterval;
057: setName("JSPWiki RSS Generator");
058: log.debug("RSS file will be at " + m_rssFile.getAbsolutePath());
059: log.debug("RSS refresh interval (seconds): " + m_rssInterval);
060: }
061:
062: public void startupTask() throws Exception {
063: m_watchdog = getEngine().getCurrentWatchDog();
064: }
065:
066: /**
067: * Runs the RSS generator thread.
068: * If a previous RSS generation operation encountered a
069: * file I/O or other error, this method will turn off generation.
070: * <code>false</code>.
071: * @see java.lang.Thread#run()
072: */
073: public void backgroundTask() throws Exception {
074: if (m_generator.isEnabled()) {
075: Writer out = null;
076: Reader in = null;
077:
078: m_watchdog.enterState("Generating RSS feed", 60);
079:
080: try {
081: //
082: // Generate RSS file, output it to
083: // default "rss.rdf".
084: //
085: log.debug("Regenerating RSS feed to " + m_rssFile);
086:
087: String feed = m_generator.generate();
088:
089: in = new StringReader(feed);
090: out = new BufferedWriter(new OutputStreamWriter(
091: new FileOutputStream(m_rssFile), "UTF-8"));
092:
093: FileUtil.copyContents(in, out);
094: } catch (IOException e) {
095: log.error("Cannot generate RSS feed to "
096: + m_rssFile.getAbsolutePath(), e);
097: m_generator.setEnabled(false);
098: } finally {
099: try {
100: if (in != null)
101: in.close();
102: if (out != null)
103: out.close();
104: } catch (IOException e) {
105: log.fatal("Could not close I/O for RSS", e);
106: m_generator.setEnabled(false);
107: }
108: m_watchdog.exitState();
109: }
110:
111: }
112: }
113:
114: }
|