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