View Javadoc

1   package com.ozacc.mail.spring;
2   
3   import java.io.File;
4   
5   import org.springframework.beans.factory.config.AbstractFactoryBean;
6   import org.springframework.core.io.Resource;
7   
8   import com.ozacc.mail.Mail;
9   import com.ozacc.mail.MailBuildException;
10  import com.ozacc.mail.MailBuilder;
11  import com.ozacc.mail.impl.XMLMailBuilderImpl;
12  
13  /***
14   * Springの設定ファイルで指定されたロケーションのXMLファイルからMailインスタンスを生成するFactoryBean。
15   * デフォルトでは、singletonプロパティはfalseに設定されます。
16   * <p>
17   * location、classPath、filePathの順で、一番先にセットされているプロパティ値がXMLファイルのパスとして使われます。
18   * 
19   * @see com.ozacc.mail.impl.XMLMailBuilderImpl
20   * 
21   * @since 1.0
22   * @author Tomohiro Otsuka
23   * @version $Id: XMLMailFactoryBean.java,v 1.4 2004/09/13 19:48:16 otsuka Exp $
24   */
25  public class XMLMailFactoryBean extends AbstractFactoryBean {
26  
27  	private String classPath;
28  
29  	private String filePath;
30  
31  	private Resource location;
32  
33  	private MailBuilder mailBuilder;
34  
35  	/***
36  	 * コンストラクタ。
37  	 */
38  	public XMLMailFactoryBean() {
39  		setSingleton(false);
40  	}
41  
42  	/***
43  	 * @see org.springframework.beans.factory.config.AbstractFactoryBean#createInstance()
44  	 */
45  	protected Object createInstance() throws Exception {
46  		if (mailBuilder == null) {
47  			init();
48  		}
49  
50  		if (getLocation() != null && getLocation().getFile() != null) {
51  			return mailBuilder.buildMail(getLocation().getFile());
52  		}
53  		if (getClassPath() != null) {
54  			return mailBuilder.buildMail(getClassPath());
55  		}
56  		if (getFilePath() != null) {
57  			return mailBuilder.buildMail(new File(getFilePath()));
58  		}
59  		throw new MailBuildException("Mailインスタンスの生成に失敗しました。XMLデータのロケーションが指定されていません。");
60  	}
61  
62  	/***
63  	 * mailBuilderインスタンスを生成します。
64  	 */
65  	private void init() {
66  		mailBuilder = new XMLMailBuilderImpl();
67  	}
68  
69  	/***
70  	 * @see org.springframework.beans.factory.FactoryBean#getObjectType()
71  	 */
72  	public Class getObjectType() {
73  		return Mail.class;
74  	}
75  
76  	/***
77  	 * <code>MailBuilder</code>インターフェースの実装クラスのインスタンスをセットします。
78  	 * デフォルトでは、<code>XMLMailBuilderImpl</code>が使用されます。
79  	 * <p>
80  	 * ただし、ここでセットしない場合は、<code>XMLMailFactoryBean</code>ひとつに付き、
81  	 * <code>XMLMailBuilderImpl</code>インスタンス一つが保持されます。
82  	 * シングルトンの<code>MailBuilder</code>インスタンスをセットすることを推奨します。
83  	 * 
84  	 * @param mailBuilder MailBuilderインスタンス
85  	 */
86  	public void setMailBuilder(MailBuilder mailBuilder) {
87  		this.mailBuilder = mailBuilder;
88  	}
89  
90  	/***
91  	 * @return Returns the classPath.
92  	 */
93  	public String getClassPath() {
94  		return classPath;
95  	}
96  
97  	/***
98  	 * @param classPath The classPath to set.
99  	 */
100 	public void setClassPath(String classPath) {
101 		this.classPath = classPath;
102 	}
103 
104 	/***
105 	 * @return Returns the filePath.
106 	 */
107 	public String getFilePath() {
108 		return filePath;
109 	}
110 
111 	/***
112 	 * @param filePath The filePath to set.
113 	 */
114 	public void setFilePath(String filePath) {
115 		this.filePath = filePath;
116 	}
117 
118 	/***
119 	 * @return Returns the location.
120 	 */
121 	public Resource getLocation() {
122 		return location;
123 	}
124 
125 	/***
126 	 * @param location The location to set.
127 	 */
128 	public void setLocation(Resource location) {
129 		this.location = location;
130 	}
131 }