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