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 2008 Sun Microsystems, Inc.
026     */
027    
028    package org.opends.server.tools;
029    import org.opends.messages.Message;
030    
031    import java.io.OutputStream;
032    import java.io.PrintStream;
033    
034    import org.opends.server.types.NullOutputStream;
035    import org.opends.server.util.SetupUtils;
036    
037    import static org.opends.messages.ToolMessages.*;
038    import static org.opends.server.util.StaticUtils.*;
039    
040    
041    /**
042      * This class is used to stop the Windows service associated with this
043      * instance on this machine.
044      * This tool allows to stop OpenDS as a Windows service.
045      */
046    public class StopWindowsService
047    {
048      /**
049        * The service was successfully stopped.
050        */
051      public static int SERVICE_STOP_SUCCESSFUL = 0;
052      /**
053        * The service could not be found.
054        */
055      public static int SERVICE_NOT_FOUND = 1;
056      /**
057        * The service was already stopped.
058        */
059      public static int SERVICE_ALREADY_STOPPED = 2;
060      /**
061        * The service could not be stopped.
062        */
063      public static int SERVICE_STOP_ERROR = 3;
064    
065      /**
066       * Invokes the net stop on the service corresponding to this server.
067       *
068       * @param  args  The command-line arguments provided to this program.
069       */
070      public static void main(String[] args)
071      {
072        int result = stopWindowsService(System.out, System.err);
073    
074        System.exit(filterExitCode(result));
075      }
076    
077      /**
078       * Invokes the net stop on the service corresponding to this server, it writes
079       * information and error messages in the provided streams.
080       * @return <CODE>SERVICE_STOP_SUCCESSFUL</CODE>,
081       * <CODE>SERVICE_NOT_FOUND</CODE>, <CODE>SERVICE_ALREADY_STOPPED</CODE> or
082       * <CODE>SERVICE_STOP_ERROR</CODE> depending on whether the service could be
083       * stopped or not.
084       * @param  outStream  The stream to write standard output messages.
085       * @param  errStream  The stream to write error messages.
086       */
087      public static int stopWindowsService(OutputStream outStream,
088                               OutputStream errStream)
089      {
090        int returnValue;
091        PrintStream out;
092        if (outStream == null)
093        {
094          out = NullOutputStream.printStream();
095        }
096        else
097        {
098          out = new PrintStream(outStream);
099        }
100    
101        PrintStream err;
102        if (errStream == null)
103        {
104          err = NullOutputStream.printStream();
105        }
106        else
107        {
108          err = new PrintStream(errStream);
109        }
110    
111        String serviceName = ConfigureWindowsService.getServiceName();
112        if (serviceName == null)
113        {
114    
115          Message message = ERR_WINDOWS_SERVICE_NOT_FOUND.get();
116          err.println(message);
117          returnValue = SERVICE_NOT_FOUND;
118        }
119        else
120        {
121          String[] cmd;
122          if (SetupUtils.isVista())
123          {
124            cmd= new String[] {
125                ConfigureWindowsService.getLauncherBinaryFullPath(),
126                ConfigureWindowsService.LAUNCHER_OPTION,
127                ConfigureWindowsService.getLauncherAdministratorBinaryFullPath(),
128                ConfigureWindowsService.LAUNCHER_OPTION,
129                "net",
130                "stop",
131                serviceName
132            };
133          }
134          else
135          {
136            cmd= new String[] {
137                "net",
138                "stop",
139                serviceName
140            };
141          }
142          /* Check if is a running service */
143          try
144          {
145            if (Runtime.getRuntime().exec(cmd).waitFor() == 0)
146            {
147              returnValue = SERVICE_STOP_SUCCESSFUL;
148            }
149            else
150            {
151              returnValue = SERVICE_STOP_ERROR;
152            }
153          }
154          catch (Throwable t)
155          {
156    
157            Message message = ERR_WINDOWS_SERVICE_STOP_ERROR.get();
158            out.println(message);
159            returnValue = SERVICE_STOP_ERROR;
160          }
161        }
162        return returnValue;
163      }
164    }
165