Quadcap Embeddable Server

com/quadcap/pop3/client/Pop3Service.java

Go to the documentation of this file.
00001 package com.quadcap.pop3.client; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.BufferedInputStream; 00042 import java.io.BufferedOutputStream; 00043 import java.io.BufferedReader; 00044 import java.io.File; 00045 import java.io.FileInputStream; 00046 import java.io.FileOutputStream; 00047 import java.io.InputStreamReader; 00048 import java.io.ObjectInputStream; 00049 import java.io.ObjectOutputStream; 00050 00051 import java.util.ArrayList; 00052 import java.util.HashMap; 00053 import java.util.Map; 00054 import java.util.Properties; 00055 00056 import com.quadcap.util.text.Text; 00057 00058 import com.quadcap.server.ServiceImpl; 00059 import com.quadcap.server.ServiceContainer; 00060 00061 import com.quadcap.util.Debug; 00062 00063 /** 00064 * Service wrapper for quadcap POP3 client fetcher 00065 * 00066 * @author Stan Bailes 00067 */ 00068 public class Pop3Service extends ServiceImpl { 00069 Thread thread; 00070 boolean terminate = false; 00071 Properties props; 00072 Object lock = new Object(); 00073 00074 public void init(ServiceContainer c, Properties p) 00075 throws Exception 00076 { 00077 super.init(c, p); 00078 props = getProperties(); 00079 final long sleepMs = 1000L * 00080 Integer.parseInt(props.getProperty("interval", "60")); 00081 terminate = false; 00082 thread = new Thread() { 00083 public void run() { 00084 while (!terminate) { 00085 try { 00086 runAgent(); 00087 } catch (Throwable t) { 00088 Debug.print(t); 00089 } 00090 try { Thread.sleep(sleepMs); } catch (Throwable t) {} 00091 } 00092 } 00093 }; 00094 thread.start(); 00095 } 00096 00097 static ArrayList getTuples(File tf) throws Exception { 00098 ArrayList a = new ArrayList(); 00099 FileInputStream fi = new FileInputStream(tf); 00100 HashMap object = null; 00101 try { 00102 InputStreamReader isr = new InputStreamReader(fi); 00103 BufferedReader r = new BufferedReader(isr); 00104 StringBuffer buf = new StringBuffer(); 00105 String line; 00106 boolean haveBuffer = false; 00107 while ((line = r.readLine()) != null) { 00108 if (line.length() > 0) { 00109 if (!haveBuffer) { 00110 buf.replace(0, buf.length(), line.trim()); 00111 haveBuffer = true; 00112 } else { 00113 if (Character.isWhitespace(line.charAt(0))) { 00114 buf.append(' '); 00115 buf.append(line.trim()); 00116 } else { 00117 object = addAttr(object, buf.toString()); 00118 buf.replace(0, buf.length(), line.trim()); 00119 } 00120 } 00121 } else { 00122 if (object != null) { 00123 if (haveBuffer) { 00124 object = addAttr(object, buf.toString()); 00125 haveBuffer = false; 00126 } 00127 a.add(object); 00128 object = null; 00129 } 00130 } 00131 } 00132 if (haveBuffer) { 00133 object = addAttr(object, buf.toString()); 00134 } 00135 if (object != null) a.add(object); 00136 return a; 00137 } finally { 00138 fi.close(); 00139 } 00140 } 00141 00142 static final HashMap addAttr(HashMap h, String s) { 00143 if (h == null) h = new HashMap(); 00144 String[] sv = Text.extractN(s, "*:*"); 00145 if (sv == null || sv.length != 2 || sv[0] == null || sv[1] == null) { 00146 //msg("Bad attribute: " + s); 00147 } else { 00148 h.put(sv[0].trim(), sv[1].trim()); 00149 } 00150 return h; 00151 } 00152 00153 public void stop() { 00154 terminate = true; 00155 synchronized (lock) { 00156 try { thread.interrupt(); } catch (Throwable t) {} 00157 } 00158 } 00159 00160 public void runAgent() throws Exception { 00161 synchronized (lock) { 00162 File accountsFile = new File(props.getProperty("accounts")); 00163 ArrayList agents = getTuples(accountsFile); 00164 String hooker = props.getProperty("hook"); 00165 Class hookerClass = Class.forName(hooker); 00166 MessageHook hook = (MessageHook)hookerClass.newInstance(); 00167 hook.init(props); 00168 for (int i = 0; i < agents.size(); i++) { 00169 Map ai = (Map)agents.get(i); 00170 Pop3Agent agent = readAgent(ai); 00171 if (agent != null) { 00172 Properties p = new Properties(); 00173 p.putAll(props); 00174 p.putAll(ai); 00175 agent.run(p, hook); 00176 writeAgent(ai, agent); 00177 } 00178 } 00179 } 00180 } 00181 00182 public Pop3Agent readAgent(Map ai) { 00183 try { 00184 String name = ai.get("name").toString(); 00185 String fileName = props.getProperty("savefile", "pop3." + 00186 name + ".save"); 00187 File file = new File(fileName); 00188 Pop3Agent agent = new Pop3Agent(); 00189 if (file.exists()) { 00190 FileInputStream fis = new FileInputStream(fileName); 00191 try { 00192 BufferedInputStream bis = new BufferedInputStream(fis); 00193 agent.read(bis); 00194 } finally { 00195 fis.close(); 00196 } 00197 } 00198 return agent; 00199 } catch (Throwable t) { 00200 Debug.print(t); 00201 return null; 00202 } 00203 } 00204 00205 public void writeAgent(Map ai, Pop3Agent agent) { 00206 FileOutputStream fos = null; 00207 try { 00208 String name = ai.get("name").toString(); 00209 String fileName = props.getProperty("savefile", "pop3." + 00210 name + ".save"); 00211 fos = new FileOutputStream(fileName); 00212 BufferedOutputStream bos = new BufferedOutputStream(fos); 00213 agent.write(bos); 00214 bos.flush(); 00215 } catch (Throwable t) { 00216 Debug.print(t); 00217 } finally { 00218 if (fos != null) { 00219 try { fos.close(); } catch (Throwable t) {} 00220 } 00221 } 00222 } 00223 }