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