mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
03.55.2015 a39783c0d0b94e6eef5e7836168d919442c77f56
OPENDJ-1778 Tools --help display the java class name instead of the command name

This is a problem since switching to using the opendj-cli argument classes.
This happens because the opendj-cli tools check the new "com.forgerock.opendj.ldap.tools.scriptName" property instead of the opends property "org.opends.server.scriptName" that is still set by the server tools.
Fixed by also checking the legacy opends property.
Factorized the code from ArgumentParser, SubCommandArgumentParser and DSConfig.


ArgumentParser.java:
Added constant PROPERTY_SCRIPT_NAME_LEGACY.
Extracted methods getScriptNameOrJava(), getLocalizableScriptName() and getScriptName().
3 files modified
89 ■■■■■ changed files
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java 54 ●●●● patch | view | raw | blame | history
opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java 27 ●●●●● patch | view | raw | blame | history
opendj-config/src/main/java/org/forgerock/opendj/config/dsconfig/DSConfig.java 8 ●●●● patch | view | raw | blame | history
opendj-cli/src/main/java/com/forgerock/opendj/cli/ArgumentParser.java
@@ -74,6 +74,8 @@
    public static final String DEFAULT_OPENDJ_PROPERTIES_FILE_EXTENSION = ".properties";
    /** The name of a command-line script used to launch a tool. */
    public static final String PROPERTY_SCRIPT_NAME = "com.forgerock.opendj.ldap.tools.scriptName";
    /** The legacy name of a command-line script used to launch a tool. */
    public static final String PROPERTY_SCRIPT_NAME_LEGACY = "org.opends.server.scriptName";
    /** The argument that will be used to indicate the file properties. */
    private StringArgument filePropertiesPathArgument;
