KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > fetchpop > FetchPOP


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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.fetchpop;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.SocketException;
22 import java.util.Enumeration;
23 import java.util.Vector;
24 import javax.mail.MessagingException;
25 import javax.mail.internet.MimeMessage;
26 import org.apache.avalon.cornerstone.services.scheduler.Target;
27 import org.apache.avalon.framework.component.ComponentException;
28 import org.apache.avalon.framework.component.ComponentManager;
29 import org.apache.avalon.framework.component.Composable;
30 import org.apache.avalon.framework.component.DefaultComponentManager;
31 import org.apache.avalon.framework.configuration.Configurable;
32 import org.apache.avalon.framework.configuration.Configuration;
33 import org.apache.avalon.framework.configuration.ConfigurationException;
34 import org.apache.avalon.framework.logger.AbstractLogEnabled;
35 import org.apache.commons.net.pop3.POP3Client;
36 import org.apache.commons.net.pop3.POP3MessageInfo;
37 import org.apache.james.services.MailServer;
38 /**
39  *
40  * A class which fetches mail from a single POP account and inserts it
41  * into the incoming spool<br>
42  *
43  * <br>$Id: FetchPOP.java,v 1.5.4.4 2004/03/15 03:54:16 noel Exp $
44  *
45  */

46 public class FetchPOP extends AbstractLogEnabled implements Configurable, Target {
47     /**
48      * The MailServer service
49      */

50     private MailServer server;
51     /**
52      * The unique, identifying name for this task
53      */

54     private String fetchTaskName;
55     /**
56      * The POP3 server host name for this fetch task
57      */

58     private String popHost;
59     /**
60      * The POP3 user name for this fetch task
61      */

62     private String popUser;
63     /**
64      * The POP3 user password for this fetch task
65      */

66     private String popPass;
67     /**
68      * @see org.apache.avalon.cornerstone.services.scheduler.Target#targetTriggered(String)
69      */

70     public void targetTriggered(String arg0) {
71         if (getLogger().isDebugEnabled()) {
72             getLogger().debug(fetchTaskName + " fetcher starting fetch");
73         }
74         POP3Client pop = new POP3Client();
75         try {
76             pop.connect(popHost);
77             pop.login(popUser, popPass);
78             if (getLogger().isDebugEnabled()) {
79                 getLogger().debug("Login:" + pop.getReplyString());
80             }
81             pop.setState(POP3Client.TRANSACTION_STATE);
82             POP3MessageInfo[] messages = pop.listMessages();
83             getLogger().debug("List:" + pop.getReplyString());
84             Vector received = new Vector();
85             for (int i = 0; i < messages.length; i++) {
86                 try {
87                     InputStream in = new ReaderInputStream(pop.retrieveMessage(messages[i].number));
88                     getLogger().debug("Retrieve:" + pop.getReplyString());
89                     MimeMessage message = null;
90                     message = new MimeMessage(null, in);
91                     in.close();
92                     message.addHeader("X-fetched-from", fetchTaskName);
93                     message.saveChanges();
94                     try {
95                         server.sendMail(message);
96                         getLogger().debug("Sent message " + message.toString());
97                         received.add(messages[i]);
98                     } catch (MessagingException innerE) {
99                         getLogger().error("can't insert message " + message.toString() + "created from "+messages[i].identifier);
100                     }
101                 } catch (MessagingException outerE) {
102                     getLogger().error("can't create message out of fetched message "+messages[i].identifier);
103                 }
104             }
105             Enumeration enum = received.elements();
106             while (enum.hasMoreElements()) {
107                 POP3MessageInfo element = (POP3MessageInfo) enum.nextElement();
108                 pop.deleteMessage(element.number);
109                 if (getLogger().isDebugEnabled()) {
110                     getLogger().debug("Delete:" + pop.getReplyString());
111                 }
112             }
113         } catch (SocketException e) {
114             getLogger().error(e.getMessage());
115         } catch (IOException e) {
116             getLogger().error(e.getMessage());
117         } finally {
118             try {
119                 pop.logout();
120                 if (getLogger().isDebugEnabled()) {
121                     getLogger().debug("logout:" + pop.getReplyString());
122                 }
123                 pop.disconnect();
124             } catch (IOException e) {
125                 getLogger().error(e.getMessage());
126             }
127         }
128     }
129     /**
130      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
131      */

132     public void compose(final ComponentManager componentManager) throws ComponentException {
133         try {
134             server = (MailServer) componentManager.lookup(MailServer.ROLE);
135         } catch (ClassCastException cce) {
136             StringBuffer errorBuffer =
137                 new StringBuffer(128).append("Component ").append(MailServer.ROLE).append(
138                     "does not implement the required interface.");
139             throw new ComponentException(errorBuffer.toString());
140         }
141     }
142     /**
143      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
144      */

145     public void configure(Configuration conf) throws ConfigurationException {
146         this.popHost = conf.getChild("host").getValue();
147         this.popUser = conf.getChild("user").getValue();
148         this.popPass = conf.getChild("password").getValue();
149         this.fetchTaskName = conf.getAttribute("name");
150         if (getLogger().isDebugEnabled()) {
151             getLogger().info("Configured FetchPOP fetch task " + fetchTaskName);
152         }
153     }
154 }
155
Popular Tags