View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.geronimo.javamail.store.pop3;
20  
21  import javax.mail.Folder; 
22  import javax.mail.Message; 
23  import javax.mail.MessagingException; 
24  import javax.mail.MethodNotSupportedException;
25  import javax.mail.Store; 
26  
27  import org.apache.geronimo.javamail.store.pop3.connection.POP3Connection; 
28  
29  /**
30   * An POP3 folder instance for the root of POP3 folder tree.  This has 
31   * some of the folder operations disabled. 
32   */
33  public class POP3RootFolder extends POP3Folder {
34      // the inbox folder is the only one that exists 
35      protected Folder inbox; 
36      
37      /**
38       * Create a default POP3RootFolder attached to a specific Store instance.
39       * 
40       * @param store  The Store instance this is the root for.
41       */
42      public POP3RootFolder(POP3Store store) {
43          // create a folder with a null string name and the default separator. 
44          super(store, ""); 
45          // this only holds folders 
46          folderType = HOLDS_FOLDERS; 
47          // this folder does exist
48          exists = true; 
49          // no messages in this folder 
50          msgCount = 0; 
51      }
52  
53      
54      /**
55       * Get the parent.  This is the root folder, which 
56       * never has a parent. 
57       * 
58       * @return Always returns null. 
59       */
60      public Folder getParent() {
61          // we never have a parent folder 
62          return null; 
63      }
64  
65      /**
66       * We have a separator because the root folder is "special". 
67       */
68      public char getSeparator() throws MessagingException {
69          return '/';
70      }
71      
72      /**
73       * Retrieve a list of folders that match a pattern.
74       * 
75       * @param pattern The match pattern.
76       * 
77       * @return An array of matching folders.
78       * @exception MessagingException
79       */
80      public Folder[] list(String pattern) throws MessagingException {
81          // I'm not sure this is correct, but the Sun implementation appears to 
82          // return a array containing the inbox regardless of what pattern was specified. 
83          return new Folder[] { getInbox() };
84      }
85      
86      /**
87       * Get a folder of a given name from the root folder.
88       * The Sun implementation seems somewhat inconsistent 
89       * here.  The docs for Store claim that only INBOX is 
90       * supported, but it will return a Folder instance for any 
91       * name.  On the other hand, the root folder raises 
92       * an exception for anything but the INBOX.
93       * 
94       * @param name   The folder name (which must be "INBOX".
95       * 
96       * @return The inbox folder instance. 
97       * @exception MessagingException
98       */
99      public Folder getFolder(String name) throws MessagingException {
100         if (!name.equalsIgnoreCase("INBOX")) {
101             throw new MessagingException("Only the INBOX folder is supported"); 
102         }
103         // return the inbox folder 
104         return getInbox(); 
105     }
106     
107     /**
108      * Override for the isOpen method.  The root folder can 
109      * never be opened. 
110      * 
111      * @return always returns false. 
112      */
113     public boolean isOpen() {
114         return false; 
115     }
116     
117     public void open(int mode) throws MessagingException {
118         throw new MessagingException("POP3 root folder cannot be opened"); 
119     }
120     
121     public void open(boolean expunge) throws MessagingException {
122         throw new MessagingException("POP3 root folder cannot be close"); 
123     }
124     
125     
126     /**
127      * Retrieve the INBOX folder from the root. 
128      * 
129      * @return The Folder instance for the inbox. 
130      * @exception MessagingException
131      */
132     protected Folder getInbox() throws MessagingException {
133         // we're the only place that creates folders, and 
134         // we only create the single instance. 
135         if (inbox == null) {
136             inbox = new POP3Folder((POP3Store)store, "INBOX"); 
137         }
138         return inbox; 
139     }
140 }
141 
142