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
030/**
031 * Contains the configuration of an individual mail host.<p>
032 *
033 * @since 6.0.0
034 */
035public class CmsMailHost implements Comparable<CmsMailHost> {
036
037    /** The name of the mail host. */
038    private String m_hostname;
039
040    /** The order of this mail host. */
041    private Integer m_order;
042
043    /** The password to use for authentication. */
044    private String m_password;
045
046    /** The port to use. */
047    private int m_port;
048
049    /** The protocol to use. */
050    private String m_protocol;
051
052    /** The user name to use for authentication. */
053    private String m_username;
054
055    /** The security setting. */
056    private String m_security;
057
058    /**
059     * Creates a new mail host.<p>
060     *
061     * @param hostname the name of the mail host
062     * @param order the order in which the host is tried
063     * @param protocol the protocol to use (default "smtp")
064     * @param security the security setting
065     * @param username the user name to use for authentication
066     * @param password the password to use for authentication
067     * @param port the port, if < 0 then 25 is used
068     */
069    public CmsMailHost(
070        String hostname,
071        Integer port,
072        Integer order,
073        String protocol,
074        String security,
075        String username,
076        String password) {
077
078        m_hostname = hostname;
079        int portInt = port.intValue();
080        m_port = (portInt < 0) ? 25 : portInt;
081        m_protocol = (protocol != null) ? protocol : CmsMailSettings.MAIL_DEFAULT_PROTOCOL;
082        m_username = username;
083        m_password = password;
084        m_security = security;
085        m_order = order;
086    }
087
088    /**
089     * @see java.lang.Comparable#compareTo(java.lang.Object)
090     */
091    public int compareTo(CmsMailHost obj) {
092
093        if (obj == this) {
094            return 0;
095        }
096        return m_order.compareTo(obj.m_order);
097    }
098
099    /**
100     * @see java.lang.Object#equals(java.lang.Object)
101     */
102    @Override
103    public boolean equals(Object obj) {
104
105        if (obj == this) {
106            return true;
107        }
108        if (obj instanceof CmsMailHost) {
109            CmsMailHost other = (CmsMailHost)obj;
110            return m_hostname.equals(other.m_hostname)
111                && m_protocol.equals(other.m_protocol)
112                && m_username.equals(other.m_username);
113        }
114        return false;
115    }
116
117    /**
118     * Returns the host name.<p>
119     *
120     * @return the host name
121     */
122    public String getHostname() {
123
124        return m_hostname;
125    }
126
127    /**
128     * Returns the order of this mail host.<p>
129     *
130     * @return the order of this mail host
131     */
132    public Integer getOrder() {
133
134        return m_order;
135    }
136
137    /**
138     * Returns the password used for authentication.<p>
139     *
140     * @return the password used for authentication
141     */
142    public String getPassword() {
143
144        return m_password;
145    }
146
147    /**
148     * Returns the port.<p>
149     *
150     * @return the port
151     */
152    public int getPort() {
153
154        return m_port;
155    }
156
157    /**
158     * Returns the protocol used for mail sending, default is "smtp".<p>
159     *
160     * @return the protocol used for mail sending
161     */
162    public String getProtocol() {
163
164        return m_protocol;
165    }
166
167    /**
168     * Gets the value of the security setting.<p>
169     *
170     * @return the value of the security setting
171     */
172    public String getSecurity() {
173
174        return m_security;
175    }
176
177    /**
178     * Returns the user name used for authentication.<p>
179     *
180     * @return the user name used for authentication
181     */
182    public String getUsername() {
183
184        return m_username;
185    }
186
187    /**
188     * @see java.lang.Object#hashCode()
189     */
190    @Override
191    public int hashCode() {
192
193        return (m_hostname.hashCode() * 1117) + (m_protocol.hashCode() * 2003) + m_username.hashCode();
194    }
195
196    /**
197     * Returns <code>true</code> only if authentication is enabled,
198     * the default is <code>false</code>.<p>
199     *
200     * Authentication is enabled only if both "username" and "password"
201     * are not <code>null</code>.<p>
202     *
203     * @return <code>true</code> only if authentication is enabled
204     */
205    public boolean isAuthenticating() {
206
207        return (m_username != null) && (m_password != null);
208    }
209
210    /**
211     * @see java.lang.Object#toString()
212     */
213    @Override
214    public String toString() {
215
216        StringBuffer buf = new StringBuffer(64);
217        buf.append(this.getClass().getName());
218        buf.append(" hostname=");
219        buf.append(getHostname());
220        buf.append(" port=");
221        buf.append(getPort());
222        buf.append(" order=");
223        buf.append(m_order);
224        buf.append(" protocol=");
225
226        buf.append(getProtocol());
227        buf.append(" security=" + getSecurity());
228        if (isAuthenticating()) {
229            buf.append(" user=");
230            buf.append(getUsername());
231            buf.append(" password=");
232            buf.append(getPassword());
233        }
234        return buf.toString();
235    }
236}