KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > transport > mailto > client > AsyncMailClientService


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  *
7  * Created on Mar 12, 2004
8  */

9 package org.jboss.net.axis.transport.mailto.client;
10
11 import java.io.IOException JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import javax.mail.Flags JavaDoc;
17 import javax.mail.Message JavaDoc;
18 import javax.mail.MessagingException JavaDoc;
19
20 import org.apache.axis.EngineConfiguration;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.client.Call;
23 import org.apache.axis.client.Service;
24 import org.apache.axis.client.async.AsyncCall;
25 import org.apache.axis.client.async.IAsyncCallback;
26 import org.apache.axis.client.async.IAsyncResult;
27 import org.apache.axis.client.async.Status;
28 import org.apache.axis.message.addressing.AddressingHeaders;
29 import org.apache.axis.message.addressing.Constants;
30 import org.apache.axis.message.addressing.MessageID;
31 import org.jboss.net.axis.server.JMXEngineConfigurationFactory;
32 import org.jboss.net.axis.transport.mailto.client.AsyncMailClientServiceMBean;
33 import org.jboss.net.axis.transport.mailto.AbstractMailTransportService;
34 import org.jboss.net.axis.transport.mailto.MailConstants;
35
36 /**
37  * <dl>
38  * <dt><b>Title: </b><dd>Asynchronous Mail Client Service</dd>
39  * <p>
40  * <dt><b>Description: </b><dd>This service allows SOAP messages to be sent via email in an asynchronous way.
41  * <p>
42  * CAUTION: Requires ws-addressing handlers to be installed in the request chain.
43  * </dd>
44  * <p>
45  * </dl>
46  * @author <a HREF="mailto:jasone@greenrivercomputing.com">Jason Essington</a>
47  * @version $Revision: 1.1 $
48  *
49  * @jmx.mbean
50  * name="jboss.net:service=AsyncMailClient"
51  * description="Asynchronous mail sender"
52  * extends="org.jboss.net.axis.transport.mailto.AbstractMailTransportServiceMBean"
53  */

54 public class AsyncMailClientService extends AbstractMailTransportService implements AsyncMailClientServiceMBean,
55    MailConstants
56 {
57
58    private HashMap JavaDoc results = new HashMap JavaDoc();
59
60    /**
61     * @jmx.managed-operation
62     */

63    public Service getService()
64    {
65       EngineConfiguration config = JMXEngineConfigurationFactory.newJMXFactory(getEngineName()).getClientEngineConfig();
66       Service service = new Service(config);
67       return service;
68    }
69
70    /**
71     * @jmx.managed-operation
72     */

73    public void sendAsynchronously(Call call, IAsyncCallback callback, Object JavaDoc[] args)
74    {
75       AsyncCall aCall = new AsyncCall(call, callback);
76       IAsyncResult result = aCall.invoke(args);
77       results.put(call.getMessageContext(), result);
78    }
79
80    protected void processMessages(Message JavaDoc[] msgs)
81    {
82       if (log.isDebugEnabled())
83          log.debug("Entering: processMessages");
84
85       // first, lets clear out any responses that are done.
86
for (Iterator JavaDoc iter = results.entrySet().iterator(); iter.hasNext();)
87       {
88          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
89          IAsyncResult result = (IAsyncResult) entry.getValue();
90          StringBuffer JavaDoc buf = new StringBuffer JavaDoc("\nRemoving the following messages:");
91          if (result.getStatus() != Status.NONE)
92          {
93             iter.remove();
94             if (log.isDebugEnabled())
95             {
96                MessageContext mc = (MessageContext) entry.getKey();
97                String JavaDoc msgID = getMessageID(mc);
98                buf.append("\n\t" + msgID + "\t" + result.getStatus().toString());
99             }
100          }
101          if (log.isDebugEnabled())
102          {
103             buf.append("\n\t");
104             log.debug(buf.toString());
105          }
106       }
107
108       // build a map of message contexts that have message-ids
109
// if there is no message ID then the message must not be sent yet
110
HashMap JavaDoc contexts = new HashMap JavaDoc(results.size());
111       for (Iterator JavaDoc iter = results.keySet().iterator(); iter.hasNext();)
112       {
113          MessageContext mc = (MessageContext) iter.next();
114          String JavaDoc msgID = getMessageID(mc);
115          StringBuffer JavaDoc buf = new StringBuffer JavaDoc("\nThe following messages are waiting for responses:");
116          if (msgID != null)
117          {
118             if (log.isDebugEnabled())
119                buf.append("\n\t" + msgID);
120             contexts.put(msgID, mc);
121          }
122          if (log.isDebugEnabled())
123          {
124             buf.append("\n");
125             log.debug(buf.toString());
126          }
127       }
128
129       // iterate over all the messages processing them in turn.
130
for (int i = 0; i < msgs.length; i++)
131       {
132
133          Message JavaDoc message = msgs[i];
134          String JavaDoc inReplyTo = null;
135          try
136          {
137             inReplyTo = message.getHeader(HEADER_IN_REPLY_TO)[0];
138             if (log.isDebugEnabled())
139                log.debug("found a message inReplyTo: " + inReplyTo);
140          }
141          catch (NullPointerException JavaDoc e)
142          {
143             // no In-Reply-To: header, so skip this msg
144
continue;
145          }
146          catch (MessagingException JavaDoc e)
147          {
148             log.warn(e);
149             continue;
150          }
151
152          if (contexts.containsKey(inReplyTo))
153          {
154             if (log.isDebugEnabled())
155                log.debug("message " + inReplyTo + " correlates to a waiting request.");
156
157             MessageContext ctx = (MessageContext) contexts.get(inReplyTo);
158             IAsyncResult result = (IAsyncResult) results.get(ctx);
159
160             try
161             {
162                org.apache.axis.Message soapMsg = new org.apache.axis.Message(message.getInputStream(), false, message
163                   .getContentType(), null);
164                ctx.setResponseMessage(soapMsg);
165                ctx.setPastPivot(true);
166             }
167             catch (IOException JavaDoc e)
168             {
169                log.fatal(e);
170             }
171             catch (MessagingException JavaDoc e)
172             {
173                log.fatal(e);
174             }
175             
176             if (getDeleteMail())
177             {
178                try
179                {
180                   message.setFlag(Flags.Flag.DELETED, true);
181                }
182                catch (MessagingException JavaDoc e)
183                {
184                   log.warn("Unable to flag the message for deletion.", e);
185                }
186             }
187
188             // This isn't really an abort, it's more like an alarm clock for our sleeping transport.
189
result.abort();
190          }
191       }
192
193       if (log.isDebugEnabled())
194          log.debug("Exiting: processMessages");
195    }
196
197    private String JavaDoc getMessageID(MessageContext ctx)
198    {
199       String JavaDoc id = "no message-id";
200       
201       if (ctx.containsProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS))
202       {
203          AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS);
204          MessageID msgid = headers.getMessageID();
205          if (msgid != null)
206          {
207             id = msgid.toString();
208          }
209       }
210       return id;
211    }
212
213
214 }
215
Popular Tags