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