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