001: package org.methodize.nntprss.rss;
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.net.Authenticator;
034: import java.net.PasswordAuthentication;
035: import java.util.Date;
036: import java.util.Iterator;
037: import java.util.Map;
038: import java.util.Properties;
039:
040: import org.methodize.nntprss.rss.db.ChannelManagerDAO;
041: import org.w3c.dom.Document;
042:
043: /**
044: * @author Jason Brome <jason@methodize.org>
045: * @version $Id: ChannelManager.java,v 1.4 2003/03/22 16:30:23 jasonbrome Exp $
046: */
047: public class ChannelManager {
048:
049: private long pollingIntervalSeconds = 60 * 60;
050:
051: private String proxyServer = null;
052: private int proxyPort = 0;
053: private String proxyUserID = null;
054: private transient String proxyPassword = null;
055:
056: private Map channels;
057: private static ChannelManager channelManager = new ChannelManager();
058: private ChannelManagerDAO channelManagerDAO;
059: private ChannelPoller channelPoller;
060:
061: private ChannelManager() {
062: // Private constructor - singleton class
063: channelManagerDAO = ChannelManagerDAO.getChannelManagerDAO();
064: }
065:
066: public static ChannelManager getChannelManager() {
067: return channelManager;
068: }
069:
070: /**
071: * RSS Manager configuration
072: *
073: * - Monitored RSS feeds
074: * feed url, and matching newsgroup name
075: *
076: * Once feeds are loaded, load existing persistent
077: * store information about feeds
078: *
079: */
080:
081: public void configure(Document config) {
082:
083: channelManagerDAO.loadConfiguration(this );
084:
085: // Set proxy configuration, if necessary.
086: if ((proxyServer != null) && (proxyServer.length() > 0)) {
087: System.setProperty("http.proxyHost", proxyServer);
088: System.setProperty("http.proxyPort", Integer
089: .toString(proxyPort));
090: System.setProperty("http.proxySet", "true");
091:
092: if ((proxyUserID != null && proxyUserID.length() > 0)
093: || (proxyPassword != null && proxyPassword.length() > 0)) {
094: Authenticator.setDefault(new Authenticator() {
095: protected PasswordAuthentication getPasswordAuthentication() {
096: return new PasswordAuthentication(proxyUserID,
097: (proxyPassword == null) ? new char[0]
098: : proxyPassword.toCharArray());
099: }
100: });
101: } else {
102: Authenticator.setDefault(null);
103: }
104: } else {
105: System.setProperty("http.proxyHost", "");
106: System.setProperty("http.proxyPort", "");
107: System.setProperty("http.proxySet", "false");
108: Authenticator.setDefault(null);
109: }
110:
111: // Load feeds...
112: channels = channelManagerDAO.loadChannels();
113:
114: // // Start feed poller...
115: // startPoller();
116:
117: }
118:
119: public void addChannel(Channel channel) {
120: channel.setCreated(new Date());
121: channelManagerDAO.addChannel(channel);
122: channels.put(channel.getName(), channel);
123: }
124:
125: public void deleteChannel(Channel channel) {
126: channels.remove(channel.getName());
127: channelManagerDAO.deleteChannel(channel);
128: }
129:
130: public void start() {
131: startPoller();
132: }
133:
134: public void shutdown() {
135: stopPoller();
136: }
137:
138: public Iterator channels() {
139: return channels.values().iterator();
140: }
141:
142: public Channel channelByName(String name) {
143: return (Channel) channels.get(name);
144: }
145:
146: private void startPoller() {
147: channelPoller = new ChannelPoller(channels);
148: channelPoller.start();
149: }
150:
151: private void stopPoller() {
152: channelPoller.shutdown();
153: }
154:
155: public void saveConfiguration() {
156: channelManagerDAO.saveConfiguration(this );
157:
158: // Update proxy configuration, if necessary.
159: if (proxyServer != null && proxyServer.length() > 0) {
160: System.setProperty("http.proxyHost", proxyServer);
161: System.setProperty("http.proxyPort", Integer
162: .toString(proxyPort));
163: System.setProperty("http.proxySet", "true");
164:
165: if ((proxyUserID != null && proxyUserID.length() > 0)
166: || (proxyPassword != null && proxyPassword.length() > 0)) {
167: Authenticator.setDefault(new Authenticator() {
168: protected PasswordAuthentication getPasswordAuthentication() {
169: return new PasswordAuthentication(proxyUserID,
170: (proxyPassword == null) ? new char[0]
171: : proxyPassword.toCharArray());
172: }
173: });
174: } else {
175: Authenticator.setDefault(null);
176: }
177:
178: } else {
179: Properties props = System.getProperties();
180: System.setProperty("http.proxyHost", "");
181: System.setProperty("http.proxyPort", "");
182: System.setProperty("http.proxySet", "false");
183: Authenticator.setDefault(null);
184: }
185:
186: }
187:
188: /**
189: * Returns the channelManagerDAO.
190: * @return ChannelManagerDAO
191: */
192: public ChannelManagerDAO getChannelManagerDAO() {
193: return channelManagerDAO;
194: }
195:
196: /**
197: * Returns the pollingIntervalSeconds.
198: * @return long
199: */
200: public long getPollingIntervalSeconds() {
201: return pollingIntervalSeconds;
202: }
203:
204: /**
205: * Sets the pollingIntervalSeconds.
206: * @param pollingIntervalSeconds The pollingIntervalSeconds to set
207: */
208: public void setPollingIntervalSeconds(long pollingIntervalSeconds) {
209: this .pollingIntervalSeconds = pollingIntervalSeconds;
210: }
211:
212: /**
213: * Returns the proxyPort.
214: * @return int
215: */
216: public int getProxyPort() {
217: return proxyPort;
218: }
219:
220: /**
221: * Returns the proxyServer.
222: * @return String
223: */
224: public String getProxyServer() {
225: return proxyServer;
226: }
227:
228: /**
229: * Sets the proxyPort.
230: * @param proxyPort The proxyPort to set
231: */
232: public void setProxyPort(int proxyPort) {
233: this .proxyPort = proxyPort;
234: }
235:
236: /**
237: * Sets the proxyServer.
238: * @param proxyServer The proxyServer to set
239: */
240: public void setProxyServer(String proxyServer) {
241: this .proxyServer = proxyServer;
242: }
243:
244: /**
245: * Returns the proxyPassword.
246: * @return String
247: */
248: public String getProxyPassword() {
249: return proxyPassword;
250: }
251:
252: /**
253: * Returns the proxyUserID.
254: * @return String
255: */
256: public String getProxyUserID() {
257: return proxyUserID;
258: }
259:
260: /**
261: * Sets the proxyPassword.
262: * @param proxyPassword The proxyPassword to set
263: */
264: public void setProxyPassword(String proxyPassword) {
265: this .proxyPassword = proxyPassword;
266: }
267:
268: /**
269: * Sets the proxyUserID.
270: * @param proxyUserID The proxyUserID to set
271: */
272: public void setProxyUserID(String proxyUserID) {
273: this.proxyUserID = proxyUserID;
274: }
275:
276: }
|