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 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2012-2015 ForgeRock AS.
026 */
027package org.opends.server.tools.status;
028
029import static com.forgerock.opendj.cli.CommonArguments.*;
030
031import static org.opends.messages.AdminToolMessages.*;
032import static org.opends.messages.ToolMessages.*;
033
034import java.io.OutputStream;
035import java.util.ArrayList;
036
037import org.opends.server.admin.client.cli.SecureConnectionCliArgs;
038import org.opends.server.admin.client.cli.SecureConnectionCliParser;
039import org.opends.server.core.DirectoryServer.DirectoryServerVersionHandler;
040
041import com.forgerock.opendj.cli.Argument;
042import com.forgerock.opendj.cli.ArgumentException;
043import com.forgerock.opendj.cli.BooleanArgument;
044import com.forgerock.opendj.cli.IntegerArgument;
045import com.forgerock.opendj.cli.StringArgument;
046
047/**
048 * The class that is used to parse the arguments provided in the status command
049 * line.
050 */
051public class StatusCliArgumentParser extends SecureConnectionCliParser
052{
053  private BooleanArgument noPromptArg;
054  /** This CLI is always using the administration connector with SSL. */
055  private static final boolean alwaysSSL = true;
056
057  /** The 'refresh' argument. */
058  private IntegerArgument refreshArg;
059  /** The 'scriptFriendly' argument. */
060  private BooleanArgument scriptFriendlyArg;
061
062  /**
063   * Creates a new instance of this argument parser with no arguments.
064   *
065   * @param mainClassName
066   *          The fully-qualified name of the Java class that should
067   *          be invoked to launch the program with which this
068   *          argument parser is associated.
069   */
070  public StatusCliArgumentParser(String mainClassName)
071  {
072    super(mainClassName, INFO_STATUS_CLI_USAGE_DESCRIPTION.get(), false);
073    setVersionHandler(new DirectoryServerVersionHandler());
074    setShortToolDescription(REF_SHORT_DESC_STATUS.get());
075  }
076
077  /**
078   * Initialize Global option.
079   *
080   * @param outStream
081   *          The output stream used for the usage.
082   * @throws ArgumentException
083   *           If there is a problem with any of the parameters used
084   *           to create this argument.
085   */
086  public void initializeGlobalArguments(OutputStream outStream)
087  throws ArgumentException
088  {
089    ArrayList<Argument> defaultArgs = new ArrayList<>(createGlobalArguments(outStream, alwaysSSL));
090    defaultArgs.remove(secureArgsList.portArg);
091    defaultArgs.remove(secureArgsList.hostNameArg);
092    defaultArgs.remove(verboseArg);
093    defaultArgs.remove(noPropertiesFileArg);
094    defaultArgs.remove(propertiesFileArg);
095    noPromptArg = getNoPrompt();
096    defaultArgs.add(0, noPromptArg);
097
098    scriptFriendlyArg = getScriptFriendly();
099    defaultArgs.add(1, scriptFriendlyArg);
100
101    StringArgument propertiesFileArgument = getPropertiesFile();
102
103    defaultArgs.add(propertiesFileArgument);
104    setFilePropertiesArgument(propertiesFileArgument);
105
106    BooleanArgument noPropertiesFileArgument = getNoPropertiesFile();
107    defaultArgs.add(noPropertiesFileArgument);
108    setNoPropertiesFileArgument(noPropertiesFileArgument);
109
110    initializeGlobalArguments(defaultArgs);
111
112    refreshArg = new IntegerArgument("refresh", 'r',
113        "refresh", false, true, INFO_PERIOD_PLACEHOLDER.get(),
114        true, 1, false, Integer.MAX_VALUE,
115        INFO_DESCRIPTION_REFRESH_PERIOD.get());
116    addGlobalArgument(refreshArg, ioArgGroup);
117  }
118
119  /**
120   * Returns the SecureConnectionCliArgs object containing the arguments
121   * of this parser.
122   * @return the SecureConnectionCliArgs object containing the arguments
123   * of this parser.
124   */
125  SecureConnectionCliArgs getSecureArgsList()
126  {
127    return secureArgsList;
128  }
129
130  /**
131   * Tells whether the user specified to have an interactive status CLI or not.
132   * This method must be called after calling parseArguments.
133   * @return <CODE>true</CODE> if the user specified to have an interactive
134   * status CLI and <CODE>false</CODE> otherwise.
135   */
136  public boolean isInteractive()
137  {
138    return !noPromptArg.isPresent();
139  }
140
141  /**
142   * Tells whether the user specified to have a script-friendly output or not.
143   * This method must be called after calling parseArguments.
144   * @return <CODE>true</CODE> if the user specified to have a script-friendly
145   * output and <CODE>false</CODE> otherwise.
146   */
147  public boolean isScriptFriendly()
148  {
149    return scriptFriendlyArg.isPresent();
150  }
151
152  /**
153   * Returns the refresh period (in seconds) specified in the command-line.
154   * If no refresh period was specified, returns -1.
155   * The code assumes that the attributes have been successfully parsed.
156   * @return the specified refresh period in the command-line.
157   */
158  public int getRefreshPeriod()
159  {
160    if (refreshArg.isPresent())
161    {
162      try
163      {
164        return refreshArg.getIntValue();
165      }
166      catch (ArgumentException ae)
167      {
168        // Bug
169        throw new IllegalStateException("Error getting value, this method "+
170            "should be called after parsing the attributes: "+ae, ae);
171      }
172    }
173    return -1;
174  }
175
176  /**
177   * Returns the bind DN explicitly provided in the command-line.
178   * @return the bind DN explicitly provided in the command-line.
179   * Returns <CODE>null</CODE> if no bind DN was explicitly provided.
180   */
181  public String getExplicitBindDn()
182  {
183    if (secureArgsList.bindDnArg.isPresent())
184    {
185      return secureArgsList.bindDnArg.getValue();
186    }
187    return null;
188  }
189
190  /**
191   * Returns the bind DN default value.
192   * @return the bind DN default value.
193   */
194  public String getDefaultBindDn()
195  {
196    return secureArgsList.bindDnArg.getDefaultValue();
197  }
198}