001: package org.methodize.nntprss;
002:
003: /* -----------------------------------------------------------
004: * nntp//rss - a bridge between the RSS world and NNTP clients
005: * Copyright (c) 2002, 2003 Jason Brome. All Rights Reserved.
006: *
007: * email: nntprss@methodize.org
008: * mail: Methodize Solutions
009: * PO Box 3865
010: * Grand Central Station
011: * New York NY 10163
012: *
013: * This file is part of nntp//rss
014: *
015: * nntp//rss is free software; you can redistribute it
016: * and/or modify it under the terms of the GNU General
017: * Public License as published by the Free Software Foundation;
018: * either version 2 of the License, or (at your option) any
019: * later version.
020: *
021: * This program is distributed in the hope that it will be
022: * useful, but WITHOUT ANY WARRANTY; without even the implied
023: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
024: * PURPOSE. See the GNU General Public License for more
025: * details.
026: *
027: * You should have received a copy of the GNU General Public
028: * License along with this program; if not, write to the
029: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
030: * Boston, MA 02111-1307 USA
031: * ----------------------------------------------------- */
032:
033: import java.io.InputStream;
034:
035: import javax.xml.parsers.DocumentBuilder;
036: import javax.xml.parsers.DocumentBuilderFactory;
037:
038: import org.apache.log4j.Logger;
039: import org.methodize.nntprss.admin.AdminServer;
040: import org.methodize.nntprss.db.DBManager;
041: import org.methodize.nntprss.nntp.NNTPServer;
042: import org.methodize.nntprss.rss.ChannelManager;
043: import org.methodize.nntprss.util.AppConstants;
044: import org.w3c.dom.Document;
045:
046: /**
047: * @author Jason Brome <jason@methodize.org>
048: * @version $Id: Main.java,v 1.5 2003/03/24 03:09:18 jasonbrome Exp $
049: */
050: public class Main {
051:
052: private Logger log = Logger.getLogger(Main.class);
053:
054: private DBManager dbManager = null;
055: private NNTPServer nntpServer = null;
056: private ChannelManager channelManager = null;
057: private AdminServer adminServer = null;
058: private WindowsSysTray windowsSysTray = null;
059:
060: private class ShutdownHook extends Thread {
061:
062: private Logger log = Logger.getLogger(Main.ShutdownHook.class);
063:
064: /**
065: * @see java.lang.Runnable#run()
066: */
067: public void run() {
068: if (log.isInfoEnabled()) {
069: log.info("Shutting down nntp//rss...");
070: }
071:
072: if (windowsSysTray != null) {
073: windowsSysTray.shutdown();
074: }
075:
076: adminServer.shutdown();
077:
078: nntpServer.shutdown();
079:
080: channelManager.shutdown();
081:
082: dbManager.shutdown();
083:
084: if (log.isInfoEnabled()) {
085: log.info("nntp//rss shutdown successfully...");
086: }
087: }
088:
089: }
090:
091: public void startNntpRss() {
092:
093: if (log.isInfoEnabled()) {
094: log.info("Starting nntp//rss v" + AppConstants.VERSION);
095: }
096:
097: try {
098: if (System.getProperty("os.name").toLowerCase().startsWith(
099: "windows")) {
100: windowsSysTray = new WindowsSysTray();
101: }
102:
103: // Load configuration
104: Document config = loadConfiguration();
105:
106: // Start DB server
107: dbManager = new DBManager();
108: dbManager.configure(config);
109: dbManager.startup();
110:
111: channelManager = ChannelManager.getChannelManager();
112: channelManager.configure(config);
113:
114: // Start NNTP server
115: nntpServer = new NNTPServer();
116: nntpServer.configure(config);
117:
118: adminServer = new AdminServer(channelManager, nntpServer);
119: adminServer.configure(config);
120:
121: Runtime.getRuntime().addShutdownHook(
122: this .new ShutdownHook());
123:
124: adminServer.start();
125: nntpServer.start();
126: channelManager.start();
127:
128: if (windowsSysTray != null) {
129: windowsSysTray.setAdminURL("http://127.0.0.1:"
130: + adminServer.getPort() + "/");
131: windowsSysTray.showStarted();
132: }
133:
134: } catch (Exception e) {
135: log.error("Exception thrown during startup", e);
136: e.printStackTrace();
137: System.exit(-1);
138: }
139:
140: }
141:
142: private Document loadConfiguration() {
143: Document configDoc = null;
144:
145: InputStream configFile = getClass().getClassLoader()
146: .getResourceAsStream(
147: AppConstants.NNTPRSS_CONFIGURATION_FILE);
148: if (configFile == null) {
149: throw new RuntimeException("Cannot load "
150: + AppConstants.NNTPRSS_CONFIGURATION_FILE
151: + " configuration file");
152: }
153:
154: try {
155: DocumentBuilderFactory dbf = DocumentBuilderFactory
156: .newInstance();
157: DocumentBuilder db = dbf.newDocumentBuilder();
158: configDoc = db.parse(configFile);
159: } catch (Exception e) {
160: // FIXME more granular exception?
161: throw new RuntimeException("Error parsing "
162: + AppConstants.NNTPRSS_CONFIGURATION_FILE
163: + " configuration file");
164: }
165:
166: return configDoc;
167:
168: }
169:
170: public static void main(String[] args) {
171:
172: Main startup = new Main();
173: startup.startNntpRss();
174:
175: }
176:
177: // Shutdown hook for Windows Java Service Wrappers
178: // e.g. JNT
179: public static void stopApplication() {
180: System.exit(0);
181: }
182: }
|