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