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