001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2009 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.mail;
021
022 import java.util.Properties;
023 import java.util.logging.Level;
024 import java.util.logging.Logger;
025 import javax.mail.*;
026
027 import net.jetrix.config.MailSessionConfig;
028 import org.apache.commons.lang.StringUtils;
029
030 /**
031 * Singleton holding the session to send mails.
032 *
033 * @author Emmanuel Bourg
034 * @version $Revision: 800 $, $Date: 2009-02-18 19:26:28 +0100 (Wed, 18 Feb 2009) $
035 * @since 0.3
036 */
037 public final class MailSessionManager
038 {
039 private Logger log = Logger.getLogger(getClass().getName());
040
041 private static MailSessionManager instance = new MailSessionManager();
042
043 private Session session;
044
045 private MailSessionManager()
046 {
047 }
048
049 public static MailSessionManager getInstance()
050 {
051 return instance;
052 }
053
054 public Session getSession()
055 {
056 return session;
057 }
058
059 /**
060 * Initialize the mail session from the specified configuration.
061 */
062 public void setConfiguration(final MailSessionConfig config)
063 {
064 try
065 {
066 if (!StringUtils.isBlank(config.getHostname()))
067 {
068 Properties props = new Properties();
069 props.setProperty("mail.transport.protocol", "smtp");
070 props.setProperty("mail.smtp.host", config.getHostname());
071 props.setProperty("mail.smtp.port", String.valueOf(config.getPort()));
072 props.setProperty("mail.smtp.auth", String.valueOf(config.isAuth()));
073
074 session = Session.getInstance(props, new Authenticator()
075 {
076 protected PasswordAuthentication getPasswordAuthentication()
077 {
078 return new PasswordAuthentication(config.getUsername(), config.getPassword());
079 }
080 });
081
082 // enable the debug mode if requested
083 if (config.isDebug())
084 {
085 session.setDebug(true);
086 }
087 }
088 else
089 {
090 log.warning("Unable to initialize the mail session, the hostname is missing");
091 }
092 }
093 catch (Exception e)
094 {
095 log.log(Level.SEVERE, "Unable to initialize the mail session", e);
096 }
097 }
098
099 /**
100 * Check if the mail session is working properly.
101 */
102 public boolean checkSession()
103 {
104 boolean check = false;
105
106 if (session != null)
107 {
108 try
109 {
110 Transport transport = session.getTransport();
111 transport.connect();
112 transport.close();
113
114 check = true;
115 }
116 catch (MessagingException e)
117 {
118 log.warning("Unable to validate the mail session (" + e.getMessage() + ")");
119 }
120 }
121
122 return check;
123 }
124 }