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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2013-2015 ForgeRock AS.
026 */
027package org.opends.server.tools;
028import static org.opends.messages.ToolMessages.*;
029
030import static com.forgerock.opendj.cli.Utils.*;
031import static com.forgerock.opendj.util.OperatingSystem.*;
032
033import java.io.OutputStream;
034import java.io.PrintStream;
035
036import org.opends.server.loggers.JDKLogging;
037import org.opends.server.types.NullOutputStream;
038
039/**
040  * This class is used to start the Windows service associated with this
041  * instance on this machine.
042  * This tool allows to start OpenDS and to make it run as a Windows service.
043  */
044public class StartWindowsService
045{
046  /** The service was successfully started. */
047  private static final int SERVICE_START_SUCCESSFUL = 0;
048  /** The service could not be found. */
049  private static final int SERVICE_NOT_FOUND = 1;
050
051  /** The service could not be started. */
052  private static final int SERVICE_START_ERROR = 2;
053
054  /**
055   * Invokes the net start on the service corresponding to this server.
056   *
057   * @param  args  The command-line arguments provided to this program.
058   */
059  public static void main(String[] args)
060  {
061    System.exit(filterExitCode(startWindowsService(System.out, System.err)));
062  }
063
064  /**
065   * Invokes the net start on the service corresponding to this server, it
066   * writes information and error messages in the provided streams.
067   *
068   * @return <CODE>SERVICE_START_SUCCESSFUL</CODE>,
069   *         <CODE>SERVICE_NOT_FOUND</CODE>,
070   *         <CODE>SERVICE_ALREADY_STARTED</CODE> or
071   *         <CODE>SERVICE_START_ERROR</CODE> depending on whether the service
072   *         could be stopped or not.
073   * @param outStream
074   *          The stream to write standard output messages.
075   * @param errStream
076   *          The stream to write error messages.
077   */
078  public static int startWindowsService(OutputStream outStream, OutputStream errStream)
079  {
080    NullOutputStream.wrapOrNullStream(outStream);
081    PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
082    JDKLogging.disableLogging();
083
084    String serviceName = ConfigureWindowsService.getServiceName();
085    if (serviceName == null)
086    {
087      printWrappedText(err, ERR_WINDOWS_SERVICE_NOT_FOUND.get());
088      return SERVICE_NOT_FOUND;
089    }
090
091    String[] cmd;
092    if (hasUAC())
093    {
094      cmd= new String[] {
095          ConfigureWindowsService.getLauncherBinaryFullPath(),
096          ConfigureWindowsService.LAUNCHER_OPTION,
097          ConfigureWindowsService.getLauncherAdministratorBinaryFullPath(),
098          ConfigureWindowsService.LAUNCHER_OPTION,
099          "net",
100          "start",
101          serviceName
102      };
103    }
104    else
105    {
106      cmd= new String[] {
107          "net",
108          "start",
109          serviceName
110      };
111    }
112    /* Check if is a running service */
113    try
114    {
115      return Runtime.getRuntime().exec(cmd).waitFor() == 0 ? SERVICE_START_SUCCESSFUL : SERVICE_START_ERROR;
116    }
117    catch (Throwable t)
118    {
119      printWrappedText(err, ERR_WINDOWS_SERVICE_START_ERROR.get());
120      printWrappedText(err, "Exception:" + t);
121      return SERVICE_START_ERROR;
122    }
123  }
124}