1 21 22 27 28 package javax.mail; 29 30 import java.io.*; 31 import java.lang.*; 32 import java.util.Vector ; 33 import java.util.StringTokenizer ; 34 import javax.mail.search.SearchTerm ; 35 import javax.mail.event.*; 36 37 98 99 public abstract class Folder { 100 101 104 protected Store store; 105 106 112 protected int mode = -1; 113 114 119 protected Folder(Store store) { 120 this.store = store; 121 } 122 123 130 public abstract String getName(); 131 132 142 public abstract String getFullName(); 143 144 152 public URLName getURLName() throws MessagingException { 153 URLName storeURL = getStore().getURLName(); 154 String fullname = getFullName(); 155 StringBuffer encodedName = new StringBuffer (); 156 char separator = getSeparator(); 157 158 if (fullname != null) { 159 174 encodedName.append(fullname); 176 } 177 178 182 return new URLName (storeURL.getProtocol(), storeURL.getHost(), 183 storeURL.getPort(), encodedName.toString(), 184 storeURL.getUsername(), 185 null ); 186 } 187 188 193 public Store getStore() { 194 return store; 195 } 196 197 207 public abstract Folder getParent() throws MessagingException ; 208 209 218 public abstract boolean exists() throws MessagingException ; 219 220 256 public abstract Folder [] list(String pattern) throws MessagingException ; 257 258 289 public Folder [] listSubscribed(String pattern) throws MessagingException { 290 return list(pattern); 291 } 292 293 306 307 public Folder [] list() throws MessagingException { 308 return list("%"); 309 } 310 311 325 public Folder [] listSubscribed() throws MessagingException { 326 return listSubscribed("%"); 327 } 328 329 338 public abstract char getSeparator() throws MessagingException ; 339 340 343 public final static int HOLDS_MESSAGES = 0x01; 344 345 348 public final static int HOLDS_FOLDERS = 0x02; 349 350 362 public abstract int getType() throws MessagingException ; 363 364 379 public abstract boolean create(int type) throws MessagingException ; 380 381 390 public boolean isSubscribed() { 391 return true; 392 } 393 394 410 public void setSubscribed(boolean subscribe) 411 throws MessagingException { 412 throw new MethodNotSupportedException (); 413 } 414 415 438 public abstract boolean hasNewMessages() throws MessagingException ; 439 440 460 public abstract Folder getFolder(String name) 461 throws MessagingException ; 462 463 531 public abstract boolean delete(boolean recurse) 532 throws MessagingException ; 533 534 551 public abstract boolean renameTo(Folder f) throws MessagingException ; 552 553 557 public static final int READ_ONLY = 1; 558 559 562 public static final int READ_WRITE = 2; 563 564 588 public abstract void open(int mode) throws MessagingException ; 589 590 603 public abstract void close(boolean expunge) throws MessagingException ; 604 605 609 public abstract boolean isOpen(); 610 611 622 public int getMode() { 623 if (!isOpen()) 624 throw new IllegalStateException ("Folder not open"); 625 return mode; 626 } 627 628 640 public abstract Flags getPermanentFlags(); 641 642 663 public abstract int getMessageCount() throws MessagingException ; 664 665 692 public synchronized int getNewMessageCount() 693 throws MessagingException { 694 if (!isOpen()) 695 return -1; 696 697 int newmsgs = 0; 698 int total = getMessageCount(); 699 for (int i = 1; i <= total; i++) { 700 try { 701 if (getMessage(i).isSet(Flags.Flag.RECENT)) 702 newmsgs++; 703 } catch (MessageRemovedException me) { 704 continue; 706 } 707 } 708 return newmsgs; 709 } 710 711 738 public synchronized int getUnreadMessageCount() 739 throws MessagingException { 740 if (!isOpen()) 741 return -1; 742 743 int unread = 0; 744 int total = getMessageCount(); 745 for (int i = 1; i <= total; i++) { 746 try { 747 if (!getMessage(i).isSet(Flags.Flag.SEEN)) 748 unread++; 749 } catch (MessageRemovedException me) { 750 continue; 752 } 753 } 754 return unread; 755 } 756 757 785 public synchronized int getDeletedMessageCount() throws MessagingException { 786 if (!isOpen()) 787 return -1; 788 789 int deleted = 0; 790 int total = getMessageCount(); 791 for (int i = 1; i <= total; i++) { 792 try { 793 if (getMessage(i).isSet(Flags.Flag.DELETED)) 794 deleted++; 795 } catch (MessageRemovedException me) { 796 continue; 798 } 799 } 800 return deleted; 801 } 802 803 835 public abstract Message getMessage(int msgnum) 836 throws MessagingException ; 837 838 862 public synchronized Message [] getMessages(int start, int end) 863 throws MessagingException { 864 Message [] msgs = new Message [end - start +1]; 865 for (int i = start; i <= end; i++) 866 msgs[i - start] = getMessage(i); 867 return msgs; 868 } 869 870 892 public synchronized Message [] getMessages(int[] msgnums) 893 throws MessagingException { 894 int len = msgnums.length; 895 Message [] msgs = new Message [len]; 896 for (int i = 0; i < len; i++) 897 msgs[i] = getMessage(msgnums[i]); 898 return msgs; 899 } 900 901 923 public synchronized Message [] getMessages() throws MessagingException { 924 if (!isOpen()) throw new IllegalStateException ("Folder not open"); 926 int total = getMessageCount(); 927 Message [] msgs = new Message [total]; 928 for (int i = 1; i <= total; i++) 929 msgs[i-1] = getMessage(i); 930 return msgs; 931 } 932 933 948 public abstract void appendMessages(Message [] msgs) 949 throws MessagingException ; 950 951 990 public void fetch(Message [] msgs, FetchProfile fp) 991 throws MessagingException { 992 return; 993 } 994 995 1022 public synchronized void setFlags(Message [] msgs, 1023 Flags flag, boolean value) throws MessagingException { 1024 for (int i = 0; i < msgs.length; i++) { 1025 try { 1026 msgs[i].setFlags(flag, value); 1027 } catch (MessageRemovedException me) { 1028 } 1030 } 1031 } 1032 1033 1065 public synchronized void setFlags(int start, int end, 1066 Flags flag, boolean value) throws MessagingException { 1067 for (int i = start; i <= end; i++) { 1068 try { 1069 Message msg = getMessage(i); 1070 msg.setFlags(flag, value); 1071 } catch (MessageRemovedException me) { 1072 } 1074 } 1075 } 1076 1077 1107 public synchronized void setFlags(int[] msgnums, 1108 Flags flag, boolean value) throws MessagingException { 1109 for (int i = 0; i < msgnums.length; i++) { 1110 try { 1111 Message msg = getMessage(msgnums[i]); 1112 msg.setFlags(flag, value); 1113 } catch (MessageRemovedException me) { 1114 } 1116 } 1117 } 1118 1119 1147 public void copyMessages(Message [] msgs, Folder folder) 1148 throws MessagingException { 1149 if (!folder.exists()) 1150 throw new FolderNotFoundException ( 1151 folder.getFullName() + " does not exist", 1152 folder); 1153 1154 folder.appendMessages(msgs); 1155 } 1156 1157 1189 public abstract Message [] expunge() throws MessagingException ; 1190 1191 1212 public Message [] search(SearchTerm term) throws MessagingException { 1213 return search(term, getMessages()); 1214 } 1215 1216 1245 public Message [] search(SearchTerm term, Message [] msgs) 1246 throws MessagingException { 1247 Vector matchedMsgs = new Vector (); 1248 1249 for (int i = 0; i < msgs.length; i++) { 1251 try { 1252 if (msgs[i].match(term)) matchedMsgs.addElement(msgs[i]); } catch(MessageRemovedException mrex) { } 1255 } 1256 1257 Message [] m = new Message [matchedMsgs.size()]; 1258 matchedMsgs.copyInto(m); 1259 return m; 1260 } 1261 1262 1282 1283 private volatile Vector connectionListeners = null; 1285 1286 1295 public synchronized void 1296 addConnectionListener(ConnectionListener l) { 1297 if (connectionListeners == null) 1298 connectionListeners = new Vector (); 1299 connectionListeners.addElement(l); 1300 } 1301 1302 1311 public synchronized void 1312 removeConnectionListener(ConnectionListener l) { 1313 if (connectionListeners != null) 1314 connectionListeners.removeElement(l); 1315 } 1316 1317 1330 protected void notifyConnectionListeners(int type) { 1331 if (connectionListeners != null) { 1332 ConnectionEvent e = new ConnectionEvent(this, type); 1333 queueEvent(e, connectionListeners); 1334 } 1335 1336 1346 if (type == ConnectionEvent.CLOSED) 1347 terminateQueue(); 1348 } 1349 1350 private volatile Vector folderListeners = null; 1352 1353 1362 public synchronized void addFolderListener(FolderListener l) { 1363 if (folderListeners == null) 1364 folderListeners = new Vector (); 1365 folderListeners.addElement(l); 1366 } 1367 1368 1377 public synchronized void removeFolderListener(FolderListener l) { 1378 if (folderListeners != null) 1379 folderListeners.removeElement(l); 1380 } 1381 1382 1397 protected void notifyFolderListeners(int type) { 1398 if (folderListeners != null) { 1399 FolderEvent e = new FolderEvent(this, this, type); 1400 queueEvent(e, folderListeners); 1401 } 1402 store.notifyFolderListeners(type, this); 1403 } 1404 1405 1422 protected void notifyFolderRenamedListeners(Folder folder) { 1423 if (folderListeners != null) { 1424 FolderEvent e = new FolderEvent(this, this, folder, 1425 FolderEvent.RENAMED); 1426 queueEvent(e, folderListeners); 1427 } 1428 store.notifyFolderRenamedListeners(this, folder); 1429 } 1430 1431 private volatile Vector messageCountListeners = null; 1433 1434 1443 public synchronized void addMessageCountListener(MessageCountListener l) { 1444 if (messageCountListeners == null) 1445 messageCountListeners = new Vector (); 1446 messageCountListeners.addElement(l); 1447 } 1448 1449 1458 public synchronized void 1459 removeMessageCountListener(MessageCountListener l) { 1460 if (messageCountListeners != null) 1461 messageCountListeners.removeElement(l); 1462 } 1463 1464 1476 protected void notifyMessageAddedListeners(Message [] msgs) { 1477 if (messageCountListeners == null) 1478 return; 1479 1480 MessageCountEvent e = new MessageCountEvent( 1481 this, 1482 MessageCountEvent.ADDED, 1483 false, 1484 msgs); 1485 1486 queueEvent(e, messageCountListeners); 1487 } 1488 1489 1501 protected void notifyMessageRemovedListeners(boolean removed, 1502 Message [] msgs) { 1503 if (messageCountListeners == null) 1504 return; 1505 1506 MessageCountEvent e = new MessageCountEvent( 1507 this, 1508 MessageCountEvent.REMOVED, 1509 removed, 1510 msgs); 1511 queueEvent(e, messageCountListeners); 1512 } 1513 1514 private volatile Vector messageChangedListeners = null; 1516 1517 1526 public synchronized void 1527 addMessageChangedListener(MessageChangedListener l) { 1528 if (messageChangedListeners == null) 1529 messageChangedListeners = new Vector (); 1530 messageChangedListeners.addElement(l); 1531 } 1532 1533 1542 public synchronized void 1543 removeMessageChangedListener(MessageChangedListener l) { 1544 if (messageChangedListeners != null) 1545 messageChangedListeners.removeElement(l); 1546 } 1547 1548 1558 protected void notifyMessageChangedListeners(int type, Message msg) { 1559 if (messageChangedListeners == null) 1560 return; 1561 1562 MessageChangedEvent e = new MessageChangedEvent(this, type, msg); 1563 queueEvent(e, messageChangedListeners); 1564 } 1565 1566 1569 private EventQueue q; 1570 1571 1577 private Object qLock = new Object (); 1578 1579 1582 private void queueEvent(MailEvent event, Vector vector) { 1583 synchronized (qLock) { 1585 if (q == null) 1586 q = new EventQueue (); 1587 } 1588 1589 1597 Vector v = (Vector )vector.clone(); 1598 q.enqueue(event, v); 1599 } 1600 1601 static class TerminatorEvent extends MailEvent { 1602 private static final long serialVersionUID = 3765761925441296565L; 1603 1604 TerminatorEvent() { 1605 super(new Object ()); 1606 } 1607 1608 public void dispatch(Object listener) { 1609 Thread.currentThread().interrupt(); 1611 } 1612 } 1613 1614 private void terminateQueue() { 1616 synchronized (qLock) { 1617 if (q != null) { 1618 Vector dummyListeners = new Vector (); 1619 dummyListeners.setSize(1); q.enqueue(new TerminatorEvent(), dummyListeners); 1621 q = null; 1622 } 1623 } 1624 } 1625 1626 protected void finalize() throws Throwable { 1627 super.finalize(); 1628 terminateQueue(); 1629 } 1630 1631 1636 1637 public String toString() { 1638 String s = getFullName(); 1639 if (s != null) 1640 return s; 1641 else 1642 return super.toString(); 1643 } 1644} 1645 | Popular Tags |