001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.quicksetup.installer;
028
029import org.opends.quicksetup.Constants;
030import org.opends.quicksetup.util.Utils;
031
032/**
033 * This class is used to provide a data model for the Data Replication
034 * Options panel of the installer.
035 */
036public class DataReplicationOptions
037{
038  /**
039   * This enumeration is used to know what the user wants to do for the data
040   * (import data or not, what use as source of the data...).
041   */
042  public enum Type
043  {
044    /**
045     * Standalone server.
046     */
047    STANDALONE,
048    /**
049     * Replicate Contents and this is the first server in topology..
050     */
051    FIRST_IN_TOPOLOGY,
052    /**
053     * Replicate Contents of the new Suffix with existing server.
054     */
055    IN_EXISTING_TOPOLOGY
056  }
057
058  private Type type;
059  private int replicationPort = getDefaultReplicationPort();
060  private boolean secureReplication;
061  private AuthenticationData authenticationData = new AuthenticationData();
062  {
063    authenticationData.setDn(Constants.DIRECTORY_MANAGER_DN);
064    authenticationData.setPort(4444);
065  }
066
067  /**
068   * Private constructor for the DataReplicationOptions object.
069   */
070  private DataReplicationOptions()
071  {
072  }
073
074  /**
075   * Construct an FIRST_IN_TOPOLOGY object.
076   * @param replicationPort the replication port.
077   * @param secureReplication whether servers must encrypt data for the
078   * replication communication with this server.
079   * @return the FIRST_IN_TOPOLOGY object.
080   */
081  public static DataReplicationOptions createFirstInTopology(
082      int replicationPort, boolean secureReplication)
083  {
084    DataReplicationOptions options = new DataReplicationOptions();
085    options.type = Type.FIRST_IN_TOPOLOGY;
086    options.replicationPort = replicationPort;
087    options.secureReplication = secureReplication;
088    return options;
089  }
090
091  /**
092   * Construct an STANDALONE object.
093   * @return the STANDALONE object.
094   */
095  public static DataReplicationOptions createStandalone()
096  {
097    DataReplicationOptions options = new DataReplicationOptions();
098    options.type = Type.STANDALONE;
099    return options;
100  }
101
102  /**
103   * Construct an IN_EXISTING_TOPOLOGY object.
104   * @param authenticationData the authentication data.
105   * @param replicationPort the replication port.
106   * @param secureReplication whether servers must encrypt data for the
107   * replication communication with this server.
108   * @return the IN_EXISTING_TOPOLOGY object.
109   */
110  public static DataReplicationOptions createInExistingTopology(
111      AuthenticationData authenticationData, int replicationPort,
112      boolean secureReplication)
113  {
114    DataReplicationOptions options = new DataReplicationOptions();
115    options.type = Type.IN_EXISTING_TOPOLOGY;
116    options.authenticationData = authenticationData;
117    options.replicationPort = replicationPort;
118    options.secureReplication = secureReplication;
119    return options;
120  }
121
122  /**
123   * Returns the type of DataReplicationOptions represented by this object
124   * (replicate or not).
125   *
126   * @return the type of DataReplicationOptions.
127   */
128  public Type getType()
129  {
130    return type;
131  }
132
133  /**
134   * Returns the AuthenticationData to the server used to replicate.
135   * If it is standalone returns null.
136   *
137   * @return the AuthenticationData to the server used to replicate.
138   */
139  public AuthenticationData getAuthenticationData()
140  {
141    return authenticationData;
142  }
143
144  /**
145   * Returns the port that is going to be used for replication.
146   *
147   * @return the replication that must be used to configure replication.
148   */
149  public int getReplicationPort()
150  {
151    return replicationPort;
152  }
153
154  /**
155   * Returns whether servers must encrypt data for the replication communication
156   * with this server.
157   *
158   * @return <CODE>true</CODE> if the servers must encrypt data for the
159   * replication communication and <CODE>false</CODE> otherwise.
160   */
161  public boolean useSecureReplication()
162  {
163    return secureReplication;
164  }
165
166  /**
167   * Provides the port that will be proposed to the user in the replication
168   * options panel of the installation wizard. It will check whether we can use
169   * ports of type X989 and if not it will return -1.
170   *
171   * @return the free port of type X989 if it is available and we can use and -1
172   * if not.
173   */
174  static int getDefaultReplicationPort()
175  {
176    int defaultPort = -1;
177
178    for (int i=0;i<10000 && defaultPort == -1;i+=1000)
179    {
180      int port = i + Constants.DEFAULT_REPLICATION_PORT;
181      if (Utils.canUseAsPort(port))
182      {
183        defaultPort = port;
184      }
185    }
186    return defaultPort;
187  }
188}
189