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 2012-2014 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 stop the Windows service associated with this
041  * instance on this machine.
042  * This tool allows to stop OpenDS as a Windows service.
043  */
044public class StopWindowsService
045{
046  /** The service was successfully stopped. */
047  public static final int SERVICE_STOP_SUCCESSFUL = 0;
048  /** The service could not be found. */
049  public static final int SERVICE_NOT_FOUND = 1;
050  /** The service could not be stopped. */
051  public static final int SERVICE_STOP_ERROR = 3;
052
053  /**
054   * Invokes the net stop on the service corresponding to this server.
055   *
056   * @param  args  The command-line arguments provided to this program.
057   */
058  public static void main(String[] args)
059  {
060    System.exit(filterExitCode(stopWindowsService(System.out, System.err)));
061  }
062
063  /**
064   * Invokes the net stop on the service corresponding to this server, it writes
065   * information and error messages in the provided streams.
066   *
067   * @return <CODE>SERVICE_STOP_SUCCESSFUL</CODE>,
068   *         <CODE>SERVICE_NOT_FOUND</CODE> or <CODE>SERVICE_STOP_ERROR</CODE>
069   *         depending on whether the service could be stopped or not.
070   * @param outStream
071   *          The stream to write standard output messages.
072   * @param errStream
073   *          The stream to write error messages.
074   */
075  public static int stopWindowsService(OutputStream outStream, OutputStream errStream)
076  {
077    NullOutputStream.wrapOrNullStream(outStream);
078    PrintStream err = NullOutputStream.wrapOrNullStream(errStream);
079    JDKLogging.disableLogging();
080
081    String serviceName = ConfigureWindowsService.getServiceName();
082    if (serviceName == null)
083    {
084      printWrappedText(err, ERR_WINDOWS_SERVICE_NOT_FOUND.get());
085      return SERVICE_NOT_FOUND;
086    }
087    String[] cmd;
088    if (hasUAC())
089    {
090      cmd= new String[] {
091          ConfigureWindowsService.getLauncherBinaryFullPath(),
092          ConfigureWindowsService.LAUNCHER_OPTION,
093          ConfigureWindowsService.getLauncherAdministratorBinaryFullPath(),
094          ConfigureWindowsService.LAUNCHER_OPTION,
095          "net",
096          "stop",
097          serviceName
098      };
099    }
100    else
101    {
102      cmd= new String[] {
103          "net",
104          "stop",
105          serviceName
106      };
107    }
108    /* Check if is a running service */
109    try
110    {
111      switch (Runtime.getRuntime().exec(cmd).waitFor())
112      {
113      case 0:
114        return SERVICE_STOP_SUCCESSFUL;
115      case 2:
116        return SERVICE_STOP_SUCCESSFUL;
117      default:
118        return SERVICE_STOP_ERROR;
119      }
120    }
121    catch (Throwable t)
122    {
123      printWrappedText(err, ERR_WINDOWS_SERVICE_STOP_ERROR.get());
124      printWrappedText(err, "Exception:" + t);
125      return SERVICE_STOP_ERROR;
126    }
127  }
128}
129