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

jvergara
17.40.2006 8a1a1478c28acdc7376fc7e57d76cca61b901441
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
/*
 * 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
 *
 *
 *      Portions Copyright 2006 Sun Microsystems, Inc.
 */
 
package org.opends.quicksetup;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
 
import javax.swing.SwingUtilities;
 
import org.opends.quicksetup.event.ButtonActionListener;
import org.opends.quicksetup.event.ButtonEvent;
import org.opends.quicksetup.event.ProgressUpdateEvent;
import org.opends.quicksetup.event.ProgressUpdateListener;
import org.opends.quicksetup.i18n.ResourceProvider;
import org.opends.quicksetup.installer.DataOptions;
import org.opends.quicksetup.installer.FieldName;
import org.opends.quicksetup.installer.InstallProgressDescriptor;
import org.opends.quicksetup.installer.InstallProgressStep;
import org.opends.quicksetup.installer.Installer;
import org.opends.quicksetup.installer.UserInstallData;
import org.opends.quicksetup.installer.UserInstallDataException;
import org.opends.quicksetup.installer.offline.OfflineInstaller;
import org.opends.quicksetup.installer.webstart.WebStartDownloader;
import org.opends.quicksetup.installer.webstart.WebStartInstaller;
import org.opends.quicksetup.ui.QuickSetupDialog;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.uninstaller.Uninstaller;
import org.opends.quicksetup.util.BackgroundTask;
import org.opends.quicksetup.util.Utils;
 
/**
 * This class is responsible for doing the following:
 *
 * Check whether we are installing or uninstalling and which type of
 * installation we are running.
 *
 * Performs all the checks and validation of the data provided by the user
 * during the setup.
 *
 * It will launch also the installation once the user clicks on 'Finish' if we
 * are installing the product.
 *
 * If we are running a web start installation it will start the background
 * downloading of the jar files that are required to perform the installation
 * (OpenDS.jar, je.jar, etc.).  The global idea is to force the user to
 * download just one jar file (quicksetup.jar) to launch the Web Start
 * installer.  Then QuickSetup will call WebStartDownloader to download the jar
 * files.  Until this class is not finished the WebStart Installer will be on
 * the InstallProgressStep.DOWNLOADING step.
 *
 */
class QuickSetup implements ButtonActionListener, ProgressUpdateListener
{
  // Contains the data provided by the user
  private UserInstallData userData;
 
  private Installer installer;
 
  private Uninstaller uninstaller;
 
  private WebStartDownloader jnlpDownloader;
 
  private CurrentInstallStatus installStatus;
 
  private Step currentStep = Step.WELCOME;
 
  private QuickSetupDialog dialog;
 
  private StringBuffer progressDetails = new StringBuffer();
 
  private InstallProgressDescriptor lastDescriptor;
 
  private InstallProgressDescriptor lastDisplayedDescriptor;
 
  private InstallProgressDescriptor descriptorToDisplay;
 
  // Constants used to do checks
  private static final int MIN_DIRECTORY_MANAGER_PWD = 1;
 
  private static final int MIN_PORT_VALUE = 1;
 
  private static final int MAX_PORT_VALUE = 65535;
 
  private static final int MIN_NUMBER_ENTRIES = 1;
 
  private static final int MAX_NUMBER_ENTRIES = 10000000;
 
  // Update period of the dialogs.
  private static final int UPDATE_PERIOD = 500;
 
  /**
   * This method creates the install/uninstall dialogs and to check the current
   * install status. This method must be called outside the event thread because
   * it can perform long operations which can make the user think that the UI is
   * blocked.
   *
   * @param args for the moment this parameter is not used but we keep it in
   * order to (in case of need) pass parameters through the command line.
   */
  public void initialize(String[] args)
  {
    if (isWebStart())
    {
      jnlpDownloader = new WebStartDownloader();
      jnlpDownloader.start(false);
    }
    installStatus = new CurrentInstallStatus();
    initLookAndFeel();
    if (Utils.isUninstall())
    {
      setCurrentStep(Step.CONFIRM_UNINSTALL);
    } else
    {
      setCurrentStep(Step.WELCOME);
    }
  }
 
