001: package org.methodize.nntprss.nntp;
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.IOException;
034: import java.io.InputStream;
035: import java.net.Socket;
036: import java.util.Enumeration;
037: import java.util.HashMap;
038: import java.util.Map;
039: import java.util.Properties;
040:
041: import org.apache.log4j.Logger;
042: import org.methodize.nntprss.rss.db.ChannelManagerDAO;
043: import org.methodize.nntprss.util.AppConstants;
044: import org.methodize.nntprss.util.SimpleThreadPool;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047:
048: /**
049: * @author Jason Brome <jason@methodize.org>
050: * @version $Id: NNTPServer.java,v 1.4 2003/03/24 03:11:35 jasonbrome Exp $
051: */
052:
053: public class NNTPServer {
054:
055: private Logger log = Logger.getLogger(NNTPServer.class);
056:
057: private NNTPServerListener listener = null;
058: private SimpleThreadPool simpleThreadPool;
059: private int listenerPort;
060:
061: private static final int MAX_NNTP_CLIENT_THREADS = 5;
062:
063: private int contentType = AppConstants.CONTENT_TYPE_MIXED;
064: private boolean secure = false;
065:
066: private ChannelManagerDAO channelManagerDAO;
067:
068: private Map users = new HashMap();
069:
070: public NNTPServer() throws Exception {
071: simpleThreadPool = new SimpleThreadPool("NNTP Client Handlers",
072: "NNTP Client Thread", 20);
073: channelManagerDAO = ChannelManagerDAO.getChannelManagerDAO();
074: }
075:
076: public void configure(Document config) {
077: // TODO configure Maximum concurrent threads etc
078: Element rootElm = config.getDocumentElement();
079: Element adminConfig =
080: (Element) rootElm.getElementsByTagName("nntp").item(0);
081: listenerPort = Integer.parseInt(adminConfig.getAttribute("port"));
082:
083: // Load DB persisted configuration
084: channelManagerDAO.loadConfiguration(this );
085:
086: if (log.isInfoEnabled()) {
087: log.info("NNTP server listener port = " + listenerPort);
088: }
089:
090: InputStream userConfig =
091: this .getClass().getResourceAsStream(
092: "/" + AppConstants.USERS_CONFIG);
093: if (userConfig != null) {
094: // Load users...
095: try {
096: Properties props = new Properties();
097: props.load(userConfig);
098: Enumeration enum = props.propertyNames();
099: while (enum.hasMoreElements()) {
100: String user = (String) enum.nextElement();
101: users.put(user, props.getProperty(user));
102: }
103:
104: if (log.isInfoEnabled()) {
105: log.info("Loaded NNTP user configuration");
106: }
107:
108: } catch (IOException ie) {
109: log.error("Error loading users", ie);
110: }
111: }
112:
113: }
114:
115: public void start() throws Exception {
116: if (listener == null) {
117: listener = new NNTPServerListener(this , listenerPort);
118: }
119: listener.start();
120: }
121:
122: public void shutdown() {
123: listener.shutdown();
124: }
125:
126: void handleConnection(Socket clientConnection) {
127: simpleThreadPool.run(new ClientHandler(this , clientConnection));
128: }
129:
130: public int getContentType() {
131: return contentType;
132: }
133:
134: public void setContentType(int contentType) {
135: this .contentType = contentType;
136: }
137:
138: public void saveConfiguration() {
139: channelManagerDAO.saveConfiguration(this );
140: }
141:
142: /**
143: * Returns secure.
144: * @return boolean
145: */
146: public boolean isSecure() {
147: return secure;
148: }
149:
150: /**
151: * Sets secure.
152: * @param secure The secure to set
153: */
154: public void setSecure(boolean secure) {
155: this .secure = secure;
156: }
157:
158: public boolean isValidUser(String user, String password) {
159: boolean valid = false;
160:
161: String actualPassword = (String) users.get(user);
162: if (actualPassword != null && actualPassword.equals(password)) {
163: valid = true;
164: }
165:
166: return valid;
167: }
168:
169: /**
170: * Returns the listenerPort.
171: * @return int
172: */
173: public int getListenerPort() {
174: return listenerPort;
175: }
176:
177: }
|