mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
17.58.2013 508d3949e86d0fd27885d1b825889a80d7a46ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2013 ForgeRock AS
 */
package org.opends.quicksetup;
 
import static org.opends.messages.QuickSetupMessages.*;
 
import java.io.*;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import org.opends.messages.Message;
import org.opends.quicksetup.util.Utils;
import org.opends.server.util.SetupUtils;
 
 
 
/**
 * This class represents the physical state of an OpenDJ installation. All the
 * operations are dependent upon the root directory that is specified in the
 * constructor.
 */
public final class Installation
{
 
  /** Relative path to bootstrap OpenDJ jar file. */
  public static final String OPENDJ_BOOTSTRAP_JAR_RELATIVE_PATH =
      "lib/bootstrap.jar";
 
  /**
   * The relative path where all the Windows binaries (batch files) are.
   */
  public static final String WINDOWS_BINARIES_PATH_RELATIVE = "bat";
 
  /**
   * The relative path where all the UNIX binaries (scripts) are.
   */
  public static final String UNIX_BINARIES_PATH_RELATIVE = "bin";
 
  /**
   * The relative path where all the MacOS X Applications are.
   */
  public static final String MAC_APPLICATIONS_PATH_RELATIVE = "bin";
 
  /**
   * The relative path where all the libraries (jar files) are.
   */
  public static final String LIBRARIES_PATH_RELATIVE =
      SetupUtils.LIBRARIES_PATH_RELATIVE;
 
  /**
   * The relative path where the resources directory (to customize the product)
   * is.
   */
  public static final String RESOURCES_PATH_RELATIVE = "resources";
 
  /**
   * The relative path where customer classes are.
   */
  public static final String CLASSES_PATH_RELATIVE = "classes";
 
  /**
   * The relative path where the database files are.
   */
  public static final String DATABASES_PATH_RELATIVE = "db";
 
  /**
   * The relative path where the log files are.
   */
  public static final String LOGS_PATH_RELATIVE = "logs";
 
  /**
   * The relative path where the LDIF files are.
   */
  public static final String LDIFS_PATH_RELATIVE = "ldif";
 
  /**
   * The relative path where the backup files are.
   */
  public static final String BACKUPS_PATH_RELATIVE = "bak";
 
  /**
   * The relative path where the config files are.
   */
  public static final String CONFIG_PATH_RELATIVE = "config";
 
  /**
   * The relative path where the config files are.
   */
  public static final String HISTORY_PATH_RELATIVE = "history";
 
  /**
   * Path to the config/upgrade directory where upgrade base files are stored.
   */
  public static final String UPGRADE_PATH = "upgrade";
 
  /**
   * Relative path to the locks directory.
   */
  public static final String LOCKS_PATH_RELATIVE = "locks";
 
  /**
   * Relative path to the locks directory.
   */
  public static final String TMP_PATH_RELATIVE = "tmp";
 
  /**
   * The relative path to the current Configuration LDIF file.
   */
  public static final String CURRENT_CONFIG_FILE_NAME = "config.ldif";
 
  /**
   * The relative path to the current Configuration LDIF file.
   */
  public static final String BASE_CONFIG_FILE_PREFIX = "config.ldif.";
 
  /**
   * The path to the default instance.
  public static final String DEFAULT_INSTANCE_PATH = "/var/opendj";
   */
 
  /**
   * The relative path to the instance.loc file.
   */
  public static final String INSTANCE_LOCATION_PATH_RELATIVE = "instance.loc";
 
  /**
   * The path to the instance.loc file.
   */
  public static final String INSTANCE_LOCATION_PATH = "/etc/opendj/"
      + INSTANCE_LOCATION_PATH_RELATIVE;
 
  /**
   * The relative path to tmpl_instance.
   */
  public static final String TEMPLATE_RELATIVE_PATH = "template";
 
  /**
   * The relative path to buildinfo file.
   */
  public static final String BUILDINFO_RELATIVE_PATH = "buildinfo";
 
  /**
   * The UNIX setup script file name.
   */
  public static final String UNIX_SETUP_FILE_NAME = "setup";
 
  /**
   * The Windows setup batch file name.
   */
  public static final String WINDOWS_SETUP_FILE_NAME = "setup.bat";
 