  /**
   * This method displays the setup dialog. This method must be called from the
   * event thread.
   */
  public void display()
  {
    getDialog().packAndShow();
  }
 
  /**
   * ButtonActionListener implementation. It assumes that we are called in the
   * event thread.
   *
   * @param ev the ButtonEvent we receive.
   */
  public void buttonActionPerformed(ButtonEvent ev)
  {
    switch (ev.getButtonName())
    {
    case NEXT:
      nextClicked();
      break;
 
    case CLOSE:
      closeClicked();
      break;
 
    case FINISH:
      finishClicked();
      break;
 
    case QUIT:
      quitClicked();
      break;
 
    case PREVIOUS:
      previousClicked();
      break;
 
    default:
      throw new IllegalArgumentException("Unknown button name: "
          + ev.getButtonName());
    }
  }
 
  /**
   * ProgressUpdateListener implementation. Here we take the ProgressUpdateEvent
   * and create a ProgressDescriptor that will be used to update the progress
   * dialog.
   *
   * @param ev the ProgressUpdateEvent we receive.
   *
   * @see #runDisplayUpdater()
   */
  public void progressUpdate(ProgressUpdateEvent ev)
  {
    synchronized (this)
    {
      InstallProgressDescriptor desc = createProgressDescriptor(ev);
      boolean isLastDescriptor =
          desc.getProgressStep() == InstallProgressStep.FINISHED_SUCCESSFULLY
              || desc.getProgressStep() ==
                InstallProgressStep.FINISHED_WITH_ERROR;
      if (isLastDescriptor)
      {
        lastDescriptor = desc;
      }
 
      descriptorToDisplay = desc;
    }
  }
 
  /**
   * This method is used to update the progress dialog.
   *
   * We are receiving notifications from the installer (this class is a
   * ProgressListener). However if we lots of notifications updating the
   * progress panel every time we get a progress update can result of a lot of
   * flickering. So the idea here is to have a minimal time between 2 updates of
   * the progress dialog (specified by UPDATE_PERIOD).
   *
   * @see #progressUpdate(ProgressUpdateEvent ev)
   *
   */
  private void runDisplayUpdater()
  {
    boolean doPool = true;
    while (doPool)
    {
      try
      {
        Thread.sleep(UPDATE_PERIOD);
      } catch (Exception ex)
      {
      }
      synchronized (this)
      {
        final InstallProgressDescriptor desc = descriptorToDisplay;
        if (desc != null)
        {
          if (desc != lastDisplayedDescriptor)
          {
            lastDisplayedDescriptor = desc;
 
            SwingUtilities.invokeLater(new Runnable()
            {
              public void run()
              {
                getDialog().displayProgress(desc);
              }
            });
          }
          doPool = desc != lastDescriptor;
        }
      }
    }
  }
 
  /**
   * Method called when user clicks 'Next' button of the wizard.
   *
   */
  private void nextClicked()
  {
    final Step cStep = getCurrentStep();
    switch (cStep)
    {
    case PROGRESS:
      throw new IllegalStateException(
          "Cannot click on next from progress step");
 
    case REVIEW:
      throw new IllegalStateException("Cannot click on next from review step");
 
    default:
      BackgroundTask worker = new BackgroundTask()
      {
        public Object processBackgroundTask() throws UserInstallDataException
        {
          try
          {
            updateUserData(cStep);
          } catch (RuntimeException re)
          {
            throw new UserInstallDataException(getCurrentStep(),
                getExceptionMsg("bug-msg", re));
          }
          return null;
        }
 
        public void backgroundTaskCompleted(Object returnValue,
            Throwable throwable)
        {
          getDialog().workerFinished();
          UserInstallDataException ude = (UserInstallDataException)throwable;
          if (ude != null)
          {
            displayError(ude.getLocalizedMessage(), getMsg("error-title"));
          } else
          {
            setCurrentStep(nextStep(cStep));
          }
        }
      };
      getDialog().workerStarted();
      worker.startBackgroundTask();
    }
  }
 
