1 21 22 27 28 package javax.mail; 29 30 import java.io.*; 31 import java.net.*; 32 import java.util.*; 33 import javax.mail.event.*; 34 35 48 49 public abstract class Service { 50 51 54 protected Session session; 55 56 59 protected URLName url = null; 60 61 65 protected boolean debug = false; 66 67 private boolean connected = false; 68 private Vector connectionListeners = null; 69 70 76 protected Service(Session session, URLName urlname) { 77 this.session = session; 78 url = urlname; 79 debug = session.getDebug(); 80 } 81 82 104 public void connect() throws MessagingException { 105 connect(null, null, null); 106 } 107 108 154 public void connect(String host, String user, String password) 155 throws MessagingException { 156 connect(host, -1, user, password); 157 } 158 159 175 public void connect(String user, String password) throws MessagingException { 176 connect(null, user, password); 177 } 178 179 193 public void connect(String host, int port, String user, String password) 194 throws MessagingException { 195 196 if (isConnected()) 198 throw new IllegalStateException ("already connected"); 199 200 PasswordAuthentication pw; 201 boolean connected = false; 202 boolean save = false; 203 String protocol = null; 204 String file = null; 205 206 if (url != null) { 210 protocol = url.getProtocol(); 211 if (host == null) 212 host = url.getHost(); 213 if (port == -1) 214 port = url.getPort(); 215 216 if (user == null) { 217 user = url.getUsername(); 218 if (password == null) password = url.getPassword(); 220 } else { 221 if (password == null && user.equals(url.getUsername())) 222 password = url.getPassword(); 224 } 225 226 file = url.getFile(); 227 } 228 229 if (protocol != null) { 231 if (host == null) 232 host = session.getProperty("mail." + protocol + ".host"); 233 if (user == null) 234 user = session.getProperty("mail." + protocol + ".user"); 235 } 236 237 if (host == null) 239 host = session.getProperty("mail.host"); 240 241 if (user == null) 242 user = session.getProperty("mail.user"); 243 244 if (user == null) { 246 try { 247 user = System.getProperty("user.name"); 248 } catch (SecurityException sex) { 249 if (debug) 250 sex.printStackTrace(session.getDebugOut()); 251 } 252 } 253 254 if (password == null && url != null) { 256 setURLName(new URLName (protocol, host, port, file, user, password)); 258 pw = session.getPasswordAuthentication(getURLName()); 259 if (pw != null) { 260 if (user == null) { 261 user = pw.getUserName(); 262 password = pw.getPassword(); 263 } else if (user.equals(pw.getUserName())) { 264 password = pw.getPassword(); 265 } 266 } else 267 save = true; 268 } 269 270 AuthenticationFailedException authEx = null; 274 try { 275 connected = protocolConnect(host, port, user, password); 276 } catch (AuthenticationFailedException ex) { 277 authEx = ex; 278 } 279 280 if (!connected) { 282 InetAddress addr; 283 try { 284 addr = InetAddress.getByName(host); 285 } catch (UnknownHostException e) { 286 addr = null; 287 } 288 pw = session.requestPasswordAuthentication( 289 addr, port, 290 protocol, 291 null, user); 292 if (pw != null) { 293 user = pw.getUserName(); 294 password = pw.getPassword(); 295 296 connected = protocolConnect(host, port, user, password); 298 } 299 } 300 301 if (!connected) { 303 if (authEx != null) 304 throw authEx; 305 else 306 throw new AuthenticationFailedException (); 307 } 308 309 setURLName(new URLName (protocol, host, port, file, user, password)); 310 311 if (save) 312 session.setPasswordAuthentication(getURLName(), 313 new PasswordAuthentication (user, password)); 314 315 setConnected(true); 317 318 notifyConnectionListeners(ConnectionEvent.OPENED); 320 } 321 322 323 354 protected boolean protocolConnect(String host, int port, String user, 355 String password) throws MessagingException { 356 return false; 357 } 358 359 371 public boolean isConnected() { 372 return connected; 373 } 374 375 388 protected void setConnected(boolean connected) { 389 this.connected = connected; 390 } 391 392 411 public synchronized void close() throws MessagingException { 412 setConnected(false); 413 notifyConnectionListeners(ConnectionEvent.CLOSED); 414 } 415 416 430 public URLName getURLName() { 431 if (url != null && (url.getPassword() != null || url.getFile() != null)) 432 return new URLName (url.getProtocol(), url.getHost(), 433 url.getPort(), null , 434 url.getUsername(), null ); 435 else 436 return url; 437 } 438 439 456 protected void setURLName(URLName url) { 457 this.url = url; 458 } 459 460 469 public synchronized void addConnectionListener(ConnectionListener l) { 470 if (connectionListeners == null) 471 connectionListeners = new Vector(); 472 connectionListeners.addElement(l); 473 } 474 475 484 public synchronized void removeConnectionListener(ConnectionListener l) { 485 if (connectionListeners != null) 486 connectionListeners.removeElement(l); 487 } 488 489 499 protected synchronized void notifyConnectionListeners(int type) { 500 if (connectionListeners != null) { 501 ConnectionEvent e = new ConnectionEvent(this, type); 502 queueEvent(e, connectionListeners); 503 } 504 505 515 if (type == ConnectionEvent.CLOSED) 516 terminateQueue(); 517 } 518 519 523 public String toString() { 524 URLName url = getURLName(); 525 if (url != null) 526 return url.toString(); 527 else 528 return super.toString(); 529 } 530 531 534 private EventQueue q; 535 536 542 private Object qLock = new Object (); 543 544 547 protected void queueEvent(MailEvent event, Vector vector) { 548 synchronized (qLock) { 550 if (q == null) 551 q = new EventQueue (); 552 } 553 554 562 Vector v = (Vector)vector.clone(); 563 q.enqueue(event, v); 564 } 565 566 static class TerminatorEvent extends MailEvent { 567 private static final long serialVersionUID = 5542172141759168416L; 568 569 TerminatorEvent() { 570 super(new Object ()); 571 } 572 573 public void dispatch(Object listener) { 574 Thread.currentThread().interrupt(); 576 } 577 } 578 579 private void terminateQueue() { 581 synchronized (qLock) { 582 if (q != null) { 583 Vector dummyListeners = new Vector(); 584 dummyListeners.setSize(1); q.enqueue(new TerminatorEvent(), dummyListeners); 586 q = null; 587 } 588 } 589 } 590 591 594 protected void finalize() throws Throwable { 595 super.finalize(); 596 terminateQueue(); 597 } 598 } 599 | Popular Tags |