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-2014 ForgeRock AS. 016 */ 017package org.opends.server.tools; 018import static org.opends.messages.ToolMessages.*; 019 020import static com.forgerock.opendj.cli.Utils.*; 021import static com.forgerock.opendj.util.OperatingSystem.*; 022 023import java.io.OutputStream; 024import java.io.PrintStream; 025 026import org.opends.server.loggers.JDKLogging; 027import org.opends.server.types.NullOutputStream; 028 029/** 030 * This class is used to stop the Windows service associated with this 031 * instance on this machine. 032 * This tool allows to stop OpenDS as a Windows service. 033 */ 034public class StopWindowsService 035{ 036 /** The service was successfully stopped. */ 037 public static final int SERVICE_STOP_SUCCESSFUL = 0; 038 /** The service could not be found. */ 039 public static final int SERVICE_NOT_FOUND = 1; 040 /** The service could not be stopped. */ 041 public static final int SERVICE_STOP_ERROR = 3; 042 043 /** 044 * Invokes the net stop on the service corresponding to this server. 045 * 046 * @param args The command-line arguments provided to this program. 047 */ 048 public static void main(String[] args) 049 { 050 System.exit(filterExitCode(stopWindowsService(System.out, System.err))); 051 } 052 053 /** 054 * Invokes the net stop on the service corresponding to this server, it writes 055 * information and error messages in the provided streams. 056 * 057 * @return <CODE>SERVICE_STOP_SUCCESSFUL</CODE>, 058 * <CODE>SERVICE_NOT_FOUND</CODE> or <CODE>SERVICE_STOP_ERROR</CODE> 059 * depending on whether the service could be stopped or not. 060 * @param outStream 061 * The stream to write standard output messages. 062 * @param errStream 063 * The stream to write error messages. 064 */ 065 public static int stopWindowsService(OutputStream outStream, OutputStream errStream) 066 { 067 NullOutputStream.wrapOrNullStream(outStream); 068 PrintStream err = NullOutputStream.wrapOrNullStream(errStream); 069 JDKLogging.disableLogging(); 070 071 String serviceName = ConfigureWindowsService.getServiceName(); 072 if (serviceName == null) 073 { 074 printWrappedText(err, ERR_WINDOWS_SERVICE_NOT_FOUND.get()); 075 return SERVICE_NOT_FOUND; 076 } 077 String[] cmd; 078 if (hasUAC()) 079 { 080 cmd= new String[] { 081 ConfigureWindowsService.getLauncherBinaryFullPath(), 082 ConfigureWindowsService.LAUNCHER_OPTION, 083 ConfigureWindowsService.getLauncherAdministratorBinaryFullPath(), 084 ConfigureWindowsService.LAUNCHER_OPTION, 085 "net", 086 "stop", 087 serviceName 088 }; 089 } 090 else 091 { 092 cmd= new String[] { 093 "net", 094 "stop", 095 serviceName 096 }; 097 } 098 /* Check if is a running service */ 099 try 100 { 101 switch (Runtime.getRuntime().exec(cmd).waitFor()) 102 { 103 case 0: 104 return SERVICE_STOP_SUCCESSFUL; 105 case 2: 106 return SERVICE_STOP_SUCCESSFUL; 107 default: 108 return SERVICE_STOP_ERROR; 109 } 110 } 111 catch (Throwable t) 112 { 113 printWrappedText(err, ERR_WINDOWS_SERVICE_STOP_ERROR.get()); 114 printWrappedText(err, "Exception:" + t); 115 return SERVICE_STOP_ERROR; 116 } 117 } 118} 119