OPENDJ-2772 Code cleanup
Extract a runToolAndExit() method in Utils to avoid code duplication in
toolkit's tools main methods.
Thanks to Jean-Noël for the help.
11 files modified
1 files added
| | |
| | | import static com.forgerock.opendj.cli.CliMessages.ERR_FILEARG_NO_SUCH_FILE; |
| | | import static com.forgerock.opendj.cli.CommonArguments.showUsageArgument; |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict; |
| | | import static com.forgerock.opendj.ldap.CoreMessages.ERR_BASE64_DECODE_INVALID_LENGTH; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException; |
| | |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.parseArguments; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static com.forgerock.opendj.util.StaticUtils.getExceptionMessage; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | |
| | | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import com.forgerock.opendj.cli.SubCommand; |
| | | import com.forgerock.opendj.cli.SubCommandArgumentParser; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * Tool that can be used for performing base64 encoding and decoding. |
| | |
| | | * sets of three bytes with eight significant bits each to sets of four bytes |
| | | * with six significant bits each. |
| | | */ |
| | | public final class Base64 extends ConsoleApplication { |
| | | public final class Base64 extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for base64 tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new Base64(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link Base64} tool with the provided arguments. |
| | | * Output and errors will be write on the provided streams. |
| | | * This method can be use to run the tool programmatically. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be use by the tool to write results and information messages. |
| | | * @param err |
| | | * {@link PrintStream} which will be use by the tool to write errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final Base64 base64 = new Base64(out, err); |
| | | try { |
| | | return base64.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(base64); |
| | | return e.getResultCode(); |
| | | } |
| | | } |
| | | |
| | | private Base64(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | Base64(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | final SubCommandArgumentParser argParser = |
| | | new SubCommandArgumentParser(Base64.class.getName(), INFO_BASE64_TOOL_DESCRIPTION.get(), false); |
| | | argParser.setShortToolDescription(REF_SHORT_DESC_BASE64.get()); |
| | |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.Utils.readBytesFromFile; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage; |
| | |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readControls; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | |
| | | import java.io.PrintStream; |
| | | import java.util.List; |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConnectionFactoryProvider; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** A tool that can be used to issue Compare requests to the Directory Server. */ |
| | | public final class LDAPCompare extends ConsoleApplication { |
| | | public final class LDAPCompare extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldapcompare tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDAPCompare(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDAPCompare} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this ldap tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDAPCompare ldapCompare = new LDAPCompare(out, err); |
| | | try { |
| | | return ldapCompare.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldapCompare); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDAPCompare(out, err), args); |
| | | } |
| | | |
| | | private BooleanArgument verbose; |
| | | |
| | | private LDAPCompare(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDAPCompare(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return verbose.isPresent(); |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final LocalizableMessage toolDescription = INFO_LDAPCOMPARE_TOOL_DESCRIPTION.get(); |
| | | final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPCompare.class.getName()) |
| | |
| | | import static com.forgerock.opendj.cli.CommonArguments.showUsageArgument; |
| | | import static com.forgerock.opendj.cli.CommonArguments.verboseArgument; |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getConnection; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printSuccessMessage; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readControls; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.i18n.LocalizableMessage.raw; |
| | | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConnectionFactoryProvider; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.opendj.ldap.Connection; |
| | |
| | | import org.forgerock.opendj.ldap.requests.DeleteRequest; |
| | | import org.forgerock.opendj.ldap.requests.Requests; |
| | | import org.forgerock.opendj.ldap.responses.Result; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | |
| | | import java.util.List; |
| | | |
| | | /** A tool that can be used to issue delete requests to the Directory Server. */ |
| | | public final class LDAPDelete extends ConsoleApplication { |
| | | public final class LDAPDelete extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldapdelete tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDAPDelete(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDAPDelete} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this ldap tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDAPDelete ldapDelete = new LDAPDelete(out, err); |
| | | try { |
| | | return ldapDelete.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldapDelete); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDAPDelete(out, err), args); |
| | | } |
| | | |
| | | private LDAPDelete(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDAPDelete(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return verbose.isPresent(); |
| | | } |
| | | |
| | | private int run(String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(String... args) throws LDAPToolException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final LocalizableMessage toolDescription = INFO_LDAPDELETE_TOOL_DESCRIPTION.get(); |
| | | final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPDelete.class.getName()) |
| | |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getConnection; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printSuccessMessage; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readAssertionControl; |
| | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage; |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.IOException; |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConnectionFactoryProvider; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * A tool that can be used to issue update (Add/Delete/Modify/ModifyDN) requests |
| | | * to the Directory Server. |
| | | */ |
| | | public final class LDAPModify extends ConsoleApplication { |
| | | public final class LDAPModify extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldapmodify tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDAPModify(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDAPModify} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this ldap tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDAPModify ldapModify = new LDAPModify(out, err); |
| | | try { |
| | | return ldapModify.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldapModify); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDAPModify(out, err), args); |
| | | } |
| | | |
| | | private final class VisitorImpl implements ChangeRecordVisitor<Integer, Void> { |
| | |
| | | private Collection<Control> controls; |
| | | private BooleanArgument verbose; |
| | | |
| | | private LDAPModify(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDAPModify(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return verbose.isPresent(); |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPModify.class.getName()) |
| | | .toolDescription(INFO_LDAPMODIFY_TOOL_DESCRIPTION.get()) |
| | |
| | | import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readControls; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | |
| | | import java.io.PrintStream; |
| | | |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConnectionFactoryProvider; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.FileBasedArgument; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * A tool that can be used to issue LDAP password modify extended requests to |
| | |
| | | * All of these are optional components that may be included or omitted from the |
| | | * request. |
| | | */ |
| | | public final class LDAPPasswordModify extends ConsoleApplication { |
| | | public final class LDAPPasswordModify extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldappasswordmodify tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDAPPasswordModify(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDAPPasswordModify} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this ldap tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDAPPasswordModify ldapPasswordModify = new LDAPPasswordModify(out, err); |
| | | try { |
| | | return ldapPasswordModify.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldapPasswordModify); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDAPPasswordModify(out, err), args); |
| | | } |
| | | |
| | | private BooleanArgument verbose; |
| | | |
| | | private LDAPPasswordModify(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDAPPasswordModify(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return verbose.isPresent(); |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final LocalizableMessage toolDescription = INFO_LDAPPWMOD_TOOL_DESCRIPTION.get(); |
| | | final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPPasswordModify.class.getName()) |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConnectionFactoryProvider; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.IntegerArgument; |
| | | import com.forgerock.opendj.cli.MultiChoiceArgument; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import com.forgerock.opendj.ldap.controls.AccountUsabilityResponseControl; |
| | | import com.forgerock.opendj.util.StaticUtils; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | import static com.forgerock.opendj.cli.CliMessages.INFO_NUM_ENTRIES_PLACEHOLDER; |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readAssertionControl; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readControls; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readFilterFromString; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.util.Utils.*; |
| | | |
| | | import static com.forgerock.opendj.cli.ArgumentConstants.*; |
| | |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | | /** A tool that can be used to issue Search requests to the Directory Server. */ |
| | | public final class LDAPSearch extends ConsoleApplication { |
| | | public final class LDAPSearch extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldapsearch tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDAPSearch(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDAPSearch} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this ldap tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDAPSearch ldapSearch = new LDAPSearch(out, err); |
| | | try { |
| | | return ldapSearch.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldapSearch); |
| | | return e.getResultCode(); |
| | | } catch (final ArgumentException e) { |
| | | ldapSearch.errPrintln(ERR_ERROR_PARSING_ARGS.get(e.getMessageObject())); |
| | | return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue(); |
| | | } |
| | | return runTool(new LDAPSearch(out, err), args); |
| | | } |
| | | |
| | | private class LDAPSearchResultHandler implements SearchResultHandler { |
| | |
| | | |
| | | private EntryWriter ldifWriter; |
| | | |
| | | private LDAPSearch(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDAPSearch(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return verbose.isPresent(); |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException, ArgumentException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | try { |
| | | return runLdapSearch(args); |
| | | } catch (final ArgumentException e) { |
| | | throw newToolParamException(e, ERR_ERROR_PARSING_ARGS.get(e.getMessageObject())); |
| | | } |
| | | } |
| | | |
| | | int runLdapSearch(final String[] args) throws LDAPToolException, ArgumentException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final LocalizableMessage toolDescription = INFO_LDAPSEARCH_TOOL_DESCRIPTION.get(); |
| | | final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPSearch.class.getName()) |
| | |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolInputStream; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.parseArguments; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.IOException; |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.ArgumentParser; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * This utility can be used to compare two LDIF files and report the differences |
| | | * in LDIF format. |
| | | */ |
| | | public final class LDIFDiff extends ConsoleApplication { |
| | | public final class LDIFDiff extends ToolConsoleApplication { |
| | | |
| | | static final int NO_DIFFERENCES_FOUND = 0; |
| | | static final int DIFFERENCES_FOUND = 1; |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDIFDiff(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDIFDiff} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDIFDiff ldifDiff = new LDIFDiff(out, err); |
| | | try { |
| | | return ldifDiff.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldifDiff); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDIFDiff(out, err), args); |
| | | } |
| | | |
| | | private LDIFDiff(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDIFDiff(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFDiff.class.getName()) |
| | | .toolDescription(INFO_LDIFDIFF_TOOL_DESCRIPTION.get()) |
| | | .trailingArguments(2, "source target") |
| | |
| | | import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolInputStream; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.parseArguments; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.ArgumentParser; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * A tool that can be used to issue update (Add/Delete/Modify/ModifyDN) requests |
| | | * to a set of entries contained in an LDIF file. |
| | | */ |
| | | public final class LDIFModify extends ConsoleApplication { |
| | | public final class LDIFModify extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldifmodify tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDIFModify(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDIFModify} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDIFModify ldifModify = new LDIFModify(out, err); |
| | | try { |
| | | return ldifModify.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldifModify); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDIFModify(out, err), args); |
| | | } |
| | | |
| | | private LDIFModify(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDIFModify(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | // Create the command-line argument parser for use with this program. |
| | | final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFModify.class.getName()) |
| | | .toolDescription(INFO_LDIFMODIFY_TOOL_DESCRIPTION.get()) |
| | |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.CommonArguments.*; |
| | | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn; |
| | |
| | | import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.parseArguments; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.readFilterFromString; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.PrintStream; |
| | |
| | | import java.util.List; |
| | | |
| | | import org.forgerock.i18n.LocalizableException; |
| | | import org.forgerock.i18n.LocalizableMessage; |
| | | import org.forgerock.i18n.LocalizedIllegalArgumentException; |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.Filter; |
| | |
| | | import com.forgerock.opendj.cli.ArgumentException; |
| | | import com.forgerock.opendj.cli.ArgumentParser; |
| | | import com.forgerock.opendj.cli.BooleanArgument; |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.cli.IntegerArgument; |
| | | import com.forgerock.opendj.cli.MultiChoiceArgument; |
| | | import com.forgerock.opendj.cli.StringArgument; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** This utility can be used to perform search operations against data in an LDIF file. */ |
| | | public final class LDIFSearch extends ConsoleApplication { |
| | | public final class LDIFSearch extends ToolConsoleApplication { |
| | | |
| | | /** |
| | | * The main method for ldifsearch tool. |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new LDIFSearch(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link LDIFSearch} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final LDIFSearch ldifSearch = new LDIFSearch(out, err); |
| | | try { |
| | | return ldifSearch.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(ldifSearch); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new LDIFSearch(out, err), args); |
| | | } |
| | | |
| | | private LDIFSearch(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | LDIFSearch(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | return false; |
| | | } |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFSearch.class.getName()) |
| | | .toolDescription(INFO_LDIFSEARCH_TOOL_DESCRIPTION.get()) |
| | | .trailingArgumentsUnbounded(2, "source filter [attributes ...]") |
| | |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolExceptionAlreadyPrinted; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException; |
| | | import static com.forgerock.opendj.ldap.tools.ToolsMessages.*; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.parseArguments; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runTool; |
| | | import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit; |
| | | import static org.forgerock.util.Utils.closeSilently; |
| | | |
| | | import java.io.BufferedWriter; |
| | |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | | import org.forgerock.opendj.ldif.EntryGenerator; |
| | | import org.forgerock.opendj.ldif.LDIFEntryWriter; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** Program that generate LDIF content based on a template. */ |
| | | public final class MakeLDIF extends ConsoleApplication { |
| | | public final class MakeLDIF extends ToolConsoleApplication { |
| | | /** The value for the constant option in LDIF generator tools. */ |
| | | public static final String OPTION_LONG_CONSTANT = "constant"; |
| | | |
| | |
| | | * The command-line arguments provided to this program. |
| | | */ |
| | | public static void main(final String[] args) { |
| | | System.exit(filterExitCode(run(System.out, System.err, args))); |
| | | runToolAndExit(new MakeLDIF(System.out, System.err), args); |
| | | } |
| | | |
| | | /** |
| | | * Run {@link MakeLDIF} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * This method should be used to run this tool programmatically. |
| | | * Output and errors will be printed on provided {@link PrintStream}. |
| | | * |
| | | * @param out |
| | | * {@link PrintStream} which will be used by the tool to write results and information messages. |
| | | * The {@link PrintStream} to use to write tool output. |
| | | * @param err |
| | | * {@link PrintStream} which will be used by the tool to write errors. |
| | | * The {@link PrintStream} to use to write tool errors. |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * The arguments to use with this tool. |
| | | * @return The code returned by the tool |
| | | */ |
| | | public static int run(final PrintStream out, final PrintStream err, final String... args) { |
| | | final MakeLDIF makeLDIF = new MakeLDIF(out, err); |
| | | try { |
| | | return makeLDIF.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(makeLDIF); |
| | | return e.getResultCode(); |
| | | } |
| | | return runTool(new MakeLDIF(out, err), args); |
| | | } |
| | | |
| | | private MakeLDIF(final PrintStream out, final PrintStream err) { |
| | | @VisibleForTesting |
| | | MakeLDIF(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | |
| | | /** The total number of entries that have been written. */ |
| | | private long numberOfEntriesWritten; |
| | | |
| | | private int run(final String[] args) throws LDAPToolException { |
| | | @Override |
| | | int run(final String... args) throws LDAPToolException { |
| | | final LocalizableMessage toolDescription = INFO_MAKELDIF_TOOL_DESCRIPTION.get(); |
| | | final ArgumentParser argParser = LDAPToolArgumentParser.builder(MakeLDIF.class.getName()) |
| | | .toolDescription(toolDescription) |
| New file |
| | |
| | | /* |
| | | * The contents of this file are subject to the terms of the Common Development and |
| | | * Distribution License (the License). You may not use this file except in compliance with the |
| | | * License. |
| | | * |
| | | * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the |
| | | * specific language governing permission and limitations under the License. |
| | | * |
| | | * When distributing Covered Software, include this CDDL Header Notice in each file and include |
| | | * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL |
| | | * Header, with the fields enclosed by brackets [] replaced by your own identifying |
| | | * information: "Portions Copyright [year] [name of copyright owner]". |
| | | * |
| | | * Copyright 2016 ForgeRock AS. |
| | | */ |
| | | package com.forgerock.opendj.ldap.tools; |
| | | |
| | | |
| | | import java.io.PrintStream; |
| | | |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | |
| | | /** Represents an OpenDJ client console-based. */ |
| | | abstract class ToolConsoleApplication extends ConsoleApplication { |
| | | |
| | | ToolConsoleApplication(final PrintStream out, final PrintStream err) { |
| | | super(out, err); |
| | | } |
| | | |
| | | /** |
| | | * Run this {@link ToolConsoleApplication} tool with the provided arguments. |
| | | * Output and errors will be written on the provided streams. |
| | | * This method can be used to run the tool programmatically. |
| | | * |
| | | * @param args |
| | | * Arguments set to pass to the tool. |
| | | * @return |
| | | * An integer which represents the result code of the tool. |
| | | * @throws LDAPToolException |
| | | * If an error occurs with either client inputs or tool execution. |
| | | */ |
| | | abstract int run(final String... args) throws LDAPToolException; |
| | | } |
| | |
| | | |
| | | import static com.forgerock.opendj.cli.ArgumentConstants.USE_SYSTEM_STREAM_TOKEN; |
| | | import static com.forgerock.opendj.cli.CliConstants.NO_WRAPPING_BY_DEFAULT; |
| | | import static com.forgerock.opendj.cli.Utils.filterExitCode; |
| | | import static com.forgerock.opendj.cli.Utils.readBytesFromFile; |
| | | import static com.forgerock.opendj.cli.Utils.secondsToTimeString; |
| | | import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException; |
| | |
| | | import com.forgerock.opendj.cli.ConsoleApplication; |
| | | import com.forgerock.opendj.ldap.controls.AccountUsabilityRequestControl; |
| | | import org.forgerock.opendj.ldap.responses.Result; |
| | | import org.forgerock.util.annotations.VisibleForTesting; |
| | | |
| | | /** |
| | | * This class provides utility functions for all the client side tools. |
| | |
| | | return NO_WRAPPING_BY_DEFAULT; |
| | | } |
| | | |
| | | static void runToolAndExit(final ToolConsoleApplication tool, final String[] args) { |
| | | System.exit(filterExitCode(runTool(tool, args))); |
| | | } |
| | | |
| | | @VisibleForTesting |
| | | static int runTool(final ToolConsoleApplication tool, final String... args) { |
| | | try { |
| | | return tool.run(args); |
| | | } catch (final LDAPToolException e) { |
| | | e.printErrorMessage(tool); |
| | | return e.getResultCode(); |
| | | } |
| | | } |
| | | |
| | | /** Prevent instantiation. */ |
| | | private Utils() { |
| | | // Do nothing. |