View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.mailrepository.javamail;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.mail.Folder;
26  import javax.mail.MessagingException;
27  import javax.mail.Store;
28  
29  public class StoreGateKeeperImpl implements StoreGateKeeper {
30      
31      private Store store;
32      
33      Map folderGateKeeperMap = new HashMap();
34      FolderGateKeeper defaultFolder = null;
35  
36      private FolderAdapterFactory folderFactory;
37      
38      public StoreGateKeeperImpl(Store store) {
39          this.store=store;
40      }
41  
42      /**
43       * @see org.apache.james.mailrepository.javamail.StoreGateKeeper#getFolder(java.lang.String)
44       */
45      public synchronized FolderGateKeeper getFolder(String name) throws MessagingException {
46          if (name.length()==0) {
47              return null;
48          }
49          if (name.equalsIgnoreCase("INBOX")) {
50              name="INBOX";
51          }
52          FolderGateKeeper fgk=(FolderGateKeeper) folderGateKeeperMap.get(name);
53          if (fgk==null) {
54              Folder f = store.getFolder(name);
55              fgk=new FolderGateKeeperImpl(folderFactory.createAdapter(f),this);
56              folderGateKeeperMap.put(name,fgk);
57          }
58          
59          return fgk;
60      }
61      
62      /**
63       * @see org.apache.james.mailrepository.javamail.StoreGateKeeper#getDefaultFolder()
64       */
65      public synchronized FolderGateKeeper getDefaultFolder() throws MessagingException {
66          if (defaultFolder==null) {
67              Folder f = store.getDefaultFolder();
68              defaultFolder=new FolderGateKeeperImpl(folderFactory.createAdapter(f),this);
69          }
70          return defaultFolder;
71      }
72  
73      /**
74       * @see org.apache.james.mailrepository.javamail.StoreGateKeeper#setFolderAdapterFactory(org.apache.james.mailrepository.javamail.FolderAdapterFactory)
75       */
76      public void setFolderAdapterFactory(FolderAdapterFactory folderFactory) {
77          this.folderFactory=folderFactory;
78          
79      }
80  
81      /**
82       * @see org.apache.james.mailrepository.javamail.StoreGateKeeper#list(java.lang.String)
83       */
84      public FolderGateKeeper[] list(String string) throws MessagingException {
85          getDefaultFolder().use();
86          FolderInterface[] folders = getDefaultFolder().getFolder().list(string);
87          FolderGateKeeper[] keepers =new FolderGateKeeper[folders.length];
88          for (int i = 0; i < keepers.length; i++) {
89              keepers[i]=getFolder(folders[i].getFullName());
90          }
91          getDefaultFolder().free();
92          
93          return keepers;
94      }
95      
96      /**
97       * @see org.apache.james.mailrepository.javamail.StoreGateKeeper#renameTo(org.apache.james.mailrepository.javamail.FolderGateKeeper, java.lang.String)
98       */
99      public void renameTo(FolderGateKeeper from,String to) throws MessagingException {
100         String fromName=from.getFullName();
101         FolderGateKeeper[] subFolders = list(from.getFullName()+".*");
102         for (int i = 0; i < subFolders.length; i++) {
103             FolderGateKeeper subFolder=subFolders[i];
104             String subFolderName=subFolder.getFullName();
105             String newSubFolderName=to+subFolderName.substring(fromName.length());
106             Folder subDestination=store.getFolder(newSubFolderName);
107             subFolder.renameTo(subDestination);
108         }
109         Folder destination=store.getFolder(to);
110         from.renameTo(destination);
111     
112     }
113 
114 }