001/*
002 * Copyright 2010-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2010-2024 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2010-2024 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util;
037
038
039
040import java.io.OutputStream;
041import java.io.PrintStream;
042
043import com.unboundid.ldap.listener.InMemoryDirectoryServerTool;
044import com.unboundid.ldap.sdk.ResultCode;
045import com.unboundid.ldap.sdk.Version;
046import com.unboundid.ldap.sdk.examples.AuthRate;
047import com.unboundid.ldap.sdk.examples.Base64Tool;
048import com.unboundid.ldap.sdk.examples.IdentifyReferencesToMissingEntries;
049import com.unboundid.ldap.sdk.examples.IdentifyUniqueAttributeConflicts;
050import com.unboundid.ldap.sdk.examples.IndentLDAPFilter;
051import com.unboundid.ldap.sdk.examples.LDAPCompare;
052import com.unboundid.ldap.sdk.examples.LDAPDebugger;
053import com.unboundid.ldap.sdk.examples.LDAPModify;
054import com.unboundid.ldap.sdk.examples.LDAPSearch;
055import com.unboundid.ldap.sdk.examples.ModRate;
056import com.unboundid.ldap.sdk.examples.SearchRate;
057import com.unboundid.ldap.sdk.examples.SearchAndModRate;
058import com.unboundid.ldap.sdk.examples.TestLDAPSDKPerformance;
059import com.unboundid.ldap.sdk.examples.ValidateLDIF;
060import com.unboundid.ldap.sdk.persist.GenerateSchemaFromSource;
061import com.unboundid.ldap.sdk.persist.GenerateSourceFromSchema;
062import com.unboundid.ldap.sdk.schema.ValidateLDAPSchema;
063import com.unboundid.ldap.sdk.transformations.TransformLDIF;
064import com.unboundid.ldif.LDIFDiff;
065import com.unboundid.ldif.LDIFModify;
066import com.unboundid.ldif.LDIFSearch;
067import com.unboundid.util.ssl.TLSCipherSuiteSelector;
068import com.unboundid.util.ssl.cert.ManageCertificates;
069
070
071
072/**
073 * This class provides an entry point that may be used to launch other tools
074 * provided as part of the LDAP SDK.  This is primarily a convenience for
075 * someone who just has the jar file and none of the scripts, since you can run
076 * "<CODE>java -jar unboundid-ldapsdk.jar {tool-name} {tool-args}</CODE>"
077 * in order to invoke any of the example tools.  Running just
078 * "<CODE>java -jar unboundid-ldapsdk.jar</CODE>" will display version
079 * information about the LDAP SDK.
080 * <BR><BR>
081 * The tool names are case-insensitive.  Supported tool names include:
082 * <UL>
083 *   <LI>authrate -- Launch the {@link AuthRate} tool.</LI>
084 *   <LI>base64 -- Launch the {@link Base64Tool} tool.</LI>
085 *   <LI>generate-schema-from-source -- Launch the
086 *       {@link GenerateSchemaFromSource} tool.</LI>
087 *   <LI>generate-source-from-schema -- Launch the
088 *       {@link GenerateSourceFromSchema} tool.</LI>
089 *   <LI>identify-references-to-missing-entries -- Launch the
090 *       {@link IdentifyReferencesToMissingEntries} tool.</LI>
091 *   <LI>identify-unique-attribute-conflicts -- Launch the
092 *       {@link IdentifyUniqueAttributeConflicts} tool.</LI>
093 *   <LI>indent-ldap-filter -- Launch the {@link IndentLDAPFilter} tool.</LI>
094 *   <LI>in-memory-directory-server -- Launch the
095 *       {@link InMemoryDirectoryServerTool} tool.</LI>
096 *   <LI>ldapcompare -- Launch the {@link LDAPCompare} tool.</LI>
097 *   <LI>ldapmodify -- Launch the {@link LDAPModify} tool.</LI>
098 *   <LI>ldapsearch -- Launch the {@link LDAPSearch} tool.</LI>
099 *   <LI>ldap-debugger -- Launch the {@link LDAPDebugger} tool.</LI>
100 *   <LI>ldifmodify -- Launch the {@link LDIFModify} tool.</LI>
101 *   <LI>ldifsearch -- Launch the {@link LDIFSearch} tool.</LI>
102 *   <LI>ldif-diff -- Launch the {@link LDIFDiff} tool.</LI>
103 *   <LI>manage-certificates -- Launch the {@link ManageCertificates} tool.</LI>
104 *   <LI>modrate -- Launch the {@link ModRate} tool.</LI>
105 *   <LI>searchrate -- Launch the {@link SearchRate} tool.</LI>
106 *   <LI>search-and-mod-rate -- Launch the {@link SearchAndModRate} tool.</LI>
107 *   <LI>tls-cipher-suite-selector -- Launch the {@link TLSCipherSuiteSelector}
108 *       tool.</LI>
109 *   <LI>transform-ldif -- Launch the {@link TransformLDIF} tool.</LI>
110 *   <LI>validate-ldap-schema -- Launch the {@link ValidateLDAPSchema}
111 *       tool.</LI>
112 *   <LI>validate-ldif -- Launch the {@link ValidateLDIF} tool.</LI>
113 *   <LI>version -- Display version information for the LDAP SDK.</LI>
114 * </UL>
115 */
116@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
117public final class Launcher
118{
119  /**
120   * Prevent this utility class from being externally instantiated.
121   */
122  private Launcher()
123  {
124    // No implementation required.
125  }
126
127
128
129  /**
130   * Parses the command-line arguments and performs any appropriate processing
131   * for this program.
132   *
133   * @param  args  The command-line arguments provided to this program.
134   */
135  public static void main(@NotNull final String... args)
136  {
137    main(System.out, System.err, args);
138  }
139
140
141
142  /**
143   * Parses the command-line arguments and performs any appropriate processing
144   * for this program.
145   *
146   * @param  outStream  The output stream to which standard out should be
147   *                    written.  It may be {@code null} if output should be
148   *                    suppressed.
149   * @param  errStream  The output stream to which standard error should be
150   *                    written.  It may be {@code null} if error messages
151   *                    should be suppressed.
152   * @param  args       The command-line arguments provided to this program.
153   *
154   * @return  A result code with information about the status of processing.
155   */
156  @NotNull()
157  public static ResultCode main(@Nullable final OutputStream outStream,
158                                @Nullable final OutputStream errStream,
159                                @Nullable final String... args)
160  {
161
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("identify-references-to-missing-entries"))
191    {
192      return IdentifyReferencesToMissingEntries.main(remainingArgs, outStream,
193           errStream);
194    }
195    else if (firstArg.equals("identify-unique-attribute-conflicts"))
196    {
197      return IdentifyUniqueAttributeConflicts.main(remainingArgs, outStream,
198           errStream);
199    }
200    else if (firstArg.equals("indent-ldap-filter"))
201    {
202      return IndentLDAPFilter.main(outStream, errStream, remainingArgs);
203    }
204    else if (firstArg.equals("in-memory-directory-server"))
205    {
206      return InMemoryDirectoryServerTool.main(remainingArgs, outStream,
207           errStream);
208    }
209    else if (firstArg.equals("generate-schema-from-source"))
210    {
211      return GenerateSchemaFromSource.main(remainingArgs, outStream, errStream);
212    }
213    else if (firstArg.equals("generate-source-from-schema"))
214    {
215      return GenerateSourceFromSchema.main(remainingArgs, outStream, errStream);
216    }
217    else if (firstArg.equals("ldapcompare"))
218    {
219      return LDAPCompare.main(remainingArgs, outStream, errStream);
220    }
221    else if (firstArg.equals("ldapmodify"))
222    {
223      return LDAPModify.main(remainingArgs, outStream, errStream);
224    }
225    else if (firstArg.equals("ldapsearch"))
226    {
227      return LDAPSearch.main(remainingArgs, outStream, errStream);
228    }
229    else if (firstArg.equals("ldap-debugger"))
230    {
231      return LDAPDebugger.main(remainingArgs, outStream, errStream);
232    }
233    else if (firstArg.equals("ldifmodify"))
234    {
235      return LDIFModify.main(outStream, errStream, remainingArgs);
236    }
237    else if (firstArg.equals("ldifsearch"))
238    {
239      return LDIFSearch.main(outStream, errStream, remainingArgs);
240    }
241    else if (firstArg.equals("ldif-diff"))
242    {
243      return LDIFDiff.main(outStream, errStream, remainingArgs);
244    }
245    else if (firstArg.equals("manage-certificates"))
246    {
247      return ManageCertificates.main(System.in, outStream, errStream,
248           remainingArgs);
249    }
250    else if (firstArg.equals("modrate"))
251    {
252      return ModRate.main(remainingArgs, outStream, errStream);
253    }
254    else if (firstArg.equals("searchrate"))
255    {
256      return SearchRate.main(remainingArgs, outStream, errStream);
257    }
258    else if (firstArg.equals("search-and-mod-rate"))
259    {
260      return SearchAndModRate.main(remainingArgs, outStream, errStream);
261    }
262    else if (firstArg.equals("test-ldap-sdk-performance"))
263    {
264      return TestLDAPSDKPerformance.main(outStream, errStream, remainingArgs);
265    }
266    else if (firstArg.equals("tls-cipher-suite-selector"))
267    {
268      return TLSCipherSuiteSelector.main(outStream, errStream, remainingArgs);
269    }
270    else if (firstArg.equals("transform-ldif"))
271    {
272      return TransformLDIF.main(outStream, errStream, remainingArgs);
273    }
274    else if (firstArg.equals("validate-ldap-schema"))
275    {
276      return ValidateLDAPSchema.main(outStream, errStream, remainingArgs);
277    }
278    else if (firstArg.equals("validate-ldif"))
279    {
280      return ValidateLDIF.main(remainingArgs, outStream, errStream);
281    }
282    else
283    {
284      if (errStream != null)
285      {
286        final PrintStream err = new PrintStream(errStream);
287        err.println("Unrecognized tool name '" + args[0] + '\'');
288        err.println("Supported tool names include:");
289        err.println("     authrate");
290        err.println("     base64");
291        err.println("     generate-schema-from-source");
292        err.println("     generate-source-from-schema");
293        err.println("     identify-references-to-missing-entries");
294        err.println("     identify-unique-attribute-conflicts");
295        err.println("     indent-ldap-filter");
296        err.println("     in-memory-directory-server");
297        err.println("     ldapcompare");
298        err.println("     ldapmodify");
299        err.println("     ldapsearch");
300        err.println("     ldap-debugger");
301        err.println("     ldifmodify");
302        err.println("     ldifsearch");
303        err.println("     ldif-diff");
304        err.println("     manage-certificates");
305        err.println("     modrate");
306        err.println("     searchrate");
307        err.println("     search-and-mod-rate");
308        err.println("     test-ldap-sdk-performance");
309        err.println("     tls-cipher-suite-selector");
310        err.println("     transform-ldif");
311        err.println("     validate-ldap-schema");
312        err.println("     validate-ldif");
313        err.println("     version");
314      }
315
316      return ResultCode.PARAM_ERROR;
317    }
318  }
319}