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/** 031 * A class used to describe the java arguments for a given command-line. 032 */ 033public class JavaArguments 034{ 035 private int maxMemory = -1; 036 private int initialMemory = -1; 037 private String[] additionalArguments = {}; 038 039 /** 040 * Returns the maximum memory allowed to execute the command-line. 041 * @return the maximum memory allowed to execute the command-line. 042 */ 043 public int getMaxMemory() 044 { 045 return maxMemory; 046 } 047 048 /** 049 * Sets the maximum memory allowed to execute the command-line. 050 * @param maxMemory the maximum memory allowed to execute the command-line. 051 */ 052 public void setMaxMemory(int maxMemory) 053 { 054 this.maxMemory = maxMemory; 055 } 056 057 /** 058 * Returns the initial memory allowed to execute the command-line. 059 * @return the initial memory allowed to execute the command-line. 060 */ 061 public int getInitialMemory() 062 { 063 return initialMemory; 064 } 065 066 /** 067 * Sets the initial memory allowed to execute the command-line. 068 * @param initialMemory the initial memory allowed to execute the 069 * command-line. 070 */ 071 public void setInitialMemory(int initialMemory) 072 { 073 this.initialMemory = initialMemory; 074 } 075 076 /** 077 * Returns the additional arguments to be used when executing the 078 * command-line. 079 * @return the additional arguments to be used when executing the 080 * command-line. 081 */ 082 public String[] getAdditionalArguments() 083 { 084 return additionalArguments; 085 } 086 087 /** 088 * Sets the additional arguments to be used when executing the 089 * command-line. 090 * @param additionalArguments the additional arguments to be used when 091 * executing the command-line. It cannot be null. 092 */ 093 public void setAdditionalArguments(String[] additionalArguments) 094 { 095 if (additionalArguments == null) 096 { 097 throw new IllegalArgumentException("additionalArguments cannot be null."); 098 } 099 this.additionalArguments = additionalArguments; 100 } 101 102 /** {@inheritDoc} */ 103 @Override 104 public boolean equals(Object o) 105 { 106 if (o == this) 107 { 108 return true; 109 } 110 if (o instanceof JavaArguments) 111 { 112 final JavaArguments that = (JavaArguments) o; 113 return initialMemory == that.initialMemory 114 && maxMemory == that.maxMemory 115 && Arrays.equals(additionalArguments, that.additionalArguments); 116 } 117 return false; 118 } 119 120 /** {@inheritDoc} */ 121 @Override 122 public int hashCode() 123 { 124 int hashCode = 44 + initialMemory + maxMemory; 125 for (String arg : additionalArguments) 126 { 127 hashCode += arg.hashCode(); 128 } 129 return hashCode; 130 } 131 132 /** {@inheritDoc} */ 133 @Override 134 public String toString() 135 { 136 StringBuilder sb = new StringBuilder(); 137 sb.append("Initial Memory: ").append(initialMemory) 138 .append(" Max Memory: ").append(maxMemory); 139 int i=1; 140 for (String arg : additionalArguments) 141 { 142 sb.append(" arg ").append(i).append(": ").append(arg); 143 i++; 144 } 145 return sb.toString(); 146 } 147 148 /** 149 * Returns the message in HTML format to be used in a JLabel representing a 150 * java arguments object. 151 * @param javaArguments the java arguments to be represented. 152 * @param defaultJavaArguments the default values for the java arguments. 153 * @param font the font to be used. 154 * @return the message representing a java arguments object. 155 */ 156 public static LocalizableMessage getMessageForJLabel(JavaArguments javaArguments, 157 JavaArguments defaultJavaArguments, Font font) 158 { 159 LocalizableMessage msg = getMessage(javaArguments, defaultJavaArguments); 160 String s = msg.toString(); 161 if (s.contains("<br>")) 162 { 163 msg = LocalizableMessage.raw("<html>"+UIFactory.applyFontToHtml(s, font)); 164 } 165 return msg; 166 } 167 168 /** 169 * Returns the message in HTML format to be used in a representing a 170 * java arguments object. Note that no formatting of font is done. 171 * @param javaArguments the java arguments to be represented. 172 * @param defaultJavaArguments the default values for the java arguments. 173 * @return the message representing a java arguments object. 174 */ 175 public static LocalizableMessage getMessage(JavaArguments javaArguments, 176 JavaArguments defaultJavaArguments) 177 { 178 LocalizableMessage msg; 179 if (javaArguments.equals(defaultJavaArguments)) 180 { 181 msg = INFO_DEFAULT_JAVA_ARGUMENTS.get(); 182 } 183 else 184 { 185 ArrayList<LocalizableMessage> lines = new ArrayList<>(); 186 if (javaArguments.getInitialMemory() != -1) 187 { 188 lines.add(INFO_INITIAL_MEMORY.get(javaArguments.getInitialMemory())); 189 } 190 if (javaArguments.getMaxMemory() != -1) 191 { 192 lines.add(INFO_MAXIMUM_MEMORY.get(javaArguments.getMaxMemory())); 193 } 194 if (javaArguments.getAdditionalArguments().length > 0) 195 { 196 StringBuilder sb = new StringBuilder(); 197 for (String arg : javaArguments.getAdditionalArguments()) 198 { 199 if (sb.length() > 0) 200 { 201 sb.append(" "); 202 } 203 sb.append(arg); 204 } 205 lines.add(INFO_ADDITIONAL_ARGUMENTS.get(sb)); 206 } 207 if (lines.isEmpty()) 208 { 209 msg = INFO_USE_JVM_DEFAULT_SETTINGS.get(); 210 } 211 else if (lines.size() == 1) 212 { 213 msg = lines.get(0); 214 } 215 else 216 { 217 msg = LocalizableMessage.raw(joinAsString("<br>", lines)); 218 } 219 } 220 return msg; 221 } 222 223 /** 224 * Returns a String representation of the arguments (the String that must 225 * be passed when invoking java). 226 * @return a String representation of the arguments (the String that must 227 * be passed when invoking java). 228 */ 229 public String getStringArguments() 230 { 231 ArrayList<String> l = new ArrayList<>(); 232 if (initialMemory != -1) 233 { 234 l.add(Utils.escapeCommandLineValue( 235 getInitialMemoryArgument(initialMemory))); 236 } 237 if (maxMemory != -1) 238 { 239 l.add(Utils.escapeCommandLineValue(getMaxMemoryArgument(maxMemory))); 240 } 241 for (String arg : additionalArguments) 242 { 243 l.add(Utils.escapeCommandLineValue(arg)); 244 } 245 return joinAsString(" ", l); 246 } 247 248 /** 249 * Returns the java argument to specify the initial memory to be used. 250 * @param value the value in megabytes to be specified. 251 * @return the java argument to specify the initial memory to be used. 252 */ 253 public static String getInitialMemoryArgument(int value) 254 { 255 return "-Xms"+value+"m"; 256 } 257 258 /** 259 * Returns a generic initial memory argument (to be used in messages). 260 * @return a generic initial memory argument (to be used in messages). 261 */ 262 public static String getInitialMemoryGenericArgument() 263 { 264 return "-Xms<"+INFO_MEMORY_PLACEHOLDER.get()+">"; 265 } 266 267 /** 268 * Returns the java argument to specify the maximum memory that can be used. 269 * @param value the value in megabytes to be specified. 270 * @return the java argument to specify the maximum memory that can be used. 271 */ 272 public static String getMaxMemoryArgument(int value) 273 { 274 return "-Xmx"+value+"m"; 275 } 276 277 /** 278 * Returns a generic maximum memory argument (to be used in messages). 279 * @return a generic maximum memory argument (to be used in messages). 280 */ 281 public static String getMaxMemoryGenericArgument() 282 { 283 return "-Xmx<"+INFO_MEMORY_PLACEHOLDER.get()+">"; 284 } 285}