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