  /**
   * The UNIX uninstall script file name.
   */
  public static final String UNIX_UNINSTALL_FILE_NAME = "uninstall";
 
  /**
   * The Windows uninstall batch file name.
   */
  public static final String WINDOWS_UNINSTALL_FILE_NAME = "uninstall.bat";
 
  /**
   * The UNIX upgrade script file name.
   */
  public static final String UNIX_UPGRADE_FILE_NAME = "upgrade";
 
  /**
   * The UNIX configure script file name.
   */
  public static final String UNIX_CONFIGURE_FILE_NAME = "configure";
 
  /**
   * The UNIX start script file name.
   */
  public static final String UNIX_START_FILE_NAME = "start-ds";
 
  /**
   * The Windows start batch file name.
   */
  public static final String WINDOWS_START_FILE_NAME = "start-ds.bat";
 
  /**
   * The UNIX stop script file name.
   */
  public static final String UNIX_STOP_FILE_NAME = "stop-ds";
 
  /**
   * The Windows stop batch file name.
   */
  public static final String WINDOWS_STOP_FILE_NAME = "stop-ds.bat";
 
  /**
   * The UNIX control panel script file name.
   */
  public static final String UNIX_CONTROLPANEL_FILE_NAME = "control-panel";
 
  /**
   * The Windows control panel batch file name.
   */
  public static final String WINDOWS_CONTROLPANEL_FILE_NAME =
      "control-panel.bat";
 
  /**
   * The MacOS X Java application stub name.
   */
  public static final String MAC_JAVA_APP_STUB_NAME = "JavaApplicationStub";
 
  /**
   * The MacOS X control panel application bundle name.
   */
  public static final String MAC_CONTROLPANEL_FILE_NAME = "ControlPanel.app";
 
  /**
   * The UNIX status command line script file name.
   */
  public static final String UNIX_STATUSCLI_FILE_NAME = "status";
 
  /**
   * The Windows status command line batch file name.
   */
  public static final String WINDOWS_STATUSCLI_FILE_NAME = "status.bat";
 
  /**
   * The UNIX import LDIF script file name.
   */
  public static final String UNIX_IMPORT_LDIF = "import-ldif";
 
  /**
   * The Windows import LDIF batch file name.
   */
  public static final String WINDOWS_IMPORT_LDIF = "import-ldif.bat";
 
  /**
   * Name of the file kept in the history directory containing logs of upgrade
   * and reversions.
   */
  public static final String HISTORY_LOG_FILE_NAME = "log";
 
  /**
   * The default java properties file.
   */
  public static final String DEFAULT_JAVA_PROPERTIES_FILE = "java.properties";
 
  /**
   * The default java properties file relative path.
   */
  public static final String RELATIVE_JAVA_PROPERTIES_FILE =
      CONFIG_PATH_RELATIVE + File.separator + "java.properties";
 
  /**
   * The set java home and arguments properties file for Windows.
   */
  public static final String SET_JAVA_PROPERTIES_FILE_WINDOWS =
      "set-java-home.bat";
 
  /**
   * script utils file for UNIX systems.
   */
  public static final String SCRIPT_UTIL_FILE_UNIX = "_script-util.sh";
 
  /**
   * script utils file for Windows.
   */
  public static final String SCRIPT_UTIL_FILE_WINDOWS = "_script-util.bat";
 
  /**
   * The set java home and arguments properties file for UNIX systems.
   */
  public static final String SET_JAVA_PROPERTIES_FILE_UNIX = "set-java-home";
 
  /**
   * Directories required to be present for this installation to be considered
   * valid.
   */
  public static final String[] REQUIRED_DIRECTORIES = new String[] {
      CONFIG_PATH_RELATIVE, DATABASES_PATH_RELATIVE, LIBRARIES_PATH_RELATIVE };
 
 
 
