001: package org.methodize.nntprss.admin;
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: import java.util.Properties;
035:
036: import org.methodize.nntprss.nntp.NNTPServer;
037: import org.methodize.nntprss.rss.ChannelManager;
038: import org.methodize.nntprss.util.AppConstants;
039: import org.mortbay.http.BasicAuthenticator;
040: import org.mortbay.http.HashUserRealm;
041: import org.mortbay.http.HttpContext;
042: import org.mortbay.http.HttpListener;
043: import org.mortbay.http.HttpServer;
044: import org.mortbay.http.SecurityConstraint;
045: import org.mortbay.http.SocketListener;
046: import org.mortbay.http.handler.SecurityHandler;
047: import org.mortbay.jetty.servlet.WebApplicationHandler;
048: import org.w3c.dom.Document;
049: import org.w3c.dom.Element;
050:
051: /**
052: * @author Jason Brome <jason@methodize.org>
053: * @version $Id: AdminServer.java,v 1.4 2003/03/24 03:10:12 jasonbrome Exp $
054: */
055: public class AdminServer {
056:
057: private HttpServer httpServer;
058: private ChannelManager channelManager;
059: private NNTPServer nntpServer;
060: private int port;
061: public static final String SERVLET_CTX_RSS_MANAGER = "rss.manager";
062: public static final String SERVLET_CTX_NNTP_SERVER = "nntp.server";
063:
064: public static final String REALM_NAME = "nntprss-realm";
065:
066: /**
067: * Constructor for AdminServer.
068: */
069:
070: public AdminServer(ChannelManager channelManager,
071: NNTPServer nntpServer) {
072: this .channelManager = channelManager;
073: this .nntpServer = nntpServer;
074: }
075:
076: public void configure(Document config) throws Exception {
077:
078: Element rootElm = config.getDocumentElement();
079: Element adminConfig = (Element) rootElm.getElementsByTagName(
080: "admin").item(0);
081: port = Integer.parseInt(adminConfig.getAttribute("port"));
082:
083: httpServer = new HttpServer();
084:
085: // Check for user realm properties file
086: // If it exists, use security.
087: InputStream userRealmConfig = this .getClass()
088: .getResourceAsStream("/" + AppConstants.USERS_CONFIG);
089: boolean useSecurity = false;
090: if (userRealmConfig != null) {
091: useSecurity = true;
092: HashUserRealm userRealm = new HashUserRealm(REALM_NAME);
093: userRealm.load(AppConstants.USERS_CONFIG);
094: httpServer.addRealm(userRealm);
095: }
096:
097: HttpContext context = httpServer.getContext("/");
098: WebApplicationHandler handler = new WebApplicationHandler();
099:
100: if (useSecurity) {
101: context.setRealmName(REALM_NAME);
102: context.setAuthenticator(new BasicAuthenticator());
103: context.addHandler(new SecurityHandler());
104: context.addSecurityConstraint("/", new SecurityConstraint(
105: "Admin", "*"));
106: }
107:
108: context.setAttribute(SERVLET_CTX_RSS_MANAGER, channelManager);
109: context.setAttribute(SERVLET_CTX_NNTP_SERVER, nntpServer);
110:
111: handler.addServlet("/", AdminServlet.class.getName());
112: context.addHandler(handler);
113:
114: httpServer.addContext(context);
115:
116: HttpListener httpListener = new SocketListener();
117: httpListener.setPort(port);
118: httpServer.addListener(httpListener);
119:
120: }
121:
122: public void start() throws Exception {
123: httpServer.start();
124: }
125:
126: public void shutdown() {
127: try {
128: httpServer.stop();
129: } catch (InterruptedException ie) {
130: // FIXME log
131: }
132: }
133:
134: /**
135: * Returns the port.
136: * @return int
137: */
138: public int getPort() {
139: return port;
140: }
141:
142: }
|