001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.admin.client.cli;
028    import org.opends.messages.Message;
029    
030    import java.io.OutputStream;
031    import java.io.PrintStream;
032    
033    import org.opends.admin.ads.ADSContextException;
034    import org.opends.server.admin.ClassLoaderProvider;
035    import org.opends.server.core.DirectoryServer;
036    import org.opends.server.types.InitializationException;
037    import org.opends.server.types.NullOutputStream;
038    import org.opends.server.util.args.ArgumentException;
039    
040    import static org.opends.server.admin.client.cli.DsFrameworkCliReturnCode.*;
041    import static org.opends.messages.AdminMessages.*;
042    import static org.opends.messages.DSConfigMessages.*;
043    import static org.opends.messages.ToolMessages.*;
044    import org.opends.messages.MessageBuilder;
045    import static org.opends.server.util.ServerConstants.*;
046    import static org.opends.server.util.StaticUtils.*;
047    
048    
049    /**
050     * This class provides a tool that can be used to Directory Server framework
051     * services.
052     */
053    public class DsFrameworkCliMain
054    {
055      /**
056       * The fully-qualified name of this class.
057       */
058      private static final String CLASS_NAME =
059          "org.opends.server.admin.client.cli.DsFrameworkCliMain";
060    
061      // The print stream to use for standard error.
062      private PrintStream err;
063    
064      // The print stream to use for standard output.
065      private PrintStream out;
066    
067    
068    
069      /**
070       * Constructor for the DsFrameworkCLI object.
071       *
072       * @param  out            The print stream to use for standard output.
073       * @param  err            The print stream to use for standard error.
074       */
075      public DsFrameworkCliMain(PrintStream out, PrintStream err)
076      {
077        this.out           = out;
078        this.err           = err;
079      }
080    
081      /**
082       * The main method for dsframework tool.
083       *
084       * @param  args  The command-line arguments provided to this program.
085       */
086    
087      public static void main(String[] args)
088      {
089        int retCode = mainCLI(args, true, System.out, System.err);
090    
091        if(retCode != 0)
092        {
093          System.exit(retCode);
094        }
095      }
096    
097      /**
098       * Parses the provided command-line arguments and uses that information to
099       * run the dsframework tool.
100       *
101       * @param  args  The command-line arguments provided to this program.
102       *
103       * @return The error code.
104       */
105    
106      public static int mainCLI(String[] args)
107      {
108        return mainCLI(args, true, System.out, System.err);
109      }
110    
111      /**
112       * Parses the provided command-line arguments and uses that information to
113       * run the dsframework tool.
114       *
115       * @param  args              The command-line arguments provided to this
116       *                           program.
117       * @param initializeServer   Indicates whether to initialize the server.
118       * @param  outStream         The output stream to use for standard output, or
119       *                           <CODE>null</CODE> if standard output is not
120       *                           needed.
121       * @param  errStream         The output stream to use for standard error, or
122       *                           <CODE>null</CODE> if standard error is not
123       *                           needed.
124       * @return The error code.
125       */
126    
127      public static int mainCLI(String[] args, boolean initializeServer,
128          OutputStream outStream, OutputStream errStream)
129      {
130        PrintStream out;
131        if (outStream == null)
132        {
133          out = NullOutputStream.printStream();
134        }
135        else
136        {
137          out = new PrintStream(outStream);
138        }
139    
140        PrintStream err;
141        if (errStream == null)
142        {
143          err = NullOutputStream.printStream();
144        }
145        else
146        {
147          err = new PrintStream(errStream);
148        }
149    
150        DsFrameworkCliMain dsFrameworkCli = new DsFrameworkCliMain(out, err);
151        return dsFrameworkCli.execute(args,initializeServer);
152      }
153    
154      /**
155       * Parses the provided command-line arguments and uses that information to
156       * run the dsframework tool.
157       *
158       * @param  args              The command-line arguments provided to this
159       *                           program.
160       * @param  initializeServer  Indicates whether to initialize the server.
161       *
162       * @return The error code.
163       */
164      public int execute(String[] args, boolean initializeServer)
165      {
166        // Create the command-line argument parser for use with this
167        // program.
168        DsFrameworkCliParser argParser ;
169        try
170        {
171          Message toolDescription = INFO_ADMIN_TOOL_DESCRIPTION.get();
172          argParser = new DsFrameworkCliParser(CLASS_NAME,
173              toolDescription, false);
174          argParser.initializeParser(out);
175        }
176        catch (ArgumentException ae)
177        {
178          Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
179    
180          err.println(wrapText(message, MAX_LINE_WIDTH));
181          return CANNOT_INITIALIZE_ARGS.getReturnCode();
182        }
183    
184        // Parse the command-line arguments provided to this program.
185        try
186        {
187          argParser.parseArguments(args);
188        }
189        catch (ArgumentException ae)
190        {
191          Message message = ERR_ERROR_PARSING_ARGS.get(ae.getMessage());
192    
193          err.println(wrapText(message, MAX_LINE_WIDTH));
194          err.println(argParser.getUsage());
195          return ERROR_PARSING_ARGS.getReturnCode();
196        }
197    
198        // If we should just display usage information, then print it and exit.
199        if (argParser.usageOrVersionDisplayed())
200        {
201          return SUCCESSFUL.getReturnCode();
202        }
203    
204        if (argParser.getSubCommand() == null)
205        {
206          Message message = ERR_ERROR_PARSING_ARGS.get(
207                  ERR_DSCFG_ERROR_MISSING_SUBCOMMAND.get());
208          err.println(wrapText(message, MAX_LINE_WIDTH));
209          err.println();
210          err.println(argParser.getHelpUsageReference());
211          return ERROR_PARSING_ARGS.getReturnCode();
212        }
213    
214        // Validate args
215        int ret = argParser.validateGlobalOptions(err);
216        if (ret != SUCCESSFUL_NOP.getReturnCode())
217        {
218          return ret;
219        }
220    
221        // Check if we need a connection
222    
223        DsFrameworkCliReturnCode returnCode = SUCCESSFUL;
224    
225    
226        // Should we initialize the server in client mode?
227        if (initializeServer)
228        {
229          // Bootstrap and initialize directory data structures.
230          DirectoryServer.bootstrapClient();
231    
232          // Bootstrap definition classes.
233          try
234          {
235            ClassLoaderProvider.getInstance().enable();
236          }
237          catch (InitializationException e)
238          {
239            err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
240            return ERROR_UNEXPECTED.getReturnCode();
241          }
242          catch (IllegalStateException e)
243          {
244            err.println(wrapText(e.getMessage(), MAX_LINE_WIDTH));
245            return ERROR_UNEXPECTED.getReturnCode();
246          }
247        }
248    
249        // perform the subCommand
250        ADSContextException adsException = null;
251        try
252        {
253          returnCode = argParser.performSubCommand(out, err);
254        }
255        catch (ADSContextException e)
256        {
257          adsException = e;
258          returnCode = DsFrameworkCliReturnCode.getReturncodeFromAdsError(e
259              .getError());
260          if (returnCode == null)
261          {
262            returnCode = ERROR_UNEXPECTED;
263          }
264        }
265        catch (ArgumentException ae)
266        {
267          Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
268    
269          err.println(wrapText(message, MAX_LINE_WIDTH));
270          return CANNOT_INITIALIZE_ARGS.getReturnCode();
271        }
272    
273        Message msg = returnCode.getMessage();
274        if ( (returnCode == SUCCESSFUL)
275             ||
276             (returnCode == SUCCESSFUL_NOP))
277        {
278          if (argParser.isVerbose())
279          {
280            out.println(wrapText(msg.toString(), MAX_LINE_WIDTH));
281          }
282        }
283        else
284        if (msg != null &&
285                msg.getDescriptor().getId() != ERR_ADMIN_NO_MESSAGE.getId())
286        {
287          MessageBuilder mb = new MessageBuilder(INFO_ADMIN_ERROR.get());
288          mb.append(msg);
289          err.println(wrapText(mb.toString(), MAX_LINE_WIDTH));
290          if (argParser.isVerbose() && (adsException != null))
291          {
292            adsException.printStackTrace();
293          }
294        }
295        return returnCode.getReturnCode();
296      }
297    }