KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > mbox > MboxStore


1 /*
2  * MboxStore.java
3  * Copyright (C) 1999 dog <dog@dog.net.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * You also have permission to link it with the Sun Microsystems, Inc.
11  * JavaMail(tm) extension and run that combination.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  */

22
23 package gnu.mail.providers.mbox;
24
25 import java.io.*;
26 import java.net.*;
27 import java.util.Vector JavaDoc;
28 import javax.mail.*;
29 import javax.mail.event.*;
30 import java.util.Hashtable JavaDoc;
31 import gnu.mail.util.*;
32 import gnu.mail.treeutil.StatusEvent;
33 import gnu.mail.treeutil.StatusListener;
34 import gnu.mail.treeutil.StatusSource;
35
36 /**
37  * The storage class implementing the Mbox mailbox file format.
38  *
39  * @author dog@dog.net.uk
40  * @version 2.0
41  */

42 public class MboxStore
43   extends Store
44   implements StatusSource
45 {
46
47   private static final char separatorChar = '/';
48   
49   static int fetchsize = 1024;
50   static boolean attemptFallback = true;
51
52   Vector JavaDoc statusListeners = new Vector JavaDoc();
53     
54   /**
55    * Constructor.
56    */

57   public MboxStore(Session session, URLName urlname)
58   {
59     super(session, urlname);
60     String JavaDoc fs = session.getProperty("mail.mbox.fetchsize");
61     if (fs!=null)
62     {
63       try
64       {
65         fetchsize = Math.max(Integer.parseInt(fs), 1024);
66       }
67       catch (NumberFormatException JavaDoc e)
68       {
69         log("fetchsize "+fs+" is not a number");
70       }
71     }
72     String JavaDoc af = session.getProperty("mail.mbox.attemptFallback");
73     if (af!=null)
74       attemptFallback = Boolean.valueOf(af).booleanValue();
75   }
76     
77   /**
78    * There isn't a protocol to implement, so this method just returns.
79    */

80   protected boolean protocolConnect(
81       String JavaDoc host,
82       int port,
83       String JavaDoc username,
84       String JavaDoc password)
85     throws MessagingException
86   {
87     return true;
88   }
89
90   /**
91    * Returns the default folder.
92    */

93   public Folder getDefaultFolder()
94     throws MessagingException
95   {
96     // If the url used to contruct the store references a file directly,
97
// return this file.
98
if (url!=null)
99     {
100       String JavaDoc file = url.getFile();
101       if (file!=null && file.length()>0)
102         return getFolder(file);
103     }
104     // Otherwise attempt to return a sensible root folder.
105
String JavaDoc mailhome = session.getProperty("mail.mbox.mailhome");
106     if (mailhome == null)
107     {
108       try
109       {
110         String JavaDoc userhome = System.getProperty("user.home");
111         mailhome = userhome+"/Mail"; // elm
112
if (!exists(mailhome))
113           mailhome = userhome+"/mail";
114         if (!exists(mailhome))
115           mailhome = null;
116       }
117       catch (SecurityException JavaDoc e)
118       {
119         log("access denied reading system properties");
120         mailhome = "/";
121       }
122     }
123     return getFolder(mailhome);
124   }
125
126   /**
127    * Returns the folder with the specified filename.
128    */

129   public Folder getFolder(String JavaDoc filename)
130     throws MessagingException
131   {
132     boolean inbox = false;
133     if ("inbox".equalsIgnoreCase(filename))
134     {
135       // First try the session property mail.mbox.inbox.
136
String JavaDoc inboxname = session.getProperty("mail.mbox.inbox");
137       if (!exists(inboxname))
138         inboxname = null;
139       if (inboxname==null && attemptFallback)
140       {
141         // Try some common (UNIX) locations.
142
try
143         {
144           String JavaDoc username = System.getProperty("user.name");
145           inboxname = "/var/mail/"+username;
146           if (!exists(inboxname))
147             inboxname = "/var/spool/mail/"+username;
148           if (!exists(inboxname))
149           {
150             inboxname = null;
151             String JavaDoc userhome = System.getProperty("user.home");
152             inboxname = userhome+"/mbox";
153           }
154           if (!exists(inboxname))
155             inboxname = null;
156         }
157         catch (SecurityException JavaDoc e)
158         {
159           // not allowed to read system properties
160
log("unable to access system properties");
161         }
162       }
163       if (inboxname!=null)
164       {
165         filename = inboxname;
166         inbox = true;
167       }
168       // otherwise we assume it is actually a file called "inbox"
169
}
170     
171     // convert into a valid filename for this platform
172
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
173     if (filename.length()<1 || filename.charAt(0)!=separatorChar)
174       buf.append(File.separator);
175     if (separatorChar!=File.separatorChar)
176       buf.append(filename.replace(separatorChar, File.separatorChar));
177     else
178       buf.append(filename);
179     filename = buf.toString();
180     
181     return new MboxFolder(this, filename, inbox);
182   }
183
184   /*
185    * Indicates whether the file referred to by the specified filename exists.
186    */

187   private boolean exists(String JavaDoc filename)
188   {
189     if (filename!=null)
190     {
191       File file = new File(filename);
192       if (separatorChar!=File.separatorChar)
193         file = new File(filename.replace(separatorChar, File.separatorChar));
194       return file.exists();
195     }
196     return false;
197   }
198
199   /**
200    * Returns the folder specified by the filename of the URLName.
201    */

202   public Folder getFolder(URLName urlname)
203     throws MessagingException
204   {
205     return getFolder(urlname.getFile());
206   }
207     
208   Session getSession()
209   {
210     return session;
211   }
212
213   /**
214    * Print a log message.
215    */

216   void log(String JavaDoc message)
217   {
218     Session.log("mbox: "+message);
219   }
220
221   // -- StatusSource --
222

223   /**
224    * Adds a status listener to this store.
225    * The listener will be informed of state changes during potentially
226    * lengthy procedures (opening and closing mboxes).
227    * @param l the status listener
228    * @see #removeStatusListener
229    */

230   public void addStatusListener(StatusListener l)
231   {
232     synchronized (statusListeners)
233     {
234       statusListeners.addElement(l);
235     }
236   }
237             
238   /**
239    * Removes a status listener from this store.
240    * @param l the status listener
241    * @see #addStatusListener
242    */

243   public void removeStatusListener(StatusListener l)
244   {
245     synchronized (statusListeners)
246     {
247       statusListeners.removeElement(l);
248     }
249   }
250
251   /**
252    * Processes a status event.
253    * This dispatches the event to all the registered listeners.
254    * @param event the status event
255    */

256   protected void processStatusEvent(StatusEvent event)
257   {
258     StatusListener[] listeners;
259     synchronized (statusListeners)
260     {
261       listeners = new StatusListener[statusListeners.size()];
262       statusListeners.copyInto(listeners);
263     }
264     switch (event.getType())
265     {
266       case StatusEvent.OPERATION_START:
267         for (int i=0; i<listeners.length; i++)
268           listeners[i].statusOperationStarted(event);
269         break;
270       case StatusEvent.OPERATION_UPDATE:
271         for (int i=0; i<listeners.length; i++)
272           listeners[i].statusProgressUpdate(event);
273         break;
274       case StatusEvent.OPERATION_END:
275         for (int i=0; i<listeners.length; i++)
276           listeners[i].statusOperationEnded(event);
277         break;
278     }
279   }
280
281 }
282
Popular Tags