001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.mail;
029
030import org.opencms.main.CmsLog;
031
032import java.util.ArrayList;
033import java.util.Collections;
034import java.util.List;
035
036import org.apache.commons.logging.Log;
037
038/**
039 * Contains the settings for the OpenCms mail service.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsMailSettings {
044
045    /** The default protocol for sending mail ("smtp"). */
046    public static final String MAIL_DEFAULT_PROTOCOL = "smtp";
047
048    /** The default mail from address. */
049    public static final String MAIL_DEFAULT_SENDER = "[email protected]";
050
051    /** The log object for this class. */
052    private static final Log LOG = CmsLog.getLog(CmsMailSettings.class);
053
054    /** The default mail "from" sender address. */
055    private String m_mailFromDefault;
056
057    /** The list of internal mail hosts. */
058    private List<CmsMailHost> m_mailHosts;
059
060    /** The default order if no order is given for a host. */
061    private int m_orderDefault;
062
063    /**
064     * Empty constructor, required for configuration.<p>
065     */
066    public CmsMailSettings() {
067
068        m_mailFromDefault = MAIL_DEFAULT_SENDER;
069        m_mailHosts = new ArrayList<CmsMailHost>();
070        if (LOG.isDebugEnabled()) {
071            LOG.debug(Messages.get().getBundle().key(Messages.LOG_EMPTY_CONSTRUCTOR_CALLED_1));
072        }
073    }
074
075    /**
076     * Adds a new mail host to the internal list of mail hosts with default port 25.<p>
077     *
078     * @param hostname the name of the mail host
079     * @param order the order in which the host is tried
080     * @param protocol the protocol to use (default "smtp")
081     * @param username the user name to use for authentication
082     * @param password the password to use for authentication
083     */
084    public void addMailHost(String hostname, String order, String protocol, String username, String password) {
085
086        addMailHost(hostname, "25", order, protocol, username, password);
087    }
088
089    /**
090       * Adds a new mail host to the internal list of mail hosts.<p>
091       *
092       * @param hostname the name of the mail host
093       * @param port the port of the mail host
094       * @param order the order in which the host is tried
095       * @param protocol the protocol to use (default "smtp")
096       * @param username the user name to use for authentication
097       * @param password the password to use for authentication
098       */
099    public void addMailHost(
100        String hostname,
101        String port,
102        String order,
103        String protocol,
104        String username,
105        String password) {
106
107        Integer thePort;
108        try {
109            thePort = Integer.valueOf(port);
110        } catch (Throwable t) {
111            thePort = Integer.valueOf(25);
112        }
113        m_orderDefault += 10;
114        Integer theOrder;
115        try {
116            theOrder = Integer.valueOf(order);
117            if (theOrder.intValue() > m_orderDefault) {
118                m_orderDefault = theOrder.intValue();
119            }
120        } catch (Throwable t) {
121            // valueOf: use jdk int cache if possible and not new operator:
122            theOrder = Integer.valueOf(m_orderDefault);
123        }
124        CmsMailHost host = new CmsMailHost(hostname, thePort, theOrder, protocol, username, password);
125        m_mailHosts.add(host);
126        if (CmsLog.INIT.isInfoEnabled()) {
127            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_ADD_HOST_1, host));
128        }
129        Collections.sort(m_mailHosts);
130    }
131
132    /**
133     * Returns the default mail host.<p>
134     *
135     * @return the default mail host
136     */
137    public CmsMailHost getDefaultMailHost() {
138
139        return m_mailHosts.get(0);
140    }
141
142    /**
143     * Returns the mail from default sender.<p>
144     *
145     * @return the mail from default sender
146     */
147    public String getMailFromDefault() {
148
149        return m_mailFromDefault;
150    }
151
152    /**
153     * Returns an unmodifiable sorted list of all configured mail hosts.<p>
154     *
155     * @return an unmodifiable sorted list of all configured mail hosts
156     */
157    public List<CmsMailHost> getMailHosts() {
158
159        return Collections.unmodifiableList(m_mailHosts);
160    }
161
162    /**
163     * Sets the mail from default sender.<p>
164     *
165     * @param sender the mail from default sender to set
166     */
167    public void setMailFromDefault(String sender) {
168
169        m_mailFromDefault = sender;
170        if (CmsLog.INIT.isInfoEnabled()) {
171            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_DEFAULT_SENDER_1, m_mailFromDefault));
172        }
173    }
174
175    /**
176     * @see java.lang.Object#toString()
177     */
178    @Override
179    public String toString() {
180
181        StringBuffer sb = new StringBuffer();
182        sb.append("[hosts:" + m_mailHosts.toString());
183        sb.append(", order:" + m_orderDefault);
184        sb.append(", from:" + m_mailFromDefault);
185        return sb.toString();
186    }
187}