1 9 package org.jboss.net.axis.transport.mailto.client; 10 11 import java.io.IOException ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import javax.mail.Flags ; 17 import javax.mail.Message ; 18 import javax.mail.MessagingException ; 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 54 public class AsyncMailClientService extends AbstractMailTransportService implements AsyncMailClientServiceMBean, 55 MailConstants 56 { 57 58 private HashMap results = new HashMap (); 59 60 63 public Service getService() 64 { 65 EngineConfiguration config = JMXEngineConfigurationFactory.newJMXFactory(getEngineName()).getClientEngineConfig(); 66 Service service = new Service(config); 67 return service; 68 } 69 70 73 public void sendAsynchronously(Call call, IAsyncCallback callback, Object [] 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 [] msgs) 81 { 82 if (log.isDebugEnabled()) 83 log.debug("Entering: processMessages"); 84 85 for (Iterator iter = results.entrySet().iterator(); iter.hasNext();) 87 { 88 Map.Entry entry = (Map.Entry ) iter.next(); 89 IAsyncResult result = (IAsyncResult) entry.getValue(); 90 StringBuffer buf = new StringBuffer ("\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 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 HashMap contexts = new HashMap (results.size()); 111 for (Iterator iter = results.keySet().iterator(); iter.hasNext();) 112 { 113 MessageContext mc = (MessageContext) iter.next(); 114 String msgID = getMessageID(mc); 115 StringBuffer buf = new StringBuffer ("\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 for (int i = 0; i < msgs.length; i++) 131 { 132 133 Message message = msgs[i]; 134 String 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 e) 142 { 143 continue; 145 } 146 catch (MessagingException 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 e) 168 { 169 log.fatal(e); 170 } 171 catch (MessagingException 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 e) 183 { 184 log.warn("Unable to flag the message for deletion.", e); 185 } 186 } 187 188 result.abort(); 190 } 191 } 192 193 if (log.isDebugEnabled()) 194 log.debug("Exiting: processMessages"); 195 } 196 197 private String getMessageID(MessageContext ctx) 198 { 199 String 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 |