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