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 package org.opends.server.tools; 028 029 030 031 import java.io.File; 032 import java.io.OutputStream; 033 import java.io.PrintStream; 034 import java.io.PrintWriter; 035 036 import org.opends.messages.Message; 037 import org.opends.server.core.DirectoryServer; 038 import org.opends.server.types.FilePermission; 039 import org.opends.server.types.NullOutputStream; 040 import org.opends.server.types.OperatingSystem; 041 import org.opends.server.util.EmbeddedUtils; 042 import org.opends.server.util.SetupUtils; 043 import org.opends.server.util.args.ArgumentParser; 044 import org.opends.server.util.args.ArgumentException; 045 import org.opends.server.util.args.BooleanArgument; 046 import org.opends.server.util.args.StringArgument; 047 048 import static org.opends.messages.ToolMessages.*; 049 import static org.opends.server.config.ConfigConstants.*; 050 import static org.opends.server.util.ServerConstants.*; 051 import static org.opends.server.util.StaticUtils.*; 052 053 054 055 /** 056 * This program provides a tool that may be used to generate an RC script that 057 * can be used to start, stop, and restart the Directory Server, as well as to 058 * display its current status. It is only intended for use on UNIX-based 059 * systems that support the use of RC scripts in a location like /etc/init.d. 060 */ 061 public class CreateRCScript 062 { 063 /** 064 * Parse the command line arguments and create an RC script that can be used 065 * to control the server. 066 * 067 * @param args The command-line arguments provided to this program. 068 */ 069 public static void main(String[] args) 070 { 071 int exitCode = main(args, System.out, System.err); 072 if (exitCode != 0) 073 { 074 System.exit(exitCode); 075 } 076 } 077 078 079 080 /** 081 * Parse the command line arguments and create an RC script that can be used 082 * to control the server. 083 * 084 * @param args The command-line arguments provided to this program. 085 * @param outStream The output stream to which standard output should be 086 * directed, or {@code null} if standard output should be 087 * suppressed. 088 * @param errStream The output stream to which standard error should be 089 * directed, or {@code null} if standard error should be 090 * suppressed. 091 * 092 * @return Zero if all processing completed successfully, or nonzero if an 093 * error occurred. 094 */ 095 public static int main(String[] args, OutputStream outStream, 096 OutputStream errStream) 097 { 098 PrintStream out; 099 if (outStream == null) 100 { 101 out = NullOutputStream.printStream(); 102 } 103 else 104 { 105 out = new PrintStream(outStream); 106 } 107 108 PrintStream err; 109 if (errStream == null) 110 { 111 err = NullOutputStream.printStream(); 112 } 113 else 114 { 115 err = new PrintStream(errStream); 116 } 117 118 119 EmbeddedUtils.initializeForClientUse(); 120 121 OperatingSystem operatingSystem = DirectoryServer.getOperatingSystem(); 122 if (! OperatingSystem.isUNIXBased(operatingSystem)) 123 { 124 err.println(ERR_CREATERC_ONLY_RUNS_ON_UNIX.get().toString()); 125 return 1; 126 } 127 128 File serverRoot = DirectoryServer.getEnvironmentConfig().getServerRoot(); 129 if (serverRoot == null) 130 { 131 err.println(ERR_CREATERC_UNABLE_TO_DETERMINE_SERVER_ROOT.get( 132 PROPERTY_SERVER_ROOT, ENV_VAR_INSTANCE_ROOT).toString()); 133 return 1; 134 } 135 136 137 Message description = INFO_CREATERC_TOOL_DESCRIPTION.get(); 138 ArgumentParser argParser = 139 new ArgumentParser(CreateRCScript.class.getName(), description, false); 140 141 BooleanArgument showUsage = null; 142 StringArgument javaArgs = null; 143 StringArgument javaHome = null; 144 StringArgument outputFile = null; 145 StringArgument userName = null; 146 147 try 148 { 149 outputFile = new StringArgument("outputfile", 'f', "outputFile", true, 150 false, true, INFO_PATH_PLACEHOLDER.get(), 151 null, null, 152 INFO_CREATERC_OUTFILE_DESCRIPTION.get()); 153 argParser.addArgument(outputFile); 154 155 156 userName = new StringArgument("username", 'u', "userName", false, false, 157 true, INFO_USER_NAME_PLACEHOLDER.get(), 158 null, null, 159 INFO_CREATERC_USER_DESCRIPTION.get()); 160 argParser.addArgument(userName); 161 162 163 javaHome = new StringArgument("javahome", 'j', "javaHome", false, false, 164 true, INFO_PATH_PLACEHOLDER.get(), null, 165 null, 166 INFO_CREATERC_JAVA_HOME_DESCRIPTION.get()); 167 argParser.addArgument(javaHome); 168 169 170 javaArgs = new StringArgument("javaargs", 'J', "javaArgs", false, false, 171 true, INFO_ARGS_PLACEHOLDER.get(), null, 172 null, 173 INFO_CREATERC_JAVA_ARGS_DESCRIPTION.get()); 174 argParser.addArgument(javaArgs); 175 176 177 showUsage = new BooleanArgument("help", 'H', "help", 178 INFO_DESCRIPTION_SHOWUSAGE.get()); 179 argParser.addArgument(showUsage); 180 argParser.setUsageArgument(showUsage); 181 } 182 catch (ArgumentException ae) 183 { 184 err.println(ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage())); 185 return 1; 186 } 187 188 try 189 { 190 argParser.parseArguments(args); 191 } 192 catch (ArgumentException ae) 193 { 194 err.println(ERR_ERROR_PARSING_ARGS.get(ae.getMessage()).toString()); 195 return 1; 196 } 197 198 if (argParser.usageOrVersionDisplayed()) 199 { 200 return 0; 201 } 202 203 204 // Determine the path to the Java installation that should be used. 205 String javaHomeDir; 206 if (javaHome.isPresent()) 207 { 208 File f = new File(javaHome.getValue()); 209 if (! (f.exists() && f.isDirectory())) 210 { 211 err.println(ERR_CREATERC_JAVA_HOME_DOESNT_EXIST.get( 212 javaHome.getValue()).toString()); 213 return 1; 214 } 215 216 javaHomeDir = f.getAbsolutePath(); 217 } 218 else 219 { 220 javaHomeDir = System.getenv(SetupUtils.OPENDS_JAVA_HOME); 221 } 222 223 224 String suString = ""; 225 if (userName.isPresent()) 226 { 227 suString = "/bin/su " + userName.getValue() + " "; 228 } 229 230 231 // Start writing the output file. 232 try 233 { 234 File f = new File(outputFile.getValue()); 235 PrintWriter w = new PrintWriter(f); 236 237 w.println("#!/bin/sh"); 238 w.println("#"); 239 240 for (String headerLine : CDDL_HEADER_LINES) 241 { 242 w.println("# " + headerLine); 243 } 244 245 w.println(); 246 w.println(); 247 248 w.println("# Set the path to the OpenDS instance to manage"); 249 w.println("INSTANCE_ROOT=\"" + serverRoot.getAbsolutePath() + "\""); 250 w.println("export INSTANCE_ROOT"); 251 w.println(); 252 253 if (javaHomeDir != null) 254 { 255 w.println("# Specify the path to the Java installation to use"); 256 w.println("OPENDS_JAVA_HOME=\"" + javaHomeDir + "\""); 257 w.println("export OPENDS_JAVA_HOME"); 258 w.println(); 259 } 260 261 if (javaArgs.isPresent()) 262 { 263 w.println("# Specify arguments that should be provided to the JVM"); 264 w.println("OPENDS_JAVA_ARGS=\"" + javaArgs.getValue() + "\""); 265 w.println("export OPENDS_JAVA_ARGS"); 266 w.println(); 267 } 268 269 w.println("# Determine what action should be performed on the server"); 270 w.println("case \"${1}\" in"); 271 w.println("start)"); 272 w.println(" " + suString + "\"${INSTANCE_ROOT}/bin/start-ds\" --quiet"); 273 w.println(" exit ${?}"); 274 w.println(" ;;"); 275 w.println("stop)"); 276 w.println(" " + suString + "\"${INSTANCE_ROOT}/bin/stop-ds\" --quiet"); 277 w.println(" exit ${?}"); 278 w.println(" ;;"); 279 w.println("restart)"); 280 w.println(" " + suString + "\"${INSTANCE_ROOT}/bin/stop-ds\" " + 281 "--restart --quiet"); 282 w.println(" exit ${?}"); 283 w.println(" ;;"); 284 w.println("*)"); 285 w.println(" echo \"Usage: $0 { start | stop | restart }\""); 286 w.println(" exit 1"); 287 w.println(" ;;"); 288 w.println("esac"); 289 w.println(); 290 291 w.close(); 292 293 if (FilePermission.canSetPermissions()) 294 { 295 FilePermission.setPermissions(f, FilePermission.decodeUNIXMode("755")); 296 } 297 } 298 catch (Exception e) 299 { 300 err.println(ERR_CREATERC_CANNOT_WRITE.get( 301 getExceptionMessage(e)).toString()); 302 return 1; 303 } 304 305 306 // If we've gotten here, then everything has completed successfully. 307 return 0; 308 } 309 } 310