001/*
002 * JPPF.
003 * Copyright (C) 2005-2016 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  /**
047   * Initialize a plain connection with default name("driver"), host ("localhost") and port (11111).
048   */
049  public DriverConnectionInfo() {
050    this("driver", false, SERVER_HOST.getDefaultValue(), SERVER_PORT.getDefaultValue());
051  }
052
053  /**
054   * Initialize a plain connection.
055   * @param name the name given to this connection.
056   * @param host the driver host name or IP address.
057   * @param port the driver port to connect to.
058   */
059  public DriverConnectionInfo(final String name, final String host, final int port) {
060    this(name, false, host, port);
061  }
062
063  /**
064   * Initialize a connection.
065   * @param name the name given to this connection.
066   * @param secure whether SSL/TLS should be used.
067   * @param host the driver host name or IP address.
068   * @param port the driver port to connect to.
069   */
070  public DriverConnectionInfo(final String name, final boolean secure, final String host, final int port) {
071    this.name = name;
072    this.secure = secure;
073    this.host = host;
074    this.port = port;
075  }
076
077  /**
078   * Get the name given to this connection.
079   * @return the connection name as a string.
080   */
081  public String getName() {
082    return name;
083  }
084
085  /**
086   * Determine whether secure (with SSL/TLS) connections should be established.
087   * @return {@code true} for secure connections, {@code false} otherwise.
088   */
089  public boolean isSecure() {
090    return secure;
091  }
092
093  /**
094   * Get the driver host name or IP address.
095   * @return the host as a string.
096   */
097  public String getHost() {
098    return host;
099  }
100
101  /**
102   * Get the driver port to connect to.
103   * @return the driver port as an int value.
104   */
105  public int getPort() {
106    return port;
107  }
108
109  @Override
110  public int hashCode() {
111    final int prime = 31;
112    int result = prime + ((name == null) ? 0 : name.hashCode());
113    result = prime * result + ((host == null) ? 0 : host.hashCode());
114    result = prime * result + port;
115    result = prime * result + (secure ? 1231 : 1237);
116    return result;
117  }
118
119  @Override
120  public boolean equals(final Object obj) {
121    if (this == obj) return true;
122    if (obj == null) return false;
123    if (getClass() != obj.getClass()) return false;
124    DriverConnectionInfo other = (DriverConnectionInfo) obj;
125    if (name == null) {
126      if (other.name != null) return false;
127    }
128    else if (!name.equals(other.name)) return false;
129    if (host == null) {
130      if (other.host != null) return false;
131    }
132    else if (!host.equals(other.host)) return false;
133    if (port != other.port) return false;
134    if (secure != other.secure) return false;
135    return true;
136  }
137
138  @Override
139  public String toString() {
140    StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
141    sb.append("name=").append(name);
142    sb.append(", secure=").append(secure);
143    sb.append(", host=").append(host);
144    sb.append(", port=").append(port);
145    sb.append(']');
146    return sb.toString();
147  }
148}