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