001: package com.quadcap.pop3.client;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedInputStream;
042: import java.io.BufferedOutputStream;
043: import java.io.BufferedReader;
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.FileOutputStream;
047: import java.io.InputStreamReader;
048: import java.io.ObjectInputStream;
049: import java.io.ObjectOutputStream;
050:
051: import java.util.ArrayList;
052: import java.util.HashMap;
053: import java.util.Map;
054: import java.util.Properties;
055:
056: import com.quadcap.util.text.Text;
057:
058: import com.quadcap.server.ServiceImpl;
059: import com.quadcap.server.ServiceContainer;
060:
061: import com.quadcap.util.Debug;
062:
063: /**
064: * Service wrapper for quadcap POP3 client fetcher
065: *
066: * @author Stan Bailes
067: */
068: public class Pop3Service extends ServiceImpl {
069: Thread thread;
070: boolean terminate = false;
071: Properties props;
072: Object lock = new Object();
073:
074: public void init(ServiceContainer c, Properties p) throws Exception {
075: super .init(c, p);
076: props = getProperties();
077: final long sleepMs = 1000L * Integer.parseInt(props
078: .getProperty("interval", "60"));
079: terminate = false;
080: thread = new Thread() {
081: public void run() {
082: while (!terminate) {
083: try {
084: runAgent();
085: } catch (Throwable t) {
086: Debug.print(t);
087: }
088: try {
089: Thread.sleep(sleepMs);
090: } catch (Throwable t) {
091: }
092: }
093: }
094: };
095: thread.start();
096: }
097:
098: static ArrayList getTuples(File tf) throws Exception {
099: ArrayList a = new ArrayList();
100: FileInputStream fi = new FileInputStream(tf);
101: HashMap object = null;
102: try {
103: InputStreamReader isr = new InputStreamReader(fi);
104: BufferedReader r = new BufferedReader(isr);
105: StringBuffer buf = new StringBuffer();
106: String line;
107: boolean haveBuffer = false;
108: while ((line = r.readLine()) != null) {
109: if (line.length() > 0) {
110: if (!haveBuffer) {
111: buf.replace(0, buf.length(), line.trim());
112: haveBuffer = true;
113: } else {
114: if (Character.isWhitespace(line.charAt(0))) {
115: buf.append(' ');
116: buf.append(line.trim());
117: } else {
118: object = addAttr(object, buf.toString());
119: buf.replace(0, buf.length(), line.trim());
120: }
121: }
122: } else {
123: if (object != null) {
124: if (haveBuffer) {
125: object = addAttr(object, buf.toString());
126: haveBuffer = false;
127: }
128: a.add(object);
129: object = null;
130: }
131: }
132: }
133: if (haveBuffer) {
134: object = addAttr(object, buf.toString());
135: }
136: if (object != null)
137: a.add(object);
138: return a;
139: } finally {
140: fi.close();
141: }
142: }
143:
144: static final HashMap addAttr(HashMap h, String s) {
145: if (h == null)
146: h = new HashMap();
147: String[] sv = Text.extractN(s, "*:*");
148: if (sv == null || sv.length != 2 || sv[0] == null
149: || sv[1] == null) {
150: //msg("Bad attribute: " + s);
151: } else {
152: h.put(sv[0].trim(), sv[1].trim());
153: }
154: return h;
155: }
156:
157: public void stop() {
158: terminate = true;
159: synchronized (lock) {
160: try {
161: thread.interrupt();
162: } catch (Throwable t) {
163: }
164: }
165: }
166:
167: public void runAgent() throws Exception {
168: synchronized (lock) {
169: File accountsFile = new File(props.getProperty("accounts"));
170: ArrayList agents = getTuples(accountsFile);
171: String hooker = props.getProperty("hook");
172: Class hookerClass = Class.forName(hooker);
173: MessageHook hook = (MessageHook) hookerClass.newInstance();
174: hook.init(props);
175: for (int i = 0; i < agents.size(); i++) {
176: Map ai = (Map) agents.get(i);
177: Pop3Agent agent = readAgent(ai);
178: if (agent != null) {
179: Properties p = new Properties();
180: p.putAll(props);
181: p.putAll(ai);
182: agent.run(p, hook);
183: writeAgent(ai, agent);
184: }
185: }
186: }
187: }
188:
189: public Pop3Agent readAgent(Map ai) {
190: try {
191: String name = ai.get("name").toString();
192: String fileName = props.getProperty("savefile", "pop3."
193: + name + ".save");
194: File file = new File(fileName);
195: Pop3Agent agent = new Pop3Agent();
196: if (file.exists()) {
197: FileInputStream fis = new FileInputStream(fileName);
198: try {
199: BufferedInputStream bis = new BufferedInputStream(
200: fis);
201: agent.read(bis);
202: } finally {
203: fis.close();
204: }
205: }
206: return agent;
207: } catch (Throwable t) {
208: Debug.print(t);
209: return null;
210: }
211: }
212:
213: public void writeAgent(Map ai, Pop3Agent agent) {
214: FileOutputStream fos = null;
215: try {
216: String name = ai.get("name").toString();
217: String fileName = props.getProperty("savefile", "pop3."
218: + name + ".save");
219: fos = new FileOutputStream(fileName);
220: BufferedOutputStream bos = new BufferedOutputStream(fos);
221: agent.write(bos);
222: bos.flush();
223: } catch (Throwable t) {
224: Debug.print(t);
225: } finally {
226: if (fos != null) {
227: try {
228: fos.close();
229: } catch (Throwable t) {
230: }
231: }
232: }
233: }
234: }
|