001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.email.faux;
031:
032: import java.util.Comparator;
033: import java.util.SortedSet;
034: import java.util.TreeSet;
035:
036: import javax.mail.Flags;
037: import javax.mail.Folder;
038: import javax.mail.Message;
039: import javax.mail.MessagingException;
040:
041: /**
042: * A minimal implementation of a JavaMail <code>Folder</code> to serve the
043: * requirements of the e-mail application.
044: */
045: public class FauxFolder extends Folder {
046:
047: private static final int MESSAGE_COUNT = 140;
048:
049: private static final Message[] INBOX_MESSAGES;
050: static {
051: try {
052: MessageGenerator messageGenerator = new MessageGenerator();
053: SortedSet sortingSet = new TreeSet(new Comparator() {
054: public int compare(Object a, Object b) {
055: try {
056: Message message1 = (Message) a;
057: Message message2 = (Message) b;
058: int dateDelta = message1.getSentDate()
059: .compareTo(message2.getSentDate());
060: if (dateDelta != 0) {
061: return dateDelta;
062: }
063: return message1.toString().compareTo(
064: message2.toString());
065: } catch (MessagingException ex) {
066: throw new RuntimeException(ex);
067: }
068: }
069: });
070: for (int i = 0; i < MESSAGE_COUNT; ++i) {
071: sortingSet.add(messageGenerator.generateMessage());
072: }
073: INBOX_MESSAGES = (Message[]) sortingSet
074: .toArray(new Message[sortingSet.size()]);
075: } catch (MessagingException ex) {
076: throw new RuntimeException(ex);
077: }
078: }
079:
080: private int type;
081: private FauxStore store;
082:
083: /**
084: * Creates the "INBOX" folder.
085: *
086: * @param store the relevant <code>Store</code>
087: * @return the created INBOX
088: */
089: public static final FauxFolder createInbox(FauxStore store) {
090: return new FauxFolder(store, HOLDS_MESSAGES);
091: }
092:
093: /**
094: * Creates the root folder.
095: *
096: * @param store the relevant <code>Store</code>
097: * @return the created root folder
098: */
099: public static final FauxFolder createRoot(FauxStore store) {
100: return new FauxFolder(store, HOLDS_FOLDERS);
101: }
102:
103: /**
104: * Creates a new <code>FauxFolder</code>
105: *
106: * @param store the relevant <code>Store</code>
107: * @param type the folder type
108: */
109: private FauxFolder(FauxStore store, int type) {
110: super (store);
111: this .store = store;
112: this .type = type;
113: }
114:
115: /**
116: * @see javax.mail.Folder#appendMessages(javax.mail.Message[])
117: */
118: public void appendMessages(Message[] messages)
119: throws MessagingException {
120: throw new UnsupportedOperationException();
121: }
122:
123: /**
124: * @see javax.mail.Folder#close(boolean)
125: */
126: public void close(boolean expunge) throws MessagingException {
127: }
128:
129: /**
130: * @see javax.mail.Folder#create(int)
131: */
132: public boolean create(int type) throws MessagingException {
133: return true;
134: }
135:
136: /**
137: * @see javax.mail.Folder#delete(boolean)
138: */
139: public boolean delete(boolean recurse) throws MessagingException {
140: return false;
141: }
142:
143: /**
144: * @see javax.mail.Folder#exists()
145: */
146: public boolean exists() throws MessagingException {
147: return type == HOLDS_MESSAGES;
148: }
149:
150: /**
151: * @see javax.mail.Folder#expunge()
152: */
153: public Message[] expunge() throws MessagingException {
154: return new Message[0];
155: }
156:
157: /**
158: * @see javax.mail.Folder#getFolder(java.lang.String)
159: */
160: public Folder getFolder(String name) throws MessagingException {
161: if (type == HOLDS_FOLDERS) {
162: return store.inboxFolder;
163: } else {
164: throw new MessagingException();
165: }
166: }
167:
168: /**
169: * @see javax.mail.Folder#getFullName()
170: */
171: public String getFullName() {
172: return getName();
173: }
174:
175: /**
176: * @see javax.mail.Folder#getMessage(int)
177: */
178: public Message getMessage(int index) throws MessagingException {
179: return INBOX_MESSAGES[index - 1];
180: }
181:
182: /**
183: * @see javax.mail.Folder#getMessageCount()
184: */
185: public int getMessageCount() throws MessagingException {
186: return INBOX_MESSAGES.length;
187: }
188:
189: /**
190: * @see javax.mail.Folder#getName()
191: */
192: public String getName() {
193: switch (type) {
194: case HOLDS_FOLDERS:
195: return "/";
196: case HOLDS_MESSAGES:
197: return "INBOX";
198: default:
199: return "Unknown";
200: }
201: }
202:
203: /**
204: * @see javax.mail.Folder#getParent()
205: */
206: public Folder getParent() throws MessagingException {
207: return type == HOLDS_MESSAGES ? store.rootFolder : null;
208: }
209:
210: /**
211: * @see javax.mail.Folder#getPermanentFlags()
212: */
213: public Flags getPermanentFlags() {
214: return new Flags();
215: }
216:
217: /**
218: * @see javax.mail.Folder#getSeparator()
219: */
220: public char getSeparator() throws MessagingException {
221: return '\u0000';
222: }
223:
224: /**
225: * @see javax.mail.Folder#getType()
226: */
227: public int getType() throws MessagingException {
228: return type;
229: }
230:
231: /**
232: * @see javax.mail.Folder#hasNewMessages()
233: */
234: public boolean hasNewMessages() throws MessagingException {
235: return false;
236: }
237:
238: /**
239: * @see javax.mail.Folder#isOpen()
240: */
241: public boolean isOpen() {
242: return true;
243: }
244:
245: /**
246: * @see javax.mail.Folder#list(java.lang.String)
247: */
248: public Folder[] list(String pattern) throws MessagingException {
249: if (type == HOLDS_FOLDERS) {
250: return new Folder[] { store.inboxFolder };
251: } else {
252: throw new MessagingException();
253: }
254: }
255:
256: /**
257: * @see javax.mail.Folder#open(int)
258: */
259: public void open(int mode) throws MessagingException {
260: }
261:
262: /**
263: * @see javax.mail.Folder#renameTo(javax.mail.Folder)
264: */
265: public boolean renameTo(Folder folder) throws MessagingException {
266: return false;
267: }
268: }
|