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.jms.consumer;
21
22 import javax.mail.MessagingException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.james.api.jms.MailConsumer;
26 import org.apache.james.services.MailServer;
27 import org.apache.mailet.Mail;
28
29 /**
30 * Consumes a mail message by sending it to the main
31 * JAMES spool for processing.
32 */
33 public class SpoolToJamesMailConsumer implements MailConsumer {
34
35 private final MailServer mailServer;
36 private final Log log;
37
38 public SpoolToJamesMailConsumer(final MailServer mailServer, final Log log) {
39 super();
40 this.mailServer = mailServer;
41 this.log = log;
42 }
43
44 public void consume(Mail mail) {
45 if (log.isDebugEnabled()) {
46 log.debug("Consuming " + mail.getName());
47 }
48 log.trace(mail);
49
50 try {
51 mailServer.sendMail(mail);
52 } catch (MessagingException e) {
53 reportIssue(e, mail);
54 }
55 }
56
57 public void reportIssue(Exception e, Object message) {
58 log.warn(e.getMessage());
59 if (log.isDebugEnabled()) {
60 log.debug("Failed to process: " + message, e);
61 log.trace(message);
62 }
63 }
64
65 /**
66 * Renders suitably for logging.
67 * @return a <code>String</code> representation
68 * of this object.
69 */
70 public String toString()
71 {
72 final String TAB = " ";
73
74 final String retValue = "SpoolToJamesMailConsumer ( "
75 + "mailServer = " + this.mailServer + TAB
76 + " )";
77
78 return retValue;
79 }
80
81
82 }