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