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 2013-2015 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 start the Windows service associated with this 031 * instance on this machine. 032 * This tool allows to start OpenDS and to make it run as a Windows service. 033 */ 034public class StartWindowsService 035{ 036 /** The service was successfully started. */ 037 private static final int SERVICE_START_SUCCESSFUL = 0; 038 /** The service could not be found. */ 039 private static final int SERVICE_NOT_FOUND = 1; 040 041 /** The service could not be started. */ 042 private static final int SERVICE_START_ERROR = 2; 043 044 /** 045 * Invokes the net start 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(startWindowsService(System.out, System.err))); 052 } 053 054 /** 055 * Invokes the net start on the service corresponding to this server, it 056 * writes information and error messages in the provided streams. 057 * 058 * @return <CODE>SERVICE_START_SUCCESSFUL</CODE>, 059 * <CODE>SERVICE_NOT_FOUND</CODE>, 060 * <CODE>SERVICE_ALREADY_STARTED</CODE> or 061 * <CODE>SERVICE_START_ERROR</CODE> depending on whether the service 062 * could be stopped or not. 063 * @param outStream 064 * The stream to write standard output messages. 065 * @param errStream 066 * The stream to write error messages. 067 */ 068 public static int startWindowsService(OutputStream outStream, OutputStream errStream) 069 { 070 NullOutputStream.wrapOrNullStream(outStream); 071 PrintStream err = NullOutputStream.wrapOrNullStream(errStream); 072 JDKLogging.disableLogging(); 073 074 String serviceName = ConfigureWindowsService.getServiceName(); 075 if (serviceName == null) 076 { 077 printWrappedText(err, ERR_WINDOWS_SERVICE_NOT_FOUND.get()); 078 return SERVICE_NOT_FOUND; 079 } 080 081 String[] cmd; 082 if (hasUAC()) 083 { 084 cmd= new String[] { 085 ConfigureWindowsService.getLauncherBinaryFullPath(), 086 ConfigureWindowsService.LAUNCHER_OPTION, 087 ConfigureWindowsService.getLauncherAdministratorBinaryFullPath(), 088 ConfigureWindowsService.LAUNCHER_OPTION, 089 "net", 090 "start", 091 serviceName 092 }; 093 } 094 else 095 { 096 cmd= new String[] { 097 "net", 098 "start", 099 serviceName 100 }; 101 } 102 /* Check if is a running service */ 103 try 104 { 105 return Runtime.getRuntime().exec(cmd).waitFor() == 0 ? SERVICE_START_SUCCESSFUL : SERVICE_START_ERROR; 106 } 107 catch (Throwable t) 108 { 109 printWrappedText(err, ERR_WINDOWS_SERVICE_START_ERROR.get()); 110 printWrappedText(err, "Exception:" + t); 111 return SERVICE_START_ERROR; 112 } 113 } 114}