OSDN Git Service

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