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