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.2 2005/04/10 05:22:34 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 return getMail(num, false); 125 } 126 127 /*** 128 * @see com.ozacc.mail.fetch.FetchMailPro#getMail(int, boolean) 129 */ 130 public synchronized ReceivedMail getMail(int num, boolean delete) throws MailException { 131 if (isConnected()) { 132 if (delete) { 133 return (ReceivedMail)receivedMails.remove(num - 1); 134 } else { 135 return (ReceivedMail)receivedMails.get(num - 1); 136 } 137 } else { 138 throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続されていません。"); 139 } 140 } 141 142 /*** 143 * @see com.ozacc.mail.fetch.FetchMailPro#getMails(boolean) 144 */ 145 public synchronized ReceivedMail[] getMails(boolean delete) throws MailException { 146 if (isConnected()) { 147 ReceivedMail[] results = (ReceivedMail[])receivedMails 148 .toArray(new ReceivedMail[receivedMails.size()]); 149 if (delete) { 150 receivedMails.clear(); 151 } 152 return results; 153 } else { 154 throw new NotConnectedException(protocol.toUpperCase() + "サーバ[" + host + "]に接続されていません。"); 155 } 156 } 157 158 /*** 159 * @see com.ozacc.mail.fetch.FetchMailPro#getMessage(int) 160 */ 161 public MimeMessage getMessage(int num) throws MailException { 162 throw new UnsupportedOperationException("申し訳ございません。MockFetchMailProでは、このメソッドをサポートしていません。"); 163 } 164 165 /*** 166 * @see com.ozacc.mail.fetch.FetchMailPro#getMessages(boolean) 167 */ 168 public MimeMessage[] getMessages(boolean delete) throws MailException { 169 throw new UnsupportedOperationException("申し訳ございません。MockFetchMailProでは、このメソッドをサポートしていません。"); 170 } 171 172 /*** 173 * @see com.ozacc.mail.fetch.FetchMailPro#changeFolder(java.lang.String) 174 */ 175 public synchronized void changeFolder(String folderName) throws MailException { 176 if (!isConnected()) { 177 log.warn("メールサーバに接続されていません。"); 178 return; 179 } 180 181 log.debug("メッセージフォルダ[" + folderName + "]をオープンするフリ。"); 182 log.debug("メッセージフォルダ[" + folderName + "]をオープンしたフリ。"); 183 } 184 185 /*** 186 * @see com.ozacc.mail.fetch.FetchMailPro#isConnected() 187 */ 188 public boolean isConnected() { 189 return connected; 190 } 191 192 /*** 193 * @return Returns the host. 194 */ 195 public String getHost() { 196 return host; 197 } 198 199 /*** 200 * @param host The host to set. 201 */ 202 public void setHost(String host) { 203 this.host = host; 204 } 205 206 /*** 207 * @return Returns the javaMailLogEnabled. 208 */ 209 public boolean isJavaMailLogEnabled() { 210 return javaMailLogEnabled; 211 } 212 213 /*** 214 * @param javaMailLogEnabled The javaMailLogEnabled to set. 215 */ 216 public void setJavaMailLogEnabled(boolean javaMailLogEnabled) { 217 this.javaMailLogEnabled = javaMailLogEnabled; 218 } 219 220 /*** 221 * @return Returns the password. 222 */ 223 public String getPassword() { 224 return password; 225 } 226 227 /*** 228 * @param password The password to set. 229 */ 230 public void setPassword(String password) { 231 this.password = password; 232 } 233 234 /*** 235 * @return Returns the port. 236 */ 237 public int getPort() { 238 return port; 239 } 240 241 /*** 242 * @param port The port to set. 243 */ 244 public void setPort(int port) { 245 this.port = port; 246 } 247 248 /*** 249 * @return Returns the protocol. 250 */ 251 public String getProtocol() { 252 return protocol; 253 } 254 255 /*** 256 * @param protocol The protocol to set. 257 */ 258 public void setProtocol(String protocol) { 259 this.protocol = protocol; 260 } 261 262 /*** 263 * @return Returns the username. 264 */ 265 public String getUsername() { 266 return username; 267 } 268 269 /*** 270 * @param username The username to set. 271 */ 272 public void setUsername(String username) { 273 this.username = username; 274 } 275 276 }