KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > email > MimeMailPoller


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You 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 implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.email;
18
19 import javax.jbi.JBIException;
20 import javax.jbi.messaging.DeliveryChannel;
21 import javax.jbi.messaging.InOnly;
22 import javax.jbi.messaging.MessageExchangeFactory;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.mail.Flags JavaDoc;
25 import javax.mail.Folder JavaDoc;
26 import javax.mail.Session JavaDoc;
27 import javax.mail.Store JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.servicemix.components.util.PollingComponentSupport;
33
34 /**
35  * A polling component which looks for emails in a mail server and sends them
36  * into the JBI bus as messages, deleting the messages by default when they
37  * are processed.
38  *
39  * @version $Revision: 426415 $
40  */

41 public class MimeMailPoller extends PollingComponentSupport {
42     
43     private static Log log = LogFactory.getLog(MimeMailPoller.class);
44     
45     private Session JavaDoc session;
46     private String JavaDoc hostName;
47     private String JavaDoc userName;
48     private String JavaDoc password;
49     private String JavaDoc mailBox;
50     private boolean debug;
51     private int maxFetchSize = 5;
52     private MimeMailMarshaler marshaler = new MimeMailMarshaler();
53
54     protected void init() throws JBIException {
55         super.init();
56         if (session == null) {
57             log.debug("No Session informed. Using default instance");
58             this.session = Session.getDefaultInstance(System.getProperties());
59         }
60         if (mailBox == null) {
61             log.debug("No mailbox informed. Using INBOX");
62             mailBox = "INBOX";
63         }
64         if (hostName == null) {
65             throw new JBIException("HostName not informed");
66         }
67         if (userName == null) {
68             throw new JBIException("UserName not informed");
69         }
70         if (password == null) {
71             throw new JBIException("Password not informed");
72         }
73         if (maxFetchSize < 1) {
74             throw new JBIException("Fetch Size must be at least 1");
75         }
76
77     }
78     
79     /**
80      * @return Returns the hostName.
81      */

82     public String JavaDoc getHostName() {
83         return hostName;
84     }
85
86     /**
87      * @param hostName The hostName to set.
88      */

89     public void setHostName(String JavaDoc hostName) {
90         this.hostName = hostName;
91     }
92
93     /**
94      * @return Returns the marshaler.
95      */

96     public MimeMailMarshaler getMarshaler() {
97         return marshaler;
98     }
99
100     /**
101      * @param marshaler The marshaler to set.
102      */

103     public void setMarshaler(MimeMailMarshaler marshaler) {
104         this.marshaler = marshaler;
105     }
106
107     public void poll() throws Exception JavaDoc {
108         Store JavaDoc store = null;
109         Folder JavaDoc folder = null;
110         try {
111             session.setDebug(isDebug());
112             store = session.getStore((mailBox.equals("INBOX")) ? "pop3"
113                     : "imap");
114             store.connect(hostName, userName, password);
115             folder = store.getFolder(mailBox);
116             if (folder == null || !folder.exists()) {
117                 throw new Exception JavaDoc("Folder not found or invalid: " + mailBox);
118             }
119             folder.open(Folder.READ_WRITE);
120             int msgCount = Math.min(folder.getMessageCount(),maxFetchSize);
121             DeliveryChannel channel = getDeliveryChannel();
122             MessageExchangeFactory mef = getExchangeFactory();
123             for(int i=1; i <= msgCount;i++) {
124                 MimeMessage JavaDoc mailMsg = (MimeMessage JavaDoc) folder.getMessage(i);
125                 InOnly io = mef.createInOnlyExchange();
126                 NormalizedMessage normalizedMessage = io.createMessage();
127                 this.marshaler.prepareExchange(io,normalizedMessage,mailMsg);
128                 io.setInMessage(normalizedMessage);
129                 channel.send(io);
130                 mailMsg.setFlag(Flags.Flag.DELETED,true);
131             }
132         } finally {
133             try {
134                 if (folder != null) {
135                     folder.close(true);
136                 }
137                 if (store != null) {
138                     store.close();
139                 }
140             } catch (Exception JavaDoc ignored) {}
141         }
142     }
143
144     /**
145      * @return Returns the debug.
146      */

147     public boolean isDebug() {
148         return debug;
149     }
150
151     /**
152      * @param debug
153      * The debug to set.
154      */

155     public void setDebug(boolean debug) {
156         this.debug = debug;
157     }
158
159     /**
160      * @return Returns the mailBox.
161      */

162     public String JavaDoc getMailBox() {
163         return mailBox;
164     }
165
166     /**
167      * @param mailBox
168      * The mailBox to set.
169      */

170     public void setMailBox(String JavaDoc mailBox) {
171         this.mailBox = mailBox;
172     }
173
174     /**
175      * @return Returns the password.
176      */

177     public String JavaDoc getPassword() {
178         return password;
179     }
180
181     /**
182      * @param password
183      * The password to set.
184      */

185     public void setPassword(String JavaDoc password) {
186         this.password = password;
187     }
188
189     /**
190      * @return Returns the userName.
191      */

192     public String JavaDoc getUserName() {
193         return userName;
194     }
195
196     /**
197      * @param userName
198      * The userName to set.
199      */

200     public void setUserName(String JavaDoc userName) {
201         this.userName = userName;
202     }
203
204     /**
205      * @return Returns the session.
206      */

207     public Session JavaDoc getSession() {
208         return session;
209     }
210
211     /**
212      * @param session
213      * The session to set.
214      */

215     public void setSession(Session JavaDoc session) {
216         this.session = session;
217     }
218
219     /**
220      * @return Returns the maxFetchSize.
221      */

222     public int getMaxFetchSize() {
223         return maxFetchSize;
224     }
225
226     /**
227      * @param maxFetchSize The maxFetchSize to set.
228      */

229     public void setMaxFetchSize(int maxFetchSize) {
230         this.maxFetchSize = maxFetchSize;
231     }
232
233 }
234
Popular Tags