001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.fetchmail;
019:
020: import javax.mail.Folder;
021: import javax.mail.MessagingException;
022: import javax.mail.Session;
023: import javax.mail.Store;
024:
025: /**
026: * <p>Class <code>StoreProcessor</code> connects to a message store, gets the
027: * target Folder and delegates its processing to <code>FolderProcessor</code>.</p>
028: *
029: * <p>Creation Date: 27-May-03</p>
030: */
031: public class StoreProcessor extends ProcessorAbstract {
032: /**
033: * Constructor for StoreProcessor.
034: * @param account
035: */
036: protected StoreProcessor(Account account) {
037: super (account);
038: }
039:
040: /**
041: * Method process connects to a Folder in a Message Store, creates a
042: * <code>FolderProcessor</code> and runs it to process the messages in
043: * the Folder.
044: *
045: * @see org.apache.james.fetchmail.ProcessorAbstract#process()
046: */
047: public void process() throws MessagingException {
048: Store store = null;
049: Folder folder = null;
050:
051: StringBuffer logMessageBuffer = new StringBuffer(
052: "Starting fetching mail from server '");
053: logMessageBuffer.append(getHost());
054: logMessageBuffer.append("' for user '");
055: logMessageBuffer.append(getUser());
056: logMessageBuffer.append("' in folder '");
057: logMessageBuffer.append(getJavaMailFolderName());
058: logMessageBuffer.append("'");
059: getLogger().info(logMessageBuffer.toString());
060:
061: try {
062: // Get a Store object
063: store = getSession().getStore(getJavaMailProviderName());
064:
065: // Connect
066: if (getHost() != null || getUser() != null
067: || getPassword() != null)
068: store.connect(getHost(), getUser(), getPassword());
069: else
070: store.connect();
071:
072: // Get the Folder
073: folder = store.getFolder(getJavaMailFolderName());
074: if (folder == null)
075: getLogger().error(
076: getFetchTaskName() + " No default folder");
077:
078: // Process the Folder
079: new FolderProcessor(folder, getAccount()).process();
080:
081: } catch (MessagingException ex) {
082: getLogger()
083: .error(
084: "A MessagingException has terminated processing of this Folder",
085: ex);
086: } finally {
087: try {
088: if (null != store && store.isConnected())
089: store.close();
090: } catch (MessagingException ex) {
091: getLogger()
092: .error(
093: "A MessagingException occured while closing the Store",
094: ex);
095: }
096: logMessageBuffer = new StringBuffer(
097: "Finished fetching mail from server '");
098: logMessageBuffer.append(getHost());
099: logMessageBuffer.append("' for user '");
100: logMessageBuffer.append(getUser());
101: logMessageBuffer.append("' in folder '");
102: logMessageBuffer.append(getJavaMailFolderName());
103: logMessageBuffer.append("'");
104: getLogger().info(logMessageBuffer.toString());
105: }
106: }
107:
108: }
|