View Javadoc

1   package com.ozacc.mail.mock;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import javax.mail.internet.MimeMessage;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import com.ozacc.mail.MailException;
12  import com.ozacc.mail.NotConnectedException;
13  import com.ozacc.mail.fetch.FetchMailPro;
14  import com.ozacc.mail.fetch.ReceivedMail;
15  
16  /***
17   * FetchMailProImplクラスのMock。
18   * 
19   * @since 1.2
20   * @author Tomohiro Otsuka
21   * @version $Id: MockFetchMailPro.java,v 1.1.2.1 2005/02/05 10:40:24 otsuka Exp $
22   */
23  public class MockFetchMailPro implements FetchMailPro {
24  
25  	private static Log log = LogFactory.getLog(MockFetchMailPro.class);
26  
27  	/*** デフォルトのSMTPサーバ。「localhost」 */
28  	public static final String DEFAULT_HOST = "localhost";
29  
30  	/*** デフォルトのプロトコル。「pop3」 */
31  	public static final String DEFAULT_PROTOCOL = "pop3";
32  
33  	/***
34  	 * デフォルトのポート。「-1」<br>
35  	 * -1はプロトコルに応じた適切なポートを設定する特別な値。
36  	 */
37  	public static final int DEFAULT_PORT = -1;
38  
39  	private static final String INBOX_NAME = "INBOX";
40  
41  	private String host = DEFAULT_HOST;
42  
43  	private String protocol = DEFAULT_PROTOCOL;
44  
45  	private int port = DEFAULT_PORT;
46  
47  	private String username;
48  
49  	private String password;
50  
51  	private boolean javaMailLogEnabled;
52  
53  	private boolean connected = false;
54  
55  	private List receivedMails;
56  
57  	/***
58  	 * コンストラクタ。
59  	 */
60  	public MockFetchMailPro() {
61  		super();
62  		receivedMails = new ArrayList();
63  	}
64  
65  	/***
66  	 * @see com.ozacc.mail.fetch.FetchMailPro#connect()
67  	 */
68  	public synchronized void connect() throws MailException {
69  		if (isConnected()) {
70  			log.warn("既にサーバ[" + host + "]に接続されています。再接続するには先に接続を切断する必要があります。");
71  			return;
72  		}
73  
74  		log.debug(protocol.toUpperCase() + "サーバ[" + host + "]に接続するフリ。");
75  		connected = true;
76  		log.info(protocol.toUpperCase() + "サーバ[" + host + "]に接続したフリ。");
77  	}
78  
79  	/***
80  	 * @see com.ozacc.mail.fetch.FetchMailPro#disconnect()
81  	 */
82  	public synchronized void disconnect() throws MailException {
83  		if (isConnected()) {
84  			log.debug(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断するフリ。");
85  			connected = false;
86  			log.debug(protocol.toUpperCase() + "サーバ[" + host + "]との接続を切断したフリ。");
87  		}
88  	}
89  
90  	/***
91  	 * <code>MockFetchMailPro</code>の<code>getMails()</code>メソッドが返す
92  	 * <code>ReceivedMail</code>インスタンスをセットします。
93  	 * 
94  	 * @param mail <code>getMails()</code>メソッドが返す<code>ReceivedMail</code>インスタンス
95  	 */
96  	public void setupGetMails(ReceivedMail mail) {
97  		receivedMails.add(mail);
98  	}
99  
100 	/***
101 	 * <code>MockFetchMailPro</code>の<code>getMails()</code>メソッドが返す
102 	 * <code>ReceivedMail</code>インスタンスをセットします。
103 	 * 
104 	 * @param mails <code>getMails()</code>メソッドが返す<code>ReceivedMail</code>インスタンス配列
105 	 */
106 	public void setupGetMails(ReceivedMail[] mails) {
107 		for (int i = 0; i < mails.length; i++) {
108 			ReceivedMail mail = mails[i];
109 			setupGetMails(mail);
110 		}
111 	}
112 
113 	/***
114 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMailCount()
115 	 */
116 	public int getMailCount() throws MailException {
117 		return receivedMails.size();
118 	}
119 
120 	/***
121 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMail(int)
122 	 */
123 	public synchronized ReceivedMail getMail(int num) throws MailException {
124 		if (isConnected()) {
125 			return (ReceivedMail)receivedMails.get(num - 1);
126 		} else {
127 			throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続されていません。");
128 		}
129 	}
130 
131 	/***
132 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMails(boolean)
133 	 */
134 	public synchronized ReceivedMail[] getMails(boolean delete) throws MailException {
135 		if (isConnected()) {
136 			ReceivedMail[] results = (ReceivedMail[])receivedMails
137 					.toArray(new ReceivedMail[receivedMails.size()]);
138 			if (delete) {
139 				receivedMails.clear();
140 			}
141 			return results;
142 		} else {
143 			throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続されていません。");
144 		}
145 	}
146 
147 	/***
148 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMessage(int)
149 	 */
150 	public MimeMessage getMessage(int num) throws MailException {
151 		throw new UnsupportedOperationException("申し訳ございません。MockFetchMailProでは、このメソッドをサポートしていません。");
152 	}
153 
154 	/***
155 	 * @see com.ozacc.mail.fetch.FetchMailPro#getMessages(boolean)
156 	 */
157 	public MimeMessage[] getMessages(boolean delete) throws MailException {
158 		throw new UnsupportedOperationException("申し訳ございません。MockFetchMailProでは、このメソッドをサポートしていません。");
159 	}
160 
161 	/***
162 	 * @see com.ozacc.mail.fetch.FetchMailPro#changeFolder(java.lang.String)
163 	 */
164 	public synchronized void changeFolder(String folderName) throws MailException {
165 		if (!isConnected()) {
166 			log.warn("メールサーバに接続されていません。");
167 			return;
168 		}
169 
170 		log.debug("メッセージフォルダ[" + folderName + "]をオープンするフリ。");
171 		log.debug("メッセージフォルダ[" + folderName + "]をオープンしたフリ。");
172 	}
173 
174 	/***
175 	 * @see com.ozacc.mail.fetch.FetchMailPro#isConnected()
176 	 */
177 	public boolean isConnected() {
178 		return connected;
179 	}
180 
181 	/***
182 	 * @return Returns the host.
183 	 */
184 	public String getHost() {
185 		return host;
186 	}
187 
188 	/***
189 	 * @param host The host to set.
190 	 */
191 	public void setHost(String host) {
192 		this.host = host;
193 	}
194 
195 	/***
196 	 * @return Returns the javaMailLogEnabled.
197 	 */
198 	public boolean isJavaMailLogEnabled() {
199 		return javaMailLogEnabled;
200 	}
201 
202 	/***
203 	 * @param javaMailLogEnabled The javaMailLogEnabled to set.
204 	 */
205 	public void setJavaMailLogEnabled(boolean javaMailLogEnabled) {
206 		this.javaMailLogEnabled = javaMailLogEnabled;
207 	}
208 
209 	/***
210 	 * @return Returns the password.
211 	 */
212 	public String getPassword() {
213 		return password;
214 	}
215 
216 	/***
217 	 * @param password The password to set.
218 	 */
219 	public void setPassword(String password) {
220 		this.password = password;
221 	}
222 
223 	/***
224 	 * @return Returns the port.
225 	 */
226 	public int getPort() {
227 		return port;
228 	}
229 
230 	/***
231 	 * @param port The port to set.
232 	 */
233 	public void setPort(int port) {
234 		this.port = port;
235 	}
236 
237 	/***
238 	 * @return Returns the protocol.
239 	 */
240 	public String getProtocol() {
241 		return protocol;
242 	}
243 
244 	/***
245 	 * @param protocol The protocol to set.
246 	 */
247 	public void setProtocol(String protocol) {
248 		this.protocol = protocol;
249 	}
250 
251 	/***
252 	 * @return Returns the username.
253 	 */
254 	public String getUsername() {
255 		return username;
256 	}
257 
258 	/***
259 	 * @param username The username to set.
260 	 */
261 	public void setUsername(String username) {
262 		this.username = username;
263 	}
264 }