pop3store.java

来自「Eclipse RCP下编写的工作管理软件代码」· Java 代码 · 共 1,194 行 · 第 1/3 页

JAVA
1,194
字号
					String rvalue = (eqpos != -1 && eqpos < pair.length() - 1) ? pair.substring(eqpos + 1) : null;
					if (lvalue != null) {
						options.put(lvalue, rvalue);
					}
				}
			}
		}
		return options;
	}

	/**
	 * UIDL僼傽僀儖傪撉傒崬傓.
	 */
	private void loadUIDL() {
		uidlFile = new File(getRootDirectory(), UIDL_FILENAME);
		try {
			FileInputStream fis = new FileInputStream(uidlFile);
			BufferedInputStream bis = new BufferedInputStream(fis, 16 * 1024);
			ObjectInputStream ois = new ObjectInputStream(bis);
			uidList = (Vector) ois.readObject();
			ois.close();
		} catch (FileNotFoundException fnfe) {
			String dbgmsg = "UIDL file " + UIDL_FILENAME + " not found.";
			debugOut(dbgmsg);
			notifyStoreListeners(StoreEvent.NOTICE, dbgmsg);
			uidList = new Vector();
		} catch (IOException ioe) {
			String dbgmsg = "IOException " + ioe;
			debugOut(dbgmsg);
			notifyStoreListeners(StoreEvent.ALERT, dbgmsg);
			uidList = new Vector();
		} catch (ClassNotFoundException cnfe) {
			String dbgmsg = "ClassNotFoundException " + cnfe;
			debugOut(dbgmsg);
			notifyStoreListeners(StoreEvent.ALERT, dbgmsg);
			uidList = new Vector();
		}
	}
	
	/**
	 * 儖乕僩僨傿儗僋僩儕傪曉偡.
	 * 儖乕僩僨傿儗僋僩儕偲偼丄儊僢僙乕僕傪曐懚偡傞僼傽僀儖傪嶌惉偡傞嵺偺
	 * 婎弨僨傿儗僋僩儕偺偙偲偱偁傞.
	 *
	 * @return	儖乕僩僨傿儗僋僩儕.
	 */
	protected File getRootDirectory() {
		return rootDirectory;
	}
	
	/**
	 * 僼僅儖僟柤偲僀儀儞僩柤偑儊僢僙乕僕庴怣懳徾偱偁傞応崌丄
	 * 僒乕僶偐傜儊僢僙乕僕傪庴怣偟偰丄巜掕偝傟偨僼僅儖僟偵捛壛偡傞.
	 * 僞僀儅偑弶婜壔偝傟偰偄側偄応崌丄弶婜壔張棟傕峴偆.
	 *
	 * @param	folder	儊僢僙乕僕傪捛壛偡傞 Folder.
	 * @param	actionName	僀儀儞僩柤.
	 */
	protected synchronized void receiveMessages(POP3Folder folder, String actionName) throws MessagingException {
		String dbgmsg = "POP3Store.receiveMessages(): folder=" + folder.getFullName() + " actionName=" + actionName;
		if (receivingFolderName.equalsIgnoreCase(folder.getFullName()) && checkActions.contains(actionName)) {
			dbgmsg += " receiving...";
			debugOut(dbgmsg);
			if (checkInterval > 0 && timerThread == null) {
				timerThread = new IntervalTimer(folder, receiver);
				timerThread.start();
			} else {
				receiver.receive(folder);
			}
		} else {
			dbgmsg += " skipped.";
			debugOut(dbgmsg);
		}
	}
	
	/**
	 * Folder.list()偑巊梡偡傞FilenameFilter偺僀儞僗僞儞僗傪曉偡丅
	 *
	 * @see	jp.gr.java_conf.roadster.net.pop.POP3Folder.list()
	 */
	FilenameFilter getFilenameFilter(String pattern) {
		return new SubFolderFilter(pattern, listHiddenFolders);
	}
	
	/**
	 * 儊僢僙乕僕庴怣僋儔僗.
	 * 僒乕僶偐傜儊僢僙乕僕傪庴怣偟丄巜掕偝傟偨僼僅儖僟偵捛壛偡傞.
	 */
	class DefaultMessageReceiver implements MessageReceiver {
		/**
		 * POP3Client.
		 */
		private POP3Client client;
		
		/**
		 * 儊僢僙乕僕傪僒乕僶偐傜庴怣偟丄僼僅儖僟偵捛壛偡傞 DefaultMessageReceiver 僆僽僕僃僋僩傪嶌惉偡傞.
		 *
		 * @param	client	POP3Client.
		 */
		public DefaultMessageReceiver(POP3Client client) {
			this.client = client;
		}
		
		/*
		 * 僒乕僶偐傜儊僢僙乕僕傪庴怣偟丄巜掕偝傟偨僼僅儖僟偵捛壛偡傞.
		 * 慡偰偺儊僢僙乕僕偑庴怣偝傟傞傑偱僽儘僢僋偡傞.
		 * 僼僅儖僟傊偺儊僢僙乕僕捛壛偼丄侾儊僢僙乕僕偢偮 appendMessage()儊僜僢僪傪
		 * 屇傃弌偡偙偲偱峴偭偰偄傞.<BR>
		 *
		 * @param	targetFolder	儊僢僙乕僕傪庴怣偡傞偨傔偺僼僅儖僟.
		 * @exception	javax.mail.MessagingException	僄儔乕偑敪惗偟偨応崌.
		 */
		public synchronized void receive(POP3Folder targetFolder) throws MessagingException {
			try {
				if (! targetFolder.exists()) {
					debugOut(targetFolder + " not exits.");
					return;
				}
				if (! client.isConnected()) {
					client.connect();
				}
				client.rset();
				final int messageCount = client.getMessageCount();
				debugOut("MessageCount=" + messageCount);
				
				// 僒乕僶偐傜庢摼偡傋偒儊僢僙乕僕傪寛掕偡傞.
				// 嵟弶偵丄僒乕僶偐傜嵟怴偺UIDL傪摼傞.
				boolean failedUIDL = false;
				Vector latestUIDL = new Vector();
				try {
					for (int i = 0; i < messageCount; ++i) {
						latestUIDL.addElement(client.getUID(i));
					}
				} catch (POP3NegativeResponseException p3nre) {
					debugOut("UIDL command failed: " + p3nre);
					failedUIDL = true;
				}
				
				// 庴怣偺昁梫偑偁傞儊僢僙乕僕傪妋掕偡傞.
				BitSet needReceive = new BitSet();
				//如果邮件服务器不支持UIDL命令,则只好全取
				if(failedUIDL){
					for(int i=0;i<messageCount;++i){
						needReceive.set(i);
					}
				}
					
				for (int i = 0; i < latestUIDL.size(); ++i) {
					if (! uidList.contains(latestUIDL.elementAt(i))) {
						needReceive.set(i);
					}
				}
				BitSet received = new BitSet();
				
				// 儊僢僙乕僕傪庴怣偡傞.
				Message msgs[] = new Message[1];
				for (int i = 0; i < messageCount; ++i) {
					if (needReceive.get(i)) {
						debugOut("Retrieve message:" + (i + 1));
						InputStream ins = client.retrieve(i);
						MimeMessage message = new MimeMessage(session, ins);
						ins.close();
						message.setFlag(Flags.Flag.SEEN, false);
						message.setFlag(Flags.Flag.RECENT, true);

						msgs[0] = message;
						targetFolder.appendMessages(msgs);
					}
					received.set(i);
				}
				
				// 儊僢僙乕僕傪嶍彍偡傞.
				if (! leaveOnServer) {	// 儊僢僙乕僕傪嶍彍偡傞.
					for (int i = 0; i < messageCount; ++i) {
						if (received.get(i)) {
							debugOut("Delete message: " + (i + 1));
							client.delete(i);
						}
					}
				}
				// 庴怣嵪傒儊乕儖偺UIDL傪峏怴偡傞.
				uidList.removeAllElements();
				for (int i = 0; i < latestUIDL.size(); ++i) {
					if (received.get(i)) {
						uidList.addElement(latestUIDL.elementAt(i));
					}
				}
				client.disconnect();
			} catch (POP3AuthenticationFailedException pafe) {
				debugOut("POP3 authentication is faield:" + pafe);
				throw new MessagingException("POP3AuthenticationFailedException", pafe);
			} catch (IOException ioe) {
				debugOut("IOException occured while receiving message(s):" + ioe);
				throw new MessagingException("IOException", ioe);
			} finally {
				try {
					saveUIDL(uidList);
				} catch (IOException ioe) {
					debugOut("IOException occured while receiving message(s):" + ioe);
					throw new MessagingException("IOException", ioe);
				}
			}
		}
		
		/**
		 * UIDL傪僼傽僀儖偵曐懚偡傞丅
		 *
		 * @param	uidList	曐懚懳徾偲側傞UIDL
		 * @throws	java.io.IOException	僄儔乕偑敪惗偟偨応崌丅
		 */
		private void saveUIDL(Vector uidList) throws IOException {
			FileOutputStream fos = new FileOutputStream(uidlFile);
			ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos, 16 * 1024));
			oos.writeObject(uidList);
			oos.close();
			debugOut("Saving UIDL file ... done");
		}
	}
	
	
	/**
	 * 僆僼儔僀儞儌乕僪帪偺MessageReceiver.
	 */
	class OfflineMessageReceiver implements MessageReceiver {
		/**
		 * 僆僼儔僀儞儌乕僪帪偵偼丄壗傕偣偢偵儕僞乕儞偡傞.
		 *
		 * @param	folder	僼僅儖僟.柍帇偝傟傞.
		 * @exception	javax.mail.MessagingException	僄儔乕偑敪惗偟偨応崌.
		 */
		public void receive(POP3Folder folder) throws MessagingException {
			return;
		}
	}
	
	/**
	 * 僶僢僋僌儔僂儞僪偱儊僢僙乕僕偺庴怣傪峴偆僋儔僗.
	 * 桪愭搙偼 NORM_PRIORITY - 1 偱摦嶌偡傞.
	 */
	class BackgroundMessageReceiver extends Thread implements MessageReceiver {
		/**
		 * 庴怣偟偨儊僢僙乕僕傪奿擺偡傞 Folder 偺僉儏乕.
		 * 偨偩偟僉儏乕偺僒僀僘偼侾偱偁傞.
		 * 乮Vector 傪巊偭偰偄傞偺偼丄僐乕僪傪撉傒傗偡偔偡傞偨傔乯
		 */
		private Vector waitingQueue = new Vector(1);
		
		/**
		 * 庴怣摦嶌拞偱偁傞偙偲傪帵偡僼儔僌.
		 */
		private volatile boolean running;
		
		/**
		 * MessageReceiver.
		 */
		private MessageReceiver receiver;
		
		/**
		 * 儊僢僙乕僕偺庴怣傪僶僢僋僌儔僂儞僪偱峴偆 BackgroundMessageReceiver 僆僽僕僃僋僩傪嶌惉偡傞.<BR>
		 * 偙偺僗儗僢僪偼 Daemon thread 偲偟偰摦嶌偡傞.
		 *
		 */
		protected BackgroundMessageReceiver(MessageReceiver receiver) {
			this.receiver = receiver;
			setDaemon(true);
		}
		
		/*
		 * 僒乕僶偐傜儊僢僙乕僕傪庴怣偟丄巜掕偝傟偨僼僅儖僟偵捛壛偡傞.
		 * <OL>
		 *  <LI>庴怣懳徾偲側傞僼僅儖僟偑僉儏乕偵擖偭偰偄傞偐傪挷傋傞.
		 *  <LI>側偗傟偽 wait() 偡傞.
		 *  <LI>僉儏乕偐傜Folder僆僽僕僃僋僩傪庢傝弌偡.
		 *  <LI>僒乕僶偐傜儊僢僙乕僕傪庴怣偟丄Folder僆僽僕僃僋僩偵捛壛偡傞.
		 *  <LI>僒乕僶偐傜愗抐偡傞.
		 * </OL>
		 * 傪孞傝曉偡.<BR>
		 * 椺奜偑敪惗偡傞偲丄偙偺儖乕僾偼廔椆偡傞.
		 */
		public void run() {
			running = true;
			try {
				while (running) {
					POP3Folder targetFolder = null;
					synchronized (this) {
						while (running && waitingQueue.isEmpty()) {
							wait();
						}
						if (! running) {
							return;
						}
						targetFolder = (POP3Folder) waitingQueue.elementAt(0);
					}
					receiver.receive(targetFolder);
					waitingQueue.removeElementAt(0);
				}
			} catch (MessagingException me) {
				debugOut("BackgroundMessageReceiver.run():MessagingException occured. " + me);
				notifyStoreListeners(StoreEvent.ALERT, me.toString());
			} catch (InterruptedException ie) {
				debugOut("MessageReceiver.run():InterruptedException occured. " + ie);
				notifyStoreListeners(StoreEvent.ALERT, ie.toString());
			} finally {
				running = false;
			}
		}
		
		/**
		 * 僼僅儖僟僉儏乕偵巜掕偝傟偨 Folder 僆僽僕僃僋僩傪捛壛偡傞.
		 * 幚嵺偺庴怣摦嶌偼丄暿僗儗僢僪偱峴傢傟傞.<BR>
		 *
		 * 庴怣懸偪僼僅儖僟僉儏乕偵偙偺僼僅儖僟傪捛壛偟丄偡偖偵儕僞乕儞偡傞.
		 * 偡偱偵僉儏乕偵僼僅儖僟偑偁偭偨応崌偵偼丄側偵傕偣偢偵儕僞乕儞偡傞.
		 *
		 * @param	folder	儊僢僙乕僕傪庴怣偡傞 folder.
		 */
		public synchronized void receive(POP3Folder folder) throws MessagingException {
			if (waitingQueue.isEmpty()) {
				debugOut("MessageReceiver.receive():waitingQueue is empty.");
				waitingQueue.addElement(folder);
				notify();
			} else {
				debugOut("MessageReceiver.receive():waitingQueue is NOT empty, skipped.");
			}
		}

		/**
		 * 儊僢僙乕僕偺庴怣傪拞巭偡傞.
		 * 拞巭偑姰椆偡傞傑偱僽儘僢僋偡傞.
		 */
		public synchronized void stopReceiving() {
			try {
				running = false;
				notify();
				join();
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			}
		}
	}
	
	/**
	 * 掕婜揑偵MessageReceiver.receive() 傪屇傃弌偡僨乕儌儞僗儗僢僪.
	 * 偙偺僗儗僢僪偼丄桪愭搙 NORM_PRIORITY + 1 偱摦嶌偡傞.
	 * 傎偲傫偳偺帪娫傪 sleep() 偟偰夁偛偟丄栚偑妎傔傞偲偡偖偵
	 * MessageReceiver.receive() 傪屇傃弌偟偰丄傑偨 sleep() 偡傞.
	 */
	private class IntervalTimer extends Thread {
		/**
		 * 儊僢僙乕僕傪捛壛偡傞 Folder.
		 */
		private POP3Folder targetFolder;
		
		/**
		 * 儊僢僙乕僕庴怣梡僆僽僕僃僋僩.
		 */
		private MessageReceiver receiver;
		
		/**
		 * 掕婜揑偵儊僢僙乕僕傪庴怣偟丄巜掕偝傟偨僼僅儖僟偵 append 偡傞
		 * IntervalTimer 僋儔僗偺僀儞僗僞儞僗傪嶌惉偡傞.
		 *
		 * @param folder	儊僢僙乕僕傪捛壛偡傞僼僅儖僟.
		 * @param	receiver	儊僢僙乕僕庴怣梡僆僽僕僃僋僩.
		 */
		public IntervalTimer(POP3Folder folder, MessageReceiver receiver) {
			targetFolder = folder;
			this.receiver = receiver;
			setPriority(Thread.NORM_PRIORITY + 1);
			setDaemon(true);
		}
		
		/**
		 * MessageReceiver.receive() 傪屇傃弌偟偨屻丄堦掕帪娫 sleep() 偡傞
		 * 偲偄偆摦嶌傪孞傝曉偡.
		 * checkInterval 偑 0 埲壓偺応崌偵偼丄側偵傕偣偢偵儕僞乕儞偡傞.
		 */
		public void run() {
			if (checkInterval <= 0) {
				debugOut("IntervalTimer.run():checkInterval <= 0");
				return;
			}
			while (isConnected()) {
				try {
					receiver.receive(targetFolder);
					sleep(checkInterval);
				} catch (InterruptedException ie) {
					debugOut("IntervalTimer.run():" + ie);
					ie.printStackTrace();
				} catch (MessagingException me) {
					debugOut("IntervalTimer.run():" + me);
					me.printStackTrace();
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?