001/*
002 * Copyright 2010-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2010-2019 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.util;
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.IndentLDAPFilter;
036import com.unboundid.ldap.sdk.examples.LDAPCompare;
037import com.unboundid.ldap.sdk.examples.LDAPDebugger;
038import com.unboundid.ldap.sdk.examples.LDAPModify;
039import com.unboundid.ldap.sdk.examples.LDAPSearch;
040import com.unboundid.ldap.sdk.examples.ModRate;
041import com.unboundid.ldap.sdk.examples.SearchRate;
042import com.unboundid.ldap.sdk.examples.SearchAndModRate;
043import com.unboundid.ldap.sdk.examples.ValidateLDIF;
044import com.unboundid.ldap.sdk.persist.GenerateSchemaFromSource;
045import com.unboundid.ldap.sdk.persist.GenerateSourceFromSchema;
046import com.unboundid.ldap.sdk.transformations.TransformLDIF;
047import com.unboundid.util.ssl.TLSCipherSuiteSelector;
048import com.unboundid.util.ssl.cert.ManageCertificates;
049
050
051
052/**
053 * This class provides an entry point that may be used to launch other tools
054 * provided as part of the LDAP SDK.  This is primarily a convenience for
055 * someone who just has the jar file and none of the scripts, since you can run
056 * "<CODE>java -jar unboundid-ldapsdk.jar {tool-name} {tool-args}</CODE>"
057 * in order to invoke any of the example tools.  Running just
058 * "<CODE>java -jar unboundid-ldapsdk.jar</CODE>" will display version
059 * information about the LDAP SDK.
060 * <BR><BR>
061 * The tool names are case-insensitive.  Supported tool names include:
062 * <UL>
063 *   <LI>authrate -- Launch the {@link AuthRate} tool.</LI>
064 *   <LI>base64 -- Launch the {@link Base64Tool} tool.</LI>
065 *   <LI>generate-schema-from-source -- Launch the
066 *       {@link GenerateSchemaFromSource} tool.</LI>
067 *   <LI>generate-source-from-schema -- Launch the
068 *       {@link GenerateSourceFromSchema} tool.</LI>
069 *   <LI>identify-references-to-missing-entries -- Launch the
070 *       {@link IdentifyReferencesToMissingEntries} tool.</LI>
071 *   <LI>identify-unique-attribute-conflicts -- Launch the
072 *       {@link IdentifyUniqueAttributeConflicts} tool.</LI>
073 *   <LI>indent-ldap-filter -- Launch the {@link IndentLDAPFilter} tool.</LI>
074 *   <LI>in-memory-directory-server -- Launch the
075 *       {@link InMemoryDirectoryServerTool} tool.</LI>
076 *   <LI>ldapcompare -- Launch the {@link LDAPCompare} tool.</LI>
077 *   <LI>ldapmodify -- Launch the {@link LDAPModify} tool.</LI>
078 *   <LI>ldapsearch -- Launch the {@link LDAPSearch} tool.</LI>
079 *   <LI>ldap-debugger -- Launch the {@link LDAPDebugger} tool.</LI>
080 *   <LI>manage-certificates -- Launch the {@link ManageCertificates} tool.</LI>
081 *   <LI>modrate -- Launch the {@link ModRate} tool.</LI>
082 *   <LI>searchrate -- Launch the {@link SearchRate} tool.</LI>
083 *   <LI>search-and-mod-rate -- Launch the {@link SearchAndModRate} tool.</LI>
084 *   <LI>tls-cipher-suite-selector -- Launch the {@link TLSCipherSuiteSelector}
085 *       tool.</LI>
086 *   <LI>transform-ldif -- Launch the {@link TransformLDIF} tool.</LI>
087 *   <LI>validate-ldif -- Launch the {@link ValidateLDIF} tool.</LI>
088 *   <LI>version -- Display version information for the LDAP SDK.</LI>
089 * </UL>
090 */
091@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
092public final class Launcher
093{
094  /**
095   * Prevent this utility class from being externally instantiated.
096   */
097  private Launcher()
098  {
099    // No implementation required.
100  }
101
102
103
104  /**
105   * Parses the command-line arguments and performs any appropriate processing
106   * for this program.
107   *
108   * @param  args  The command-line arguments provided to this program.
109   */
110  public static void main(final String... args)
111  {
112    main(System.out, System.err, args);
113  }
114
115
116
117  /**
118   * Parses the command-line arguments and performs any appropriate processing
119   * for this program.
120   *
121   * @param  outStream  The output stream to which standard out should be
122   *                    written.  It may be {@code null} if output should be
123   *                    suppressed.
124   * @param  errStream  The output stream to which standard error should be
125   *                    written.  It may be {@code null} if error messages
126   *                    should be suppressed.
127   * @param  args       The command-line arguments provided to this program.
128   *
129   * @return  A result code with information about the status of processing.
130   */
131  public static ResultCode main(final OutputStream outStream,
132                                final OutputStream errStream,
133                                final String... args)
134  {
135
136
137    if ((args == null) || (args.length == 0) ||
138        args[0].equalsIgnoreCase("version"))
139    {
140      if (outStream != null)
141      {
142        final PrintStream out = new PrintStream(outStream);
143        for (final String line : Version.getVersionLines())
144        {
145          out.println(line);
146        }
147      }
148
149      return ResultCode.SUCCESS;
150    }
151
152    final String firstArg = StaticUtils.toLowerCase(args[0]);
153    final String[] remainingArgs = new String[args.length - 1];
154    System.arraycopy(args, 1, remainingArgs, 0, remainingArgs.length);
155
156    if (firstArg.equals("authrate"))
157    {
158      return AuthRate.main(remainingArgs, outStream, errStream);
159    }
160    else if (firstArg.equals("base64"))
161    {
162      return Base64Tool.main(System.in, outStream, errStream, remainingArgs);
163    }
164    else if (firstArg.equals("identify-references-to-missing-entries"))
165    {
166      return IdentifyReferencesToMissingEntries.main(remainingArgs, outStream,
167           errStream);
168    }
169    else if (firstArg.equals("identify-unique-attribute-conflicts"))
170    {
171      return IdentifyUniqueAttributeConflicts.main(remainingArgs, outStream,
172           errStream);
173    }
174    else if (firstArg.equals("indent-ldap-filter"))
175    {
176      return IndentLDAPFilter.main(outStream, errStream, remainingArgs);
177    }
178    else if (firstArg.equals("in-memory-directory-server"))
179    {
180      return InMemoryDirectoryServerTool.main(remainingArgs, outStream,
181           errStream);
182    }
183    else if (firstArg.equals("generate-schema-from-source"))
184    {
185      return GenerateSchemaFromSource.main(remainingArgs, outStream, errStream);
186    }
187    else if (firstArg.equals("generate-source-from-schema"))
188    {
189      return GenerateSourceFromSchema.main(remainingArgs, outStream, errStream);
190    }
191    else if (firstArg.equals("ldapcompare"))
192    {
193      return LDAPCompare.main(remainingArgs, outStream, errStream);
194    }
195    else if (firstArg.equals("ldapmodify"))
196    {
197      return LDAPModify.main(remainingArgs, outStream, errStream);
198    }
199    else if (firstArg.equals("ldapsearch"))
200    {
201      return LDAPSearch.main(remainingArgs, outStream, errStream);
202    }
203    else if (firstArg.equals("ldap-debugger"))
204    {
205      return LDAPDebugger.main(remainingArgs, outStream, errStream);
206    }
207    else if (firstArg.equals("manage-certificates"))
208    {
209      return ManageCertificates.main(System.in, outStream, errStream,
210           remainingArgs);
211    }
212    else if (firstArg.equals("modrate"))
213    {
214      return ModRate.main(remainingArgs, outStream, errStream);
215    }
216    else if (firstArg.equals("searchrate"))
217    {
218      return SearchRate.main(remainingArgs, outStream, errStream);
219    }
220    else if (firstArg.equals("search-and-mod-rate"))
221    {
222      return SearchAndModRate.main(remainingArgs, outStream, errStream);
223    }
224    else if (firstArg.equals("tls-cipher-suite-selector"))
225    {
226      return TLSCipherSuiteSelector.main(outStream, errStream, remainingArgs);
227    }
228    else if (firstArg.equals("transform-ldif"))
229    {
230      return TransformLDIF.main(outStream, errStream, remainingArgs);
231    }
232    else if (firstArg.equals("validate-ldif"))
233    {
234      return ValidateLDIF.main(remainingArgs, outStream, errStream);
235    }
236    else
237    {
238      if (errStream != null)
239      {
240        final PrintStream err = new PrintStream(errStream);
241        err.println("Unrecognized tool name '" + args[0] + '\'');
242        err.println("Supported tool names include:");
243        err.println("     authrate");
244        err.println("     base64");
245        err.println("     generate-schema-from-source");
246        err.println("     generate-source-from-schema");
247        err.println("     identify-references-to-missing-entries");
248        err.println("     identify-unique-attribute-conflicts");
249        err.println("     indent-ldap-filter");
250        err.println("     in-memory-directory-server");
251        err.println("     ldapcompare");
252        err.println("     ldapmodify");
253        err.println("     ldapsearch");
254        err.println("     ldap-debugger");
255        err.println("     manage-certificates");
256        err.println("     modrate");
257        err.println("     searchrate");
258        err.println("     search-and-mod-rate");
259        err.println("     tls-cipher-suite-selector");
260        err.println("     transform-ldif");
261        err.println("     validate-ldif");
262        err.println("     version");
263      }
264
265      return ResultCode.PARAM_ERROR;
266    }
267  }
268}