1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
* Transport.java
* Copyright (C) 2002 The Free Software Foundation
*
* This file is part of GNU JavaMail, a library.
*
* GNU JavaMail is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNU JavaMail is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* As a special exception, if you link this library with other files to
* produce an executable, this library does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* This exception does not however invalidate any other reasons why the
* executable file might be covered by the GNU General Public License.
*/
package javax.mail;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import javax.mail.event.TransportEvent;
import javax.mail.event.TransportListener;
/**
* An abstract class that models a message transport.
* Subclasses provide actual implementations.
* <p>
* Note that Transport extends the Service class, which provides many common
* methods for naming transports, connecting to transports, and listening to
* connection events.
*
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
* @version 1.3
*/
public abstract class Transport
extends Service
{
/*
* Transport listener list.
*/
private ArrayList transportListeners = null;
/**
* Constructor.
* @param session Session object for this Transport.
* @param url URLName object to be used for this Transport
*/
public Transport(Session session, URLName url)
{
super(session, url);
}
/**
* Send a message.
* The message will be sent to all recipient addresses specified in the
* message (as returned from the Message method
* <code>getAllRecipients</code>), using message transports appropriate to
* each address. The <code>send</code> method calls the
* <code>saveChanges</code> method on the message before sending it.
* <p>
* If any of the recipient addresses is detected to be invalid by the
* Transport during message submission, a SendFailedException is thrown.
* Clients can get more detail about the failure by examining the exception.
* Whether or not the message is still sent succesfully to any valid
* addresses depends on the Transport implementation. See
* SendFailedException for more details. Note also that success does not
* imply that the message was delivered to the ultimate recipient, as
* failures may occur in later stages of delivery. Once a Transport
* accepts a message for delivery to a recipient, failures that occur
* later should be reported to the user via another mechanism, such as
* returning the undeliverable message.
* @param msg the message to send
* @exception SendFailedException if the message could not be sent to
* some or any of the recipients.
*/
public static void send(Message msg)
throws MessagingException
{
msg.saveChanges();
/*
try
{
msg.writeTo(System.out);
} catch (java.io.IOException e)
{
e.printStackTrace();
}
*/
doSend(msg, msg.getAllRecipients());
}
/**
* Send the message to the specified addresses, ignoring any recipients
* specified in the message itself. The <code>send</code> method calls
* the <code>saveChanges</code> method on the message before sending it.
* @param msg the message to send
* @param addresses the addresses to which to send the message
* @exception SendFailedException if the message could not be sent to
* some or any of the recipients.
*/
public static void send(Message msg, Address[] addresses)
throws MessagingException
{
msg.saveChanges();
doSend(msg, addresses);
}
/*
* Performs the send after saveChanges() has been called.
*/
private static void doSend(Message msg, Address[] addresses)
throws MessagingException
{
if (addresses==null || addresses.length==0)
throw new SendFailedException("No recipient addresses");
HashMap addressesByType = new HashMap();
for (int i = 0; i<addresses.length; i++)
{
String type = addresses[i].getType();
if (addressesByType.containsKey(type))
((ArrayList)addressesByType.get(type)).add(addresses[i]);
else
{
ArrayList addressList = new ArrayList();
addressList.add(addresses[i]);
addressesByType.put(type, addressList);
}
}
int size = addressesByType.size();
if (size==0)
throw new SendFailedException("No recipient addresses");
Session session = msg.session;
if (msg.session==null)
msg.session = Session.getDefaultInstance(System.getProperties(), null);
MessagingException ex = null;
boolean error = false;
ArrayList validSent = new ArrayList();
ArrayList validUnsent = new ArrayList();
ArrayList invalid = new ArrayList();
for (Iterator i = addressesByType.values().iterator(); i.hasNext();)
{
ArrayList addressList = (ArrayList)i.next();
Address[] addressArray = new Address[addressList.size()];
addressList.toArray(addressArray);
if (addressArray.length<1)
break;
Transport transport = session.getTransport(addressArray[0]);
if (transport==null)
invalid.addAll(Arrays.asList(addressArray));
else
{
try
{
transport.connect();
transport.sendMessage(msg, addressArray);
}
catch (SendFailedException sfex)
{
error = true;
if (ex==null)
ex = sfex;
else
ex.setNextException(sfex);
Address[] a;
a = sfex.getValidSentAddresses();
if (a!=null)
validSent.addAll(Arrays.asList(a));
a = sfex.getValidUnsentAddresses();
if (a!=null)
validUnsent.addAll(Arrays.asList(a));
a = sfex.getInvalidAddresses();
if (a!=null)
invalid.addAll(Arrays.asList(a));
}
catch (MessagingException mex)
{
error = true;
if (ex==null)
ex = mex;
else
ex.setNextException(mex);
}
finally
{
transport.close();
}
}
}
if (error || invalid.size()!=0 || validSent.size()!=0)
{
Address[] validSentAddresses = null;
Address[] validUnsentAddresses = null;
Address[] invalidAddresses = null;
if (validSent.size() > 0)
{
validSentAddresses = new Address[validSent.size()];
validSent.toArray(validSentAddresses);
}
if (validUnsent.size() > 0)
{
validUnsentAddresses = new Address[validUnsent.size()];
validUnsent.toArray(validUnsentAddresses);
}
if (invalid.size() > 0)
{
invalidAddresses = new Address[invalid.size()];
invalid.toArray(invalidAddresses);
}
throw new SendFailedException("Send failed", ex,
validSentAddresses, validUnsentAddresses, invalidAddresses);
}
}
/**
* Send the Message to the specified list of addresses.
* An appropriate TransportEvent indicating the delivery status is
* delivered to any TransportListener registered on this Transport.
* Also, if any of the addresses is invalid, a SendFailedException is
* thrown. Note however, that the message is sent to the valid addresses.
* <p>
* Unlike the static <code>send</code> method, the <code>sendMessage</code>
* method does not call the <code>saveChanges</code> method on the message;
* the caller should do so.
* @param msg The Message to be sent
* @param addresses List of addresses to send this message to
* @exception SendFailedException if the send failed because of
* invalid addresses.
* @exception MessagingException if the connection is dead
* or not in the connected state
*/
public abstract void sendMessage(Message msg, Address[] addresses)
throws MessagingException;
// -- Event management --
/*
* Because the propagation of events of different kinds in the JavaMail
* API is so haphazard, I have here sacrificed a small time advantage for
* readability and consistency.
*
* All the various propagation methods now call a method with a name based
* on the eventual listener method name prefixed by 'fire', as is the
* preferred pattern for usage of the EventListenerList in Swing.
*
* Note that all events are currently delivered synchronously, where in
* Sun's implementation a different thread is used for event delivery.
*
* TODO Examine the impact of this.
*/
// -- Transport events --
/**
* Add a listener for Transport events.
*/
public void addTransportListener(TransportListener l)
{
if (transportListeners==null)
transportListeners = new ArrayList();
synchronized (transportListeners)
{
transportListeners.add(l);
}
}
/**
* Remove a listener for Transport events.
*/
public void removeTransportListener(TransportListener l)
{
if (transportListeners!=null)
{
synchronized (transportListeners)
{
transportListeners.remove(l);
}
}
}
/**
* Notify all TransportListeners. Transport implementations are expected to
* use this method to broadcast TransportEvents.
*/
protected void notifyTransportListeners(int type,
Address[] validSent, Address[] validUnsent, Address[] invalid,
Message msg)
{
TransportEvent event =
new TransportEvent(this, type, validSent, validUnsent, invalid, msg);
switch (type)
{
case TransportEvent.MESSAGE_DELIVERED:
fireMessageDelivered(event);
break;
case TransportEvent.MESSAGE_NOT_DELIVERED:
fireMessageNotDelivered(event);
break;
case TransportEvent.MESSAGE_PARTIALLY_DELIVERED:
fireMessagePartiallyDelivered(event);
break;
}
}
/*
* Propagates a MESSAGE_DELIVERED TransportEvent
* to all registered listeners.
*/
void fireMessageDelivered(TransportEvent event)
{
if (transportListeners!=null)
{
TransportListener[] l = null;
synchronized (transportListeners)
{
l = new TransportListener[transportListeners.size()];
transportListeners.toArray(l);
}
for (int i=0; i<l.length; i++)
l[i].messageDelivered(event);
}
}
/*
* Propagates a MESSAGE_NOT_DELIVERED TransportEvent
* to all registered listeners.
*/
void fireMessageNotDelivered(TransportEvent event)
{
if (transportListeners!=null)
{
TransportListener[] l = null;
synchronized (transportListeners)
{
l = new TransportListener[transportListeners.size()];
transportListeners.toArray(l);
}
for (int i=0; i<l.length; i++)
l[i].messageNotDelivered(event);
}
}
/*
* Propagates a MESSAGE_PARTIALLY_DELIVERED TransportEvent
* to all registered listeners.
*/
void fireMessagePartiallyDelivered(TransportEvent event)
{
if (transportListeners!=null)
{
TransportListener[] l = null;
synchronized (transportListeners)
{
l = new TransportListener[transportListeners.size()];
transportListeners.toArray(l);
}
for (int i=0; i<l.length; i++)
l[i].messagePartiallyDelivered(event);
}
}
}
|