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