1 package com.quadcap.pop3.client; 2 3 40 41 import java.io.BufferedInputStream ; 42 import java.io.BufferedOutputStream ; 43 import java.io.BufferedReader ; 44 import java.io.File ; 45 import java.io.FileInputStream ; 46 import java.io.FileOutputStream ; 47 import java.io.InputStreamReader ; 48 import java.io.ObjectInputStream ; 49 import java.io.ObjectOutputStream ; 50 51 import java.util.ArrayList ; 52 import java.util.HashMap ; 53 import java.util.Map ; 54 import java.util.Properties ; 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 68 public class Pop3Service extends ServiceImpl { 69 Thread thread; 70 boolean terminate = false; 71 Properties props; 72 Object lock = new Object (); 73 74 public void init(ServiceContainer c, Properties p) 75 throws Exception 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 () { 83 public void run() { 84 while (!terminate) { 85 try { 86 runAgent(); 87 } catch (Throwable t) { 88 Debug.print(t); 89 } 90 try { Thread.sleep(sleepMs); } catch (Throwable t) {} 91 } 92 } 93 }; 94 thread.start(); 95 } 96 97 static ArrayList getTuples(File tf) throws Exception { 98 ArrayList a = new ArrayList (); 99 FileInputStream fi = new FileInputStream (tf); 100 HashMap object = null; 101 try { 102 InputStreamReader isr = new InputStreamReader (fi); 103 BufferedReader r = new BufferedReader (isr); 104 StringBuffer buf = new StringBuffer (); 105 String 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 addAttr(HashMap h, String s) { 143 if (h == null) h = new HashMap (); 144 String [] sv = Text.extractN(s, "*:*"); 145 if (sv == null || sv.length != 2 || sv[0] == null || sv[1] == null) { 146 } 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 t) {} 157 } 158 } 159 160 public void runAgent() throws Exception { 161 synchronized (lock) { 162 File accountsFile = new File (props.getProperty("accounts")); 163 ArrayList agents = getTuples(accountsFile); 164 String hooker = props.getProperty("hook"); 165 Class 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 ai = (Map )agents.get(i); 170 Pop3Agent agent = readAgent(ai); 171 if (agent != null) { 172 Properties p = new Properties (); 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 ai) { 183 try { 184 String name = ai.get("name").toString(); 185 String fileName = props.getProperty("savefile", "pop3." + 186 name + ".save"); 187 File file = new File (fileName); 188 Pop3Agent agent = new Pop3Agent(); 189 if (file.exists()) { 190 FileInputStream fis = new FileInputStream (fileName); 191 try { 192 BufferedInputStream bis = new BufferedInputStream (fis); 193 agent.read(bis); 194 } finally { 195 fis.close(); 196 } 197 } 198 return agent; 199 } catch (Throwable t) { 200 Debug.print(t); 201 return null; 202 } 203 } 204 205 public void writeAgent(Map ai, Pop3Agent agent) { 206 FileOutputStream fos = null; 207 try { 208 String name = ai.get("name").toString(); 209 String fileName = props.getProperty("savefile", "pop3." + 210 name + ".save"); 211 fos = new FileOutputStream (fileName); 212 BufferedOutputStream bos = new BufferedOutputStream (fos); 213 agent.write(bos); 214 bos.flush(); 215 } catch (Throwable t) { 216 Debug.print(t); 217 } finally { 218 if (fos != null) { 219 try { fos.close(); } catch (Throwable t) {} 220 } 221 } 222 } 223 } 224 | Popular Tags |