001/*
002 * Copyright 2010-2017 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2017 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.ldap.sdk.unboundidds;
022
023
024
025import java.io.OutputStream;
026import java.io.PrintStream;
027
028import com.unboundid.ldap.listener.InMemoryDirectoryServerTool;
029import com.unboundid.ldap.sdk.ResultCode;
030import com.unboundid.ldap.sdk.Version;
031import com.unboundid.ldap.sdk.examples.AuthRate;
032import com.unboundid.ldap.sdk.examples.Base64Tool;
033import com.unboundid.ldap.sdk.examples.IdentifyReferencesToMissingEntries;
034import com.unboundid.ldap.sdk.examples.IdentifyUniqueAttributeConflicts;
035import com.unboundid.ldap.sdk.examples.LDAPCompare;
036import com.unboundid.ldap.sdk.examples.LDAPDebugger;
037import com.unboundid.ldap.sdk.examples.ModRate;
038import com.unboundid.ldap.sdk.examples.SearchRate;
039import com.unboundid.ldap.sdk.examples.SearchAndModRate;
040import com.unboundid.ldap.sdk.examples.ValidateLDIF;
041import com.unboundid.ldap.sdk.persist.GenerateSchemaFromSource;
042import com.unboundid.ldap.sdk.persist.GenerateSourceFromSchema;
043import com.unboundid.ldap.sdk.transformations.TransformLDIF;
044import com.unboundid.ldap.sdk.unboundidds.examples.DumpDNs;
045import com.unboundid.ldap.sdk.unboundidds.examples.SubtreeAccessibility;
046import com.unboundid.ldap.sdk.unboundidds.examples.SummarizeAccessLog;
047import com.unboundid.ldap.sdk.unboundidds.tools.GenerateTOTPSharedSecret;
048import com.unboundid.ldap.sdk.unboundidds.tools.LDAPModify;
049import com.unboundid.ldap.sdk.unboundidds.tools.LDAPSearch;
050import com.unboundid.ldap.sdk.unboundidds.tools.ManageAccount;
051import com.unboundid.ldap.sdk.unboundidds.tools.SplitLDIF;
052import com.unboundid.util.StaticUtils;
053import com.unboundid.util.ThreadSafety;
054import com.unboundid.util.ThreadSafetyLevel;
055
056
057
058/**
059 * This class provides an entry point that may be used to launch other tools
060 * provided as part of the LDAP SDK.  This is primarily a convenience for
061 * someone who just has the jar file and none of the scripts, since you can run
062 * "<CODE>java -jar unboundid-ldapsdk.jar {tool-name} {tool-args}</CODE>"
063 * in order to invoke any of the example tools.  Running just
064 * "<CODE>java -jar unboundid-ldapsdk.jar</CODE>" will display version
065 * information about the LDAP SDK.
066 * <BR>
067 * <BLOCKQUOTE>
068 *   <B>NOTE:</B>  This class, and other classes within the
069 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
070 *   supported for use against Ping Identity, UnboundID, and Alcatel-Lucent 8661
071 *   server products.  These classes provide support for proprietary
072 *   functionality or for external specifications that are not considered stable
073 *   or mature enough to be guaranteed to work in an interoperable way with
074 *   other types of LDAP servers.
075 * </BLOCKQUOTE>
076 * <BR>
077 * The tool names are case-insensitive.  Supported tool names include:
078 * <UL>
079 *   <LI>authrate -- Launch the {@link AuthRate} tool.</LI>
080 *   <LI>base64 -- Launch the {@link Base64Tool} tool.</LI>
081 *   <LI>deliver-one-time-password -- Launch the
082 *       {@link DeliverOneTimePassword} tool.</LI>
083 *   <LI>deliver-password-reset-token -- Launch the
084 *       {@link DeliverPasswordResetToken} tool.</LI>
085 *   <LI>dump-dns -- Launch the {@link DumpDNs} tool.</LI>
086 *   <LI>generate-schema-from-source -- Launch the
087 *       {@link GenerateSchemaFromSource} tool.</LI>
088 *   <LI>generate-source-from-schema -- Launch the
089 *       {@link GenerateSourceFromSchema} tool.</LI>
090 *   <LI>generate-totp-shared-secret -- Launch the
091 *       {@link GenerateTOTPSharedSecret} tool.</LI>
092 *   <LI>identify-references-to-missing-entries -- Launch the
093 *       {@link IdentifyReferencesToMissingEntries} tool.</LI>
094 *   <LI>identify-unique-attribute-conflicts -- Launch the
095 *       {@link IdentifyUniqueAttributeConflicts} tool.</LI>
096 *   <LI>in-memory-directory-server -- Launch the
097 *       {@link InMemoryDirectoryServerTool} tool.</LI>
098 *   <LI>ldapcompare -- Launch the {@link LDAPCompare} tool.</LI>
099 *   <LI>ldapmodify -- Launch the {@link LDAPModify} tool.</LI>
100 *   <LI>ldapsearch -- Launch the {@link LDAPSearch} tool.</LI>
101 *   <LI>ldap-debugger -- Launch the {@link LDAPDebugger} tool.</LI>
102 *   <LI>manage-account -- Launch the {@link ManageAccount} tool.</LI>
103 *   <LI>modrate -- Launch the {@link ModRate} tool.</LI>
104 *   <LI>move-subtree -- Launch the {@link MoveSubtree} tool.</LI>
105 *   <LI>register-yubikey-otp-device -- Launch the
106 *       {@link RegisterYubiKeyOTPDevice} tool.</LI>
107 *   <LI>searchrate -- Launch the {@link SearchRate} tool.</LI>
108 *   <LI>search-and-mod-rate -- Launch the {@link SearchAndModRate} tool.</LI>
109 *   <LI>split-ldif -- Launch the {@link SplitLDIF} tool.</LI>
110 *   <LI>subtree-accessibility -- Launch the {@link SubtreeAccessibility}
111 *       tool.</LI>
112 *   <LI>summarize-access-log -- Launch the {@link SummarizeAccessLog}
113 *       tool.</LI>
114 *   <LI>transform-ldif -- Launch the {@link TransformLDIF} tool.</LI>
115 *   <LI>validate-ldif -- Launch the {@link ValidateLDIF} tool.</LI>
116 *   <LI>version -- Display version information for the LDAP SDK.</LI>
117 * </UL>
118 */
119@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
120public final class Launcher
121{
122  /**
123   * Prevent this utility class from being instantiated.
124   */
125  private Launcher()
126  {
127    // No implementation required.
128  }
129
130
131
132  /**
133   * Parses the command-line arguments and performs any appropriate processing
134   * for this program.
135   *
136   * @param  args  The command-line arguments provided to this program.
137   */
138  public static void main(final String... args)
139  {
140    main(System.out, System.err, args);
141  }
142
143
144
145  /**
146   * Parses the command-line arguments and performs any appropriate processing
147   * for this program.
148   *
149   * @param  outStream  The output stream to which standard out should be
150   *                    written.  It may be {@code null} if output should be
151   *                    suppressed.
152   * @param  errStream  The output stream to which standard error should be
153   *                    written.  It may be {@code null} if error messages
154   *                    should be suppressed.
155   * @param  args       The command-line arguments provided to this program.
156   *
157   * @return  A result code with information about the status of processing.
158   */
159  public static ResultCode main(final OutputStream outStream,
160                                final OutputStream errStream,
161                                final String... args)
162  {
163    if ((args == null) || (args.length == 0) ||
164        args[0].equalsIgnoreCase("version"))
165    {
166      if (outStream != null)
167      {
168        final PrintStream out = new PrintStream(outStream);
169        for (final String line : Version.getVersionLines())
170        {
171          out.println(line);
172        }
173      }
174
175      return ResultCode.SUCCESS;
176    }
177
178    final String firstArg = StaticUtils.toLowerCase(args[0]);
179    final String[] remainingArgs = new String[args.length - 1];
180    System.arraycopy(args, 1, remainingArgs, 0, remainingArgs.length);
181
182    if (firstArg.equals("authrate"))
183    {
184      return AuthRate.main(remainingArgs, outStream, errStream);
185    }
186    else if (firstArg.equals("base64"))
187    {
188      return Base64Tool.main(System.in, outStream, errStream, remainingArgs);
189    }
190    else if (firstArg.equals("deliver-one-time-password"))
191    {
192      return DeliverOneTimePassword.main(remainingArgs, outStream, errStream);
193    }
194    else if (firstArg.equals("deliver-password-reset-token"))
195    {
196      return DeliverPasswordResetToken.main(remainingArgs, outStream,
197           errStream);
198    }
199    else if (firstArg.equals("dump-dns"))
200    {
201      return DumpDNs.main(remainingArgs, outStream, errStream);
202    }
203    else if (firstArg.equals("identify-references-to-missing-entries"))
204    {
205      return IdentifyReferencesToMissingEntries.main(remainingArgs, outStream,
206           errStream);
207    }
208    else if (firstArg.equals("identify-unique-attribute-conflicts"))
209    {
210      return IdentifyUniqueAttributeConflicts.main(remainingArgs, outStream,
211           errStream);
212    }
213    else if (firstArg.equals("in-memory-directory-server"))
214    {
215      return InMemoryDirectoryServerTool.main(remainingArgs, outStream,
216           errStream);
217    }
218    else if (firstArg.equals("generate-schema-from-source"))
219    {
220      return GenerateSchemaFromSource.main(remainingArgs, outStream, errStream);
221    }
222    else if (firstArg.equals("generate-source-from-schema"))
223    {
224      return GenerateSourceFromSchema.main(remainingArgs, outStream, errStream);
225    }
226    else if (firstArg.equals("generate-totp-shared-secret"))
227    {
228      return GenerateTOTPSharedSecret.main(outStream, errStream, remainingArgs);
229    }
230    else if (firstArg.equals("ldapcompare"))
231    {
232      return LDAPCompare.main(remainingArgs, outStream, errStream);
233    }
234    else if (firstArg.equals("ldapmodify"))
235    {
236      return LDAPModify.main(System.in, outStream, errStream, remainingArgs);
237    }
238    else if (firstArg.equals("ldapsearch"))
239    {
240      return LDAPSearch.main(outStream, errStream, remainingArgs);
241    }
242    else if (firstArg.equals("ldap-debugger"))
243    {
244      return LDAPDebugger.main(remainingArgs, outStream, errStream);
245    }
246    else if (firstArg.equals("manage-account"))
247    {
248      return ManageAccount.main(outStream, errStream, remainingArgs);
249    }
250    else if (firstArg.equals("modrate"))
251    {
252      return ModRate.main(remainingArgs, outStream, errStream);
253    }
254    else if (firstArg.equals("move-subtree"))
255    {
256      return MoveSubtree.main(remainingArgs, outStream, errStream);
257    }
258    else if (firstArg.equals("register-yubikey-otp-device"))
259    {
260      return RegisterYubiKeyOTPDevice.main(remainingArgs, outStream, errStream);
261    }
262    else if (firstArg.equals("searchrate"))
263    {
264      return SearchRate.main(remainingArgs, outStream, errStream);
265    }
266    else if (firstArg.equals("search-and-mod-rate"))
267    {
268      return SearchAndModRate.main(remainingArgs, outStream, errStream);
269    }
270    else if (firstArg.equals("split-ldif"))
271    {
272      return SplitLDIF.main(outStream, errStream, remainingArgs);
273    }
274    else if (firstArg.equals("subtree-accessibility"))
275    {
276      return SubtreeAccessibility.main(remainingArgs, outStream, errStream);
277    }
278    else if (firstArg.equals("summarize-access-log"))
279    {
280      return SummarizeAccessLog.main(remainingArgs, outStream, errStream);
281    }
282    else if (firstArg.equals("transform-ldif"))
283    {
284      return TransformLDIF.main(outStream, errStream, remainingArgs);
285    }
286    else if (firstArg.equals("validate-ldif"))
287    {
288      return ValidateLDIF.main(remainingArgs, outStream, errStream);
289    }
290    else
291    {
292      if (errStream != null)
293      {
294        final PrintStream err = new PrintStream(errStream);
295        err.println("Unrecognized tool name '" + args[0] + '\'');
296        err.println("Supported tool names include:");
297        err.println("     authrate");
298        err.println("     base64");
299        err.println("     deliver-one-time-password");
300        err.println("     deliver-password-reset-token");
301        err.println("     dump-dns");
302        err.println("     identify-references-to-missing-entries");
303        err.println("     identify-unique-attribute-conflicts");
304        err.println("     in-memory-directory-server");
305        err.println("     generate-schema-from-source");
306        err.println("     generate-source-from-schema");
307        err.println("     generate-totp-shared-secret");
308        err.println("     ldapcompare");
309        err.println("     ldapmodify");
310        err.println("     ldapsearch");
311        err.println("     ldap-debugger");
312        err.println("     manage-account");
313        err.println("     modrate");
314        err.println("     move-subtree");
315        err.println("     register-yubikey-otp-device");
316        err.println("     searchrate");
317        err.println("     search-and-mod-rate");
318        err.println("     split-ldif");
319        err.println("     subtree-accessibility");
320        err.println("     summarize-access-log");
321        err.println("     transform-ldif");
322        err.println("     validate-ldif");
323        err.println("     version");
324      }
325
326      return ResultCode.PARAM_ERROR;
327    }
328  }
329}