  /**
   * Performs validation on the specified file to make sure that it is an actual
   * OpenDJ installation.
   *
   * @param rootDirectory
   *          File directory candidate
   * @throws IllegalArgumentException
   *           if root directory does not appear to be an OpenDJ installation
   *           root. The thrown exception contains a localized message
   *           indicating the reason why <code>rootDirectory</code> is not a
   *           valid OpenDJ install root.
   */
  static public void validateRootDirectory(File rootDirectory)
      throws IllegalArgumentException
  {
    Message failureReason = null;
    if (rootDirectory == null)
    {
      failureReason = INFO_ERROR_INSTALL_ROOT_DIR_NULL.get();
    }
    else if (!rootDirectory.exists())
    {
      failureReason = INFO_ERROR_INSTALL_ROOT_DIR_NO_EXIST.get(Utils
          .getPath(rootDirectory));
    }
    else if (!rootDirectory.isDirectory())
    {
      failureReason = INFO_ERROR_INSTALL_ROOT_DIR_NOT_DIR.get(Utils
          .getPath(rootDirectory));
    }
    else
    {
      String[] children = rootDirectory.list();
      if (children != null)
      {
        Set<String> childrenSet = new HashSet<String>(Arrays.asList(children));
        for (String dir : REQUIRED_DIRECTORIES)
        {
          if (!childrenSet.contains(dir))
          {
            failureReason = INFO_ERROR_INSTALL_ROOT_DIR_NO_DIR.get(
                Utils.getPath(rootDirectory), dir);
          }
        }
      }
      else
      {
        failureReason = INFO_ERROR_INSTALL_ROOT_DIR_EMPTY.get(Utils
            .getPath(rootDirectory));
      }
    }
    if (failureReason != null)
    {
      throw new IllegalArgumentException(failureReason.toString());
    }
  }
 
 
 
  static private Installation local;
 
 
 
  /**
   * Obtains the installation by reading the classpath of the running JVM to
   * determine the location of the jars and determine the installation root.
   *
   * @return Installation obtained by reading the classpath
   */
  static public Installation getLocal()
  {
    if (local == null)
    {
 
      // This allows testing of configuration components when the OpenDJ.jar
      // in the classpath does not necessarily point to the server's
      String installRoot = System.getProperty("org.opends.quicksetup.Root");
      String instanceRoot = System
          .getProperty("org.opends.quicksetup.instance");
 
      if (installRoot == null)
      {
        installRoot = Utils.getInstallPathFromClasspath();
      }
      if (instanceRoot == null)
      {
        instanceRoot = Utils.getInstancePathFromInstallPath(installRoot);
      }
      local = new Installation(installRoot, instanceRoot);
    }
    return local;
  }
 
 
 
  static private final Logger LOG = Logger.getLogger(Installation.class
      .getName());
 
  private File rootDirectory;
 
  private File instanceDirectory;
 
  private Status status;
 
  private Configuration configuration;
 
  private Configuration baseConfiguration;
 
  private BuildInformation buildInformation;
 
  private BuildInformation instanceInformation;
 
 
  /**
   * Creates a new instance from a root directory specified as a string.
   *
   * @param rootDirectory
   *          of this installation
   * @param instanceRootDirectory
   *          The instance root directory
   */
  public Installation(String rootDirectory, String instanceRootDirectory)
  {
    this(new File(rootDirectory), new File(instanceRootDirectory));
  }
 
 
 
  /**
   * Creates a new instance from a root directory specified as a File.
   *
   * @param rootDirectory
   *          of this installation
   * @param instanceDirectory
   *          of the instance
   */
  public Installation(File rootDirectory, File instanceDirectory)
  {
    setRootDirectory(rootDirectory);
    setInstanceDirectory(instanceDirectory);
  }
 
 
 
  /**
   * Gets the top level directory of an OpenDJ installation.
   *
   * @return File object representing the top level directory of and OpenDJ
   *         installation
   */
  public File getRootDirectory()
  {
    return this.rootDirectory;
  }
 
 
 
  /**
   * Gets the top level directory of an OpenDJ instance.
   *
   * @return File object representing the top level directory of and OpenDK
   *         installation
   */
  public File getInstanceDirectory()
  {
    return this.instanceDirectory;
  }
 
 
 
