[jetrix-cvs] SF.net SVN: jetrix:[800] jetrix/trunk
Brought to you by:
smanux
From: <sm...@us...> - 2009-02-18 18:26:37
|
Revision: 800 http://jetrix.svn.sourceforge.net/jetrix/?rev=800&view=rev Author: smanux Date: 2009-02-18 18:26:28 +0000 (Wed, 18 Feb 2009) Log Message: ----------- Implemented the mail API and its integration in the server configuration Modified Paths: -------------- jetrix/trunk/build.xml jetrix/trunk/doc/todo.txt jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java jetrix/trunk/src/admin/server.jsp jetrix/trunk/src/etc/conf/server.xml jetrix/trunk/src/etc/tetrinet-server.dtd jetrix/trunk/src/java/net/jetrix/Server.java jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java Added Paths: ----------- jetrix/trunk/lib/mailapi-1.4.1.jar jetrix/trunk/lib/smtp-1.4.1.jar jetrix/trunk/src/java/net/jetrix/config/MailSessionConfig.java jetrix/trunk/src/java/net/jetrix/mail/ jetrix/trunk/src/java/net/jetrix/mail/MailMessage.java jetrix/trunk/src/java/net/jetrix/mail/MailSessionManager.java Modified: jetrix/trunk/build.xml =================================================================== --- jetrix/trunk/build.xml 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/build.xml 2009-02-18 18:26:28 UTC (rev 800) @@ -157,6 +157,8 @@ <antcall target="pack.lib" inheritAll="true"><param name="library" value="servlet-light"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="jasper-runtime"/></antcall> <antcall target="pack.lib" inheritAll="true"><param name="library" value="jcrontab-1.4.1-light"/></antcall> + <antcall target="pack.lib" inheritAll="true"><param name="library" value="mailapi-1.4.1"/></antcall> + <antcall target="pack.lib" inheritAll="true"><param name="library" value="smtp-1.4.1"/></antcall> </target> <target name="pack.lib" description="Pack the library specified by the ${library} property"> Modified: jetrix/trunk/doc/todo.txt =================================================================== --- jetrix/trunk/doc/todo.txt 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/doc/todo.txt 2009-02-18 18:26:28 UTC (rev 800) @@ -47,3 +47,6 @@ - automatic generation of the commands documentation - java web start demo - compress the files on the patch server + +Code +- split the code in two parts : the core tetrinet classes and the server specific classes Added: jetrix/trunk/lib/mailapi-1.4.1.jar =================================================================== (Binary files differ) Property changes on: jetrix/trunk/lib/mailapi-1.4.1.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Added: jetrix/trunk/lib/smtp-1.4.1.jar =================================================================== (Binary files differ) Property changes on: jetrix/trunk/lib/smtp-1.4.1.jar ___________________________________________________________________ Added: svn:mime-type + application/octet-stream Modified: jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java =================================================================== --- jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/admin/WEB-INF/classes/net/jetrix/servlets/ServerAction.java 2009-02-18 18:26:28 UTC (rev 800) @@ -28,6 +28,7 @@ import javax.servlet.ServletException; import net.jetrix.*; +import net.jetrix.mail.*; import net.jetrix.commands.*; import net.jetrix.config.*; @@ -39,7 +40,6 @@ */ public class ServerAction extends HttpServlet { - protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String action = request.getParameter("action"); @@ -64,25 +64,25 @@ else if ("listener.start".equals(action)) { int index = Integer.parseInt(request.getParameter("index")); - Listener listener = (Listener) config.getListeners().get(index); + Listener listener = config.getListeners().get(index); listener.start(); } else if ("listener.stop".equals(action)) { int index = Integer.parseInt(request.getParameter("index")); - Listener listener = (Listener) config.getListeners().get(index); + Listener listener = config.getListeners().get(index); listener.stop(); } else if ("service.start".equals(action)) { int index = Integer.parseInt(request.getParameter("index")); - Service service = (Service) config.getServices().get(index); + Service service = config.getServices().get(index); service.start(); } else if ("service.stop".equals(action)) { int index = Integer.parseInt(request.getParameter("index")); - Service service = (Service) config.getServices().get(index); + Service service = config.getServices().get(index); service.stop(); } else if ("command.remove".equals(action)) @@ -122,6 +122,19 @@ config.addDataSource(datasource); DataSourceManager.getInstance().setDataSource(datasource, datasource.getName()); } + else if ("mailsession.update".equals(action)) + { + MailSessionConfig mailconfig = new MailSessionConfig(); + mailconfig.setHostname(request.getParameter("hostname")); + mailconfig.setPort(Integer.parseInt(request.getParameter("port"))); + mailconfig.setAuth("true".equals(request.getParameter("auth"))); + mailconfig.setUsername(request.getParameter("username")); + mailconfig.setPassword(request.getParameter("password")); + mailconfig.setDebug("true".equals(request.getParameter("debug"))); + + config.setMailSessionConfig(mailconfig); + MailSessionManager.getInstance().setConfiguration(mailconfig); + } else if ("gc".equals(action)) { System.gc(); Modified: jetrix/trunk/src/admin/server.jsp =================================================================== --- jetrix/trunk/src/admin/server.jsp 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/admin/server.jsp 2009-02-18 18:26:28 UTC (rev 800) @@ -455,8 +455,67 @@ <% } %> </div> - + <div class="tab-page" style="height: 400px"> + <h2 class="tab">Mail</h2> + + <form id="mailsession" action="/servlet/<%= ServerAction.class.getName() %>"> + <input type="hidden" name="action" value="mailsession.update"> + + <table class="thin" style="width: 400px"> + <tr> + <th width="30%">Hostname</th> + <td><input class="thin" type="text" name="hostname" value="<%= conf.getMailSessionConfig().getHostname() %>" style="width: 98%"></td> + </tr> + <tr> + <th>Port</th> + <td><input class="thin" type="text" name="port" value="<%= conf.getMailSessionConfig().getPort() %>" size="5"></td> + </tr> + <tr> + <th>Authentication</th> + <td> + <table> + <tr> + <td><input type="radio" value="true" name="auth" id="auth1" <%= conf.getMailSessionConfig().isAuth() ? "checked" : "" %>></td> + <td><label for="auth1">Enabled</label></td> + <td><input type="radio" value="false" name="auth" id="auth2" <%= !conf.getMailSessionConfig().isAuth() ? "checked" : "" %>></td> + <td><label for="auth2">Disabled</label></td> + </tr> + </table> + </td> + </tr> + <tr> + <th>Username</th> + <td><input class="thin" type="text" name="username" value="<%= conf.getMailSessionConfig().getUsername() %>"></td> + </tr> + <tr> + <th>Password</th> + <td><input class="thin" type="text" name="password" value="<%= conf.getMailSessionConfig().getPassword() %>"></td> + </tr> + <tr> + <th>Debug Mode</th> + <td> + <table> + <tr> + <td><input type="radio" value="true" name="debug" id="debug1" <%= conf.getMailSessionConfig().isDebug() ? "checked" : "" %>></td> + <td><label for="debug1">Enabled</label></td> + <td><input type="radio" value="false" name="debug" id="debug2" <%= !conf.getMailSessionConfig().isDebug() ? "checked" : "" %>></td> + <td><label for="debug2">Disabled</label></td> + </tr> + </table> + </td> + </tr> + </table> + + <br> + + <input type="submit" value="Update"> + + </form> + + </div> + + <div class="tab-page" style="height: 400px"> <h2 class="tab">System</h2> <% Modified: jetrix/trunk/src/etc/conf/server.xml =================================================================== --- jetrix/trunk/src/etc/conf/server.xml 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/etc/conf/server.xml 2009-02-18 18:26:28 UTC (rev 800) @@ -124,4 +124,7 @@ </datasource> </datasources> + <!-- Mail server parameters --> + <mailserver host="localhost" port="25" auth="false" username="" password="" debug="false"/> + </tetrinet-server> Modified: jetrix/trunk/src/etc/tetrinet-server.dtd =================================================================== --- jetrix/trunk/src/etc/tetrinet-server.dtd 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/etc/tetrinet-server.dtd 2009-02-18 18:26:28 UTC (rev 800) @@ -23,7 +23,8 @@ <!ELEMENT tetrinet-server (name?, language?, timeout?, max-channels?, max-players?, max-connections?, op-password, admin-password, access-log, - error-log, channels?, listeners, services?, commands, ban?, datasources?)> + error-log, channels?, listeners, services?, commands, ban?, datasources?, + mailserver?)> <!ATTLIST tetrinet-server host CDATA #IMPLIED> <!ELEMENT name (#PCDATA)> @@ -79,3 +80,11 @@ <!ELEMENT password (#PCDATA)> <!ELEMENT min-idle (#PCDATA)> <!ELEMENT max-active (#PCDATA)> + +<!ELEMENT mailserver EMPTY> +<!ATTLIST mailserver host CDATA #REQUIRED> +<!ATTLIST mailserver port CDATA #IMPLIED> +<!ATTLIST mailserver auth CDATA #IMPLIED> +<!ATTLIST mailserver username CDATA #IMPLIED> +<!ATTLIST mailserver password CDATA #IMPLIED> +<!ATTLIST mailserver debug CDATA #IMPLIED> Modified: jetrix/trunk/src/java/net/jetrix/Server.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/Server.java 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/java/net/jetrix/Server.java 2009-02-18 18:26:28 UTC (rev 800) @@ -24,6 +24,8 @@ import java.util.concurrent.*; import java.util.logging.*; +import javax.mail.MessagingException; + import net.jetrix.clients.*; import net.jetrix.commands.*; import net.jetrix.config.*; @@ -31,6 +33,8 @@ import net.jetrix.messages.channel.*; import net.jetrix.services.VersionService; import net.jetrix.listeners.ShutdownListener; +import net.jetrix.mail.MailSessionManager; +import net.jetrix.mail.MailMessage; /** * Main class, starts the server components and handle the server level messages. @@ -113,6 +117,13 @@ } } + if (config.getMailSessionConfig() != null) + { + log.info("Initializing the mail session..."); + MailSessionManager.getInstance().setConfiguration(config.getMailSessionConfig()); + MailSessionManager.getInstance().checkSession(); + } + // display the systray icon (windows only) SystrayManager.open(); Added: jetrix/trunk/src/java/net/jetrix/config/MailSessionConfig.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/MailSessionConfig.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/config/MailSessionConfig.java 2009-02-18 18:26:28 UTC (rev 800) @@ -0,0 +1,96 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2009 Emmanuel Bourg + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix.config; + +/** + * Configuration of the mail session. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + */ +public class MailSessionConfig +{ + private String hostname; + private int port = 25; + private boolean auth; + private String username; + private String password; + private boolean debug; + + public String getHostname() + { + return hostname; + } + + public void setHostname(String hostname) + { + this.hostname = hostname; + } + + public int getPort() + { + return port; + } + + public void setPort(int port) + { + this.port = port; + } + + public boolean isAuth() + { + return auth; + } + + public void setAuth(boolean auth) + { + this.auth = auth; + } + + public String getUsername() + { + return username; + } + + public void setUsername(String username) + { + this.username = username; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } + + public boolean isDebug() + { + return debug; + } + + public void setDebug(boolean debug) + { + this.debug = debug; + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/config/MailSessionConfig.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Modified: jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/java/net/jetrix/config/ServerConfig.java 2009-02-18 18:26:28 UTC (rev 800) @@ -75,9 +75,12 @@ private int status; private Statistics statistics = new Statistics(); - // datasource configuration + /** Datasources configuration */ private Map<String, DataSourceConfig> datasources = new LinkedHashMap<String, DataSourceConfig>(); + /** Mail session configuration */ + private MailSessionConfig mailSessionConfig; + private URL serverConfigURL; private URL channelsConfigURL; @@ -315,6 +318,28 @@ out.println(); } + if (mailSessionConfig != null) + { + StringBuilder buffer = new StringBuilder(); + buffer.append(" <mailserver host=\"").append(mailSessionConfig.getHostname()).append("\""); + buffer.append(" port=\"").append(mailSessionConfig.getPort()).append("\""); + if (mailSessionConfig.isAuth()) + { + buffer.append(" auth=\"true\""); + buffer.append(" username=\"").append(mailSessionConfig.getUsername()).append("\""); + buffer.append(" password=\"").append(mailSessionConfig.getPassword()).append("\""); + } + if (mailSessionConfig.isDebug()) + { + buffer.append(" debug=\"true\""); + } + buffer.append("/>"); + + out.println(" <!-- Mail server parameters -->"); + out.println(buffer.toString()); + out.println(); + } + out.println("</tetrinet-server>"); @@ -962,4 +987,20 @@ { datasources.put(datasource.getName(), datasource); } + + /** + * @since 0.3 + */ + public MailSessionConfig getMailSessionConfig() + { + return mailSessionConfig; + } + + /** + * @since 0.3 + */ + public void setMailSessionConfig(MailSessionConfig mailSessionConfig) + { + this.mailSessionConfig = mailSessionConfig; + } } Modified: jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java 2009-02-18 16:28:08 UTC (rev 799) +++ jetrix/trunk/src/java/net/jetrix/config/ServerRuleSet.java 2009-02-18 18:26:28 UTC (rev 800) @@ -30,7 +30,6 @@ */ class ServerRuleSet extends RuleSetBase { - public void addRuleInstances(Digester digester) { // server parameters @@ -86,6 +85,21 @@ digester.addCallMethod("*/datasource/password", "setPassword", 0); digester.addCallMethod("*/datasource/min-idle", "setMinIdle", 0); digester.addCallMethod("*/datasource/max-active", "setMaxActive", 0); + + // mail session + digester.addObjectCreate("*/mailserver", "net.jetrix.config.MailSessionConfig"); + digester.addSetNext("*/mailserver", "setMailSessionConfig", "net.jetrix.config.MailSessionConfig"); + digester.addCallMethod("*/mailserver", "setHostname", 1); + digester.addCallParam("*/mailserver", 0, "host"); + digester.addCallMethod("*/mailserver", "setPort", 1, new Class[] {Integer.TYPE}); + digester.addCallParam("*/mailserver", 0, "port"); + digester.addCallMethod("*/mailserver", "setAuth", 1, new Class[] {Boolean.TYPE}); + digester.addCallParam("*/mailserver", 0, "auth"); + digester.addCallMethod("*/mailserver", "setUsername", 1); + digester.addCallParam("*/mailserver", 0, "username"); + digester.addCallMethod("*/mailserver", "setPassword", 1); + digester.addCallParam("*/mailserver", 0, "password"); + digester.addCallMethod("*/mailserver", "setDebug", 1, new Class[] {Boolean.TYPE}); + digester.addCallParam("*/mailserver", 0, "debug"); } - } Added: jetrix/trunk/src/java/net/jetrix/mail/MailMessage.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/mail/MailMessage.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/mail/MailMessage.java 2009-02-18 18:26:28 UTC (rev 800) @@ -0,0 +1,221 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2009 Emmanuel Bourg + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix.mail; + +import java.io.File; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import javax.activation.DataHandler; +import javax.activation.DataSource; +import javax.activation.FileDataSource; +import javax.mail.Address; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +/** + * A simple mail message. This class intends to abstract the complexity + * of the JavaMail API for simple usages. It relies on the MailSessionManager + * to provide a valid mail session. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + * @since 0.3 + */ +public class MailMessage +{ + private static final String DEFAULT_CHARSET = "iso-8859-1"; + + private CharSequence subject; + private CharSequence body; + + private InternetAddress from; + + private Set<InternetAddress> recipients = new HashSet<InternetAddress>(); + private Set<InternetAddress> recipientsCC = new HashSet<InternetAddress>(); + private Set<InternetAddress> recipientsBCC = new HashSet<InternetAddress>(); + + private Set<DataSource> attachments = new HashSet<DataSource>(); + + public MailMessage() + { + } + + public void setSubject(CharSequence subject) + { + this.subject = subject; + } + + public void setBody(CharSequence body) + { + this.body = body; + } + + public void setFrom(InternetAddress from) + { + this.from = from; + } + + public void setFrom(String from) throws AddressException + { + this.from = new InternetAddress(from, false); + } + + public void addRecipient(String address) throws AddressException + { + recipients.add(new InternetAddress(address)); + } + + public void addRecipientCC(String address) throws AddressException + { + recipientsCC.add(new InternetAddress(address)); + } + + public void addRecipientBCC(String address) throws AddressException + { + recipientsBCC.add(new InternetAddress(address)); + } + + public void addRecipient(InternetAddress address) + { + recipients.add(address); + } + + public void addRecipientCC(InternetAddress address) + { + recipientsCC.add(address); + } + + public void addRecipientBCC(InternetAddress address) + { + recipientsBCC.add(address); + } + + public void addAttachment(File file) + { + attachments.add(new FileDataSource(file)); + } + + public void addAttachment(DataSource datasource) + { + attachments.add(datasource); + } + + /** + * Send the message, and if requested, asynchronously in a separate thread. + * + * @param asynchronous send the message in a separate thread + */ + public void send(boolean asynchronous) throws MessagingException + { + if (!asynchronous) + { + send(); + } + else + { + new Thread("MailMessage.send()") + { + public void run() + { + try + { + send(); + } + catch (MessagingException e) + { + throw new RuntimeException("An error occured when sending the mail", e); + } + } + }.start(); + } + } + + /** + * Send the message. + */ + public void send() throws MessagingException + { + Session session = MailSessionManager.getInstance().getSession(); + + // build the message + MimeMessage message = new MimeMessage(session); + message.setSubject(subject.toString(), DEFAULT_CHARSET); + message.setFrom(from); + + message.addRecipients(Message.RecipientType.TO, recipients.toArray(new Address[recipients.size()])); + message.addRecipients(Message.RecipientType.CC, recipientsCC.toArray(new Address[recipientsCC.size()])); + message.addRecipients(Message.RecipientType.BCC, recipientsBCC.toArray(new Address[recipientsBCC.size()])); + + message.setHeader("Content-Transfer-Encoding", "8bit"); + message.setSentDate(new Date()); + + MimeBodyPart part1 = new MimeBodyPart(); + if (body.toString().toLowerCase().trim().startsWith("<html") || body.toString().toLowerCase().trim().startsWith("<!doctype html")) + { + part1.setContent(body.toString(), "text/html; charset=" + DEFAULT_CHARSET); + } + else + { + part1.setText(body.toString(), DEFAULT_CHARSET); + } + part1.setHeader("Content-Transfer-Encoding", "8bit"); + + Multipart content = new MimeMultipart(); + content.addBodyPart(part1); + + // add attachments + for (DataSource attachment : attachments) + { + MimeBodyPart part2 = new MimeBodyPart(); + part2.setDataHandler(new DataHandler(attachment)); + part2.setFileName(attachment.getName()); + + content.addBodyPart(part2); + } + + message.setContent(content); + message.saveChanges(); + + // send the message + Transport transport = session.getTransport(); + + try + { + transport.connect(); + transport.sendMessage(message, message.getAllRecipients()); + } + finally + { + if (transport.isConnected()) + { + transport.close(); + } + } + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/mail/MailMessage.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native Added: jetrix/trunk/src/java/net/jetrix/mail/MailSessionManager.java =================================================================== --- jetrix/trunk/src/java/net/jetrix/mail/MailSessionManager.java (rev 0) +++ jetrix/trunk/src/java/net/jetrix/mail/MailSessionManager.java 2009-02-18 18:26:28 UTC (rev 800) @@ -0,0 +1,124 @@ +/** + * Jetrix TetriNET Server + * Copyright (C) 2009 Emmanuel Bourg + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.jetrix.mail; + +import java.util.Properties; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.mail.*; + +import net.jetrix.config.MailSessionConfig; +import org.apache.commons.lang.StringUtils; + +/** + * Singleton holding the session to send mails. + * + * @author Emmanuel Bourg + * @version $Revision$, $Date$ + * @since 0.3 + */ +public final class MailSessionManager +{ + private Logger log = Logger.getLogger(getClass().getName()); + + private static MailSessionManager instance = new MailSessionManager(); + + private Session session; + + private MailSessionManager() + { + } + + public static MailSessionManager getInstance() + { + return instance; + } + + public Session getSession() + { + return session; + } + + /** + * Initialize the mail session from the specified configuration. + */ + public void setConfiguration(final MailSessionConfig config) + { + try + { + if (!StringUtils.isBlank(config.getHostname())) + { + Properties props = new Properties(); + props.setProperty("mail.transport.protocol", "smtp"); + props.setProperty("mail.smtp.host", config.getHostname()); + props.setProperty("mail.smtp.port", String.valueOf(config.getPort())); + props.setProperty("mail.smtp.auth", String.valueOf(config.isAuth())); + + session = Session.getInstance(props, new Authenticator() + { + protected PasswordAuthentication getPasswordAuthentication() + { + return new PasswordAuthentication(config.getUsername(), config.getPassword()); + } + }); + + // enable the debug mode if requested + if (config.isDebug()) + { + session.setDebug(true); + } + } + else + { + log.warning("Unable to initialize the mail session, the hostname is missing"); + } + } + catch (Exception e) + { + log.log(Level.SEVERE, "Unable to initialize the mail session", e); + } + } + + /** + * Check if the mail session is working properly. + */ + public boolean checkSession() + { + boolean check = false; + + if (session != null) + { + try + { + Transport transport = session.getTransport(); + transport.connect(); + transport.close(); + + check = true; + } + catch (MessagingException e) + { + log.warning("Unable to validate the mail session (" + e.getMessage() + ")"); + } + } + + return check; + } +} Property changes on: jetrix/trunk/src/java/net/jetrix/mail/MailSessionManager.java ___________________________________________________________________ Added: svn:keywords + Date Author Id Revision HeadURL Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |