001/*
002 * JPPF.
003 * Copyright (C) 2005-2015 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.node.connection;
020
021import org.jppf.comm.discovery.JPPFConnectionInformation;
022
023/**
024 * A default implementation for the {@link DriverConnectionInfo} interface.
025 * @author Laurent Cohen
026 * @since 4.1
027 */
028public class JPPFDriverConnectionInfo implements DriverConnectionInfo {
029  /**
030   * Whether SSL/TLS should be used.
031   */
032  protected boolean secure;
033  /**
034   * The driver host name or IP address.
035   */
036  protected String host;
037  /**
038   * The driver port to connect to.
039   */
040  protected int port;
041  /**
042   * The driver recovery port to connect to.
043   */
044  protected int recoveryPort;
045
046  /**
047   * Default constructor which initializes the parameters to default values.
048   */
049  public JPPFDriverConnectionInfo() {
050    secure = false;
051    host = "localhost";
052    port = 11111;
053    recoveryPort = -1;
054  }
055
056  /**
057   * Initialize the parameters with the specified values.
058   * @param secure whether SSL/TLS should be used.
059   * @param host the driver host name or IP address.
060   * @param port the driver port to connect to.
061   * @param recoveryPort the driver recovery port to connect to.
062   */
063  public JPPFDriverConnectionInfo(final boolean secure, final String host, final int port, final int recoveryPort) {
064    this.secure = secure;
065    this.host = host;
066    this.port = port;
067    this.recoveryPort = recoveryPort;
068  }
069
070  @Override
071  public boolean isSecure() {
072    return secure;
073  }
074
075  /**
076   * Specify whether SSL/TLS should be used.
077   * @param secure {@code true} for a secure connection, {@code false} otherwise.
078   */
079  public void setSecure(final boolean secure)
080  {
081    this.secure = secure;
082  }
083
084  @Override
085  public String getHost() {
086    return host;
087  }
088
089  /**
090   * Set the driver host name or IP address.
091   * @param host the host as a string.
092   */
093  public void setHost(final String host) {
094    this.host = host;
095  }
096
097  @Override
098  public int getPort() {
099    return port;
100  }
101
102  /**
103   * Set the driver port to connect to.
104   * @param port the port as an int value.
105   */
106  public void setPort(final int port) {
107    this.port = port;
108  }
109
110  @Override
111  public int getRecoveryPort() {
112    return recoveryPort;
113  }
114
115  /**
116   * Set the driver recovery port to connect to.
117   * @param recoveryPort the recovery port as an int value.
118   */
119  public void setRecoveryPort(final int recoveryPort) {
120    this.recoveryPort = recoveryPort;
121  }
122
123  @Override
124  public int hashCode() {
125    final int prime = 31;
126    int result = 1;
127    result = prime * result + ((host == null) ? 0 : host.hashCode());
128    result = prime * result + port;
129    result = prime * result + recoveryPort;
130    result = prime * result + (secure ? 1231 : 1237);
131    return result;
132  }
133
134  @Override
135  public boolean equals(final Object obj) {
136    if (this == obj) return true;
137    if (obj == null) return false;
138    if (getClass() != obj.getClass()) return false;
139    JPPFDriverConnectionInfo other = (JPPFDriverConnectionInfo) obj;
140    if (host == null) {
141      if (other.host != null) return false;
142    }
143    else if (!host.equals(other.host)) return false;
144    if (port != other.port) return false;
145    if (recoveryPort != other.recoveryPort) return false;
146    if (secure != other.secure) return false;
147    return true;
148  }
149
150  @Override
151  public String toString() {
152    StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
153    sb.append("secure=").append(secure);
154    sb.append(", host=").append(host);
155    sb.append(", port=").append(port);
156    sb.append(", recoveryPort=").append(recoveryPort);
157    sb.append(']');
158    return sb.toString();
159  }
160
161  /**
162   * Convert the specified {@link JPPFConnectionInformation} object into a {@link DriverConnectionInfo}.
163   * @param ci the {@link JPPFConnectionInformation} object to convert from.
164   * @param ssl whether ssl is enabled or not.
165   * @param recovery whether discovery is enabled or not.
166   * @return a {@link DriverConnectionInfo} instance.
167   */
168  public static DriverConnectionInfo fromJPPFConnectionInformation(final JPPFConnectionInformation ci, final boolean ssl, final boolean recovery) {
169    int port = ssl ? ci.sslServerPorts[0] : ci.serverPorts[0];
170    boolean recoveryEnabled = recovery && (ci.recoveryPort >= 0);
171    return new JPPFDriverConnectionInfo(ssl, ci.host, port, recoveryEnabled ? ci.recoveryPort: -1);
172  }
173}