  /**
   * Method called when user clicks 'Finish' button of the wizard.
   *
   */
  private void finishClicked()
  {
    Step cStep = getCurrentStep();
    switch (cStep)
    {
    case REVIEW:
      updateUserDataForReviewPanel();
      launchInstallation();
      setCurrentStep(Step.PROGRESS);
      break;
 
    case CONFIRM_UNINSTALL:
      launchUninstallation();
      setCurrentStep(Step.PROGRESS);
      break;
 
    default:
      throw new IllegalStateException(
          "Cannot click on finish when we are not in the Review window");
    }
  }
 
  /**
   * Method called when user clicks 'Previous' button of the wizard.
   *
   */
  private void previousClicked()
  {
    Step cStep = getCurrentStep();
    switch (cStep)
    {
    case WELCOME:
      throw new IllegalStateException(
          "Cannot click on previous from progress step");
 
    case PROGRESS:
      throw new IllegalStateException(
          "Cannot click on previous from progress step");
 
    default:
      setCurrentStep(previousStep(cStep));
    }
  }
 
  /**
   * Method called when user clicks 'Quit' button of the wizard.
   *
   */
  private void quitClicked()
  {
    Step cStep = getCurrentStep();
    switch (cStep)
    {
    case PROGRESS:
      throw new IllegalStateException(
          "Cannot click on quit from progress step");
 
    default:
      if (installStatus.isInstalled())
      {
        quit();
      } else if (displayConfirmation(getMsg("confirm-quit-quicksetup-msg"),
          getMsg("confirm-quit-quicksetup-title")))
      {
        quit();
      }
    }
  }
 
  /**
   * Method called when user clicks 'Close' button of the wizard.
   *
   */
  private void closeClicked()
  {
    Step cStep = getCurrentStep();
    switch (cStep)
    {
    case PROGRESS:
      if (Utils.isUninstall())
      {
        boolean finished = uninstaller.isFinished();
        if (finished
            || displayConfirmation(getMsg("confirm-close-uninstall-msg"),
                getMsg("confirm-close-uninstall-title")))
        {
          quit();
        }
      } else
      {
        boolean finished = installer.isFinished();
        if (finished
            || displayConfirmation(getMsg("confirm-close-quicksetup-msg"),
                getMsg("confirm-close-quicksetup-title")))
        {
          quit();
        }
      }
      break;
 
    default:
      throw new IllegalStateException(
          "Close only can be clicked on PROGRESS step");
    }
  }
 
  /**
   * Method called when we want to quit the setup (for instance when the user
   * clicks on 'Close' or 'Quit' buttons and has confirmed that (s)he wants to
   * quit the program.
   *
   */
  private void quit()
  {
    System.exit(0);
  }
 
  /**
   * These methods validate the data provided by the user in the panels and
   * update the UserData object according to that content.
   *
   * @param cStep
   *          the current step of the wizard
   *
   * @throws an
   *           UserInstallDataException if the data provided by the user is not
   *           valid.
   *
   */
  private void updateUserData(Step cStep) throws UserInstallDataException
  {
    switch (cStep)
    {
    case SERVER_SETTINGS:
      updateUserDataForServerSettingsPanel();
      break;
 
    case DATA_OPTIONS:
      updateUserDataForDataOptionsPanel();
      break;
 
    case REVIEW:
      updateUserDataForReviewPanel();
      break;
    }
  }
 
