KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > adapter > mail > inflow > MailActivation


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.resource.adapter.mail.inflow;
23
24 import java.lang.reflect.Method JavaDoc;
25 import javax.resource.spi.endpoint.MessageEndpointFactory JavaDoc;
26 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
27 import javax.resource.spi.work.Work JavaDoc;
28 import javax.mail.Message JavaDoc;
29
30 import org.jboss.resource.adapter.mail.MailResourceAdapter;
31 import org.jboss.logging.Logger;
32
33 /**
34  * The MailActivation encapsulates a MailResourceAdapter#endpointActivation
35  * {@link javax.resource.spi.ResourceAdapter#endpointActivation(MessageEndpointFactory,javax.resource.spi.ActivationSpec)}
36  *
37  * @author Scott.Stark@jboss.org
38  * @version $Revision: 37459 $
39  */

40 public class MailActivation
41    implements Comparable JavaDoc, Work JavaDoc
42 {
43    private static final Logger log = Logger.getLogger(MailActivation.class);
44    /**
45     * The MailListener.onMessage method
46     */

47    public static final Method JavaDoc ON_MESSAGE;
48
49    static
50    {
51       try
52       {
53          Class JavaDoc[] sig = {Message JavaDoc.class};
54          ON_MESSAGE = MailListener.class.getMethod("onMessage", sig);
55       }
56       catch (Exception JavaDoc e)
57       {
58          throw new RuntimeException JavaDoc(e);
59       }
60    }
61
62    /** A flag indicated if the unit of work has been released */
63    private boolean released;
64    /** The logging trace level flag */
65    private boolean trace;
66    /** The time at which the next new msgs check should be performed */
67    private long nextNewMsgCheckTime;
68    /** The resource adapter */
69    protected MailResourceAdapter ra;
70    /** The activation spec for the mail folder */
71    protected MailActivationSpec spec;
72    /** The message endpoint factory */
73    protected MessageEndpointFactory JavaDoc endpointFactory;
74
75    public MailActivation(MailResourceAdapter ra, MessageEndpointFactory JavaDoc endpointFactory,
76       MailActivationSpec spec)
77    {
78       this.ra = ra;
79       this.endpointFactory = endpointFactory;
80       this.spec = spec;
81       this.trace = log.isTraceEnabled();
82    }
83
84    public long getNextNewMsgCheckTime()
85    {
86       return nextNewMsgCheckTime;
87    }
88    public void updateNextNewMsgCheckTime(long now)
89    {
90       nextNewMsgCheckTime = now + spec.getPollingInterval();
91    }
92
93    public int compareTo(Object JavaDoc obj)
94    {
95       MailActivation ma = (MailActivation) obj;
96       long compareTo = nextNewMsgCheckTime - ma.getNextNewMsgCheckTime();
97       return (int) compareTo;
98    }
99
100    public boolean isReleased()
101    {
102       return released;
103    }
104
105    // --- Begin Work interface
106
public void release()
107    {
108       released = true;
109       if( trace )
110          log.trace("released");
111    }
112
113    public void run()
114    {
115       released = false;
116       if( trace )
117          log.trace("Begin new msgs check");
118       try
119       {
120          MailFolder mailFolder = new MailFolder(spec);
121          mailFolder.open();
122          Message JavaDoc[] msgs = mailFolder.getNewMessages();
123          for(int n = 0; released == false && n < msgs.length; n ++)
124          {
125             Message JavaDoc msg = msgs[n];
126             deliverMsg(msg);
127          }
128          mailFolder.close();
129       }
130       catch (Exception JavaDoc e)
131       {
132          log.error("Failed to execute folder check, spec="+spec);
133       }
134       if( trace )
135          log.trace("End new msgs check");
136    }
137    // --- End Work interface
138

139    private void deliverMsg(Message JavaDoc msg)
140    {
141       MessageEndpoint JavaDoc endpoint = null;
142       try
143       {
144          endpoint = endpointFactory.createEndpoint(null);
145          if (endpoint != null)
146          {
147             if( trace )
148                log.trace("deliverMsg, msg subject="+msg.getSubject());
149             MailListener listener = (MailListener) endpoint;
150             listener.onMessage(msg);
151          }
152       }
153       catch (Throwable JavaDoc e)
154       {
155          log.debug("onMessage delivery failure", e);
156       }
157       finally
158       {
159          if (endpoint != null)
160          {
161             endpoint.release();
162          }
163       }
164    }
165
166 }
167
Popular Tags