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

Maxim Thomas
27.52.2022 4b1bcd45a69d24170872af32154801883a424090
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
/*
 * 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 2010 Sun Microsystems, Inc.
 * Portions Copyright 2013-2016 ForgeRock AS.
 */
package org.opends.quicksetup;
 
import java.awt.Font;
import java.util.ArrayList;
import java.util.Arrays;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.quicksetup.ui.UIFactory;
import org.opends.quicksetup.util.Utils;
 
import static org.forgerock.util.Utils.*;
import static org.opends.messages.QuickSetupMessages.*;
 
/** A class used to describe the java arguments for a given command-line. */
public class JavaArguments
{
  private int maxMemory = -1;
  private int initialMemory = -1;
  private String[] additionalArguments = {};
 
  /**
   * Returns the maximum memory allowed to execute the command-line.
   * @return the maximum memory allowed to execute the command-line.
   */
  public int getMaxMemory()
  {
    return maxMemory;
  }
 
  /**
   * Sets the maximum memory allowed to execute the command-line.
   * @param maxMemory the maximum memory allowed to execute the command-line.
   */
  public void setMaxMemory(int maxMemory)
  {
    this.maxMemory = maxMemory;
  }
 
  /**
   * Returns the initial memory allowed to execute the command-line.
   * @return the initial memory allowed to execute the command-line.
   */
  public int getInitialMemory()
  {
    return initialMemory;
  }
 
  /**
   * Sets the initial memory allowed to execute the command-line.
   * @param initialMemory the initial memory allowed to execute the
   * command-line.
   */
  public void setInitialMemory(int initialMemory)
  {
    this.initialMemory = initialMemory;
  }
 
  /**
   * Returns the additional arguments to be used when executing the
   * command-line.
   * @return the additional arguments to be used when executing the
   * command-line.
   */
  public String[] getAdditionalArguments()
  {
    return additionalArguments;
  }
 
  /**
   * Sets the additional arguments to be used when executing the
   * command-line.
   * @param additionalArguments the additional arguments to be used when
   * executing the command-line.  It cannot be null.
   */
  public void setAdditionalArguments(String[] additionalArguments)
  {
    if (additionalArguments == null)
    {
      throw new IllegalArgumentException("additionalArguments cannot be null.");
    }
    this.additionalArguments = additionalArguments;
  }
 
  @Override
  public boolean equals(Object o)
  {
    if (o == this)
    {
      return true;
    }
    if (o instanceof JavaArguments)
    {
      final JavaArguments that = (JavaArguments) o;
      return initialMemory == that.initialMemory
          && maxMemory == that.maxMemory
          && Arrays.equals(additionalArguments, that.additionalArguments);
    }
    return false;
  }
 
  @Override
  public int hashCode()
  {
    int hashCode = 44 + initialMemory + maxMemory;
    for (String arg : additionalArguments)
    {
      hashCode += arg.hashCode();
    }
    return hashCode;
  }
 
  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append("Initial Memory: ").append(initialMemory)
        .append("  Max Memory: ").append(maxMemory);
    int i=1;
    for (String arg : additionalArguments)
    {
      sb.append(" arg ").append(i).append(": ").append(arg);
      i++;
    }
    return sb.toString();
  }
 
  /**
   * Returns the message in HTML format to be used in a JLabel representing a
   * java arguments object.
   * @param javaArguments the java arguments to be represented.
   * @param defaultJavaArguments the default values for the java arguments.
   * @param font the font to be used.
   * @return the message representing a java arguments object.
   */
  public static LocalizableMessage getMessageForJLabel(JavaArguments javaArguments,
      JavaArguments defaultJavaArguments, Font font)
  {
    LocalizableMessage msg = getMessage(javaArguments, defaultJavaArguments);
    String s = msg.toString();
    if (s.contains("<br>"))
    {
      msg = LocalizableMessage.raw("<html>"+UIFactory.applyFontToHtml(s, font));
    }
    return msg;
  }
 
  /**
   * Returns the message in HTML format to be used in a representing a
   * java arguments object.  Note that no formatting of font is done.
   * @param javaArguments the java arguments to be represented.
   * @param defaultJavaArguments the default values for the java arguments.
   * @return the message representing a java arguments object.
   */
  private static LocalizableMessage getMessage(JavaArguments javaArguments,
      JavaArguments defaultJavaArguments)
  {
    LocalizableMessage msg;
    if (javaArguments.equals(defaultJavaArguments))
    {
      msg = INFO_DEFAULT_JAVA_ARGUMENTS.get();
    }
    else
    {
      ArrayList<LocalizableMessage> lines = new ArrayList<>();
      if (javaArguments.getInitialMemory() != -1)
      {
        lines.add(INFO_INITIAL_MEMORY.get(javaArguments.getInitialMemory()));
      }
      if (javaArguments.getMaxMemory() != -1)
      {
        lines.add(INFO_MAXIMUM_MEMORY.get(javaArguments.getMaxMemory()));
      }
      if (javaArguments.getAdditionalArguments().length > 0)
      {
        StringBuilder sb = new StringBuilder();
        for (String arg : javaArguments.getAdditionalArguments())
        {
          if (sb.length() > 0)
          {
            sb.append(" ");
          }
          sb.append(arg);
        }
        lines.add(INFO_ADDITIONAL_ARGUMENTS.get(sb));
      }
      if (lines.isEmpty())
      {
        msg = INFO_USE_JVM_DEFAULT_SETTINGS.get();
      }
      else if (lines.size() == 1)
      {
        msg = lines.get(0);
      }
      else
      {
        msg = LocalizableMessage.raw(joinAsString("<br>", lines));
      }
    }
    return msg;
  }
 
  /**
   * Returns a String representation of the arguments (the String that must
   * be passed when invoking java).
   * @return a String representation of the arguments (the String that must
   * be passed when invoking java).
   */
  public String getStringArguments()
  {
    ArrayList<String> l = new ArrayList<>();
    if (initialMemory != -1)
    {
      l.add(Utils.escapeCommandLineValue(
          getInitialMemoryArgument(initialMemory)));
    }
    if (maxMemory != -1)
    {
      l.add(Utils.escapeCommandLineValue(getMaxMemoryArgument(maxMemory)));
    }
    for (String arg : additionalArguments)
    {
      l.add(Utils.escapeCommandLineValue(arg));
    }
    return joinAsString(" ", l);
  }
 
  /**
   * Returns the java argument to specify the initial memory to be used.
   * @param value the value in megabytes to be specified.
   * @return the java argument to specify the initial memory to be used.
   */
  public static String getInitialMemoryArgument(int value)
  {
    return "-Xms"+value+"m";
  }
 
  /**
   * Returns a generic initial memory argument (to be used in messages).
   * @return a generic initial memory argument (to be used in messages).
   */
  public static String getInitialMemoryGenericArgument()
  {
    return "-Xms<"+INFO_MEMORY_PLACEHOLDER.get()+">";
  }
 
  /**
   * Returns the java argument to specify the maximum memory that can be used.
   * @param value the value in megabytes to be specified.
   * @return the java argument to specify the maximum memory that can be used.
   */
  public static String getMaxMemoryArgument(int value)
  {
    return "-Xmx"+value+"m";
  }
 
  /**
   * Returns a generic maximum memory argument (to be used in messages).
   * @return a generic maximum memory argument (to be used in messages).
   */
  public static String getMaxMemoryGenericArgument()
  {
    return "-Xmx<"+INFO_MEMORY_PLACEHOLDER.get()+">";
  }
}