001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (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 & Co. KG, 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 = "opencms@unconfigured.com";
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, null, 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 security the security mode
097       * @param username the user name to use for authentication
098       * @param password the password to use for authentication
099       */
100    public void addMailHost(
101        String hostname,
102        String port,
103        String order,
104        String protocol,
105        String security,
106        String username,
107        String password) {
108
109        Integer thePort;
110        try {
111            thePort = Integer.valueOf(port);
112        } catch (Throwable t) {
113            thePort = Integer.valueOf(25);
114        }
115        m_orderDefault += 10;
116        Integer theOrder;
117        try {
118            theOrder = Integer.valueOf(order);
119            if (theOrder.intValue() > m_orderDefault) {
120                m_orderDefault = theOrder.intValue();
121            }
122        } catch (Throwable t) {
123            // valueOf: use jdk int cache if possible and not new operator:
124            theOrder = Integer.valueOf(m_orderDefault);
125        }
126        CmsMailHost host = new CmsMailHost(hostname, thePort, theOrder, protocol, security, username, password);
127        m_mailHosts.add(host);
128        if (CmsLog.INIT.isInfoEnabled()) {
129            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_ADD_HOST_1, host));
130        }
131        Collections.sort(m_mailHosts);
132    }
133
134    /**
135     * Returns the default mail host.<p>
136     *
137     * @return the default mail host
138     */
139    public CmsMailHost getDefaultMailHost() {
140
141        return m_mailHosts.get(0);
142    }
143
144    /**
145     * Returns the mail from default sender.<p>
146     *
147     * @return the mail from default sender
148     */
149    public String getMailFromDefault() {
150
151        return m_mailFromDefault;
152    }
153
154    /**
155     * Returns an unmodifiable sorted list of all configured mail hosts.<p>
156     *
157     * @return an unmodifiable sorted list of all configured mail hosts
158     */
159    public List<CmsMailHost> getMailHosts() {
160
161        return Collections.unmodifiableList(m_mailHosts);
162    }
163
164    /**
165     * Sets the mail from default sender.<p>
166     *
167     * @param sender the mail from default sender to set
168     */
169    public void setMailFromDefault(String sender) {
170
171        m_mailFromDefault = sender;
172        if (CmsLog.INIT.isInfoEnabled()) {
173            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.LOG_DEFAULT_SENDER_1, m_mailFromDefault));
174        }
175    }
176
177    /**
178     * @see java.lang.Object#toString()
179     */
180    @Override
181    public String toString() {
182
183        StringBuffer sb = new StringBuffer();
184        sb.append("[hosts:" + m_mailHosts.toString());
185        sb.append(", order:" + m_orderDefault);
186        sb.append(", from:" + m_mailFromDefault);
187        return sb.toString();
188    }
189}