  /**
   * Sets the root directory of this installation.
   *
   * @param rootDirectory
   *          File of this installation
   */
  public void setRootDirectory(File rootDirectory)
  {
 
    // Hold off on doing validation of rootDirectory since
    // some applications (like the Installer) create an Installation
    // before the actual bits have been laid down on the file system.
    this.rootDirectory = rootDirectory;
 
    // Obtaining build information is a fairly time consuming operation.
    // Try to get a head start if possible.
    if (isValid(rootDirectory))
    {
      try
      {
        BuildInformation bi = getBuildInformation();
        LOG.log(Level.INFO, "build info for " + rootDirectory.getName() + ": "
            + bi);
      }
      catch (ApplicationException e)
      {
        LOG.log(Level.INFO, "error determining build information", e);
      }
    }
  }
 
 
 
  /**
   * Sets the root directory of this instance.
   *
   * @param instanceDirectory
   *          File of this instance
   */
  public void setInstanceDirectory(File instanceDirectory)
  {
 
    // Hold off on doing validation of rootDirectory since
    // some applications (like the Installer) create an Installation
    // before the actual bits have been laid down on the filesyste.
    this.instanceDirectory = instanceDirectory;
 
    // Obtaining build information is a fairly time consuming operation.
    // Try to get a head start if possible.
    if (isValid(instanceDirectory))
    {
      try
      {
        BuildInformation bi = getBuildInformation();
        LOG.log(Level.INFO, "build info for " + instanceDirectory.getName()
            + ": " + bi);
      }
      catch (ApplicationException e)
      {
        LOG.log(Level.INFO, "error determining build information", e);
      }
    }
  }
 
 
 
  /**
   * Indicates whether or not this installation appears to be an actual OpenDJ
   * installation.
   *
   * @param file
   *          The root directory
   * @return boolean where true indicates that this does indeed appear to be a
   *         valid OpenDJ installation; false otherwise
   */
  public boolean isValid(File file)
  {
    boolean valid = true;
    try
    {
      validateRootDirectory(file);
    }
    catch (IllegalArgumentException e)
    {
      valid = false;
    }
    return valid;
  }
 
 
 
  /**
   * Creates a string explaining why this is not a legitimate OpenDJ
   * installation. Null if this is in fact a vaild installation.
   *
   * @return localized message indicating the reason this is not an OpenDJ
   *         installation
   */
  public String getInvalidityReason()
  {
    String reason = null;
    try
    {
      validateRootDirectory(rootDirectory);
    }
    catch (IllegalArgumentException e)
    {
      reason = e.getLocalizedMessage();
    }
    return reason;
  }
 
 
 
  /**
   * Gets the Configuration object representing this file. The current
   * configuration is stored in config/config.ldif.
   *
   * @return Configuration representing the current configuration.
   */
  public Configuration getCurrentConfiguration()
  {
    if (configuration == null)
    {
      configuration = new Configuration(this, getCurrentConfigurationFile());
    }
    return configuration;
  }
 
 
 
  /**
   * Gets the Configuration object representing this file. The base
   * configuration is stored in config/upgrade/config.ldif.[svn rev].
   *
   * @return Configuration object representing the base configuration.
   * @throws ApplicationException
   *           if there was a problem determining the svn rev number.
   */
  public Configuration getBaseConfiguration() throws ApplicationException
  {
    if (baseConfiguration == null)
    {
      baseConfiguration = new Configuration(this, getBaseConfigurationFile());
    }
    return baseConfiguration;
  }
 
 
 
  /**
   * Gets the current status of this installation.
   *
   * @return Status object representing the state of this installation.
   */
  public Status getStatus()
  {
    if (status == null)
    {
      status = new Status(this);
    }
    return status;
  }
 
 
 
