/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at
* trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable,
* add the following below this CDDL HEADER, with the fields enclosed
* by brackets "[]" replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
*
* Copyright 2008-2010 Sun Microsystems, Inc.
* Portions Copyright 2012-2013 ForgeRock AS
*/
/**
* This class provides an abstract base class which can be used as the basis of
* a console-based application.
*/
public abstract class ConsoleApplication
{
/**
* A null reader.
*/
{
/**
* {@inheritDoc}
*/
{
// Do nothing.
}
/**
* {@inheritDoc}
*/
{
return -1;
}
}
/**
* Defines the different line styles for output.
*/
public enum Style {
/**
* Defines a title.
*/
/**
* Defines a subtitle.
*/
/**
* Defines a notice.
*/
/**
* Defines a normal line.
*/
/**
* Defines an error.
*/
/**
* Defines a breakline.
*/
}
// The error stream which this application should use.
// The input stream reader which this application should use.
// The output stream which this application should use.
/**
* The maximum number of times we try to confirm.
*/
/**
* The String used to write comments in a shell (or batch) script.
*/
/**
* Creates a new console application instance.
*
* @param in
* The application input stream.
* @param out
* The application output stream.
* @param err
* The application error stream.
*/
{
{
}
else
{
}
{
}
else
{
}
{
}
else
{
}
}
/**
* Creates a new console application instance.
*
* @param in
* The application input stream.
* @param out
* The application output stream.
* @param err
* The application error stream.
*/
{
{
}
else
{
}
{
}
else
{
}
{
}
else
{
}
}
/**
* Interactively confirms whether a user wishes to perform an action. If the
* application is non-interactive, then the provided default is returned
* automatically.
*
* @param prompt
* The prompt describing the action.
* @param defaultValue
* The default value for the confirmation message. This will be
* returned if the application is non-interactive or if the user just
* presses return.
* @return Returns <code>true</code> if the user wishes the action to be
* performed, or <code>false</code> if they refused, or if an
* exception occurred.
* @throws CLIException
* If the user's response could not be read from the console for
* some reason.
*/
throws CLIException
{
if (!isInteractive())
{
return defaultValue;
}
prompt =
{
{
{
return defaultValue;
}
{
return false;
}
{
return true;
}
else
{
// Try again...
}
return null;
}
};
}
/**
* Gets the application error stream.
*
* @return Returns the application error stream.
*/
{
return err;
}
/**
* Gets the application input stream.
*
* @return Returns the application input stream.
*/
{
return in;
}
/**
* Gets the application output stream.
*
* @return Returns the application output stream.
*/
{
return out;
}
/**
* Indicates whether or not the user has requested advanced mode.
*
* @return Returns <code>true</code> if the user has requested advanced mode.
*/
public abstract boolean isAdvancedMode();
/**
* Indicates whether or not the user has requested interactive behavior.
*
* @return Returns <code>true</code> if the user has requested interactive
* behavior.
*/
public abstract boolean isInteractive();
/**
* Indicates whether or not this console application is running in its
* menu-driven mode. This can be used to dictate whether output should go to
* the error stream or not. In addition, it may also dictate whether or not
* sub-menus should display a cancel option as well as a quit option.
*
* @return Returns <code>true</code> if this console application is running in
* its menu-driven mode.
*/
public abstract boolean isMenuDrivenMode();
/**
* Indicates whether or not the user has requested quiet output.
*
* @return Returns <code>true</code> if the user has requested quiet output.
*/
public abstract boolean isQuiet();
/**
* Indicates whether or not the user has requested script-friendly output.
*
* @return Returns <code>true</code> if the user has requested script-friendly
* output.
*/
public abstract boolean isScriptFriendly();
/**
* Indicates whether or not the user has requested verbose output.
*
* @return Returns <code>true</code> if the user has requested verbose output.
*/
public abstract boolean isVerbose();
/**
* Interactively prompts the user to press return to continue. This method
* should be called in situations where a user needs to be given a chance to
* read some documentation before continuing (continuing may cause the
* documentation to be scrolled out of view).
*/
public final void pressReturnToContinue()
{
try
{
}
catch (CLIException e)
{
// Ignore the exception - applications don't care.
}
}
/**
* Displays a blank line to the error stream.
*/
public final void println()
{
}
/**
* Displays a message to the error stream.
*
* @param msg
* The message.
*/
{
}
/**
* Displays a message to the error stream.
*
* @param msg
* The message.
*/
{
}
/**
* Print a line with EOL in the output stream.
*
* @param msg
* The message to display in normal mode.
* @param indent
* The indentation.
*/
{
}
/**
* Print a line with EOL in the output stream.
*
* @param msgStyle
* The type of formatted output desired.
* @param msg
* The message to display in normal mode.
* @param indent
* The indentation.
*/
final int indent)
{
if (!isQuiet())
{
switch (msgStyle)
{
case TITLE:
break;
case SUBTITLE:
break;
case NOTICE:
break;
case ERROR:
break;
case BREAKLINE:
break;
default:
break;
}
}
}
/**
* Displays a blank line to the output stream if we are not in quiet mode.
*/
public final void printlnProgress()
{
if (!isQuiet())
{
}
}
/**
* Displays a message to the output stream if we are not in quiet mode.
* Message is wrap to max line width.
*
* @param msg
* The message.
*/
{
if (!isQuiet())
{
}
}
/**
* Displays a message to the output stream if we are not in quiet mode.
*
* @param msg
* The message.
*/
{
if (!isQuiet())
{
}
}
/**
* Prints a progress bar on the same output stream line if not in quiet mode.
*
* <pre>
* Like
* msg...... 50%
* if progress is up to 100 :
* msg..................... 100%
* if progress is < 0 :
* msg.... FAIL
* msg..................... FAIL
* </pre>
*
* @param linePos
* The progress bar starts at this position on the line.
* @param progress
* The current percentage progress to print.
*/
{
if (!isQuiet())
{
if (progress != 0)
{
for (int i = 0; i < 50; i++)
{
{
}
}
}
if(progress >= 0) {
} else {
}
for (int i = 0; i < endBuilder; i++)
{
}
{
}
}
}
/**
* Display the batch progress string to the error stream, if we are not in
* quiet mode.
*
* @param s
* The string to display
*/
{
if (!isQuiet())
{
}
}
/**
* Displays a message to the error stream indented by the specified number of
* columns.
*
* @param msg
* The message.
* @param indent
* The number of columns to indent.
*/
{
}
/**
* Displays a message to the error stream if verbose mode is enabled.
*
* @param msg
* The verbose message.
*/
{
if (isVerbose() || isInteractive())
{
}
}
/**
* Interactively retrieves a line of input from the console.
*
* @param prompt
* The prompt.
* @return Returns the line of input, or <code>null</code> if the end of input
* has been reached.
* @throws CLIException
* If the line of input could not be retrieved for some reason.
*/
{
{
}
try
{
if (s == null)
{
throw CLIException
}
else
{
return s;
}
}
catch (IOException e)
{
throw CLIException.adaptInputException(e);
}
}
/**
* Displays a message and read the user's input from output.
*
* @param prompt
* The message to display.
* @param defaultValue
* The default answer by default.
* @param msgStyle
* The formatted style chosen.
* @return The user's input as a string.
* @throws CLIException
* If an Exception occurs during the process.
*/
throws CLIException
{
final Message messageToDisplay =
{
println();
}
try
{
// Reads the user input.
}
catch (IOException e)
{
throw CLIException.adaptInputException(e);
}
{
println();
}
{
if (defaultValue == null)
{
}
else
{
return defaultValue;
}
}
return answer;
}
/**
* Commodity method that interactively prompts (on error output) the user to
* provide a string value. Any non-empty string will be allowed (the empty
* string will indicate that the default should be used, if there is one).
*
* @param prompt
* The prompt to present to the user.
* @param defaultValue
* The default value to assume if the user presses ENTER without
* typing anything, or <CODE>null</CODE> if there should not be a
* default and the user must explicitly provide a value.
* @throws CLIException
* If the line of input could not be retrieved for some reason.
* @return The string value read from the user.
*/
throws CLIException
{
while (true)
{
if (defaultValue != null)
{
prompt =
}
{
if (defaultValue == null)
{
}
else
{
return defaultValue;
}
}
else
{
return response;
}
}
}
/**
* Commodity method that interactively prompts (on error output) the user to
* provide a string value. Any non-empty string will be allowed (the empty
* string will indicate that the default should be used, if there is one). If
* an error occurs a message will be logged to the provided logger.
*
* @param prompt
* The prompt to present to the user.
* @param defaultValue
* The default value to assume if the user presses ENTER without
* typing anything, or <CODE>null</CODE> if there should not be a
* default and the user must explicitly provide a value.
* @param logger
* the Logger to be used to log the error message.
* @return The string value read from the user.
*/
{
String s = defaultValue;
try
{
}
catch (CLIException ce)
{
}
return s;
}
/**
* Interactively retrieves a password from the console.
*
* @param prompt
* The password prompt.
* @return Returns the password.
* @throws CLIException
* If the password could not be retrieved for some reason.
*/
{
char[] pwChars;
try
{
}
catch (Exception e)
{
throw CLIException.adaptInputException(e);
}
}
/**
* Commodity method that interactively retrieves a password from the console.
* If there is an error an error message is logged to the provided Logger and
* <CODE>null</CODE> is returned.
*
* @param prompt
* The password prompt.
* @param logger
* the Logger to be used to log the error message.
* @return Returns the password.
*/
{
try
{
}
catch (CLIException ce)
{
}
return pwd;
}
/**
* Interactively retrieves a port value from the console.
*
* @param prompt
* The port prompt.
* @param defaultValue
* The port default value.
* @return Returns the port.
* @throws CLIException
* If the port could not be retrieved for some reason.
*/
throws CLIException
{
{
throws CLIException
{
{
return defaultValue;
}
else
{
try
{
if (i < 1 || i > 65535)
{
throw new NumberFormatException();
}
return i;
}
catch (NumberFormatException e)
{
// Try again...
return null;
}
}
}
};
if (defaultValue != -1)
{
prompt =
.valueOf(defaultValue));
}
}
/**
* Returns a message object for the given NamingException.
*
* @param ne
* the NamingException.
* @param hostPort
* the hostPort representation of the server we were contacting when
* the NamingException occurred.
* @return a message object for the given NamingException.
*/
{
}
/**
* Commodity method used to repeatidly ask the user to provide a port value.
*
* @param prompt
* the prompt message.
* @param defaultValue
* the default value of the port to be proposed to the user.
* @param logger
* the logger where the errors will be written.
* @return the port value provided by the user.
*/
{
int port = -1;
while (port == -1)
{
try
{
}
catch (CLIException ce)
{
port = -1;
}
}
return port;
}
/**
* Interactively prompts for user input and continues until valid input is
* provided.
*
* @param <T>
* The type of decoded user input.
* @param prompt
* The interactive prompt which should be displayed on each input
* attempt.
* @param validator
* An input validator responsible for validating and decoding the
* user's response.
* @return Returns the decoded user's response.
* @throws CLIException
* If an unexpected error occurred which prevented validation.
*/
{
while (true)
{
{
return value;
}
}
}
/**
* Interactively prompts for user input and continues until valid input is
* provided.
*
* @param <T>
* The type of decoded user input.
* @param prompt
* The interactive prompt which should be displayed on each input
* attempt.
* @param validator
* An input validator responsible for validating and decoding the
* user's response.
* @param maxTries
* The maximum number of tries that we can make.
* @return Returns the decoded user's response.
* @throws CLIException
* If an unexpected error occurred which prevented validation or if
* the maximum number of tries was reached.
*/
{
int nTries = 0;
{
{
return value;
}
nTries++;
}
}
/**
* Commodity method that interactively confirms whether a user wishes to
* perform an action. If the application is non-interactive, then the provided
* default is returned automatically. If there is an error an error message is
* logged to the provided Logger and the defaul value is returned.
*
* @param prompt
* The prompt describing the action.
* @param defaultValue
* The default value for the confirmation message. This will be
* returned if the application is non-interactive or if the user just
* presses return.
* @param logger
* the Logger to be used to log the error message.
* @return Returns <code>true</code> if the user wishes the action to be
* performed, or <code>false</code> if they refused.
* @throws CLIException
* if the user did not provide valid answer after a certain number
* of tries (ConsoleApplication.CONFIRMATION_MAX_TRIES)
*/
{
boolean v = defaultValue;
boolean done = false;
int nTries = 0;
{
nTries++;
try
{
done = true;
}
catch (CLIException ce)
{
{
throw ce;
}
// Try again...
println();
}
}
if (!done)
{
// This means we reached the maximum number of tries
}
return v;
}
/**
* Returns an InitialLdapContext using the provided parameters. We try to
* guarantee that the connection is able to read the configuration.
*
* @param host
* the host name.
* @param port
* the port to connect.
* @param useSSL
* whether to use SSL or not.
* @param useStartTLS
* whether to use StartTLS or not.
* @param bindDn
* the bind dn to be used.
* @param pwd
* the password.
* @param connectTimeout
* the timeout in milliseconds to connect to the server.
* @param trustManager
* the trust manager.
* @return an InitialLdapContext connected.
* @throws NamingException
* if there was an error establishing the connection.
*/
throws NamingException
{
if (useSSL)
{
ctx =
}
else if (useStartTLS)
{
ctx =
}
else
{
}
{
.toString());
}
return ctx;
}
/**
* Creates an Initial LDAP Context interacting with the user if the
* application is interactive.
*
* @param ci
* the LDAPConnectionConsoleInteraction object that is assumed to
* have been already run.
* @return the initial LDAP context or <CODE>null</CODE> if the user did not
* accept to trust the certificates.
* @throws ClientException
* if there was an error establishing the connection.
*/
{
&& ci.isTrustStoreInMemory());
}
/**
* Creates an Initial LDAP Context interacting with the user if the
* application is interactive.
*
* @param ci
* the LDAPConnectionConsoleInteraction object that is assumed to
* have been already run.
* @param promptForCertificate
* whether we should prompt for the certificate or not.
* @return the initial LDAP context or <CODE>null</CODE> if the user did not
* accept to trust the certificates.
* @throws ClientException
* if there was an error establishing the connection.
*/
throws ClientException
{
// Interact with the user though the console to get
// LDAP connection information
{
while (true)
{
try
{
ctx =
break;
}
catch (NamingException e)
{
if (promptForCertificate)
{
{
if (trustManager instanceof ApplicationTrustManager)
{
}
{
// If the certificate is trusted, update the trust manager.
// Try to connect again.
continue;
}
else
{
// Assume user canceled.
return null;
}
}
}
{
{
if (getCertificateRootException(e) != null
|| (e.getCause() instanceof SSLHandshakeException))
{
throw new ClientException(
}
}
if (e.getCause() instanceof SSLException)
{
throw new ClientException(
}
}
message);
}
}
}
else if (ci.useStartTLS())
{
while (true)
{
try
{
ctx =
break;
}
catch (NamingException e)
{
if (promptForCertificate)
{
{
if (trustManager instanceof ApplicationTrustManager)
{
}
{
// If the certificate is trusted, update the trust manager.
// Try to connect again.
continue;
}
else
{
// Assume user cancelled.
return null;
}
}
else
{
.valueOf(portNumber));
throw new ClientException(
}
}
.valueOf(portNumber));
message);
}
}
}
else
{
while (true)
{
try
{
ctx =
break;
}
catch (NamingException e)
{
.valueOf(portNumber));
message);
}
}
}
return ctx;
}
/**
* Returns the message to be displayed in the file with the equivalent
* command-line with information about the current time.
*
* @return the message to be displayed in the file with the equivalent
* command-line with information about the current time.
*/
{
}
/**
*
* @param date
* to format; null if <code>date</code> is null
* @return string representation of the date
*/
{
{
}
return timeStr;
}
/**
* Prompts the user to give the Global Administrator UID.
*
* @param defaultValue
* the default value that will be proposed in the prompt message.
* @param logger
* the Logger to be used to log the error message.
* @return the Global Administrator UID as provided by the user.
*/
{
String s = defaultValue;
try
{
}
catch (CLIException ce)
{
}
return s;
}
/**
* Prompts the user to give the Global Administrator password.
*
* @param logger
* the Logger to be used to log the error message.
* @return the Global Administrator password as provided by the user.
*/
{
return pwd;
}
{
{
t = t.getCause();
if (t instanceof OpendsCertificateException)
{
oce = (OpendsCertificateException) t;
}
}
return oce;
}
/**
* Commodity method used to repeatidly ask the user to provide an integer
* value.
*
* @param prompt
* the prompt message.
* @param defaultValue
* the default value to be proposed to the user.
* @param logger
* the logger where the errors will be written.
* @return the value provided by the user.
*/
{
int newInt = -1;
while (newInt == -1)
{
try
{
}
catch (CLIException ce)
{
newInt = -1;
}
}
return newInt;
}
/**
* Interactively retrieves an integer value from the console.
*
* @param prompt
* The message prompt.
* @param defaultValue
* The default value.
* @return Returns the value.
* @throws CLIException
* If the value could not be retrieved for some reason.
*/
throws CLIException
{
{
throws CLIException
{
{
return defaultValue;
}
else
{
try
{
if (i < 1)
{
throw new NumberFormatException();
}
return i;
}
catch (NumberFormatException e)
{
// Try again...
return null;
}
}
}
};
if (defaultValue != -1)
{
prompt =
.valueOf(defaultValue));
}
}
}