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 2009-2010 Sun Microsystems, Inc.
025 *      Portions Copyright 2011-2015 ForgeRock AS.
026 */
027package org.opends.guitools.controlpanel;
028
029import static org.opends.messages.ToolMessages.*;
030
031import java.util.LinkedHashSet;
032
033import org.forgerock.i18n.LocalizableMessage;
034import org.opends.quicksetup.Constants;
035import org.opends.quicksetup.UserData;
036import org.opends.quicksetup.util.Utils;
037import org.opends.server.admin.AdministrationConnector;
038import org.opends.server.core.DirectoryServer.DirectoryServerVersionHandler;
039
040import com.forgerock.opendj.cli.ArgumentException;
041import com.forgerock.opendj.cli.ArgumentParser;
042import com.forgerock.opendj.cli.BooleanArgument;
043import com.forgerock.opendj.cli.CommonArguments;
044import com.forgerock.opendj.cli.FileBasedArgument;
045import com.forgerock.opendj.cli.IntegerArgument;
046import com.forgerock.opendj.cli.StringArgument;
047
048/**
049 * Class used to parse the arguments of the control panel command-line.
050 */
051public class ControlPanelArgumentParser extends ArgumentParser
052{
053  /** The 'hostName' global argument. */
054  private StringArgument hostNameArg;
055  /** The 'port' global argument. */
056  private IntegerArgument portArg;
057
058  /** The 'bindDN' global argument. */
059  private StringArgument bindDnArg;
060  /** The 'bindPasswordFile' global argument. */
061  private FileBasedArgument bindPasswordFileArg;
062  /** The 'bindPassword' global argument. */
063  private StringArgument bindPasswordArg;
064
065  /** The 'trustAllArg' global argument. */
066  private BooleanArgument trustAllArg;
067  /** The 'remoteArg' global argument. */
068  private BooleanArgument remoteArg;
069  /** Argument to specify the connect timeout. */
070  private IntegerArgument connectTimeoutArg;
071  private BooleanArgument showUsageArg;
072
073  /**
074   * The default constructor for this class.
075   * @param mainClassName the class name of the main class for the command-line
076   * that is being used.
077   * @param msg the usage message.
078   */
079  public ControlPanelArgumentParser(String mainClassName,
080      LocalizableMessage msg)
081  {
082    super(mainClassName, msg, false);
083    setShortToolDescription(REF_SHORT_DESC_CONTROL_PANEL.get());
084    setVersionHandler(new DirectoryServerVersionHandler());
085  }
086
087  /**
088   * Returns the default value for the administration port.
089   * @return the default value for the administration port.
090   */
091  public static int getDefaultAdministrationPort()
092  {
093    return AdministrationConnector.DEFAULT_ADMINISTRATION_CONNECTOR_PORT;
094  }
095
096  /**
097   * Returns the default bind DN.
098   * @return the default bind DN.
099   */
100  public static String getDefaultBindDN()
101  {
102    return "cn=Directory Manager";
103  }
104
105  /**
106   * Initializes the arguments without parsing them.
107   * @throws ArgumentException if there was an error creating or adding the
108   * arguments.  If this occurs is likely to be a bug.
109   */
110  public void initializeArguments() throws ArgumentException
111  {
112    hostNameArg = CommonArguments.getHostName(UserData.getDefaultHostName());
113    addArgument(hostNameArg);
114
115    portArg =
116        CommonArguments.getPort(getDefaultAdministrationPort(),
117            INFO_DESCRIPTION_ADMIN_PORT.get());
118    addArgument(portArg);
119
120    bindDnArg = CommonArguments.getBindDN(getDefaultBindDN());
121    addArgument(bindDnArg);
122
123    bindPasswordArg = CommonArguments.getBindPassword();
124    addArgument(bindPasswordArg);
125
126    bindPasswordFileArg = CommonArguments.getBindPasswordFile();
127    addArgument(bindPasswordFileArg);
128
129    trustAllArg = CommonArguments.getTrustAll();
130    addArgument(trustAllArg);
131
132    remoteArg = CommonArguments.getRemote();
133    addArgument(remoteArg);
134
135    connectTimeoutArg = CommonArguments.getConnectTimeOut();
136    connectTimeoutArg.setHidden(false);
137    addArgument(connectTimeoutArg);
138
139    showUsageArg = CommonArguments.getShowUsage();
140    addArgument(showUsageArg);
141    setUsageArgument(showUsageArg);
142  }
143
144  /** {@inheritDoc} */
145  @Override
146  public void parseArguments(String[] args) throws ArgumentException
147  {
148    LinkedHashSet<LocalizableMessage> errorMessages = new LinkedHashSet<>();
149    try
150    {
151      super.parseArguments(args);
152    }
153    catch (ArgumentException ae)
154    {
155      errorMessages.add(ae.getMessageObject());
156    }
157
158    if (bindPasswordArg.isPresent() && bindPasswordFileArg.isPresent())
159    {
160      LocalizableMessage message = ERR_TOOL_CONFLICTING_ARGS.get(
161          bindPasswordArg.getLongIdentifier(),
162          bindPasswordFileArg.getLongIdentifier());
163      errorMessages.add(message);
164    }
165
166    if (!errorMessages.isEmpty())
167    {
168      throw new ArgumentException(ERR_CANNOT_INITIALIZE_ARGS.get(
169          Utils.getMessageFromCollection(errorMessages, Constants.LINE_SEPARATOR)));
170    }
171  }
172
173  /**
174   * Returns the host name explicitly provided in the command-line.
175   * @return the host name bind DN explicitly provided in the command-line.
176   * Returns <CODE>null</CODE> if no bind DN was explicitly provided.
177   */
178  public String getExplicitHostName()
179  {
180    String hostName = null;
181    if (hostNameArg.isPresent())
182    {
183      hostName = hostNameArg.getValue();
184    }
185    return hostName;
186  }
187
188  /**
189   * Returns the administration port explicitly provided in the command-line.
190   * @return the administration port explicitly provided in the command-line.
191   * Returns -1 if no port was explicitly provided.
192   */
193  public int getExplicitPort()
194  {
195    int port = -1;
196    if (portArg.isPresent())
197    {
198      try
199      {
200        port = portArg.getIntValue();
201      }
202      catch (ArgumentException ae)
203      {
204        throw new IllegalStateException("Error parsing data: "+ae, ae);
205      }
206    }
207    return port;
208  }
209
210  /**
211   * Returns the bind DN explicitly provided in the command-line.
212   * @return the bind DN explicitly provided in the command-line.
213   * Returns <CODE>null</CODE> if no bind DN was explicitly provided.
214   */
215  public String getExplicitBindDn()
216  {
217    String dn = null;
218    if (bindDnArg.isPresent())
219    {
220      dn = bindDnArg.getValue();
221    }
222    return dn;
223  }
224
225  /**
226   * Get the password which has to be used for the command without prompting
227   * the user.  If no password was specified, return <CODE>null</CODE>.
228   *
229   * @return The password stored into the specified file on by the
230   *         command line argument, or <CODE>null</CODE> it if not specified.
231   */
232  public String getBindPassword()
233  {
234    return getBindPassword(bindPasswordArg, bindPasswordFileArg);
235  }
236
237  /**
238   * Returns whether the user specified to trust all certificates or not.
239   * @return whether the user specified to trust all certificates or not.
240   */
241  public boolean isTrustAll()
242  {
243    return trustAllArg.isPresent();
244  }
245
246  /**
247   * Returns the timeout to be used to connect in milliseconds.  The method
248   * must be called after parsing the arguments.
249   * @return the timeout to be used to connect in milliseconds.  Returns
250   * {@code 0} if there is no timeout.
251   * @throw {@code IllegalStateException} if the method is called before
252   * parsing the arguments.
253   */
254  public int getConnectTimeout()
255  {
256    try
257    {
258      return connectTimeoutArg.getIntValue();
259    }
260    catch (ArgumentException ae)
261    {
262      throw new IllegalStateException("Argument parser is not parsed: "+ae, ae);
263    }
264  }
265
266  /**
267   * Returns whether the user specified to connect to a remote server.
268   * @return whether the user specified to connect to a remote server.
269   */
270  public boolean isRemote()
271  {
272    return remoteArg.isPresent();
273  }
274}