  /**
   * Validate the data provided by the user in the server settings panel and
   * update the UserData object according to that content.
   *
   * @throws an
   *           UserInstallDataException if the data provided by the user is not
   *           valid.
   *
   */
  private void updateUserDataForServerSettingsPanel()
      throws UserInstallDataException
  {
    ArrayList<String> errorMsgs = new ArrayList<String>();
 
    if (isWebStart())
    {
      // Check the server location
      String serverLocation = getFieldStringValue(FieldName.SERVER_LOCATION);
 
      if ((serverLocation == null) || ("".equals(serverLocation.trim())))
      {
        errorMsgs.add(getMsg("empty-server-location"));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
      } else if (!Utils.parentDirectoryExists(serverLocation))
      {
        String[] arg =
          { serverLocation };
        errorMsgs.add(getMsg("parent-directory-does-not-exist", arg));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
      } else if (Utils.fileExists(serverLocation))
      {
        String[] arg =
          { serverLocation };
        errorMsgs.add(getMsg("file-exists", arg));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
      } else if (Utils.directoryExistsAndIsNotEmpty(serverLocation))
      {
        String[] arg =
          { serverLocation };
        errorMsgs.add(getMsg("directory-exists-not-empty", arg));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
      } else if (!Utils.canWrite(serverLocation))
      {
        String[] arg =
          { serverLocation };
        errorMsgs.add(getMsg("directory-not-writable", arg));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
 
      } else if (!Utils.hasEnoughSpace(serverLocation,
          getRequiredInstallSpace()))
      {
        long requiredInMb = getRequiredInstallSpace() / (1024 * 1024);
        String[] args =
          { serverLocation, String.valueOf(requiredInMb) };
        errorMsgs.add(getMsg("not-enough-disk-space", args));
        displayFieldInvalid(FieldName.SERVER_LOCATION, true);
 
      } else
      {
        getUserData().setServerLocation(serverLocation);
        displayFieldInvalid(FieldName.SERVER_LOCATION, false);
      }
    }
 
    // Check the port
    String sPort = getFieldStringValue(FieldName.SERVER_PORT);
    try
    {
      int port = Integer.parseInt(sPort);
      if ((port < MIN_PORT_VALUE) || (port > MAX_PORT_VALUE))
      {
        String[] args =
          { String.valueOf(MIN_PORT_VALUE), String.valueOf(MAX_PORT_VALUE) };
        errorMsgs.add(getMsg("invalid-port-value-range", args));
        displayFieldInvalid(FieldName.SERVER_PORT, true);
      } else if (!Utils.canUseAsPort(port))
      {
        if (Utils.isPriviledgedPort(port))
        {
          errorMsgs.add(getMsg("cannot-bind-priviledged-port", new String[]
            { String.valueOf(port) }));
        } else
        {
          errorMsgs.add(getMsg("cannot-bind-port", new String[]
            { String.valueOf(port) }));
        }
        displayFieldInvalid(FieldName.SERVER_PORT, true);
 
      } else
      {
        getUserData().setServerPort(port);
        displayFieldInvalid(FieldName.SERVER_PORT, false);
      }
 
    } catch (NumberFormatException nfe)
    {
      String[] args =
        { String.valueOf(MIN_PORT_VALUE), String.valueOf(MAX_PORT_VALUE) };
      errorMsgs.add(getMsg("invalid-port-value-range", args));
      displayFieldInvalid(FieldName.SERVER_PORT, true);
    }
 
    // Check the Directory Manager DN
    String dmDn = getFieldStringValue(FieldName.DIRECTORY_MANAGER_DN);
 
    if ((dmDn == null) || (dmDn.trim().length() == 0))
    {
      errorMsgs.add(getMsg("empty-directory-manager-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_DN, true);
    } else if (!Utils.isDn(dmDn))
    {
      errorMsgs.add(getMsg("not-a-directory-manager-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_DN, true);
    } else if (Utils.isConfigurationDn(dmDn))
    {
      errorMsgs.add(getMsg("directory-manager-dn-is-config-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_DN, true);
    } else
    {
      getUserData().setDirectoryManagerDn(dmDn);
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_DN, false);
    }
 
    // Check the provided passwords
    String pwd1 = getFieldStringValue(FieldName.DIRECTORY_MANAGER_PWD);
    String pwd2 = getFieldStringValue(FieldName.DIRECTORY_MANAGER_PWD_CONFIRM);
    if (pwd1 == null)
    {
      pwd1 = "";
    }
 
    boolean pwdValid = true;
    if (!pwd1.equals(pwd2))
    {
      errorMsgs.add(getMsg("not-equal-pwd"));
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_PWD_CONFIRM, true);
      pwdValid = false;
 
    }
    if (pwd1.length() < MIN_DIRECTORY_MANAGER_PWD)
    {
      errorMsgs.add(getMsg(("pwd-too-short"), new String[]
        { String.valueOf(MIN_DIRECTORY_MANAGER_PWD) }));
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_PWD, true);
      if ((pwd2 == null) || (pwd2.length() < MIN_DIRECTORY_MANAGER_PWD))
      {
        displayFieldInvalid(FieldName.DIRECTORY_MANAGER_PWD_CONFIRM, true);
      }
      pwdValid = false;
    }
 
    if (pwdValid)
    {
      getUserData().setDirectoryManagerPwd(pwd1);
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_PWD, false);
      displayFieldInvalid(FieldName.DIRECTORY_MANAGER_PWD_CONFIRM, false);
    }
 
    if (errorMsgs.size() > 0)
    {
      throw new UserInstallDataException(Step.SERVER_SETTINGS,
          getStringFromCollection(errorMsgs));
    }
  }
 
  /**
   * Validate the data provided by the user in the data options panel and update
   * the UserData object according to that content.
   *
   * @throws an
   *           UserInstallDataException if the data provided by the user is not
   *           valid.
   *
   */
  private void updateUserDataForDataOptionsPanel()
      throws UserInstallDataException
  {
    ArrayList<String> errorMsgs = new ArrayList<String>();
 
    DataOptions dataOptions = null;
 
    // Check the base dn
    boolean validBaseDn = false;
    String baseDn = getFieldStringValue(FieldName.DIRECTORY_BASE_DN);
    if ((baseDn == null) || (baseDn.trim().length() == 0))
    {
      errorMsgs.add(getMsg("empty-base-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_BASE_DN, true);
    } else if (!Utils.isDn(baseDn))
    {
      errorMsgs.add(getMsg("not-a-base-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_BASE_DN, true);
    } else if (Utils.isConfigurationDn(baseDn))
    {
      errorMsgs.add(getMsg("base-dn-is-configuration-dn"));
      displayFieldInvalid(FieldName.DIRECTORY_BASE_DN, true);
    } else
    {
      displayFieldInvalid(FieldName.DIRECTORY_BASE_DN, false);
      validBaseDn = true;
    }
 
    // Check the data options
    DataOptions.Type type =
        (DataOptions.Type) getFieldValue(FieldName.DATA_OPTIONS);
 
    switch (type)
    {
    case IMPORT_FROM_LDIF_FILE:
      String ldifPath = getFieldStringValue(FieldName.LDIF_PATH);
      if ((ldifPath == null) || (ldifPath.trim().equals("")))
      {
        errorMsgs.add(getMsg("no-ldif-path"));
        displayFieldInvalid(FieldName.LDIF_PATH, true);
      } else if (!Utils.fileExists(ldifPath))
      {
        errorMsgs.add(getMsg("ldif-file-does-not-exist"));
        displayFieldInvalid(FieldName.LDIF_PATH, true);
      } else if (validBaseDn)
      {
        dataOptions = new DataOptions(type, baseDn, ldifPath);
        displayFieldInvalid(FieldName.LDIF_PATH, false);
      }
      break;
 
    case IMPORT_AUTOMATICALLY_GENERATED_DATA:
      // variable used to know if everything went ok during these
      // checks
      int startErrors = errorMsgs.size();
 
      // Check the number of entries
      String nEntries = getFieldStringValue(FieldName.NUMBER_ENTRIES);
      if ((nEntries == null) || (nEntries.trim().equals("")))
      {
        errorMsgs.add(getMsg("no-number-entries"));
        displayFieldInvalid(FieldName.NUMBER_ENTRIES, true);
      } else
      {
        boolean nEntriesValid = false;
        try
        {
          int n = Integer.parseInt(nEntries);
 
          nEntriesValid = n >= MIN_NUMBER_ENTRIES && n <= MAX_NUMBER_ENTRIES;
        } catch (NumberFormatException nfe)
        {
        }
 
        if (!nEntriesValid)
        {
          String[] args =
                { String.valueOf(MIN_NUMBER_ENTRIES),
                    String.valueOf(MAX_NUMBER_ENTRIES) };
          errorMsgs.add(getMsg("invalid-number-entries-range", args));
          displayFieldInvalid(FieldName.NUMBER_ENTRIES, true);
        } else
        {
          displayFieldInvalid(FieldName.NUMBER_ENTRIES, false);
        }
      }
      if (startErrors == errorMsgs.size() && validBaseDn)
      {
        // No validation errors
        dataOptions = new DataOptions(type, baseDn, new Integer(nEntries));
      }
      break;
 
    default:
      displayFieldInvalid(FieldName.LDIF_PATH, false);
      displayFieldInvalid(FieldName.NUMBER_ENTRIES, false);
      if (validBaseDn)
      {
        dataOptions = new DataOptions(type, baseDn);
      }
    }
 
    if (dataOptions != null)
    {
      getUserData().setDataOptions(dataOptions);
    }
 
    if (errorMsgs.size() > 0)
    {
      throw new UserInstallDataException(Step.DATA_OPTIONS,
          getStringFromCollection(errorMsgs));
    }
  }
 
  /**
   * Update the UserData object according to the content of the review panel.
   *
   */
  private void updateUserDataForReviewPanel()
  {
    Boolean b = (Boolean) getFieldValue(FieldName.SERVER_START);
    getUserData().setStartServer(b.booleanValue());
  }
 
  /**
   * Launch the installation of Open DS. Depending on whether we are running a
   * web start or not it will use on Installer object or other.
   *
   */
  private void launchInstallation()
  {
    if (isWebStart())
    {
      installer = new WebStartInstaller(getUserData(), jnlpDownloader);
    } else
    {
      installer = new OfflineInstaller(getUserData());
    }
    installer.addProgressUpdateListener(this);
    installer.start();
    Thread t = new Thread(new Runnable()
    {
      public void run()
      {
        runDisplayUpdater();
      }
    });
    t.start();
  }
 
  /**
   * Launch the uninstallation of Open DS.
   *
   */
  private void launchUninstallation()
  {
    uninstaller = new Uninstaller();
    // TO COMPLETE
  }
 
  /**
   * Provides the object representing the data provided by the user in the
   * install.
   *
   * @return the UserInstallData representing the data provided by the user in
   *         the Install wizard.
   */
  private UserInstallData getUserData()
  {
    if (userData == null)
    {
      userData = new UserInstallData();
    }
    return userData;
  }
 
  /**
   * Provides an object representing the default data/install parameters that
   * will be proposed to the user in the Installation wizard. This data includes
   * elements such as the default dn of the directory manager or the default
   * install location.
   *
   * @return the UserInstallData representing the default data/parameters that
   *         will be proposed to the user.
   */
  private UserInstallData getDefaultUserData()
  {
    UserInstallData defaultUserData = new UserInstallData();
 
    DataOptions defaultDataOptions = new DefaultDataOptions();
 
    defaultUserData.setServerLocation(Utils.getDefaultServerLocation());
    // See what we can propose as port
    int defaultPort = getDefaultPort();
    if (defaultPort != -1)
    {
      defaultUserData.setServerPort(defaultPort);
    }
    defaultUserData.setDirectoryManagerDn("cn=Directory Manager");
 
    defaultUserData.setDataOptions(defaultDataOptions);
    defaultUserData.setStartServer(true);
 
    return defaultUserData;
  }
 
  /**
   * The following three methods are just commodity methods to get localized
   * messages.
   */
  private String getMsg(String key)
  {
    return getI18n().getMsg(key);
  }
 
  private String getMsg(String key, String[] args)
  {
    return getI18n().getMsg(key, args);
  }
 
  private String getExceptionMsg(String key, Exception ex)
  {
    return Utils.getExceptionMsg(getI18n(), key, null, ex);
  }
 
  private ResourceProvider getI18n()
  {
    return ResourceProvider.getInstance();
  }
 
  /**
   * Get the current step.
   *
   * @return the currently displayed Step of the wizard.
   */
  private Step getCurrentStep()
  {
    return currentStep;
  }
 
  /**
   * Set the current step. This will basically make the required calls in the
   * dialog to display the panel that corresponds to the step passed as
   * argument.
   *
   * @param step.
   *          The step to be displayed.
   */
  private void setCurrentStep(Step step)
  {
    if (step == null)
    {
      throw new NullPointerException("step is null");
    }
    currentStep = step;
    getDialog().setDisplayedStep(step, getUserData());
  }
 
  /**
   * Gets the next step corresponding to the step passed as parameter.
   *
   * @param step,
   *          the step of which we want to get the new step.
   * @return the next step for the current step.
   * @throws IllegalArgumentException
   *           if the current step has not a next step.
   */
  private Step nextStep(Step step)
  {
    Iterator<Step> it = EnumSet.range(step, Step.PROGRESS).iterator();
    it.next();
    if (!it.hasNext())
    {
      throw new IllegalArgumentException("No next for step: " + step);
    }
    return it.next();
  }
 
  /**
   * Gets the previous step corresponding to the step passed as parameter.
   *
   * @param step,
   *          the step of which we want to get the previous step.
   * @return the next step for the current step.
   * @throws IllegalArgumentException
   *           if the current step has not a previous step.
   */
  private Step previousStep(Step step)
  {
    Step previous = null;
    for (Step s : Step.values())
    {
      if (s == step)
      {
        return previous;
      }
      previous = s;
    }
    throw new IllegalArgumentException("No previous for step: " + step);
  }
 
  /**
   * Get the dialog that is displayed.
   *
   * @return the dialog.
   */
  private QuickSetupDialog getDialog()
  {
    if (dialog == null)
    {
      dialog = new QuickSetupDialog(getDefaultUserData(), installStatus);
      dialog.addButtonActionListener(this);
    }
    return dialog;
  }
 
  /**
   * Displays an error message dialog.
   *
   * @param msg
   *          the error message.
   * @param title
   *          the title for the dialog.
   */
  private void displayError(String msg, String title)
  {
    getDialog().displayError(msg, title);
  }
 
  /**
   * Displays a confirmation message dialog.
   *
   * @param msg
   *          the confirmation message.
   * @param title
   *          the title of the dialog.
   * @return <CODE>true</CODE> if the user confirms the message, or
   * <CODE>false</CODE> if not.
   */
  private boolean displayConfirmation(String msg, String title)
  {
    return getDialog().displayConfirmation(msg, title);
  }
 
  /**
   * Gets the string value for a given field name.
   *
   * @param fieldName
   *          the field name object.
   * @return the string value for the field name.
   */
  private String getFieldStringValue(FieldName fieldName)
  {
    String sValue = null;
 
    Object value = getFieldValue(fieldName);
    if (value != null)
    {
      if (value instanceof String)
      {
        sValue = (String) value;
      } else
      {
        sValue = String.valueOf(value);
      }
    }
    return sValue;
  }
 
  /**
   * Gets the value for a given field name.
   *
   * @param fieldName
   *          the field name object.
   * @return the value for the field name.
   */
  private Object getFieldValue(FieldName fieldName)
  {
    return getDialog().getFieldValue(fieldName);
  }
 
  /**
   * Marks the fieldName as valid or invalid depending on the value of the
   * invalid parameter. With the current implementation this implies basically
   * using a red color in the label associated with the fieldName object. The
   * color/style used to mark the label invalid is specified in UIFactory.
   *
   * @param fieldName
   *          the field name object.
   * @param invalid
   *          whether to mark the field valid or invalid.
   */
  private void displayFieldInvalid(FieldName fieldName, boolean invalid)
  {
    getDialog().displayFieldInvalid(fieldName, invalid);
  }
 
  /**
   * This is a helper method that gets a String representation of the elements
   * in the Collection. The String will display the different elements separated
   * by a '\n' character.
   *
   * @param col
   *          the collection containing the String.
   * @return the String representation for the collection.
   */
  private String getStringFromCollection(Collection<String> col)
  {
    String msg = null;
    for (String m : col)
    {
      if (msg == null)
      {
        msg = m;
      } else
      {
        msg += "\n" + m;
      }
    }
    return msg;
  }
 
  /**
   * A method to initialize the look and feel.
   *
   */
  private void initLookAndFeel()
  {
    UIFactory.initialize();
  }
 
  /**
   * A methods that creates a InstallProgressDescriptor based on the value of a
   * ProgressUpdateEvent.
   *
   * @param ev
   *          the ProgressUpdateEvent used to generate the
   *          InstallProgressDescriptor.
   * @return the InstallProgressDescriptor.
   */
  private InstallProgressDescriptor createProgressDescriptor(
      ProgressUpdateEvent ev)
  {
    InstallProgressStep status = ev.getProgressStep();
    String newProgressLabel = ev.getCurrentPhaseSummary();
    String additionalDetails = ev.getNewLogs();
    Integer ratio = ev.getProgressRatio();
 
    if (additionalDetails != null)
    {
      progressDetails.append(additionalDetails);
    }
 
    return new InstallProgressDescriptor(status, ratio, newProgressLabel,
        progressDetails.toString());
  }
 
  /**
   * Indicates whether we are in a web start installation or not.
   *
   * @return <CODE>true</CODE> if we are in a web start installation and
   *         <CODE>false</CODE> if not.
   */
  private boolean isWebStart()
  {
    return Utils.isWebStart();
  }
 
  /**
   * Provides the port that will be proposed to the user in the second page of
   * the installation wizard. It will check whether we can use 389 and if not it
   * will return -1.
   *
   * @return the port 389 if it is available and we can use and -1 if not.
   */
  private int getDefaultPort()
  {
    int defaultPort = -1;
    if (Utils.canUseAsPort(389))
    {
      defaultPort = 389;
    }
    return defaultPort;
  }
 
  /**
   * Returns the number of free disk space in bytes required to install Open DS
   *
   * For the moment we just return 15 Megabytes. TODO we might want to have
   * something dynamic to calculate the required free disk space for the
   * installation.
   *
   * @return the number of free disk space required to install Open DS.
   */
  private long getRequiredInstallSpace()
  {
    return 15 * 1024 * 1024;
  }
}
 
/**
 * This class is just used to specify which are the default values that will be
 * proposed to the user in the Data Options panel of the Installation wizard.
 *
 */
class DefaultDataOptions extends DataOptions
{
  /**
   * Default constructor.
   *
   */
  public DefaultDataOptions()
  {
    super(Type.CREATE_BASE_ENTRY, "dc=example,dc=com");
  }
 
  /**
   * Get the number of entries that will be automatically generated.
   *
   * @return the number of entries that will be automatically generated.
   */
  public int getNumberEntries()
  {
    return 2000;
  }
}