001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2010 Sun Microsystems, Inc. 015 * Portions Copyright 2013-2016 ForgeRock AS. 016 */ 017package org.opends.quicksetup; 018 019import java.awt.Font; 020import java.util.ArrayList; 021import java.util.Arrays; 022 023import org.forgerock.i18n.LocalizableMessage; 024import org.opends.quicksetup.ui.UIFactory; 025import org.opends.quicksetup.util.Utils; 026 027import static org.forgerock.util.Utils.*; 028import static org.opends.messages.QuickSetupMessages.*; 029 030/** A class used to describe the java arguments for a given command-line. */ 031public class JavaArguments 032{ 033 private int maxMemory = -1; 034 private int initialMemory = -1; 035 private String[] additionalArguments = {}; 036 037 /** 038 * Returns the maximum memory allowed to execute the command-line. 039 * @return the maximum memory allowed to execute the command-line. 040 */ 041 public int getMaxMemory() 042 { 043 return maxMemory; 044 } 045 046 /** 047 * Sets the maximum memory allowed to execute the command-line. 048 * @param maxMemory the maximum memory allowed to execute the command-line. 049 */ 050 public void setMaxMemory(int maxMemory) 051 { 052 this.maxMemory = maxMemory; 053 } 054 055 /** 056 * Returns the initial memory allowed to execute the command-line. 057 * @return the initial memory allowed to execute the command-line. 058 */ 059 public int getInitialMemory() 060 { 061 return initialMemory; 062 } 063 064 /** 065 * Sets the initial memory allowed to execute the command-line. 066 * @param initialMemory the initial memory allowed to execute the 067 * command-line. 068 */ 069 public void setInitialMemory(int initialMemory) 070 { 071 this.initialMemory = initialMemory; 072 } 073 074 /** 075 * Returns the additional arguments to be used when executing the 076 * command-line. 077 * @return the additional arguments to be used when executing the 078 * command-line. 079 */ 080 public String[] getAdditionalArguments() 081 { 082 return additionalArguments; 083 } 084 085 /** 086 * Sets the additional arguments to be used when executing the 087 * command-line. 088 * @param additionalArguments the additional arguments to be used when 089 * executing the command-line. It cannot be null. 090 */ 091 public void setAdditionalArguments(String[] additionalArguments) 092 { 093 if (additionalArguments == null) 094 { 095 throw new IllegalArgumentException("additionalArguments cannot be null."); 096 } 097 this.additionalArguments = additionalArguments; 098 } 099 100 @Override 101 public boolean equals(Object o) 102 { 103 if (o == this) 104 { 105 return true; 106 } 107 if (o instanceof JavaArguments) 108 { 109 final JavaArguments that = (JavaArguments) o; 110 return initialMemory == that.initialMemory 111 && maxMemory == that.maxMemory 112 && Arrays.equals(additionalArguments, that.additionalArguments); 113 } 114 return false; 115 } 116 117 @Override 118 public int hashCode() 119 { 120 int hashCode = 44 + initialMemory + maxMemory; 121 for (String arg : additionalArguments) 122 { 123 hashCode += arg.hashCode(); 124 } 125 return hashCode; 126 } 127 128 @Override 129 public String toString() 130 { 131 StringBuilder sb = new StringBuilder(); 132 sb.append("Initial Memory: ").append(initialMemory) 133 .append(" Max Memory: ").append(maxMemory); 134 int i=1; 135 for (String arg : additionalArguments) 136 { 137 sb.append(" arg ").append(i).append(": ").append(arg); 138 i++; 139 } 140 return sb.toString(); 141 } 142 143 /** 144 * Returns the message in HTML format to be used in a JLabel representing a 145 * java arguments object. 146 * @param javaArguments the java arguments to be represented. 147 * @param defaultJavaArguments the default values for the java arguments. 148 * @param font the font to be used. 149 * @return the message representing a java arguments object. 150 */ 151 public static LocalizableMessage getMessageForJLabel(JavaArguments javaArguments, 152 JavaArguments defaultJavaArguments, Font font) 153 { 154 LocalizableMessage msg = getMessage(javaArguments, defaultJavaArguments); 155 String s = msg.toString(); 156 if (s.contains("<br>")) 157 { 158 msg = LocalizableMessage.raw("<html>"+UIFactory.applyFontToHtml(s, font)); 159 } 160 return msg; 161 } 162 163 /** 164 * Returns the message in HTML format to be used in a representing a 165 * java arguments object. Note that no formatting of font is done. 166 * @param javaArguments the java arguments to be represented. 167 * @param defaultJavaArguments the default values for the java arguments. 168 * @return the message representing a java arguments object. 169 */ 170 private static LocalizableMessage getMessage(JavaArguments javaArguments, 171 JavaArguments defaultJavaArguments) 172 { 173 LocalizableMessage msg; 174 if (javaArguments.equals(defaultJavaArguments)) 175 { 176 msg = INFO_DEFAULT_JAVA_ARGUMENTS.get(); 177 } 178 else 179 { 180 ArrayList<LocalizableMessage> lines = new ArrayList<>(); 181 if (javaArguments.getInitialMemory() != -1) 182 { 183 lines.add(INFO_INITIAL_MEMORY.get(javaArguments.getInitialMemory())); 184 } 185 if (javaArguments.getMaxMemory() != -1) 186 { 187 lines.add(INFO_MAXIMUM_MEMORY.get(javaArguments.getMaxMemory())); 188 } 189 if (javaArguments.getAdditionalArguments().length > 0) 190 { 191 StringBuilder sb = new StringBuilder(); 192 for (String arg : javaArguments.getAdditionalArguments()) 193 { 194 if (sb.length() > 0) 195 { 196 sb.append(" "); 197 } 198 sb.append(arg); 199 } 200 lines.add(INFO_ADDITIONAL_ARGUMENTS.get(sb)); 201 } 202 if (lines.isEmpty()) 203 { 204 msg = INFO_USE_JVM_DEFAULT_SETTINGS.get(); 205 } 206 else if (lines.size() == 1) 207 { 208 msg = lines.get(0); 209 } 210 else 211 { 212 msg = LocalizableMessage.raw(joinAsString("<br>", lines)); 213 } 214 } 215 return msg; 216 } 217 218 /** 219 * Returns a String representation of the arguments (the String that must 220 * be passed when invoking java). 221 * @return a String representation of the arguments (the String that must 222 * be passed when invoking java). 223 */ 224 public String getStringArguments() 225 { 226 ArrayList<String> l = new ArrayList<>(); 227 if (initialMemory != -1) 228 { 229 l.add(Utils.escapeCommandLineValue( 230 getInitialMemoryArgument(initialMemory))); 231 } 232 if (maxMemory != -1) 233 { 234 l.add(Utils.escapeCommandLineValue(getMaxMemoryArgument(maxMemory))); 235 } 236 for (String arg : additionalArguments) 237 { 238 l.add(Utils.escapeCommandLineValue(arg)); 239 } 240 return joinAsString(" ", l); 241 } 242 243 /** 244 * Returns the java argument to specify the initial memory to be used. 245 * @param value the value in megabytes to be specified. 246 * @return the java argument to specify the initial memory to be used. 247 */ 248 public static String getInitialMemoryArgument(int value) 249 { 250 return "-Xms"+value+"m"; 251 } 252 253 /** 254 * Returns a generic initial memory argument (to be used in messages). 255 * @return a generic initial memory argument (to be used in messages). 256 */ 257 public static String getInitialMemoryGenericArgument() 258 { 259 return "-Xms<"+INFO_MEMORY_PLACEHOLDER.get()+">"; 260 } 261 262 /** 263 * Returns the java argument to specify the maximum memory that can be used. 264 * @param value the value in megabytes to be specified. 265 * @return the java argument to specify the maximum memory that can be used. 266 */ 267 public static String getMaxMemoryArgument(int value) 268 { 269 return "-Xmx"+value+"m"; 270 } 271 272 /** 273 * Returns a generic maximum memory argument (to be used in messages). 274 * @return a generic maximum memory argument (to be used in messages). 275 */ 276 public static String getMaxMemoryGenericArgument() 277 { 278 return "-Xmx<"+INFO_MEMORY_PLACEHOLDER.get()+">"; 279 } 280}