File: Store.java

package info (click to toggle)
libgnumail-java 1.0-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,620 kB
  • ctags: 2,193
  • sloc: java: 17,470; sh: 9,912; makefile: 432; xml: 159
file content (348 lines) | stat: -rw-r--r-- 10,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Store.java
 * Copyright (C) 2002 The Free Software Foundation
 * 
 * This file is part of GNU JavaMail, a library.
 * 
 * GNU JavaMail is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GNU JavaMail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * As a special exception, if you link this library with other files to
 * produce an executable, this library does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * This exception does not however invalidate any other reasons why the
 * executable file might be covered by the GNU General Public License.
 */

package javax.mail;

import java.util.ArrayList;
import javax.mail.event.FolderEvent;
import javax.mail.event.FolderListener;
import javax.mail.event.StoreEvent;
import javax.mail.event.StoreListener;

/**
 * An abstract class that models a message store and its access protocol,
 * for storing and retrieving messages.
 * Subclasses provide actual implementations.
 * <p>
 * Note that Store extends the Service class, which provides many common 
 * methods for naming stores, connecting to stores, and listening to 
 * connection events.
 *
 * @author <a href="mailto:dog@gnu.org">Chris Burdess</a>
 * @version 1.3
 */
public abstract class Store 
  extends Service
{

  private ArrayList storeListeners = null;
  private ArrayList folderListeners = null;

  /**
   * Constructor.
   * @param session Session object for this Store.
   * @param url URLName object to be used for this Store
   */
  protected Store(Session session, URLName url)
  {
    super(session, url);
  }

  /**
   * Returns a Folder object that represents the 'root' of the default 
   * namespace presented to the user by the Store.
   * @exception IllegalStateException if this Store is not connected.
   */
  public abstract Folder getDefaultFolder()
    throws MessagingException;

  /**
   * Return the Folder object corresponding to the given name.
   * Note that a Folder object is returned even if the named folder 
   * does not physically exist on the Store. The <code>exists()</code>
   * method on the folder object indicates whether this folder really exists.
   * <p>
   * Folder objects are not cached by the Store, so invoking this method on 
   * the same name multiple times will return that many distinct Folder 
   * objects.
   * @param name The name of the Folder. In some Stores, <code>name</code>
   * can be an absolute path if it starts with the hierarchy delimiter.
   * Else it is interpreted relative to the 'root' of this namespace.
   * @exception IllegalStateException if this Store is not connected.
   */
  public abstract Folder getFolder(String name)
    throws MessagingException;

  /**
   * Return a closed Folder object, corresponding to the given URLName.
   * The store specified in the given URLName should refer to this Store 
   * object.
   * <p>
   * Implementations of this method may obtain the name of the actual folder
   * using the <code>getFile()</code> method on URLName, and use that name 
   * to create the folder.
   * @param url URLName that denotes a folder
   * @exception IllegalStateException if this Store is not connected.
   */
  public abstract Folder getFolder(URLName url)
    throws MessagingException;

  /**
   * Return a set of folders representing the personal namespaces for the
   * current user. A personal namespace is a set of names that is considered
   * within the personal scope of the authenticated user. Typically, only the
   * authenticated user has access to mail folders in their personal namespace.
   * If an INBOX exists for a user, it must appear within the user's personal
   * namespace. In the typical case, there should be only one personal 
   * namespace for each user in each Store.
   * <p>
   * This implementation returns an array with a single entry containing the
   * return value of the getDefaultFolder method. Subclasses should override
   * this method to return appropriate information.
   */
  public Folder[] getPersonalNamespaces()
    throws MessagingException
  {
    Folder[] folders = new Folder[1];
    folders[0] = getDefaultFolder();
    return folders;
  }

  /**
   * Return a set of folders representing the namespaces for user.
   * The namespaces returned represent the personal namespaces for the user.
   * To access mail folders in the other user's namespace, the currently
   * authenticated user must be explicitly granted access rights. For example,
   * it is common for a manager to grant to their secretary access rights to
   * their mail folders.
   * <p>
   * This implementation returns an empty array. Subclasses should override 
   * this method to return appropriate information.
   */
  public Folder[] getUserNamespaces(String user)
    throws MessagingException
  {
    return new Folder[0];
  }

  /**
   * Return a set of folders representing the shared namespaces.
   * A shared namespace is a namespace that consists of mail folders that 
   * are intended to be shared amongst users and do not exist within a 
   * user's personal namespace.
   * <p>
   * This implementation returns an empty array. Subclasses should override 
   * this method to return appropriate information.
   */
  public Folder[] getSharedNamespaces()
    throws MessagingException
  {
    return new Folder[0];
  }

  // -- Event management --
  
  /*
   * Because the propagation of events of different kinds in the JavaMail
   * API is so haphazard, I have here sacrificed a small time advantage for
   * readability and consistency.
   *
   * All the various propagation methods now call a method with a name based
   * on the eventual listener method name prefixed by 'fire', as is the
   * preferred pattern for usage of the EventListenerList in Swing.
   *
   * Note that all events are currently delivered synchronously, where in
   * Sun's implementation a different thread is used for event delivery.
   * 
   * TODO Examine the impact of this.
   */
  
  // -- Store events --

  /**
   * Add a listener for StoreEvents on this Store.
   */
  public void addStoreListener(StoreListener l)
  {
    if (storeListeners==null)
      storeListeners = new ArrayList();
    synchronized (storeListeners)
    {
      storeListeners.add(l);
    }
  }

  /**
   * Remove a listener for Store events.
   */
  public void removeStoreListener(StoreListener l)
  {
    if (storeListeners!=null)
    {
      synchronized (storeListeners)
      {
        storeListeners.remove(l);
      }
    }
  }

  /**
   * Notify all StoreListeners.
   * Store implementations are expected to use this method to broadcast 
   * StoreEvents.
   */
  protected void notifyStoreListeners(int type, String message)
  {
    StoreEvent event = new StoreEvent(this, type, message);
    fireNotification(event);
  }

  /*
   * Propagates a StoreEvent to all registered listeners.
   */
  void fireNotification(StoreEvent event)
  {
    if (storeListeners!=null)
    {
      StoreListener[] l = null;
      synchronized (storeListeners)
      {
        l = new StoreListener[storeListeners.size()];
        storeListeners.toArray(l);
      }
      for (int i=0; i<l.length; i++)
        l[i].notification(event);
    }
  }

  // -- Folder events --

  /**
   * Add a listener for Folder events on any Folder object obtained from this
   * Store. FolderEvents are delivered to FolderListeners on the affected 
   * Folder as well as to FolderListeners on the containing Store.
   */
  public void addFolderListener(FolderListener l)
  {
    if (folderListeners==null)
      folderListeners = new ArrayList();
    synchronized (folderListeners)
    {
      folderListeners.add(l);
    }
  }

  /**
   * Remove a listener for Folder events.
   */
  public void removeFolderListener(FolderListener l)
  {
    if (folderListeners!=null)
    {
      synchronized (folderListeners)
      {
        folderListeners.remove(l);
      }
    }
  }

  /**
   * Notify all FolderListeners. Store implementations are expected to use 
   * this method to broadcast Folder events.
   */
  protected void notifyFolderListeners(int type, Folder folder)
  {
    FolderEvent event = new FolderEvent(this, folder, type);
    switch (type)
    {
      case FolderEvent.CREATED:
        fireFolderCreated(event);
        break;
      case FolderEvent.DELETED:
        fireFolderDeleted(event);
        break;
    }
  }

  /**
   * Notify all FolderListeners about the renaming of a folder. Store
   * implementations are expected to use this method to broadcast Folder 
   * events indicating the renaming of folders.
   */
  protected void notifyFolderRenamedListeners(Folder oldFolder, 
      Folder newFolder)
  {
    FolderEvent event = new FolderEvent(this, oldFolder, newFolder, 
        FolderEvent.RENAMED);
    fireFolderRenamed(event);
  }

  /*
   * Propagates a CREATED FolderEvent to all registered listeners.
   */
  void fireFolderCreated(FolderEvent event)
  {
    if (folderListeners!=null)
    {
      FolderListener[] l = null;
      synchronized (folderListeners)
      {
        l = new FolderListener[folderListeners.size()];
        folderListeners.toArray(l);
      }
      for (int i=0; i<l.length; i++)
        l[i].folderCreated(event);
    }
  }

  /*
   * Propagates a DELETED FolderEvent to all registered listeners.
   */
  void fireFolderDeleted(FolderEvent event)
  {
    if (folderListeners!=null)
    {
      FolderListener[] l = null;
      synchronized (folderListeners)
      {
        l = new FolderListener[folderListeners.size()];
        folderListeners.toArray(l);
      }
      for (int i=0; i<l.length; i++)
        l[i].folderDeleted(event);
    }
  }

  /*
   * Propagates a RENAMED FolderEvent to all registered listeners.
   */
  void fireFolderRenamed(FolderEvent event)
  {
    if (folderListeners!=null)
    {
      FolderListener[] l = null;
      synchronized (folderListeners)
      {
        l = new FolderListener[folderListeners.size()];
        folderListeners.toArray(l);
      }
      for (int i=0; i<l.length; i++)
        l[i].folderRenamed(event);
    }
  }

}