KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > pop3 > client > Pop3Service


1 package com.quadcap.pop3.client;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedInputStream JavaDoc;
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.BufferedReader JavaDoc;
44 import java.io.File JavaDoc;
45 import java.io.FileInputStream JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.InputStreamReader JavaDoc;
48 import java.io.ObjectInputStream JavaDoc;
49 import java.io.ObjectOutputStream JavaDoc;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import com.quadcap.util.text.Text;
57
58 import com.quadcap.server.ServiceImpl;
59 import com.quadcap.server.ServiceContainer;
60
61 import com.quadcap.util.Debug;
62
63 /**
64  * Service wrapper for quadcap POP3 client fetcher
65  *
66  * @author Stan Bailes
67  */

68 public class Pop3Service extends ServiceImpl {
69     Thread JavaDoc thread;
70     boolean terminate = false;
71     Properties JavaDoc props;
72     Object JavaDoc lock = new Object JavaDoc();
73     
74     public void init(ServiceContainer c, Properties JavaDoc p)
75         throws Exception JavaDoc
76     {
77         super.init(c, p);
78         props = getProperties();
79         final long sleepMs = 1000L *
80             Integer.parseInt(props.getProperty("interval", "60"));
81         terminate = false;
82         thread = new Thread JavaDoc() {
83                 public void run() {
84                     while (!terminate) {
85                         try {
86                             runAgent();
87                         } catch (Throwable JavaDoc t) {
88                             Debug.print(t);
89                         }
90                         try { Thread.sleep(sleepMs); } catch (Throwable JavaDoc t) {}
91                     }
92                 }
93             };
94         thread.start();
95     }
96
97     static ArrayList JavaDoc getTuples(File JavaDoc tf) throws Exception JavaDoc {
98         ArrayList JavaDoc a = new ArrayList JavaDoc();
99         FileInputStream JavaDoc fi = new FileInputStream JavaDoc(tf);
100         HashMap JavaDoc object = null;
101         try {
102             InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(fi);
103             BufferedReader JavaDoc r = new BufferedReader JavaDoc(isr);
104             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
105             String JavaDoc line;
106             boolean haveBuffer = false;
107             while ((line = r.readLine()) != null) {
108                 if (line.length() > 0) {
109                     if (!haveBuffer) {
110                         buf.replace(0, buf.length(), line.trim());
111                         haveBuffer = true;
112                     } else {
113                         if (Character.isWhitespace(line.charAt(0))) {
114                             buf.append(' ');
115                             buf.append(line.trim());
116                         } else {
117                             object = addAttr(object, buf.toString());
118                             buf.replace(0, buf.length(), line.trim());
119                         }
120                     }
121                 } else {
122                     if (object != null) {
123                         if (haveBuffer) {
124                             object = addAttr(object, buf.toString());
125                             haveBuffer = false;
126                         }
127                         a.add(object);
128                         object = null;
129                     }
130                 }
131             }
132             if (haveBuffer) {
133                 object = addAttr(object, buf.toString());
134             }
135             if (object != null) a.add(object);
136             return a;
137         } finally {
138             fi.close();
139         }
140     }
141
142     static final HashMap JavaDoc addAttr(HashMap JavaDoc h, String JavaDoc s) {
143         if (h == null) h = new HashMap JavaDoc();
144         String JavaDoc[] sv = Text.extractN(s, "*:*");
145         if (sv == null || sv.length != 2 || sv[0] == null || sv[1] == null) {
146             //msg("Bad attribute: " + s);
147
} else {
148             h.put(sv[0].trim(), sv[1].trim());
149         }
150         return h;
151     }
152
153     public void stop() {
154         terminate = true;
155         synchronized (lock) {
156             try { thread.interrupt(); } catch (Throwable JavaDoc t) {}
157         }
158     }
159
160     public void runAgent() throws Exception JavaDoc {
161         synchronized (lock) {
162             File JavaDoc accountsFile = new File JavaDoc(props.getProperty("accounts"));
163             ArrayList JavaDoc agents = getTuples(accountsFile);
164             String JavaDoc hooker = props.getProperty("hook");
165             Class JavaDoc hookerClass = Class.forName(hooker);
166             MessageHook hook = (MessageHook)hookerClass.newInstance();
167             hook.init(props);
168             for (int i = 0; i < agents.size(); i++) {
169                 Map JavaDoc ai = (Map JavaDoc)agents.get(i);
170                 Pop3Agent agent = readAgent(ai);
171                 if (agent != null) {
172                     Properties JavaDoc p = new Properties JavaDoc();
173                     p.putAll(props);
174                     p.putAll(ai);
175                     agent.run(p, hook);
176                     writeAgent(ai, agent);
177                 }
178             }
179         }
180     }
181
182     public Pop3Agent readAgent(Map JavaDoc ai) {
183         try {
184             String JavaDoc name = ai.get("name").toString();
185             String JavaDoc fileName = props.getProperty("savefile", "pop3." +
186                                                 name + ".save");
187             File JavaDoc file = new File JavaDoc(fileName);
188             Pop3Agent agent = new Pop3Agent();
189             if (file.exists()) {
190                 FileInputStream JavaDoc fis = new FileInputStream JavaDoc(fileName);
191                 try {
192                     BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(fis);
193                     agent.read(bis);
194                 } finally {
195                     fis.close();
196                 }
197             }
198             return agent;
199         } catch (Throwable JavaDoc t) {
200             Debug.print(t);
201             return null;
202         }
203     }
204
205     public void writeAgent(Map JavaDoc ai, Pop3Agent agent) {
206         FileOutputStream JavaDoc fos = null;
207         try {
208             String JavaDoc name = ai.get("name").toString();
209             String JavaDoc fileName = props.getProperty("savefile", "pop3." +
210                                                 name + ".save");
211             fos = new FileOutputStream JavaDoc(fileName);
212             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
213             agent.write(bos);
214             bos.flush();
215         } catch (Throwable JavaDoc t) {
216             Debug.print(t);
217         } finally {
218             if (fos != null) {
219                 try { fos.close(); } catch (Throwable JavaDoc t) {}
220             }
221         }
222     }
223 }
224
Popular Tags