KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > pop3 > POP3Store


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)POP3Store.java 1.29 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.pop3;
29
30 import java.util.Properties JavaDoc;
31 import java.lang.reflect.*;
32
33 import javax.mail.*;
34 import javax.mail.internet.*;
35 import java.io.IOException JavaDoc;
36 import java.io.EOFException JavaDoc;
37
38 /**
39  * A POP3 Message Store. Contains only one folder, "INBOX".
40  *
41  * See the <a HREF="package-summary.html">com.sun.mail.pop3</a> package
42  * documentation for further information on the POP3 protocol provider. <p>
43  *
44  * @author Bill Shannon
45  * @author John Mani
46  */

47 public class POP3Store extends Store {
48
49     private String JavaDoc name = "pop3"; // my protocol name
50
private int defaultPort = 110; // default POP3 port
51
private boolean isSSL = false; // use SSL?
52

53     private Protocol port = null; // POP3 port for self
54
private POP3Folder portOwner = null; // folder owning port
55
private String JavaDoc host = null; // host
56
private int portNum = -1;
57     private String JavaDoc user = null;
58     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc cl = this.getClass().getClassLoader();
96
97         // now load the class
98
Class JavaDoc messageClass = null;
99         try {
100             // First try the "application's" class loader.
101
// This should eventually be replaced by
102
// Thread.currentThread().getContextClassLoader().
103
messageClass = cl.loadClass(s);
104         } catch (ClassNotFoundException JavaDoc ex1) {
105             // That didn't work, now try the "system" class loader.
106
// (Need both of these because JDK 1.1 class loaders
107
// may not delegate to their parent class loader.)
108
messageClass = Class.forName(s);
109         }
110
111         Class JavaDoc[] c = {javax.mail.Folder JavaDoc.class, int.class};
112         messageConstructor = messageClass.getConstructor(c);
113         } catch (Exception JavaDoc 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 JavaDoc host, int portNum,
122         String JavaDoc user, String JavaDoc passwd) throws MessagingException {
123             
124     // check for non-null values of host, password, user
125
if (host == null || passwd == null || user == null)
126         return false;
127
128     // if port is not specified, set it to value of mail.pop3.port
129
// property if it exists, otherwise default to 110
130
if (portNum == -1) {
131         String JavaDoc 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 JavaDoc eex) {
146         throw new AuthenticationFailedException(eex.getMessage());
147     } catch (IOException JavaDoc ioex) {
148         throw new MessagingException("Connect failed", ioex);
149     }
150
151     return true;
152     }
153
154     /**
155      * Check whether this store is connected. Override superclass
156      * method, to actually ping our server connection. <p>
157      */

158     /*
159      * Note that we maintain somewhat of an illusion of being connected
160      * even if we're not really connected. This is because a Folder
161      * can use the connection and close it when it's done. If we then
162      * ask whether the Store's connected we want the answer to be true,
163      * as long as we can reconnect at that point. This means that we
164      * need to be able to reconnect the Store on demand.
165      */

166     public synchronized boolean isConnected() {
167     if (!super.isConnected())
168         // if we haven't been connected at all, don't bother with
169
// the NOOP.
170
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 JavaDoc ioex) {
179         // no longer connected, close it down
180
try {
181             super.close(); // notifies listeners
182
} catch (MessagingException mex) {
183             // ignore it
184
} finally {
185             return false;
186         }
187         }
188     }
189     }
190
191     synchronized Protocol getPort(POP3Folder owner) throws IOException JavaDoc {
192     Protocol p;
193
194     // if we already have a port, remember who's using it
195
if (port != null && portOwner == null) {
196         portOwner = owner;
197         return port;
198     }
199
200     // need a new port, create it and try to login
201
p = new Protocol(host, portNum, session.getDebug(),
202         session.getDebugOut(), session.getProperties(), "mail." + name,
203         isSSL);
204
205     String JavaDoc msg = null;
206     if ((msg = p.login(user, passwd)) != null) {
207         try {
208         p.quit();
209         } catch (IOException JavaDoc ioex) {
210         } finally {
211         throw new EOFException JavaDoc(msg);
212         }
213     }
214
215     /*
216      * If a Folder closes the port, and then a Folder
217      * is opened, the Store won't have a port. In that
218      * case, the getPort call will come from Folder.open,
219      * but we need to keep track of the port in the Store
220      * so that a later call to Folder.isOpen, which calls
221      * Store.isConnected, will use the same port.
222      */

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 JavaDoc ioex) {
244     } finally {
245         port = null;
246
247         // to set the state and send the closed connection event
248
super.close();
249     }
250     }
251
252     public Folder getDefaultFolder() throws MessagingException {
253     checkConnected();
254     return new DefaultFolder(this);
255     }
256
257     /**
258      * Only the name "INBOX" is supported.
259      */

260     public Folder getFolder(String JavaDoc 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 JavaDoc {
271     super.finalize();
272
273     if (port != null) // don't force a connection attempt
274
close();
275     }
276
277     private void checkConnected() throws MessagingException {
278     if (!super.isConnected())
279         throw new MessagingException("Not connected");
280     }
281 }
282
Popular Tags