1 21 22 27 28 package com.sun.mail.pop3; 29 30 import java.util.Properties ; 31 import java.lang.reflect.*; 32 33 import javax.mail.*; 34 import javax.mail.internet.*; 35 import java.io.IOException ; 36 import java.io.EOFException ; 37 38 47 public class POP3Store extends Store { 48 49 private String name = "pop3"; private int defaultPort = 110; private boolean isSSL = false; 53 private Protocol port = null; private POP3Folder portOwner = null; private String host = null; private int portNum = -1; 57 private String user = null; 58 private String passwd = null; 59 boolean rsetBeforeQuit = false; 60 boolean disableTop = false; 61 boolean forgetTopHeaders = false; 62 Constructor messageConstructor = null; 63 64 public POP3Store(Session session, URLName url) { 65 this(session, url, "pop3", 110, false); 66 } 67 68 public POP3Store(Session session, URLName url, 69 String name, int defaultPort, boolean isSSL) { 70 super(session, url); 71 if (url != null) 72 name = url.getProtocol(); 73 this.name = name; 74 this.defaultPort = defaultPort; 75 this.isSSL = isSSL; 76 77 String s = session.getProperty("mail." + name + ".rsetbeforequit"); 78 if (s != null && s.equalsIgnoreCase("true")) 79 rsetBeforeQuit = true; 80 81 s = session.getProperty("mail." + name + ".disabletop"); 82 if (s != null && s.equalsIgnoreCase("true")) 83 disableTop = true; 84 85 s = session.getProperty("mail." + name + ".forgettopheaders"); 86 if (s != null && s.equalsIgnoreCase("true")) 87 forgetTopHeaders = true; 88 89 s = session.getProperty("mail." + name + ".message.class"); 90 if (s != null) { 91 if (session.getDebug()) 92 session.getDebugOut().println( 93 "DEBUG: POP3 message class: " + s); 94 try { 95 ClassLoader cl = this.getClass().getClassLoader(); 96 97 Class messageClass = null; 99 try { 100 messageClass = cl.loadClass(s); 104 } catch (ClassNotFoundException ex1) { 105 messageClass = Class.forName(s); 109 } 110 111 Class [] c = {javax.mail.Folder .class, int.class}; 112 messageConstructor = messageClass.getConstructor(c); 113 } catch (Exception ex) { 114 if (session.getDebug()) 115 session.getDebugOut().println( 116 "DEBUG: failed to load POP3 message class: " + ex); 117 } 118 } 119 } 120 121 protected synchronized boolean protocolConnect(String host, int portNum, 122 String user, String passwd) throws MessagingException { 123 124 if (host == null || passwd == null || user == null) 126 return false; 127 128 if (portNum == -1) { 131 String portstring = session.getProperty("mail." + name + ".port"); 132 if (portstring != null) 133 portNum = Integer.parseInt(portstring); 134 } 135 136 if (portNum == -1) 137 portNum = defaultPort; 138 139 this.host = host; 140 this.portNum = portNum; 141 this.user = user; 142 this.passwd = passwd; 143 try { 144 port = getPort(null); 145 } catch (EOFException eex) { 146 throw new AuthenticationFailedException(eex.getMessage()); 147 } catch (IOException ioex) { 148 throw new MessagingException("Connect failed", ioex); 149 } 150 151 return true; 152 } 153 154 158 166 public synchronized boolean isConnected() { 167 if (!super.isConnected()) 168 return false; 171 synchronized (this) { 172 try { 173 if (port == null) 174 port = getPort(null); 175 else 176 port.noop(); 177 return true; 178 } catch (IOException ioex) { 179 try { 181 super.close(); } catch (MessagingException mex) { 183 } finally { 185 return false; 186 } 187 } 188 } 189 } 190 191 synchronized Protocol getPort(POP3Folder owner) throws IOException { 192 Protocol p; 193 194 if (port != null && portOwner == null) { 196 portOwner = owner; 197 return port; 198 } 199 200 p = new Protocol(host, portNum, session.getDebug(), 202 session.getDebugOut(), session.getProperties(), "mail." + name, 203 isSSL); 204 205 String msg = null; 206 if ((msg = p.login(user, passwd)) != null) { 207 try { 208 p.quit(); 209 } catch (IOException ioex) { 210 } finally { 211 throw new EOFException (msg); 212 } 213 } 214 215 223 if (port == null && owner != null) { 224 port = p; 225 portOwner = owner; 226 } 227 if (portOwner == null) 228 portOwner = owner; 229 return p; 230 } 231 232 synchronized void closePort(POP3Folder owner) { 233 if (portOwner == owner) { 234 port = null; 235 portOwner = null; 236 } 237 } 238 239 public synchronized void close() throws MessagingException { 240 try { 241 if (port != null) 242 port.quit(); 243 } catch (IOException ioex) { 244 } finally { 245 port = null; 246 247 super.close(); 249 } 250 } 251 252 public Folder getDefaultFolder() throws MessagingException { 253 checkConnected(); 254 return new DefaultFolder(this); 255 } 256 257 260 public Folder getFolder(String name) throws MessagingException { 261 checkConnected(); 262 return new POP3Folder(this, name); 263 } 264 265 public Folder getFolder(URLName url) throws MessagingException { 266 checkConnected(); 267 return new POP3Folder(this, url.getFile()); 268 } 269 270 protected void finalize() throws Throwable { 271 super.finalize(); 272 273 if (port != null) close(); 275 } 276 277 private void checkConnected() throws MessagingException { 278 if (!super.isConnected()) 279 throw new MessagingException("Not connected"); 280 } 281 } 282 | Popular Tags |