@@ -425,7 +427,7 @@
        // We have a location for the properties file.
        final Properties argumentProperties = new Properties();
        final String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        final String scriptName = getScriptName();
        try {
            final Properties p = new Properties();
            final FileInputStream fis = new FileInputStream(propertiesFilePath);
@@ -651,13 +653,8 @@
     */
    private void getUsage(final StringBuilder buffer) {
        usageOrVersionDisplayed = true;
        final String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (scriptName == null || scriptName.length() == 0) {
            buffer.append(INFO_ARGPARSER_USAGE_JAVA_CLASSNAME.get(mainClassName));
        } else {
            buffer.append(INFO_ARGPARSER_USAGE_JAVA_SCRIPTNAME.get(scriptName));
        }
        buffer.append(getLocalizableScriptName());
        if (allowsTrailingArguments) {
            buffer.append(" ");
            if (trailingArgsDisplayName != null) {
@@ -741,6 +738,49 @@
    }
    /**
     * Returns the script name or a Java equivalent command-line string.
     *
     * @return the script name or a Java equivalent command-line string
     */
    String getScriptNameOrJava() {
        final String scriptName = getScriptName();
        if (scriptName != null && scriptName.length() != 0) {
            return scriptName;
        }
        return "java " + getMainClassName();
    }
    /**
     * Returns the script name as a {@link LocalizableMessage}.
     *
     * @return the script name as a {@link LocalizableMessage}
     */
    LocalizableMessage getLocalizableScriptName() {
        final String scriptName = getScriptName();
        if (scriptName == null || scriptName.length() == 0) {
            return INFO_ARGPARSER_USAGE_JAVA_CLASSNAME.get(mainClassName);
        }
        return INFO_ARGPARSER_USAGE_JAVA_SCRIPTNAME.get(scriptName);
    }
    /**
     * Returns the script name if set.
     *
     * @return the script name, or {@code null} if not set
     */
    String getScriptName() {
        final String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (scriptName != null && scriptName.length() != 0) {
            return scriptName;
        }
        final String legacyScriptName = System.getProperty(PROPERTY_SCRIPT_NAME_LEGACY);
        if (legacyScriptName != null && legacyScriptName.length() != 0) {
            return legacyScriptName;
        }
        return null;
    }
    /**
     * Returns the usage argument.
     *
     * @return the usageArgument
opendj-cli/src/main/java/com/forgerock/opendj/cli/SubCommandArgumentParser.java
@@ -772,11 +772,8 @@
     *            The subcommand for which to display the usage information.
     */
    public void getSubCommandUsage(StringBuilder buffer, SubCommand subCommand) {
        String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (scriptName == null || scriptName.length() == 0) {
            scriptName = "java " + getMainClassName();
        }
        buffer.append(INFO_ARGPARSER_USAGE_JAVA_SCRIPTNAME.get(scriptName));
        final String scriptName = getScriptNameOrJava();
        buffer.append(getLocalizableScriptName());
        buffer.append("  ");
        buffer.append(scriptName);
@@ -867,13 +864,9 @@
     */
    public LocalizableMessage getHelpUsageReference() {
        setUsageOrVersionDisplayed(true);
        String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (scriptName == null || scriptName.length() == 0) {
            scriptName = "java " + getMainClassName();
        }
        LocalizableMessageBuilder buffer = new LocalizableMessageBuilder();
        buffer.append(INFO_GLOBAL_HELP_REFERENCE.get(scriptName));
        buffer.append(INFO_GLOBAL_HELP_REFERENCE.get(getScriptNameOrJava()));
        buffer.append(EOL);
        return buffer.toMessage();
    }
@@ -919,13 +912,9 @@
            buffer.append(EOL).append(EOL);
        }
        String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (scriptName == null || scriptName.length() == 0) {
            scriptName = "java " + getMainClassName();
        }
        buffer.append(INFO_ARGPARSER_USAGE.get());
        buffer.append("  ");
        buffer.append(scriptName);
        buffer.append(getScriptNameOrJava());
        if (subCommands.isEmpty()) {
            buffer.append(" ").append(INFO_SUBCMDPARSER_OPTIONS.get());
@@ -1175,10 +1164,14 @@
     * @return Refsect2 representation of the subcommand.
     */
    private String toRefSect2(SubCommand sc) {
        final String toolName = "dsconfig";
        final String scriptName = getScriptName();
        if (scriptName == null) {
            throw new RuntimeException("The script name should have been set via the environment property '"
                    + PROPERTY_SCRIPT_NAME + "'.");
        }
        final StringBuilder sb = new StringBuilder();
        sb.append("<refsect2 xml:id=\"").append(toolName).append("-").append(sc.getName()).append("\">").append(EOL);
        sb.append("<refsect2 xml:id=\"").append(scriptName).append("-").append(sc.getName()).append("\">").append(EOL);
        sb.append(" <title>dsconfig ").append(sc.getName()).append("</title>").append(EOL);
        sb.append(" <para>").append(sc.getDescription()).append("</para>").append(EOL);
opendj-config/src/main/java/org/forgerock/opendj/config/dsconfig/DSConfig.java
@@ -167,7 +167,7 @@
                            new ArrayList<PropertyDefinition<?>>(defn.getAllPropertyDefinitions());
                    Collections.sort(props);
                    final String propPrefix = DSCONFIGTOOLNAME + "-" + sc.getName() + "-" + argLongID + "-";
                    final String propPrefix = getScriptName() + "-" + sc.getName() + "-" + argLongID + "-";
                    sb.append(EOL);
                    toSimpleList(props, propPrefix, sb);
                    sb.append(EOL);
@@ -1239,7 +1239,7 @@
     * @return <T> The builded command.
     */
    <T> CommandBuilder getCommandBuilder(final T subCommand) {
        final String commandName = getCommandName();
        final String commandName = getScriptName();
        final String subCommandName;
        if (subCommand instanceof SubCommandHandler) {
            subCommandName = ((SubCommandHandler) subCommand).getSubCommand().getName();
@@ -1272,7 +1272,7 @@
        return commandBuilder;
    }
    private String getCommandName() {
    private String getScriptName() {
        final String commandName = System.getProperty(PROPERTY_SCRIPT_NAME);
        if (commandName != null && commandName.length() != 0) {
            return commandName;
@@ -1338,7 +1338,7 @@
     */
    private String getSessionStartTimeMessage() {
        final String date = formatDateTimeStringForEquivalentCommand(new Date(sessionStartTime));
        return INFO_DSCFG_SESSION_START_TIME_MESSAGE.get(getCommandName(), date).toString();
        return INFO_DSCFG_SESSION_START_TIME_MESSAGE.get(getScriptName(), date).toString();
    }
    private void handleBatchFile(String[] args) {