  /**
   * Returns the path to the libraries.
   *
   * @return the path to the libraries.
   */
  public File getLibrariesDirectory()
  {
    return new File(getRootDirectory(), LIBRARIES_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the resources directory.
   *
   * @return the path to the resources directory.
   */
  public File getResourcesDirectory()
  {
    return new File(getRootDirectory(), RESOURCES_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the classes directory.
   *
   * @return the path to the classes directory.
   */
  public File getClassesDirectory()
  {
    return new File(getRootDirectory(), CLASSES_PATH_RELATIVE);
  }
 
 
 
  /**
   * Creates a File object representing config/upgrade/schema.ldif.current which
   * the server creates the first time it starts if there are schema
   * customizations.
   *
   * @return File object with no
   */
  public File getSchemaConcatFile()
  {
    return new File(getConfigurationUpgradeDirectory(), "schema.ldif.current");
  }
 
 
 
  /**
   * Creates a File object representing config/upgrade/schema.ldif.current which
   * the server creates the first time it starts if there are schema
   * customizations.
   *
   * @return File object with no
   * @throws ApplicationException
   *           if there was a problem determining the svn revision number
   */
  public File getBaseSchemaFile() throws ApplicationException
  {
    return new File(getConfigurationUpgradeDirectory(), "schema.ldif."
        + getInstanceSvnRev().toString());
  }
 
 
 
  /**
   * Creates a File object representing config/upgrade/schema.ldif.current which
   * the server creates the first time it starts if there are schema
   * customizations.
   *
   * @return File object with no
   * @throws ApplicationException
   *           if there was a problem determining the svn revision number
   */
  public File getBaseConfigurationFile() throws ApplicationException
  {
    return new File(getConfigurationUpgradeDirectory(), BASE_CONFIG_FILE_PREFIX
        + getInstanceSvnRev().toString());
  }
 
 
 
  /**
   * Gets the SVN revision number of the build.
   *
   * @return Integer representing the svn number
   * @throws ApplicationException
   *           if for some reason the number could not be determined
   */
  public Integer getSvnRev() throws ApplicationException
  {
    BuildInformation bi = getBuildInformation();
    return bi.getRevisionNumber();
  }
 
 
 
  /**
   * Gets the SVN revision number of the instance.
   *
   * @return Integer representing the svn number
   * @throws ApplicationException
   *           if for some reason the number could not be determined
   */
  public Integer getInstanceSvnRev() throws ApplicationException
  {
    BuildInformation bi = getInstanceBuildInformation();
    return bi.getRevisionNumber();
  }
 
 
 
  /**
   * Returns the path to the configuration file of the directory server. Note
   * that this method assumes that this code is being run locally.
   *
   * @return the path of the configuration file of the directory server.
   */
  public File getCurrentConfigurationFile()
  {
    return new File(getConfigurationDirectory(), CURRENT_CONFIG_FILE_NAME);
  }
 
 
 
  /**
   * Returns the relative path of the directory containing the binaries/scripts
   * of the Open DS installation. The path is relative to the installation path.
   *
   * @return the relative path of the directory containing the binaries/scripts
   *         of the Open DS installation.
   */
  public File getBinariesDirectory()
  {
    File binPath;
    if (Utils.isWindows())
    {
      binPath = new File(getRootDirectory(), WINDOWS_BINARIES_PATH_RELATIVE);
    }
    else
    {
      binPath = new File(getRootDirectory(), UNIX_BINARIES_PATH_RELATIVE);
    }
    return binPath;
  }
 
 
 
  /**
   * Returns the path to the database files under the install path.
   *
   * @return the path to the database files under the install path.
   */
  public File getDatabasesDirectory()
  {
    return new File(getInstanceDirectory(), DATABASES_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the backup files under the install path.
   *
   * @return the path to the backup files under the install path.
   */
  public File getBackupDirectory()
  {
    return new File(getInstanceDirectory(), BACKUPS_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the config files under the install path.
   *
   * @return the path to the config files under the install path.
   */
  public File getConfigurationDirectory()
  {
    return new File(getInstanceDirectory(), CONFIG_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the log files under the install path.
   *
   * @return the path to the log files under the install path.
   */
  public File getLogsDirectory()
  {
    return new File(getInstanceDirectory(), LOGS_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the directory where the lock files are stored.
   *
   * @return the path to the lock files.
   */
  public File getLocksDirectory()
  {
    return new File(getInstanceDirectory(), LOCKS_PATH_RELATIVE);
  }
 
 
 
  /**
   * Gets the directory used to store the template configuration.
   *
   * @return The directory used to store the template configuration.
   */
  public File getTemplateDirectory()
  {
    return new File(getRootDirectory(), TEMPLATE_RELATIVE_PATH);
  }
 
 
 
  /**
   * Gets the directory used to store files temporarily.
   *
   * @return File temporary directory
   */
  public File getTemporaryDirectory()
  {
    return new File(getInstanceDirectory(), TMP_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the directory where the lock files are stored.
   *
   * @return the path to the lock files.
   */
  public File getHistoryDirectory()
  {
    return new File(getInstanceDirectory(), HISTORY_PATH_RELATIVE);
  }
 
 
 
  /**
   * Creates a new directory in the history directory appropriate for backing up
   * an installation during an upgrade.
   *
   * @return File representing a new backup directory. The directory can be
   *         assumed to exist if this method returns cleanly.
   * @throws IOException
   *           if an error occurred creating the directory.
   */
  public File createHistoryBackupDirectory() throws IOException
  {
    File backupDirectory = new File(getHistoryDirectory(), Long.toString(System
        .currentTimeMillis()));
    if (backupDirectory.exists())
    {
      backupDirectory.delete();
    }
    if (!backupDirectory.mkdirs())
    {
      throw new IOException("failed to create history backup directory");
    }
    return backupDirectory;
  }
 
 
 
  /**
   * Gets the log file where the history of upgrades and reversions is kept.
   *
   * @return File containing upgrade/reversion history.
   */
  public File getHistoryLogFile()
  {
    return new File(getHistoryDirectory(), HISTORY_LOG_FILE_NAME);
  }
 
 
 
  /**
   * Gets the directory config/upgrade.
   *
   * @return File representing the config/upgrade directory
   */
  public File getConfigurationUpgradeDirectory()
  {
    return new File(getConfigurationDirectory(), UPGRADE_PATH);
  }
 
 
 
  /**
   * Gets the directory where the upgrader stores files temporarily.
   *
   * @return File representing the upgrader's temporary directory
   */
  public File getTemporaryUpgradeDirectory()
  {
    return new File(getTemporaryDirectory(), UPGRADE_PATH);
  }
 
 
 
  /**
   * Gets the file for invoking a particular command appropriate for the current
   * operating system.
   *
   * @param command
   *          namd of the command
   * @return File representing the command
   */
  public File getCommandFile(String command)
  {
    File commandFile;
    if (Utils.isWindows())
    {
      commandFile = new File(getBinariesDirectory(), command + ".bat");
    }
    else
    {
      commandFile = new File(getBinariesDirectory(), command);
    }
    return commandFile;
  }
 
 
 
  /**
   * Gets the file responsible for stopping the server appropriate for the
   * current operating system.
   *
   * @return File representing the stop command
   */
  public File getServerStartCommandFile()
  {
    File startCommandFile;
    if (Utils.isWindows())
    {
      startCommandFile = new File(getBinariesDirectory(),
          WINDOWS_START_FILE_NAME);
    }
    else
    {
      startCommandFile = new File(getBinariesDirectory(), UNIX_START_FILE_NAME);
    }
    return startCommandFile;
  }
 
 
 
  /**
   * Gets the file responsible for stopping the server appropriate for the
   * current operating system.
   *
   * @return File representing the stop command
   */
  public File getServerStopCommandFile()
  {
    File stopCommandFile;
    if (Utils.isWindows())
    {
      stopCommandFile = new File(getBinariesDirectory(),
          WINDOWS_STOP_FILE_NAME);
    }
    else
    {
      stopCommandFile = new File(getBinariesDirectory(),
          UNIX_STOP_FILE_NAME);
    }
    return stopCommandFile;
  }
 
 
 
  /**
   * Returns the 'ldif' directory.
   *
   * @return the 'ldif' directory.
   */
  public File getLdifDirectory()
  {
    return new File(getRootDirectory(), LDIFS_PATH_RELATIVE);
  }
 
 
 
  /**
   * Returns the path to the quicksetup jar file.
   *
   * @return the path to the quicksetup jar file.
   */
  public File getQuicksetupJarFile()
  {
    return new File(getLibrariesDirectory(), "quicksetup.jar");
  }
 
 
 
  /**
   * Returns the path to the opends jar file.
   *
   * @return the path to the opends jar file.
   */
  public File getOpenDSJarFile()
  {
    return new File(getLibrariesDirectory(), "OpenDJ.jar");
  }
 
 
 
  /**
   * Returns the path to the uninstall.bat file.
   *
   * @return the path to the uninstall.bat file.
   */
  public File getUninstallBatFile()
  {
    return new File(getRootDirectory(), "uninstall.bat");
  }
 
 
 
  /**
   * Gets the control panel command file appropriate for the current operating
   * system.
   *
   * @return File object representing the control panel command
   */
  public File getControlPanelCommandFile()
  {
    File controlPanelCommandFile;
    if (Utils.isWindows())
    {
      controlPanelCommandFile = new File(getBinariesDirectory(),
          WINDOWS_CONTROLPANEL_FILE_NAME);
    }
    else if (Utils.isMacOS())
    {
      controlPanelCommandFile = new File(getRootDirectory() + File.separator
          + MAC_APPLICATIONS_PATH_RELATIVE, MAC_CONTROLPANEL_FILE_NAME);
    }
    else
    {
      controlPanelCommandFile = new File(getBinariesDirectory(),
          UNIX_CONTROLPANEL_FILE_NAME);
    }
    return controlPanelCommandFile;
  }
 
 
 
   /**
   * Gets information about the build that was used to produce the bits for this
   * installation.
   *
   * @return BuildInformation object describing this installation
   * @throws ApplicationException
   *           if there is a problem obtaining the build information
   */
  public BuildInformation getBuildInformation() throws ApplicationException
  {
    return getBuildInformation(true);
  }
 
 
 
  /**
   * Gets information about the build that was used to produce the bits for this
   * installation.
   *
   * @param useCachedVersion
   *          where true indicates that a potentially cached version of the
   *          build information is acceptable for use; false indicates the the
   *          build information will be created from scratch which is
   *          potentially time consuming
   * @return BuildInformation object describing this installation
   * @throws ApplicationException
   *           if there is a problem obtaining the build information
   */
  public BuildInformation getBuildInformation(boolean useCachedVersion)
      throws ApplicationException
  {
    if (buildInformation == null || !useCachedVersion)
    {
      FutureTask<BuildInformation> ft = new FutureTask<BuildInformation>(
          new Callable<BuildInformation>()
          {
 
            @Override
            public BuildInformation call() throws ApplicationException
            {
              return BuildInformation.create(Installation.this);
            }
          });
      new Thread(ft).start();
      try
      {
        buildInformation = ft.get();
      }
      catch (InterruptedException e)
      {
        LOG.log(Level.INFO, "interrupted trying to get build information", e);
      }
      catch (ExecutionException e)
      {
        throw (ApplicationException) e.getCause();
      }
    }
    return buildInformation;
  }
 
 
 
  /**
   * Gets information about the build that was used to produce the instance.
   *
   * @return BuildInformation object describing this instance
   */
  public BuildInformation getInstanceBuildInformation()
  {
    return getInstanceBuildInformation(true);
  }
 
 
 
  /**
   * Gets information about the build that was used to produce the instance.
   *
   * @param useCachedVersion
   *          where true indicates that a potentially cached version of the
   *          build information is acceptable for use; false indicates the build
   *          information will be created from scratch which is potentially time
   *          consuming
   * @return BuildInformation object describing this instance
   */
  public BuildInformation getInstanceBuildInformation(boolean useCachedVersion)
  {
    if (instanceInformation == null || !useCachedVersion)
    {
      try
      {
        File bif = new File(getConfigurationDirectory(),
            BUILDINFO_RELATIVE_PATH);
 
        if (bif.exists())
        {
          BufferedReader reader = new BufferedReader(new FileReader(bif));
 
          // Read the first line and close the file.
          String line;
          try
          {
            line = reader.readLine();
            instanceInformation = BuildInformation.fromBuildString(line);
          }
          finally
          {
            try
            {
              reader.close();
            }
            catch (Exception e)
            {
              // do nothing
            }
          }
        }
        else
        {
          return getBuildInformation();
        }
      }
      catch (Exception e)
      {
        LOG.log(Level.SEVERE, "error getting build information for "
            + "current instance", e);
      }
    }
    return instanceInformation;
 
  }
 
}