View Javadoc

1   /************************************************************************
2    * Copyright (c) 2003-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * 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     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17   
18  package org.apache.james.fetchmail;
19  
20  import javax.mail.Folder;
21  import javax.mail.MessagingException;
22  import javax.mail.Session;
23  import javax.mail.Store;
24  
25  /***
26   * <p>Class <code>StoreProcessor</code> connects to a message store, gets the
27   * target Folder and delegates its processing to <code>FolderProcessor</code>.</p>
28   * 
29   * <p>Creation Date: 27-May-03</p>
30   */
31  public class StoreProcessor extends ProcessorAbstract
32  {
33      /***
34       * Constructor for StoreProcessor.
35       * @param account 
36       */
37      protected StoreProcessor(Account account)
38      {
39          super(account);        
40      }
41      
42      /***
43       * Method process connects to a Folder in a Message Store, creates a
44       * <code>FolderProcessor</code> and runs it to process the messages in
45       * the Folder.
46       * 
47       * @see org.apache.james.fetchmail.ProcessorAbstract#process()
48       */
49      public void process() throws MessagingException
50      {
51          Store store = null;
52          Folder folder = null;
53  
54          StringBuffer logMessageBuffer =
55              new StringBuffer("Starting fetching mail from server '");
56          logMessageBuffer.append(getHost());
57          logMessageBuffer.append("' for user '");
58          logMessageBuffer.append(getUser());
59          logMessageBuffer.append("' in folder '");
60          logMessageBuffer.append(getJavaMailFolderName());
61          logMessageBuffer.append("'");
62          getLogger().info(logMessageBuffer.toString());
63  
64          try
65          {
66              // Get a Store object
67              store = getSession().getStore(getJavaMailProviderName());
68  
69              // Connect
70              if (getHost() != null
71                  || getUser() != null
72                  || getPassword() != null)
73                  store.connect(getHost(), getUser(), getPassword());
74              else
75                  store.connect();
76  
77              // Get the Folder
78              folder = store.getFolder(getJavaMailFolderName());
79              if (folder == null)
80                  getLogger().error(getFetchTaskName() + " No default folder");
81  
82              // Process the Folder
83              new FolderProcessor(folder, getAccount()).process();
84  
85          }
86          catch (MessagingException ex)
87          {
88              getLogger().error(
89                  "A MessagingException has terminated processing of this Folder",
90                  ex);
91          }
92          finally
93          {
94              try
95              {
96                  if (null != store && store.isConnected())
97                      store.close();
98              }
99              catch (MessagingException ex)
100             {
101                 getLogger().error(
102                     "A MessagingException occured while closing the Store",
103                     ex);
104             }
105             logMessageBuffer =
106                 new StringBuffer("Finished fetching mail from server '");
107             logMessageBuffer.append(getHost());
108             logMessageBuffer.append("' for user '");
109             logMessageBuffer.append(getUser());
110             logMessageBuffer.append("' in folder '");
111             logMessageBuffer.append(getJavaMailFolderName());
112             logMessageBuffer.append("'");
113             getLogger().info(logMessageBuffer.toString());
114         }
115     }
116 
117 }