KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > mail > MailService


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.mail;
8
9 import java.util.Properties JavaDoc;
10 import javax.mail.Authenticator JavaDoc;
11 import javax.mail.PasswordAuthentication JavaDoc;
12 import javax.mail.Session JavaDoc;
13 import javax.naming.NamingException JavaDoc;
14 import javax.naming.Reference JavaDoc;
15
16 import org.jfox.ioc.common.AbstractService;
17 import org.jfox.ioc.ext.ActiveComponent;
18 import org.jfox.jndi.InitialContextHelper;
19
20 /**
21  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
22  */

23
24 public class MailService extends AbstractService implements ActiveComponent {
25     private String JavaDoc user;
26     private String JavaDoc password;
27     private String JavaDoc m_jndiName = "mail";
28
29     private final static String JavaDoc mailStoreProtocolKey = "mail.store.protocol";
30     private String JavaDoc mailStoreProtocol = "pop3";
31
32     private final static String JavaDoc mailTransportProtocolKey = "mail.transport.protocol";
33     private String JavaDoc mailTransportProtocol = "smtp";
34
35     private final static String JavaDoc mailPop3HostKey = "mail.pop3.host";
36     private String JavaDoc mailPop3Host = "pop3.foxsource.org";
37
38     private final static String JavaDoc mailSmtpHostKey = "mail.smtp.host";
39     private String JavaDoc mailSmtpHost = "smtp.foxsource.org";
40
41     private final static String JavaDoc mailFromKey = "mail.from";
42     private String JavaDoc mailFrom = "young_yy@hotmail.com";
43
44     private final static String JavaDoc mailSmtpAuthKey = "mail.smtp.auth";
45     private boolean mailSmtpAuth = false;
46
47
48     private Session JavaDoc session = null;
49
50     public void setUser(String JavaDoc user) {
51         this.user = user;
52     }
53
54     public String JavaDoc getUser() {
55         return user;
56     }
57
58     public void setPassword(String JavaDoc password) {
59         this.password = password;
60     }
61
62     public String JavaDoc getPassword() {
63         return password;
64     }
65
66     public String JavaDoc getMailStoreProtocol() {
67         return mailStoreProtocol;
68     }
69
70     public void setMailStoreProtocol(String JavaDoc mailStoreProtocol) {
71         this.mailStoreProtocol = mailStoreProtocol;
72     }
73
74     public String JavaDoc getMailTransportProtocol() {
75         return mailTransportProtocol;
76     }
77
78     public void setMailTransportProtocol(String JavaDoc mailTransportProtocol) {
79         this.mailTransportProtocol = mailTransportProtocol;
80     }
81
82     public String JavaDoc getMailPop3Host() {
83         return mailPop3Host;
84     }
85
86     public void setMailPop3Host(String JavaDoc mailPop3Host) {
87         this.mailPop3Host = mailPop3Host;
88     }
89
90     public String JavaDoc getMailSmtpHost() {
91         return mailSmtpHost;
92     }
93
94     public void setMailSmtpHost(String JavaDoc mailSmtpHost) {
95         this.mailSmtpHost = mailSmtpHost;
96     }
97
98     public String JavaDoc getMailFrom() {
99         return mailFrom;
100     }
101
102     public void setMailFrom(String JavaDoc mailFrom) {
103         this.mailFrom = mailFrom;
104     }
105
106     public void setJNDIName(String JavaDoc name) {
107         if(this.isInitialized()) {
108             logger.warn("Mail Service has started, can't change JNDI name.");
109         }
110         else {
111             m_jndiName = name;
112         }
113     }
114
115     public String JavaDoc getJNDIName() {
116         return m_jndiName;
117     }
118
119     public boolean isMailSmtpAuth() {
120         return mailSmtpAuth;
121     }
122
123     public void setMailSmtpAuth(boolean mailSmtpAuth) {
124         this.mailSmtpAuth = mailSmtpAuth;
125     }
126
127     protected void doInit() throws Exception JavaDoc {
128         final PasswordAuthentication JavaDoc pa = new PasswordAuthentication JavaDoc(getUser(), getPassword());
129         Authenticator JavaDoc auth = new Authenticator JavaDoc() {
130             protected PasswordAuthentication JavaDoc getPasswordAuthentication() {
131                 return pa;
132             }
133         };
134         Properties JavaDoc prop = new Properties JavaDoc();
135         prop.setProperty(mailStoreProtocolKey, getMailStoreProtocol());
136         prop.setProperty(mailTransportProtocolKey, getMailTransportProtocol());
137         prop.setProperty(mailPop3HostKey, getMailPop3Host());
138         prop.setProperty(mailSmtpHostKey, getMailSmtpHost());
139         prop.setProperty(mailFromKey, getMailFrom());
140
141         if(isMailSmtpAuth()) {
142             prop.setProperty(mailSmtpAuthKey, "true");
143         }
144
145         // Finally create auth mail session
146
session = Session.getInstance(prop, auth);
147
148     }
149
150     protected void doDestroy() throws Exception JavaDoc {
151         session = null;
152     }
153
154     public void run() {
155         // ignore, not need start new thread
156
}
157
158     protected void doStart() throws Exception JavaDoc {
159         bind(session);
160     }
161
162     protected void doStop() {
163         MailSessionObjectFactory.removeMailSession(getJNDIName());
164         // Unbind from JNDI
165
try {
166             unbind();
167         }
168         catch(NamingException JavaDoc x) {
169             logger.error("unbind failure", x);
170         }
171     }
172
173     private void bind(Session JavaDoc session) throws NamingException JavaDoc {
174         String JavaDoc bindName = getJNDIName();
175         Reference JavaDoc ref = new Reference JavaDoc(Session JavaDoc.class.getName(), MailSessionObjectFactory.class.getName(), null);
176         InitialContextHelper.getInitialContext().bind(bindName, ref);
177
178         MailSessionObjectFactory.addMailSession(bindName, session);
179         logger.info("Mail Service bound to " + bindName);
180     }
181
182     private void unbind() throws NamingException JavaDoc {
183         String JavaDoc bindName = getJNDIName();
184         InitialContextHelper.getInitialContext().unbind(bindName);
185         logger.info("Mail service '" + getJNDIName() + "' removed from JNDI");
186     }
187
188     public static void main(String JavaDoc[] args) {
189
190     }
191 }
192
193
Popular Tags