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 2011-2015 ForgeRock AS. 016 */ 017package org.opends.server.tools; 018 019import static org.opends.messages.ToolMessages.*; 020import static org.opends.server.util.ServerConstants.*; 021 022import static com.forgerock.opendj.cli.Utils.*; 023import static com.forgerock.opendj.util.OperatingSystem.*; 024 025import java.io.BufferedReader; 026import java.io.BufferedWriter; 027import java.io.File; 028import java.io.FileNotFoundException; 029import java.io.FileReader; 030import java.io.FileWriter; 031import java.io.IOException; 032import java.io.InputStream; 033import java.io.OutputStream; 034import java.io.PrintStream; 035import java.util.Enumeration; 036import java.util.Properties; 037 038import org.forgerock.i18n.LocalizableMessage; 039import org.opends.messages.ToolMessages; 040import org.opends.quicksetup.Constants; 041import org.opends.server.types.NullOutputStream; 042 043import com.forgerock.opendj.cli.ArgumentException; 044import com.forgerock.opendj.cli.ConsoleApplication; 045 046 047/** 048 * This class is used to update the scripts that are used to launch the command 049 * lines. We read the contents of a given properties file and we update the 050 * scripts setting the arguments and JVM to be used by the different scripts. 051 * 052 */ 053public class JavaPropertiesTool extends ConsoleApplication 054{ 055 /** The argument parser. */ 056 private JavaPropertiesToolArgumentParser argParser; 057 058 /** 059 * The enumeration containing the different return codes that the command-line 060 * can have. 061 * 062 */ 063 public enum ErrorReturnCode 064 { 065 /** 066 * Successful setup. 067 */ 068 SUCCESSFUL(0), 069 /** 070 * We did no have an error but the setup was not executed (displayed version 071 * or usage). 072 */ 073 SUCCESSFUL_NOP(0), 074 /** 075 * Unexpected error (potential bug). 076 */ 077 ERROR_UNEXPECTED(1), 078 /** 079 * Cannot parse arguments or data provided by user is not valid. 080 */ 081 ERROR_USER_DATA(2), 082 /** 083 * Error writing to destination file. 084 */ 085 ERROR_WRITING_FILE(3), 086 /** 087 * Conflicting command line arguments. 088 */ 089 CONFLICTING_ARGS(18); 090 091 private int returnCode; 092 private ErrorReturnCode(int returnCode) 093 { 094 this.returnCode = returnCode; 095 } 096 097 /** 098 * Get the corresponding return code value. 099 * 100 * @return The corresponding return code value. 101 */ 102 public int getReturnCode() 103 { 104 return returnCode; 105 } 106 } 107 108 private static final String DEFAULT_JAVA_HOME_PROP_NAME = "default.java-home"; 109 private static final String DEFAULT_JAVA_ARGS_PROP_NAME = "default.java-args"; 110 private static final String OVERWRITE_ENV_JAVA_HOME_PROP_NAME = 111 "overwrite-env-java-home"; 112 private static final String OVERWRITE_ENV_JAVA_ARGS_PROP_NAME = 113 "overwrite-env-java-args"; 114 115 /** 116 * Constructor for the JavaPropertiesTool object. 117 * 118 * @param out the print stream to use for standard output. 119 * @param err the print stream to use for standard error. 120 * @param in the input stream to use for standard input. 121 */ 122 public JavaPropertiesTool(PrintStream out, PrintStream err, InputStream in) 123 { 124 super(out, err); 125 } 126 127 /** 128 * The main method for the java properties tool. 129 * 130 * @param args the command-line arguments provided to this program. 131 */ 132 133 public static void main(String[] args) 134 { 135 int retCode = mainCLI(args, System.out, System.err, System.in); 136 137 System.exit(retCode); 138 } 139 140 /** 141 * Parses the provided command-line arguments and uses that information to 142 * run the java properties tool. 143 * 144 * @param args the command-line arguments provided to this program. 145 * 146 * @return The error code. 147 */ 148 149 public static int mainCLI(String... args) 150 { 151 return mainCLI(args, System.out, System.err, System.in); 152 } 153 154 /** 155 * Parses the provided command-line arguments and uses that information to 156 * run the java properties tool. 157 * 158 * @param args The command-line arguments provided to this 159 * program. 160 * @param outStream The output stream to use for standard output, or 161 * <CODE>null</CODE> if standard output is not 162 * needed. 163 * @param errStream The output stream to use for standard error, or 164 * <CODE>null</CODE> if standard error is not 165 * needed. 166 * @param inStream The input stream to use for standard input. 167 * @return The error code. 168 */ 169 170 public static int mainCLI(String[] args, OutputStream outStream, 171 OutputStream errStream, InputStream inStream) 172 { 173 PrintStream out = NullOutputStream.wrapOrNullStream(outStream); 174 175 System.setProperty(Constants.CLI_JAVA_PROPERTY, "true"); 176 177 PrintStream err = NullOutputStream.wrapOrNullStream(errStream); 178 179 JavaPropertiesTool tool = new JavaPropertiesTool(out, err, inStream); 180 181 return tool.execute(args); 182 } 183 184 /** 185 * Parses the provided command-line arguments and uses that information to 186 * run the java properties tool. 187 * 188 * @param args the command-line arguments provided to this program. 189 * 190 * @return the return code (SUCCESSFUL, USER_DATA_ERROR or BUG). 191 */ 192 public int execute(String[] args) 193 { 194 argParser = new JavaPropertiesToolArgumentParser( 195 JavaPropertiesTool.class.getName()); 196 try 197 { 198 argParser.initializeArguments(); 199 } 200 catch (ArgumentException ae) 201 { 202 LocalizableMessage message = 203 ToolMessages.ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage()); 204 println(message); 205 return ErrorReturnCode.ERROR_UNEXPECTED.getReturnCode(); 206 } 207 208 // Validate user provided data 209 try 210 { 211 argParser.parseArguments(args); 212 } 213 catch (ArgumentException ae) 214 { 215 argParser.displayMessageAndUsageReference(getErrStream(), ERR_ERROR_PARSING_ARGS.get(ae.getMessage())); 216 return ErrorReturnCode.ERROR_USER_DATA.getReturnCode(); 217 } 218 219 if (argParser.usageOrVersionDisplayed()) 220 { 221 return ErrorReturnCode.SUCCESSFUL_NOP.getReturnCode(); 222 } 223 224 Properties properties = new Properties(); 225 BufferedReader reader; 226 String propertiesFile = argParser.propertiesFileArg.getValue(); 227 try 228 { 229 reader = new BufferedReader(new FileReader(propertiesFile)); 230 } 231 catch (FileNotFoundException fnfe) 232 { 233 println(ERR_JAVAPROPERTIES_WITH_PROPERTIES_FILE.get(propertiesFile)); 234 return ErrorReturnCode.ERROR_USER_DATA.getReturnCode(); 235 } 236 try 237 { 238 updateProperties(reader, properties); 239 } 240 catch (IOException ioe) 241 { 242 println(ERR_JAVAPROPERTIES_WITH_PROPERTIES_FILE.get(propertiesFile)); 243 return ErrorReturnCode.ERROR_USER_DATA.getReturnCode(); 244 } 245 246 String destinationFile = argParser.destinationFileArg.getValue(); 247 248 BufferedWriter writer; 249 try 250 { 251 File f = new File(destinationFile); 252 writer = new BufferedWriter(new FileWriter(f)); 253 f.setReadable(true, false); 254 } 255 catch (IOException ioe) 256 { 257 println(ERR_JAVAPROPERTIES_WITH_DESTINATION_FILE.get(destinationFile)); 258 return ErrorReturnCode.ERROR_USER_DATA.getReturnCode(); 259 } 260 261 Enumeration<?> propertyNames = properties.propertyNames(); 262 263 boolean overwriteEnvJavaHome = true; 264 boolean overwriteEnvJavaArgs = true; 265 String defaultJavaHome = null; 266 String defaultJavaArgs = null; 267 268 while (propertyNames.hasMoreElements()) 269 { 270 String name = propertyNames.nextElement().toString(); 271 String value = properties.getProperty(name); 272 273 if (value != null) 274 { 275 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME)) 276 { 277 defaultJavaHome = value; 278 } 279 else if (name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME)) 280 { 281 defaultJavaArgs = value; 282 } 283 else if (name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME)) 284 { 285 if ("false".equalsIgnoreCase(value)) 286 { 287 overwriteEnvJavaHome = false; 288 } 289 } 290 else if (name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 291 { 292 if ("false".equalsIgnoreCase(value)) 293 { 294 overwriteEnvJavaArgs = false; 295 } 296 } 297 } 298 } 299 300 try 301 { 302 String value; 303 if (isWindows()) 304 { 305 value = getWindowsContents(overwriteEnvJavaHome, overwriteEnvJavaArgs, 306 defaultJavaHome, defaultJavaArgs, properties); 307 } 308 else 309 { 310 value = getUnixContents(overwriteEnvJavaHome, overwriteEnvJavaArgs, 311 defaultJavaHome, defaultJavaArgs, properties); 312 } 313 314 writer.write(value); 315 writer.newLine(); 316 writer.close(); 317 } 318 catch (IOException ioe) 319 { 320 println(getThrowableMsg( 321 ERR_JAVAPROPERTIES_WRITING_DESTINATION_FILE.get(destinationFile), 322 ioe)); 323 return ErrorReturnCode.ERROR_WRITING_FILE.getReturnCode(); 324 } 325 326 // Add some information if we are not in quiet mode about 327 // what is going to happen. 328 File f1 = new File(argParser.destinationFileArg.getValue()); 329 File f2 = new File(argParser.destinationFileArg.getDefaultValue()); 330 if (f1.equals(f2)) 331 { 332 print(INFO_JAVAPROPERTIES_SUCCESSFUL.get( 333 argParser.propertiesFileArg.getValue())); 334 } 335 else 336 { 337 print(INFO_JAVAPROPERTIES_SUCCESSFUL_NON_DEFAULT.get( 338 argParser.destinationFileArg.getValue(), 339 argParser.propertiesFileArg.getValue(), 340 argParser.destinationFileArg.getDefaultValue())); 341 } 342 println(); 343 344 345 return ErrorReturnCode.SUCCESSFUL.getReturnCode(); 346 } 347 348 /** 349 * Reads the contents of the provided reader and updates the provided 350 * Properties object with it. This is required because '\' characters in 351 * windows paths generates problems. 352 * @param reader the buffered reader. 353 * @param properties the properties. 354 * @throws IOException if there is an error reading the buffered reader. 355 */ 356 public static void updateProperties( 357 BufferedReader reader, Properties properties) 358 throws IOException 359 { 360 String line; 361 boolean slashInLastLine = false; 362 String key = null; 363 StringBuilder sbValue = null; 364 while ((line = reader.readLine()) != null) 365 { 366 line = line.trim(); 367 if (!line.startsWith("#")) 368 { 369 if (!slashInLastLine) 370 { 371 key = null; 372 sbValue = new StringBuilder(); 373 int index = line.indexOf('='); 374 if (index > 0) 375 { 376 key = line.substring(0, index); 377 if (key.indexOf(' ') != -1) 378 { 379 key = null; 380 } 381 } 382 } 383 384 // Consider the space: in windows the user might add a path ending 385 // with '\'. With this approach we minimize the possibilities of 386 // error. 387 boolean hasSlash = line.endsWith(" \\"); 388 389 if (hasSlash) 390 { 391 line = line.substring(0, line.length() - 1); 392 } 393 394 String lineValue = null; 395 396 if (slashInLastLine) 397 { 398 lineValue = line; 399 } 400 else if (key != null) 401 { 402 int index = line.indexOf('='); 403 if (index != -1 && index + 1 < line.length()) 404 { 405 lineValue = line.substring(index+1); 406 } 407 } 408 if (lineValue != null && lineValue.length() > 0) 409 { 410 if (sbValue == null) 411 { 412 sbValue = new StringBuilder(); 413 } 414 sbValue.append(lineValue); 415 } 416 if (!hasSlash && key != null && sbValue != null) 417 { 418 properties.put(key, sbValue.toString()); 419 } 420 slashInLastLine = hasSlash; 421 } 422 } 423 } 424 425 /** {@inheritDoc} */ 426 @Override 427 public boolean isQuiet() 428 { 429 return argParser.quietArg.isPresent(); 430 } 431 432 /** {@inheritDoc} */ 433 @Override 434 public boolean isInteractive() 435 { 436 return false; 437 } 438 439 /** {@inheritDoc} */ 440 @Override 441 public boolean isMenuDrivenMode() { 442 return true; 443 } 444 445 /** {@inheritDoc} */ 446 @Override 447 public boolean isScriptFriendly() { 448 return false; 449 } 450 451 /** {@inheritDoc} */ 452 @Override 453 public boolean isAdvancedMode() { 454 return false; 455 } 456 457 458 /** {@inheritDoc} */ 459 @Override 460 public boolean isVerbose() { 461 return true; 462 } 463 464 private String getUnixContents(boolean overwriteJavaHome, 465 boolean overwriteJavaArgs, String defaultJavaHome, String defaultJavaArgs, 466 Properties properties) 467 { 468 StringBuilder buf = new StringBuilder(); 469 buf.append("#!/bin/sh").append(EOL).append(EOL); 470 471 if (!overwriteJavaHome) 472 { 473 buf.append("# See if the environment variables for java home are set").append(EOL) 474 .append("# in the path and try to figure it out.").append(EOL) 475 .append("if test ! -f \"${OPENDJ_JAVA_BIN}\"").append(EOL) 476 .append("then").append(EOL) 477 .append(" if test ! -d \"${OPENDJ_JAVA_HOME}\"").append(EOL) 478 .append(" then").append(EOL) 479 .append(" if test ! -f \"${OPENDS_JAVA_BIN}\"").append(EOL) 480 .append(" then").append(EOL) 481 .append(" if test ! -d \"${OPENDS_JAVA_HOME}\"").append(EOL); 482 } 483 484 boolean propertiesAdded = false; 485 486 Enumeration<?> propertyNames = properties.propertyNames(); 487 int nIfs = 0; 488 while (propertyNames.hasMoreElements()) 489 { 490 String name = propertyNames.nextElement().toString(); 491 String value = properties.getProperty(name); 492 493 if (value != null) 494 { 495 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 496 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 497 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 498 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 499 { 500 // Already handled 501 } 502 else if (name.endsWith(".java-home")) 503 { 504 propertiesAdded = true; 505 String s; 506 if (nIfs > 0) 507 { 508 if (!overwriteJavaHome) 509 { 510 s = " "; 511 } 512 else 513 { 514 s = ""; 515 } 516 buf.append(s).append("elif test \"${SCRIPT_NAME}.java-home\" = \"").append(name).append("\"").append(EOL); 517 } 518 else if (!overwriteJavaHome) 519 { 520 buf.append(" then").append(EOL) 521 .append(" if test \"${SCRIPT_NAME}.java-home\" = \"").append(name).append("\"").append(EOL); 522 s = " "; 523 } 524 else 525 { 526 buf.append("if test \"${SCRIPT_NAME}.java-home\" = \"").append(name).append("\"").append(EOL); 527 s = ""; 528 } 529 530 buf 531 .append(s).append("then").append(EOL) 532 .append(s).append(" TEMP=\"").append(value).append("/bin/java\"").append(EOL) 533 .append(s).append(" if test -f \"${TEMP}\"").append(EOL) 534 .append(s).append(" then").append(EOL) 535 .append(s).append(" OPENDJ_JAVA_BIN=\"").append(value).append("/bin/java\"").append(EOL) 536 .append(s).append(" export OPENDJ_JAVA_BIN").append(EOL) 537 .append(s).append(" fi").append(EOL); 538 nIfs++; 539 } 540 } 541 } 542 if (defaultJavaHome != null) 543 { 544 if (propertiesAdded) 545 { 546 String s; 547 if (!overwriteJavaHome) 548 { 549 s = " "; 550 } 551 else 552 { 553 s = ""; 554 } 555 buf.append(s).append("else").append(EOL) 556 .append(s).append(" OPENDJ_JAVA_BIN=\"").append(defaultJavaHome).append("/bin/java\"").append(EOL) 557 .append(s).append(" export OPENDJ_JAVA_BIN").append(EOL); 558 } 559 else 560 { 561 if (!overwriteJavaHome) 562 { 563 buf.append(" then").append(EOL) 564 .append(" TEMP=\"").append(defaultJavaHome).append("/bin/java\"").append(EOL) 565 .append(" if test -f \"${TEMP}\"").append(EOL) 566 .append(" then").append(EOL) 567 .append(" OPENDJ_JAVA_BIN=\"${TEMP}\"").append(EOL) 568 .append(" export OPENDJ_JAVA_BIN").append(EOL) 569 .append(" fi").append(EOL); 570 } 571 else 572 { 573 buf.append("OPENDJ_JAVA_BIN=\"").append(defaultJavaHome).append("/bin/java\"").append(EOL) 574 .append("export OPENDJ_JAVA_BIN").append(EOL); 575 } 576 } 577 propertiesAdded = true; 578 } 579 580 if (nIfs > 0) 581 { 582 String s; 583 if (!overwriteJavaHome) 584 { 585 s = " "; 586 } 587 else 588 { 589 s = ""; 590 } 591 buf.append(s).append("fi").append(EOL); 592 } 593 594 595 if (!overwriteJavaHome) 596 { 597 if (!propertiesAdded) 598 { 599 // No properties added: this is required not to break the script 600 buf.append(" then").append(EOL) 601 .append(" OPENDJ_JAVA_BIN=\"${OPENDJ_JAVA_BIN}\"").append(EOL); 602 } 603 buf.append(" else").append(EOL) 604 .append(" OPENDJ_JAVA_BIN=\"${OPENDS_JAVA_HOME}/bin/java\"").append(EOL) 605 .append(" export OPENDJ_JAVA_BIN").append(EOL) 606 .append(" fi").append(EOL) 607 .append(" else").append(EOL) 608 .append(" OPENDJ_JAVA_BIN=\"${OPENDS_JAVA_BIN}\"").append(EOL) 609 .append(" export OPENDJ_JAVA_BIN").append(EOL) 610 .append(" fi").append(EOL) 611 .append(" else").append(EOL) 612 .append(" OPENDJ_JAVA_BIN=\"${OPENDJ_JAVA_HOME}/bin/java\"").append(EOL) 613 .append(" export OPENDJ_JAVA_BIN").append(EOL) 614 .append(" fi").append(EOL) 615 .append("fi").append(EOL) 616 .append(EOL); 617 } 618 else if (defaultJavaHome == null) 619 { 620 buf.append(EOL) 621 .append("if test ! -f \"${OPENDJ_JAVA_BIN}\"").append(EOL) 622 .append("then").append(EOL) 623 .append(" if test ! -d \"${OPENDJ_JAVA_HOME}\"").append(EOL) 624 .append(" then").append(EOL) 625 .append(" if test ! -f \"${OPENDS_JAVA_BIN}\"").append(EOL) 626 .append(" then").append(EOL) 627 .append(" if test ! -d \"${OPENDS_JAVA_HOME}\"").append(EOL) 628 .append(" then").append(EOL) 629 .append(" if test ! -f \"${JAVA_BIN}\"").append(EOL) 630 .append(" then").append(EOL) 631 .append(" if test ! -d \"${JAVA_HOME}\"").append(EOL) 632 .append(" then").append(EOL) 633 .append(" OPENDJ_JAVA_BIN=`which java 2> /dev/null`").append(EOL) 634 .append(" if test ${?} -eq 0").append(EOL) 635 .append(" then").append(EOL) 636 .append(" export OPENDJ_JAVA_BIN").append(EOL) 637 .append(" else").append(EOL) 638 .append(" echo \"You must specify the path to a valid Java 7.0 ") 639 .append("or higher version in the\"").append(EOL) 640 .append(" echo \"properties file and then run the ") 641 .append("dsjavaproperties tool. \"").append(EOL) 642 .append(" echo \"The procedure to follow is:\"").append(EOL) 643 .append(" echo \"You must specify the path to a valid Java 7.0 ") 644 .append("or higher version. The \"").append(EOL) 645 .append(" echo \"procedure to follow is:\"").append(EOL) 646 .append(" echo \"1. Delete the file ") 647 .append("${INSTANCE_ROOT}/lib/set-java-home\"").append(EOL) 648 .append(" echo \"2. Set the environment variable ") 649 .append("OPENDJ_JAVA_HOME to the root of a valid \"").append(EOL) 650 .append(" echo \"Java 7.0 installation.\"").append(EOL) 651 .append(" echo \"If you want to have specificjava settings for") 652 .append(" each command line you must\"").append(EOL) 653 .append(" echo \"follow the steps 3 and 4\"").append(EOL) 654 .append(" echo \"3. Edit the properties file specifying the ") 655 .append("java binary and the java arguments\"").append(EOL) 656 .append(" echo \"for each command line. The java properties ") 657 .append("file is located in:\"").append(EOL) 658 .append(" echo \"${INSTANCE_ROOT}/config/java.properties.\"").append(EOL) 659 .append(" echo \"4. Run the command-line ") 660 .append("${INSTANCE_ROOT}/bin/dsjavaproperties\"").append(EOL) 661 .append(" exit 1").append(EOL) 662 .append(" fi").append(EOL) 663 .append(" else").append(EOL) 664 .append(" OPENDJ_JAVA_BIN=\"${JAVA_HOME}/bin/java\"").append(EOL) 665 .append(" export OPENDJ_JAVA_BIN").append(EOL) 666 .append(" fi").append(EOL) 667 .append(" else").append(EOL) 668 .append(" OPENDJ_JAVA_BIN=\"${JAVA_BIN}\"").append(EOL) 669 .append(" export OPENDJ_JAVA_BIN").append(EOL) 670 .append(" fi").append(EOL) 671 .append(" else").append(EOL) 672 .append(" OPENDJ_JAVA_BIN=\"${OPENDS_JAVA_HOME}/bin/java\"").append(EOL) 673 .append(" export OPENDJ_JAVA_BIN").append(EOL) 674 .append(" fi").append(EOL) 675 .append(" else").append(EOL) 676 .append(" OPENDJ_JAVA_BIN=\"${OPENDS_JAVA_BIN}\"").append(EOL) 677 .append(" export OPENDJ_JAVA_BIN").append(EOL) 678 .append(" fi").append(EOL) 679 .append(" else").append(EOL) 680 .append(" OPENDJ_JAVA_BIN=\"${OPENDJ_JAVA_HOME}/bin/java\"").append(EOL) 681 .append(" export OPENDJ_JAVA_BIN").append(EOL) 682 .append(" fi").append(EOL) 683 .append("fi").append(EOL) 684 .append(EOL); 685 } 686 687 688 if (!overwriteJavaArgs) 689 { 690 buf.append(EOL) 691 .append("# See if the environment variables for arguments are set.").append(EOL) 692 .append("if test -z \"${OPENDJ_JAVA_ARGS}\"").append(EOL) 693 .append("then").append(EOL) 694 .append(" if test -z \"${OPENDS_JAVA_ARGS}\"").append(EOL); 695 } 696 697 propertiesAdded = false; 698 699 propertyNames = properties.propertyNames(); 700 nIfs = 0; 701 while (propertyNames.hasMoreElements()) 702 { 703 String name = propertyNames.nextElement().toString(); 704 String value = properties.getProperty(name); 705 706 String s = overwriteJavaArgs? "":" "; 707 708 if (value != null) 709 { 710 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 711 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 712 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 713 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 714 { 715 // Already handled 716 } 717 else if (name.endsWith(".java-args")) 718 { 719 propertiesAdded = true; 720 if (nIfs > 0) 721 { 722 buf.append(s).append(" elif test \"${SCRIPT_NAME}.java-args\" = \"").append(name).append("\"").append(EOL); 723 } 724 else if (!overwriteJavaArgs) 725 { 726 buf.append(" then").append(EOL) 727 .append(" if test \"${SCRIPT_NAME}.java-args\" = \"").append(name).append("\"").append(EOL); 728 } 729 else 730 { 731 buf.append(" if test \"${SCRIPT_NAME}.java-args\" = \"").append(name).append("\"").append(EOL); 732 } 733 buf 734 .append(s).append(" then").append(EOL) 735 .append(s).append(" OPENDJ_JAVA_ARGS=\"").append(value).append("\"").append(EOL) 736 .append(s).append(" export OPENDJ_JAVA_ARGS").append(EOL); 737 nIfs++; 738 } 739 } 740 } 741 if (defaultJavaArgs != null) 742 { 743 String s = overwriteJavaArgs? "":" "; 744 if (propertiesAdded) 745 { 746 buf.append(s).append(" else").append(EOL) 747 .append(s).append(" OPENDJ_JAVA_ARGS=\"").append(defaultJavaArgs).append("\"").append(EOL) 748 .append(s).append(" export OPENDJ_JAVA_ARGS").append(EOL); 749 } 750 else 751 { 752 if (!overwriteJavaArgs) 753 { 754 buf.append(" then").append(EOL) 755 .append(" OPENDJ_JAVA_ARGS=\"").append(defaultJavaArgs).append("\"").append(EOL) 756 .append(" export OPENDJ_JAVA_ARGS").append(EOL); 757 } 758 else 759 { 760 buf.append(EOL) 761 .append(" OPENDJ_JAVA_ARGS=\"").append(defaultJavaArgs).append("\"").append(EOL) 762 .append(" export OPENDJ_JAVA_ARGS").append(EOL); 763 } 764 } 765 propertiesAdded = true; 766 } 767 if (nIfs > 0) 768 { 769 String s = overwriteJavaArgs? "":" "; 770 buf.append(s).append("fi").append(EOL); 771 } 772 773 if (!overwriteJavaArgs) 774 { 775 if (!propertiesAdded) 776 { 777 // No properties added: this is required not to break the script 778 buf 779 .append(" then").append(EOL) 780 .append(" OPENDJ_JAVA_ARGS=${OPENDJ_JAVA_ARGS}").append(EOL); 781 } 782 buf 783 .append(" else").append(EOL) 784 .append(" OPENDJ_JAVA_ARGS=${OPENDS_JAVA_ARGS}").append(EOL) 785 .append(" export OPENDJ_JAVA_ARGS").append(EOL) 786 .append(" fi").append(EOL) 787 .append("fi").append(EOL); 788 } 789 790 return buf.toString(); 791 } 792 793 private String getWindowsContents(boolean overwriteJavaHome, 794 boolean overwriteJavaArgs, String defaultJavaHome, String defaultJavaArgs, 795 Properties properties) 796 { 797 StringBuilder buf = new StringBuilder(); 798 799 String javaHomeLabel1; 800 String javaArgsLabel1; 801 String javaHomeLabel2; 802 String javaArgsLabel2; 803 804 final String CHECK_ENV_JAVA_HOME = "checkEnvJavaHome"; 805 final String CHECK_ENV_JAVA_ARGS = "checkEnvJavaArgs"; 806 final String CHECK_JAVA_HOME = "checkJavaHome"; 807 final String CHECK_JAVA_ARGS = "checkJavaArgs"; 808 final String CHECK_DEFAULT_JAVA_HOME = "checkDefaultJavaHome"; 809 final String CHECK_DEFAULT_JAVA_ARGS = "checkDefaultJavaArgs"; 810 final String LEGACY = "Legacy"; 811 812 if (!overwriteJavaHome) 813 { 814 javaHomeLabel1 = CHECK_ENV_JAVA_HOME; 815 javaHomeLabel2 = CHECK_JAVA_HOME; 816 } 817 else 818 { 819 javaHomeLabel1 = CHECK_JAVA_HOME; 820 javaHomeLabel2 = CHECK_ENV_JAVA_HOME; 821 } 822 823 if (!overwriteJavaArgs) 824 { 825 javaArgsLabel1 = CHECK_ENV_JAVA_ARGS; 826 javaArgsLabel2 = CHECK_JAVA_ARGS; 827 } 828 else 829 { 830 javaArgsLabel1 = CHECK_JAVA_ARGS; 831 javaArgsLabel2 = CHECK_ENV_JAVA_ARGS; 832 } 833 834 buf.append("goto ").append(javaHomeLabel1).append(EOL).append(EOL); 835 836 buf.append(":").append(CHECK_ENV_JAVA_HOME).append(EOL) 837 .append("if \"%OPENDJ_JAVA_BIN%\" == \"\" goto checkEnvJavaHome").append(LEGACY).append(EOL) 838 .append("if not exist \"%OPENDJ_JAVA_BIN%\" goto checkEnvJavaHome").append(LEGACY).append(EOL) 839 .append("goto ").append(javaArgsLabel1).append(EOL) 840 .append(EOL) 841 .append(":checkEnvJavaHome").append(LEGACY).append(EOL) 842 .append("if \"%OPENDS_JAVA_BIN%\" == \"\" goto checkOpendjJavaHome").append(EOL) 843 .append("if not exist \"%OPENDS_JAVA_BIN%\" goto checkOpendjJavaHome").append(EOL) 844 .append("goto ").append(javaArgsLabel1).append(EOL) 845 .append(EOL) 846 .append(":checkOpendjJavaHome").append(EOL); 847 848 if (javaHomeLabel1 == CHECK_ENV_JAVA_HOME) 849 { 850 buf.append("if \"%OPENDJ_JAVA_HOME%\" == \"\" goto ").append(javaHomeLabel2).append(LEGACY).append(EOL) 851 .append("set TEMP_EXE=%OPENDJ_JAVA_HOME%\\bin\\java.exe").append(EOL) 852 .append("if not exist \"%TEMP_EXE%\" goto ").append(javaHomeLabel2).append(LEGACY).append(EOL) 853 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 854 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL) 855 .append(":").append(javaHomeLabel2).append(LEGACY).append(EOL) 856 .append("if \"%OPENDS_JAVA_HOME%\" == \"\" goto ") 857 .append(javaHomeLabel2).append(EOL) 858 .append("set TEMP_EXE=%OPENDS_JAVA_HOME%\\bin\\java.exe").append(EOL) 859 .append("if not exist \"%TEMP_EXE%\" goto ").append(javaHomeLabel2).append(EOL) 860 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 861 .append("goto ").append(javaArgsLabel1).append(EOL) 862 .append(EOL); 863 } 864 else 865 { 866 buf.append("if \"%OPENDJ_JAVA_HOME%\" == \"\" goto ").append(javaArgsLabel1).append(LEGACY).append(EOL) 867 .append("set TEMP_EXE=%OPENDJ_JAVA_HOME%\\bin\\java.exe").append(EOL) 868 .append("if not exist \"%TEMP_EXE%\" goto ").append(javaArgsLabel1).append(LEGACY).append(EOL) 869 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 870 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL) 871 .append(":").append(javaArgsLabel1).append(LEGACY).append(EOL) 872 .append("if \"%OPENDS_JAVA_HOME%\" == \"\" goto ") 873 .append(javaArgsLabel1).append(EOL) 874 .append("set TEMP_EXE=%OPENDS_JAVA_HOME%\\bin\\java.exe").append(EOL) 875 .append("if not exist \"%TEMP_EXE%\" goto ").append(javaArgsLabel1).append(EOL) 876 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 877 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL); 878 } 879 880 if (defaultJavaHome != null) 881 { 882 if (javaHomeLabel1 == CHECK_ENV_JAVA_HOME) 883 { 884 buf.append(":").append(CHECK_DEFAULT_JAVA_HOME).append(EOL) 885 .append("set TEMP_EXE=").append(defaultJavaHome).append("\\bin\\java.exe").append(EOL) 886 .append("if not exist \"%TEMP_EXE%\" goto ").append(javaArgsLabel1).append(EOL) 887 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 888 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL); 889 } 890 else 891 { 892 buf.append(":").append(CHECK_DEFAULT_JAVA_HOME).append(EOL) 893 .append("set TEMP_EXE=").append(defaultJavaHome).append("\\bin\\java.exe").append(EOL) 894 .append("if not exist \"%TEMP_EXE%\" goto ").append(CHECK_ENV_JAVA_HOME).append(EOL) 895 .append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 896 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL); 897 } 898 } 899 900 buf.append(":").append(CHECK_JAVA_HOME).append(EOL); 901 Enumeration<?> propertyNames = properties.propertyNames(); 902 while (propertyNames.hasMoreElements()) 903 { 904 String name = propertyNames.nextElement().toString(); 905 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 906 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 907 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 908 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 909 { 910 // Already handled 911 } 912 else if (name.endsWith(".java-home")) 913 { 914 String scriptName = name.substring(0, 915 name.length() - ".java-home".length()); 916 buf.append("if \"%SCRIPT_NAME%.java-home\" == \"").append(name) 917 .append("\" goto check").append(scriptName).append("JavaHome").append(EOL); 918 } 919 } 920 if (defaultJavaHome != null) 921 { 922 buf.append("goto ").append(CHECK_DEFAULT_JAVA_HOME).append(EOL).append(EOL); 923 } 924 else if (javaHomeLabel1 != CHECK_ENV_JAVA_HOME) 925 { 926 buf.append("goto ").append(CHECK_ENV_JAVA_HOME).append(EOL).append(EOL); 927 } 928 else 929 { 930 buf.append("goto ").append(javaArgsLabel1).append(EOL).append(EOL); 931 } 932 933 propertyNames = properties.propertyNames(); 934 while (propertyNames.hasMoreElements()) 935 { 936 String name = propertyNames.nextElement().toString(); 937 String value = properties.getProperty(name); 938 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 939 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 940 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 941 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 942 { 943 // Already handled 944 } 945 else if (name.endsWith(".java-home")) 946 { 947 String scriptName = name.substring(0, 948 name.length() - ".java-home".length()); 949 buf.append(":check").append(scriptName).append("JavaHome").append(EOL) 950 .append("set TEMP_EXE=").append(value).append("\\bin\\java.exe").append(EOL); 951 if (defaultJavaHome != null) 952 { 953 buf.append("if not exist \"%TEMP_EXE%\" goto ").append(CHECK_DEFAULT_JAVA_HOME).append(EOL); 954 } 955 else if (javaHomeLabel1 != CHECK_ENV_JAVA_HOME) 956 { 957 buf.append("if not exist \"%TEMP_EXE%\" goto ").append(CHECK_ENV_JAVA_HOME).append(EOL); 958 } 959 buf.append("set OPENDJ_JAVA_BIN=%TEMP_EXE%").append(EOL) 960 .append("goto ").append(javaArgsLabel1).append(EOL).append(EOL); 961 } 962 } 963 964 buf.append(":").append(CHECK_ENV_JAVA_ARGS).append(EOL); 965 if (javaArgsLabel1 == CHECK_ENV_JAVA_ARGS) 966 { 967 buf.append("if \"%OPENDJ_JAVA_ARGS%\" == \"\" goto ").append(javaArgsLabel2).append(LEGACY).append(EOL) 968 .append("goto end").append(EOL).append(EOL) 969 .append(":").append(javaArgsLabel2).append(LEGACY).append(EOL) 970 .append("if \"%OPENDS_JAVA_ARGS%\" == \"\" goto ").append(javaArgsLabel2).append(EOL) 971 .append("set OPENDJ_JAVA_ARGS=%OPENDS_JAVA_ARGS%").append(EOL) 972 .append("goto end").append(EOL).append(EOL); 973 } 974 else 975 { 976 buf.append("goto end").append(EOL).append(EOL); 977 } 978 979 if (defaultJavaArgs != null) 980 { 981 buf.append(":").append(CHECK_DEFAULT_JAVA_ARGS).append(EOL) 982 .append("set OPENDJ_JAVA_ARGS=").append(defaultJavaArgs).append(EOL) 983 .append("goto end").append(EOL).append(EOL); 984 } 985 986 buf.append(":").append(CHECK_JAVA_ARGS).append(EOL); 987 propertyNames = properties.propertyNames(); 988 while (propertyNames.hasMoreElements()) 989 { 990 String name = propertyNames.nextElement().toString(); 991 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 992 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 993 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 994 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 995 { 996 // Already handled 997 } 998 else if (name.endsWith(".java-args")) 999 { 1000 String scriptName = name.substring(0, 1001 name.length() - ".java-args".length()); 1002 buf.append("if \"%SCRIPT_NAME%.java-args\" == \"").append(name) 1003 .append("\" goto check").append(scriptName).append("JavaArgs").append(EOL); 1004 } 1005 } 1006 if (defaultJavaArgs != null) 1007 { 1008 buf.append("goto ").append(CHECK_DEFAULT_JAVA_ARGS).append(EOL).append(EOL); 1009 } 1010 else if (javaArgsLabel1 != CHECK_ENV_JAVA_ARGS) 1011 { 1012 buf.append("goto ").append(CHECK_ENV_JAVA_ARGS).append(EOL).append(EOL); 1013 } 1014 else 1015 { 1016 buf.append("goto end").append(EOL).append(EOL); 1017 } 1018 1019 propertyNames = properties.propertyNames(); 1020 while (propertyNames.hasMoreElements()) 1021 { 1022 String name = propertyNames.nextElement().toString(); 1023 String value = properties.getProperty(name); 1024 if (name.equalsIgnoreCase(DEFAULT_JAVA_HOME_PROP_NAME) || 1025 name.equalsIgnoreCase(DEFAULT_JAVA_ARGS_PROP_NAME) || 1026 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_HOME_PROP_NAME) || 1027 name.equalsIgnoreCase(OVERWRITE_ENV_JAVA_ARGS_PROP_NAME)) 1028 { 1029 // Already handled 1030 } 1031 else if (name.endsWith(".java-args")) 1032 { 1033 String scriptName = name.substring(0, 1034 name.length() - ".java-args".length()); 1035 buf.append(":check").append(scriptName).append("JavaArgs").append(EOL) 1036 .append("set OPENDJ_JAVA_ARGS=").append(value).append(EOL) 1037 .append("goto end").append(EOL).append(EOL); 1038 } 1039 } 1040 1041 buf.append(":end").append(EOL); 1042 1043 return buf.toString(); 1044 } 1045}