001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel;
018
019import static com.forgerock.opendj.cli.Utils.addErrorMessageIfArgumentsConflict;
020import static org.opends.messages.ToolMessages.*;
021
022import static com.forgerock.opendj.cli.CommonArguments.*;
023
024import java.util.LinkedHashSet;
025
026import org.forgerock.i18n.LocalizableMessage;
027import org.opends.quicksetup.Constants;
028import org.opends.quicksetup.UserData;
029import org.opends.quicksetup.util.Utils;
030import org.opends.server.config.AdministrationConnector;
031import org.opends.server.core.DirectoryServer.DirectoryServerVersionHandler;
032
033import com.forgerock.opendj.cli.ArgumentException;
034import com.forgerock.opendj.cli.ArgumentParser;
035import com.forgerock.opendj.cli.BooleanArgument;
036import com.forgerock.opendj.cli.FileBasedArgument;
037import com.forgerock.opendj.cli.IntegerArgument;
038import com.forgerock.opendj.cli.StringArgument;
039
040/** Class used to parse the arguments of the control panel command-line. */
041public class ControlPanelArgumentParser extends ArgumentParser
042{
043  /** The 'hostName' global argument. */
044  private StringArgument hostNameArg;
045  /** The 'port' global argument. */
046  private IntegerArgument portArg;
047
048  /** The 'bindDN' global argument. */
049  private StringArgument bindDnArg;
050  /** The 'bindPasswordFile' global argument. */
051  private FileBasedArgument bindPasswordFileArg;
052  /** The 'bindPassword' global argument. */
053  private StringArgument bindPasswordArg;
054
055  /** The 'trustAllArg' global argument. */
056  private BooleanArgument trustAllArg;
057  /** The 'remoteArg' global argument. */
058  private BooleanArgument remoteArg;
059  /** Argument to specify the connect timeout. */
060  private IntegerArgument connectTimeoutArg;
061  private BooleanArgument showUsageArg;
062
063  /**
064   * The default constructor for this class.
065   * @param mainClassName the class name of the main class for the command-line
066   * that is being used.
067   * @param msg the usage message.
068   */
069  public ControlPanelArgumentParser(String mainClassName,
070      LocalizableMessage msg)
071  {
072    super(mainClassName, msg, false);
073    setShortToolDescription(REF_SHORT_DESC_CONTROL_PANEL.get());
074    setVersionHandler(new DirectoryServerVersionHandler());
075  }
076
077  /**
078   * Returns the default value for the administration port.
079   * @return the default value for the administration port.
080   */
081  public static int getDefaultAdministrationPort()
082  {
083    return AdministrationConnector.DEFAULT_ADMINISTRATION_CONNECTOR_PORT;
084  }
085
086  /**
087   * Returns the default bind DN.
088   * @return the default bind DN.
089   */
090  public static String getDefaultBindDN()
091  {
092    return "cn=Directory Manager";
093  }
094
095  /**
096   * Initializes the arguments without parsing them.
097   * @throws ArgumentException if there was an error creating or adding the
098   * arguments.  If this occurs is likely to be a bug.
099   */
100  public void initializeArguments() throws ArgumentException
101  {
102    hostNameArg = hostNameArgument(UserData.getDefaultHostName());
103    addArgument(hostNameArg);
104
105    portArg =
106        portArgument(getDefaultAdministrationPort(),
107            INFO_DESCRIPTION_ADMIN_PORT.get());
108    addArgument(portArg);
109
110    bindDnArg = bindDNArgument(getDefaultBindDN());
111    addArgument(bindDnArg);
112
113    bindPasswordArg = bindPasswordArgument();
114    addArgument(bindPasswordArg);
115
116    bindPasswordFileArg = bindPasswordFileArgument();
117    addArgument(bindPasswordFileArg);
118
119    trustAllArg = trustAllArgument();
120    addArgument(trustAllArg);
121
122    remoteArg = remoteArgument();
123    addArgument(remoteArg);
124
125    connectTimeoutArg = connectTimeOutArgument();
126    addArgument(connectTimeoutArg);
127
128    showUsageArg = showUsageArgument();
129    addArgument(showUsageArg);
130    setUsageArgument(showUsageArg);
131  }
132
133  @Override
134  public void parseArguments(String[] args) throws ArgumentException
135  {
136    LinkedHashSet<LocalizableMessage> errorMessages = new LinkedHashSet<>();
137    try
138    {
139      super.parseArguments(args);
140    }
141    catch (ArgumentException ae)
142    {
143      errorMessages.add(ae.getMessageObject());
144    }
145    addErrorMessageIfArgumentsConflict(errorMessages, bindPasswordArg, bindPasswordFileArg);
146
147    if (!errorMessages.isEmpty())
148    {
149      throw new ArgumentException(ERR_CANNOT_INITIALIZE_ARGS.get(
150          Utils.getMessageFromCollection(errorMessages, Constants.LINE_SEPARATOR)));
151    }
152  }
153
154  /**
155   * Returns the host name explicitly provided in the command-line.
156   * @return the host name bind DN explicitly provided in the command-line.
157   * Returns {@code null} if no bind DN was explicitly provided.
158   */
159  public String getExplicitHostName()
160  {
161    return hostNameArg.isPresent() ? hostNameArg.getValue() : null;
162  }
163
164  /**
165   * Returns the administration port explicitly provided in the command-line.
166   * @return the administration port explicitly provided in the command-line.
167   * Returns -1 if no port was explicitly provided.
168   */
169  public int getExplicitPort()
170  {
171    if (portArg.isPresent())
172    {
173      try
174      {
175        return portArg.getIntValue();
176      }
177      catch (ArgumentException ae)
178      {
179        throw new IllegalStateException("Error parsing data: "+ae, ae);
180      }
181    }
182    return -1;
183  }
184
185  /**
186   * Returns the bind DN explicitly provided in the command-line.
187   * @return the bind DN explicitly provided in the command-line.
188   * Returns {@code null} if no bind DN was explicitly provided.
189   */
190  public String getExplicitBindDn()
191  {
192    return bindDnArg.isPresent() ? bindDnArg.getValue() : null;
193  }
194
195  /**
196   * Get the password which has to be used for the command without prompting
197   * the user.  If no password was specified, return {@code null}.
198   *
199   * @return The password stored into the specified file on by the
200   *         command line argument, or {@code null} if not specified.
201   */
202  public String getBindPassword()
203  {
204    return getBindPassword(bindPasswordArg, bindPasswordFileArg);
205  }
206
207  /**
208   * Returns whether the user specified to trust all certificates or not.
209   * @return whether the user specified to trust all certificates or not.
210   */
211  public boolean isTrustAll()
212  {
213    return trustAllArg.isPresent();
214  }
215
216  /**
217   * Returns the timeout to be used to connect in milliseconds.  The method
218   * must be called after parsing the arguments.
219   * @return the timeout to be used to connect in milliseconds.  Returns
220   * {@code 0} if there is no timeout.
221   * @throws IllegalStateException if the method is called before
222   * parsing the arguments.
223   */
224  public int getConnectTimeout() throws IllegalStateException
225  {
226    try
227    {
228      return connectTimeoutArg.getIntValue();
229    }
230    catch (ArgumentException ae)
231    {
232      throw new IllegalStateException("Argument parser is not parsed: "+ae, ae);
233    }
234  }
235
236  /**
237   * Returns whether the user specified to connect to a remote server.
238   * @return whether the user specified to connect to a remote server.
239   */
240  public boolean isRemote()
241  {
242    return remoteArg.isPresent();
243  }
244}