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

Valery Kharseko
2 days ago 0138b5924ff14ef709398bf878dbb9bf2a0b7c33
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
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2010 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 * Portions Copyright 2026 3A Systems, LLC.
 */
package org.opends.quicksetup.installer;
 
import static org.opends.messages.QuickSetupMessages.*;
import static org.opends.messages.ToolMessages.*;
import static org.opends.server.util.ServerConstants.*;
 
import static com.forgerock.opendj.util.StaticUtils.registerBcProvider;
 
import java.io.File;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.quicksetup.CliApplication;
import org.opends.quicksetup.Installation;
import org.opends.quicksetup.Launcher;
import org.opends.quicksetup.ReturnCode;
import org.opends.quicksetup.util.IncompatibleVersionException;
import org.opends.quicksetup.util.Utils;
import org.opends.server.tools.InstallDS;
import org.opends.server.tools.InstallDSArgumentParser;
import org.opends.server.util.DynamicConstants;
 
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.ArgumentParser;
 
/**
 * This class is called by the setup command line to launch the setup
 * of the Directory Server. It just checks the command line arguments and the
 * environment and determines whether the graphical or the command line
 * based setup much be launched.
 */
public class SetupLauncher extends Launcher {
 
  private static final String LOG_FILE_PREFIX = "opendj-setup-";
 
  /**
   * The main method which is called by the setup command lines.
   *
   * @param args the arguments passed by the command lines.  In the case
   * we want to launch the cli setup they are basically the arguments that we
   * will pass to the org.opends.server.tools.InstallDS class.
   */
  public static void main(String[] args) {
    new SetupLauncher(args).launch();
  }
 
  private InstallDSArgumentParser argParser;
 
  /**
   * Creates a launcher.
   *
   * @param args the arguments passed by the command lines.
   */
  public SetupLauncher(String[] args) {
    super(args, LOG_FILE_PREFIX, instanceLogsDirectory());
    if (System.getProperty(PROPERTY_SCRIPT_NAME) == null)
    {
      System.setProperty(PROPERTY_SCRIPT_NAME, Installation.getSetupFileName());
    }
    initializeParser();
  }
 
  /**
   * The {@code logs/} directory of the instance being set up, where the setup log is kept.
   * <p>
   * The launcher scripts point {@code java.io.tmpdir} at {@code <instance>/tmp}, the scratch
   * space of every tool, which {@code start-ds} - run by setup itself to start the server -
   * used to sweep clean (issue #1030). The log of a failed setup belongs next to the server's
   * own logs instead, where {@code server.out} tells the other half of the story.
   * <p>
   * Neither the directory nor the log is created here: {@link Launcher#getTempLogFile()}
   * creates both when an install is about to run, so that a road which installs nothing -
   * {@code setup --help} on a package whose instance directory is not laid down yet, for one -
   * leaves nothing behind.
   *
   * @return the logs directory of the instance, or {@code null} when the launcher is not
   *         running from an installation and the OS temporary directory has to do.
   */
  private static File instanceLogsDirectory()
  {
    final String installPath = Utils.getInstallPathFromClasspath();
    if (installPath == null)
    {
      return null;
    }
    return new File(Utils.getInstancePathFromInstallPath(installPath), Installation.LOGS_PATH_RELATIVE);
  }
 
  /** Initialize the contents of the argument parser. */
  protected void initializeParser()
  {
    argParser = new InstallDSArgumentParser(InstallDS.class.getName());
    try
    {
      argParser.initializeArguments();
    }
    catch (ArgumentException ae)
    {
      LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
      System.out.println(message);
    }
  }
 
  @Override
  public void launch() {
    try
    {
      argParser.parseArguments(args);
      
      registerBcProvider();
 
      if (argParser.isVersionArgumentPresent())
      {
        System.exit(ReturnCode.PRINT_VERSION.getReturnCode());
      }
      // The second condition is required when the user specifies '?'
      else if (argParser.isUsageArgumentPresent() ||
          argParser.usageOrVersionDisplayed())
      {
        System.exit(ReturnCode.SUCCESSFUL.getReturnCode());
      }
      else if (isCli())
      {
        Utils.checkJavaVersion();
        System.exit(InstallDS.mainCLI(args, this::getTempLogFile));
      }
      else
      {
        willLaunchGui();
        // The java version is checked in the launchGui code to be sure
        // that if there is a problem with the java version the message
        // (if possible) is displayed graphically.
        int exitCode = launchGui(args);
        if (exitCode != 0) {
          guiLaunchFailed();
          Utils.checkJavaVersion();
          System.exit(InstallDS.mainCLI(args, this::getTempLogFile));
        }
      }
    }
    catch (ArgumentException ae)
    {
      argParser.displayMessageAndUsageReference(System.err, ERR_ERROR_PARSING_ARGS.get(ae.getMessage()));
      System.exit(ReturnCode.USER_DATA_ERROR.getReturnCode());
    }
    catch (IncompatibleVersionException ive)
    {
      System.err.println(ive.getMessageObject());
      System.exit(ReturnCode.JAVA_VERSION_INCOMPATIBLE.getReturnCode());
    }
  }
 
  @Override
  public ArgumentParser getArgumentParser() {
    return this.argParser;
  }
 
  @Override
  protected void guiLaunchFailed() {
    // No log is named here: none exists yet, and creating one to name would leave it behind on
    // every command line road that installs nothing (issue #1030). The reason the GUI failed
    // goes into the log of the install that follows, and a failed install names that log.
    System.err.println(INFO_SETUP_LAUNCHER_GUI_LAUNCHED_FAILED.get());
  }
 
  @Override
  protected void willLaunchGui() {
    System.out.println(INFO_SETUP_LAUNCHER_LAUNCHING_GUI.get());
    System.setProperty("org.opends.quicksetup.Application.class", Installer.class.getName());
  }
 
  @Override
  protected LocalizableMessage getFrameTitle() {
    return Utils.getCustomizedObject("INFO_FRAME_INSTALL_TITLE",
        INFO_FRAME_INSTALL_TITLE.get(DynamicConstants.PRODUCT_NAME),
        LocalizableMessage.class);
  }
 
  @Override
  protected CliApplication createCliApplication() {
    return null;
  }
 
  @Override
  protected boolean isCli() {
    return argParser.isCli();
  }
}