KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > faux > FauxFolder


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email.faux;
31
32 import java.util.Comparator JavaDoc;
33 import java.util.SortedSet JavaDoc;
34 import java.util.TreeSet JavaDoc;
35
36 import javax.mail.Flags JavaDoc;
37 import javax.mail.Folder JavaDoc;
38 import javax.mail.Message JavaDoc;
39 import javax.mail.MessagingException JavaDoc;
40
41 /**
42  * A minimal implementation of a JavaMail <code>Folder</code> to serve the
43  * requirements of the e-mail application.
44  */

45 public class FauxFolder extends Folder JavaDoc {
46     
47     private static final int MESSAGE_COUNT = 140;
48     
49     private static final Message JavaDoc[] INBOX_MESSAGES;
50     static {
51         try {
52             MessageGenerator messageGenerator = new MessageGenerator();
53             SortedSet JavaDoc sortingSet = new TreeSet JavaDoc(new Comparator JavaDoc(){
54                  public int compare(Object JavaDoc a, Object JavaDoc b) {
55                      try {
56                          Message JavaDoc message1 = (Message JavaDoc) a;
57                          Message JavaDoc message2 = (Message JavaDoc) b;
58                          int dateDelta = message1.getSentDate().compareTo(message2.getSentDate());
59                          if (dateDelta != 0) {
60                              return dateDelta;
61                          }
62                          return message1.toString().compareTo(message2.toString());
63                      } catch (MessagingException JavaDoc ex) {
64                          throw new RuntimeException JavaDoc(ex);
65                      }
66                  }
67             });
68             for (int i = 0; i < MESSAGE_COUNT; ++i) {
69                 sortingSet.add(messageGenerator.generateMessage());
70             }
71             INBOX_MESSAGES = (Message JavaDoc[]) sortingSet.toArray(new Message JavaDoc[sortingSet.size()]);
72         } catch (MessagingException JavaDoc ex) {
73             throw new RuntimeException JavaDoc(ex);
74         }
75     }
76     
77     private int type;
78     private FauxStore store;
79     
80     /**
81      * Creates the "INBOX" folder.
82      *
83      * @param store the relevant <code>Store</code>
84      * @return the created INBOX
85      */

86     public static final FauxFolder createInbox(FauxStore store) {
87         return new FauxFolder(store, HOLDS_MESSAGES);
88     }
89     
90     /**
91      * Creates the root folder.
92      *
93      * @param store the relevant <code>Store</code>
94      * @return the created root folder
95      */

96     public static final FauxFolder createRoot(FauxStore store) {
97         return new FauxFolder(store, HOLDS_FOLDERS);
98     }
99     
100     /**
101      * Creates a new <code>FauxFolder</code>
102      *
103      * @param store the relevant <code>Store</code>
104      * @param type the folder type
105      */

106     private FauxFolder(FauxStore store, int type) {
107         super(store);
108         this.store = store;
109         this.type = type;
110     }
111
112     /**
113      * @see javax.mail.Folder#appendMessages(javax.mail.Message[])
114      */

115     public void appendMessages(Message JavaDoc[] messages)
116     throws MessagingException JavaDoc {
117         throw new UnsupportedOperationException JavaDoc();
118     }
119     
120     /**
121      * @see javax.mail.Folder#close(boolean)
122      */

123     public void close(boolean expunge)
124     throws MessagingException JavaDoc { }
125
126     /**
127      * @see javax.mail.Folder#create(int)
128      */

129     public boolean create(int type) throws MessagingException JavaDoc {
130         return true;
131     }
132     
133     /**
134      * @see javax.mail.Folder#delete(boolean)
135      */

136     public boolean delete(boolean recurse)
137     throws MessagingException JavaDoc {
138         return false;
139     }
140     
141     /**
142      * @see javax.mail.Folder#exists()
143      */

144     public boolean exists() throws MessagingException JavaDoc {
145         return type == HOLDS_MESSAGES;
146     }
147     
148     /**
149      * @see javax.mail.Folder#expunge()
150      */

151     public Message JavaDoc[] expunge() throws MessagingException JavaDoc {
152         return new Message JavaDoc[0];
153     }
154
155     /**
156      * @see javax.mail.Folder#getFolder(java.lang.String)
157      */

158     public Folder JavaDoc getFolder(String JavaDoc name)
159     throws MessagingException JavaDoc {
160         if (type == HOLDS_FOLDERS) {
161             return store.inboxFolder;
162         } else {
163             throw new MessagingException JavaDoc();
164         }
165     }
166
167     /**
168      * @see javax.mail.Folder#getFullName()
169      */

170     public String JavaDoc getFullName() {
171         return getName();
172     }
173     
174     /**
175      * @see javax.mail.Folder#getMessage(int)
176      */

177     public Message JavaDoc getMessage(int index) throws MessagingException JavaDoc {
178         return INBOX_MESSAGES[index - 1];
179     }
180
181     /**
182      * @see javax.mail.Folder#getMessageCount()
183      */

184     public int getMessageCount() throws MessagingException JavaDoc {
185         return INBOX_MESSAGES.length;
186     }
187     
188     /**
189      * @see javax.mail.Folder#getName()
190      */

191     public String JavaDoc getName() {
192         switch (type) {
193         case HOLDS_FOLDERS:
194             return "/";
195         case HOLDS_MESSAGES:
196             return "INBOX";
197         default:
198             return "Unknown";
199         }
200     }
201     
202     /**
203      * @see javax.mail.Folder#getParent()
204      */

205     public Folder JavaDoc getParent() throws MessagingException JavaDoc {
206         return type == HOLDS_MESSAGES ? store.rootFolder : null;
207     }
208     
209     /**
210      * @see javax.mail.Folder#getPermanentFlags()
211      */

212     public Flags JavaDoc getPermanentFlags() {
213         return new Flags JavaDoc();
214     }
215     
216     /**
217      * @see javax.mail.Folder#getSeparator()
218      */

219     public char getSeparator() throws MessagingException JavaDoc {
220         return '\u0000';
221     }
222     
223     /**
224      * @see javax.mail.Folder#getType()
225      */

226     public int getType() throws MessagingException JavaDoc {
227         return type;
228     }
229
230     /**
231      * @see javax.mail.Folder#hasNewMessages()
232      */

233     public boolean hasNewMessages() throws MessagingException JavaDoc {
234         return false;
235     }
236     
237     /**
238      * @see javax.mail.Folder#isOpen()
239      */

240     public boolean isOpen() {
241         return true;
242     }
243     
244     /**
245      * @see javax.mail.Folder#list(java.lang.String)
246      */

247     public Folder JavaDoc[] list(String JavaDoc pattern) throws MessagingException JavaDoc {
248         if (type == HOLDS_FOLDERS) {
249             return new Folder JavaDoc[]{store.inboxFolder};
250         } else {
251             throw new MessagingException JavaDoc();
252         }
253     }
254     
255     /**
256      * @see javax.mail.Folder#open(int)
257      */

258     public void open(int mode) throws MessagingException JavaDoc {
259     }
260
261     /**
262      * @see javax.mail.Folder#renameTo(javax.mail.Folder)
263      */

264     public boolean renameTo(Folder JavaDoc folder) throws MessagingException JavaDoc {
265         return false;
266     }
267 }
Popular Tags