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;
21
22 import javax.jms.JMSException;
23 import javax.jms.Message;
24 import javax.jms.MessageListener;
25 import javax.jms.TextMessage;
26 import javax.mail.MessagingException;
27
28 import org.apache.james.api.jms.MailBuilder;
29 import org.apache.james.api.jms.MailConsumer;
30 import org.apache.mailet.Mail;
31
32 /**
33 * <p>Listeners for mail.
34 * Supported message content is built into a {@link Mail}
35 * by the {@link MailBuilder} stategy. The <code>Mail</code>
36 * is then passed to the {@link MailConsumer} for further
37 * processing.
38 * </p><p>
39 * Responsible for extracting content from known message
40 * types.
41 * </p>
42 */
43 public class MailMessageListener implements MessageListener {
44
45 private final MailConsumer consumer;
46 private final MailBuilder builder;
47
48 public MailMessageListener(final MailConsumer consumer, final MailBuilder builder) {
49 this.consumer = consumer;
50 this.builder = builder;
51 }
52
53 /**
54 * Processes a message.
55 * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
56 */
57 public void onMessage(final Message message) {
58 try {
59 if (message instanceof TextMessage) {
60 final TextMessage textMessage = (TextMessage)message;
61 final String text = textMessage.getText();
62 final Mail mail = builder.build(text);
63 consumer.consume(mail);
64 }
65 } catch (JMSException e) {
66 consumer.reportIssue(e, message);
67 } catch (MessagingException e) {
68 consumer.reportIssue(e, message);
69 }
70 }
71 }