001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.discovery;
020
021import static org.jppf.utils.configuration.JPPFProperties.*;
022
023/**
024 * Superclass for connection info in custom driver discovery mechanisms.
025 * @author Laurent Cohen
026 * @since 5.2.1
027 */
028public class DriverConnectionInfo {
029  /**
030   * The name given to this connection, used as umbered prefix for individual connection names.
031   */
032  final String name;
033  /**
034   * Whether SSL/TLS should be used.
035   */
036  final boolean secure;
037  /**
038   * The driver host name or IP address.
039   */
040  final String host;
041  /**
042   * The driver port to connect to.
043   */
044  final int port;
045  /**
046   * The connection pool size.
047   */
048  final int poolSize;
049  /**
050   * Whether the heartbeat mechanism is enabled for the connection pool.
051   */
052  final boolean heartbeatEnabled;
053
054  /**
055   * Initialize a plain connection with default name("driver"), pool size (1), host ("localhost") and port (11111).
056   */
057  public DriverConnectionInfo() {
058    this("driver", false, SERVER_HOST.getDefaultValue(), SERVER_PORT.getDefaultValue(), 1, false);
059  }
060
061  /**
062   * Initialize a plain connection with a default pool size of 1.
063   * @param name the name given to this connection.
064   * @param host the driver host name or IP address.
065   * @param port the driver port to connect to.
066   */
067  public DriverConnectionInfo(final String name, final String host, final int port) {
068    this(name, false, host, port, 1, false);
069  }
070
071  /**
072   * Initialize a connection with a default pool size of 1.
073   * @param name the name given to this connection.
074   * @param secure whether SSL/TLS should be used.
075   * @param host the driver host name or IP address.
076   * @param port the driver port to connect to.
077   */
078  public DriverConnectionInfo(final String name, final boolean secure, final String host, final int port) {
079    this(name, secure, host, port, 1, false);
080  }
081
082  /**
083   * Initialize a connection.
084   * @param name the name given to this connection.
085   * @param secure whether SSL/TLS should be used.
086   * @param host the driver host name or IP address.
087   * @param port the driver port to connect to.
088   * @param poolSize the number of driver connections in the pool.
089   */
090  public DriverConnectionInfo(final String name, final boolean secure, final String host, final int port, final int poolSize) {
091    this(name, secure, host, port, poolSize, false);
092  }
093
094  /**
095   * Initialize a connection.
096   * @param name the name given to this connection.
097   * @param secure whether SSL/TLS should be used.
098   * @param host the driver host name or IP address.
099   * @param port the driver port to connect to.
100   * @param poolSize the number of driver connections in the pool.
101   * @param heartbeatEnabled whether the heartbeat mechanism is enabled for the connection pool.
102   */
103  public DriverConnectionInfo(final String name, final boolean secure, final String host, final int port, final int poolSize, final boolean heartbeatEnabled) {
104    this.name = name;
105    this.secure = secure;
106    this.host = host;
107    this.port = port;
108    this.poolSize = poolSize >= 1 ? poolSize : 1;
109    this.heartbeatEnabled = heartbeatEnabled;
110  }
111
112  /**
113   * Get the name given to this connection.
114   * @return the connection name as a string.
115   */
116  public String getName() {
117    return name;
118  }
119
120  /**
121   * Determine whether secure (with SSL/TLS) connections should be established.
122   * @return {@code true} for secure connections, {@code false} otherwise.
123   */
124  public boolean isSecure() {
125    return secure;
126  }
127
128  /**
129   * Get the driver host name or IP address.
130   * @return the host as a string.
131   */
132  public String getHost() {
133    return host;
134  }
135
136  /**
137   * Get the driver port to connect to.
138   * @return the driver port as an int value.
139   */
140  public int getPort() {
141    return port;
142  }
143
144  /**
145   * Get the connection pool size.
146   * @return the connection pool size as an int value.
147   */
148  public int getPoolSize() {
149    return poolSize;
150  }
151
152  /**
153   * Determine whether the heartbeat mechanism is enabled for the connection pool.
154   * @return {@code true} if heartbeat is enabled, {@code false} otherwise.
155   */
156  public boolean isHeartbeatEnabled() {
157    return heartbeatEnabled;
158  }
159
160  @Override
161  public int hashCode() {
162    final int prime = 31;
163    int result = prime + ((name == null) ? 0 : name.hashCode());
164    result = prime * result + ((host == null) ? 0 : host.hashCode());
165    result = prime * result + port;
166    result = prime * result + (secure ? 1231 : 1237);
167    return result;
168  }
169
170  @Override
171  public boolean equals(final Object obj) {
172    if (this == obj) return true;
173    if (obj == null) return false;
174    if (getClass() != obj.getClass()) return false;
175    final DriverConnectionInfo other = (DriverConnectionInfo) obj;
176    if (name == null) {
177      if (other.name != null) return false;
178    }
179    else if (!name.equals(other.name)) return false;
180    if (host == null) {
181      if (other.host != null) return false;
182    }
183    else if (!host.equals(other.host)) return false;
184    if (port != other.port) return false;
185    if (secure != other.secure) return false;
186    if (poolSize != other.poolSize) return false;
187    if (heartbeatEnabled != other.heartbeatEnabled) return false;
188    return true;
189  }
190
191  @Override
192  public String toString() {
193    return new StringBuilder(getClass().getSimpleName()).append('[')
194      .append("name=").append(name)
195      .append(", secure=").append(secure)
196      .append(", host=").append(host)
197      .append(", port=").append(port)
198      .append(", poolSize=").append(poolSize)
199      .append(", heartbeatEnabled=").append(heartbeatEnabled)
200      .append(']').toString();
201  }
202}